什么时候空白在Perl6语法中真的很重要? [英] When is white space really important in Perl6 grammars?

查看:68
本文介绍了什么时候空白在Perl6语法中真的很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以澄清Perl 6语法中空格在规则中的重要性吗?我正在通过反复试验来学习一些方法,但是似乎无法在文档中找到实际的规则.

示例1:

rule number {
    <pm> \d '.'? \d*[ <pm> \d* ]?
}

rule pm {
    [ '+' || '-' ]?
}

将匹配数字2.68156e+154,而不关心rule number中存在的空格.但是,如果在\d*之后添加空格,它将失败. (即<pm> \d '.'? \d* [ <pm> \d* ]?失败).

示例2: 如果我想在单词的中间找到字面量,那么它们之间的间距很重要.即,在找到条目Double_t Delta_phi_R_1_9_pTproj_13_dat_cent_fx3001[52] = {

grammar TOP {
    ^ .*? <word-to-find> .* ?
}
rule word-to-find {
    \w*?fx\w*
}

会找到单词.但是,如果规则word-to-find的定义更改为: fx\w* fx\w*\w*fx \w*,则不会匹配.

此外,定义'[52]'将匹配,而定义'fx[52]'将不匹配.

感谢您的见解.指向文档中正确点的指针将大有帮助! 谢谢

解决方案

有人可以澄清Perl 6语法中空格在规则中的重要性吗?

:sigspace生效时.

我将在下面提供更多详细信息.如果您或其他阅读此书的人需要进一步的详细信息,请通过评论告知我,我们将进一步扩展.

首先,在提供doc链接之前,让我们消除一个可能造成混淆的原因,即在Perl 6中上下文 rule regex 的含义.

规则一词可用于一般意义 ("Perl 6的正则表达式,字符串匹配和通用分析工具")或作为关键字(rule).同样, regex 可能与通用规则或关键字(regex)含义相同.

不带该序言,这是指向 :sigspace文档部分.

请注意,rule关键字隐式会插入:sigspace,以使其在声明的规则中的第一个原子之后立即生效,并且该效果是词法的.有关这两个重要细节的详细讨论,请参见 @smls对另一个SO问题的答案,尤其是前两个要点. /p>

您也许还会发现我对另一个有关空格/令牌化的SO问题的回答很有帮助.

高度.

can someone clarify when white space is significant in rules in Perl 6 grammars? I am learning some by trial and error, but can't seem to find the actual rules in the documentation.

Example 1:

rule number {
    <pm> \d '.'? \d*[ <pm> \d* ]?
}

rule pm {
    [ '+' || '-' ]?
}

Will match a number 2.68156e+154, and not care about the spaces that are present in rule number. However, if I add a space after \d*, it will fail. (i.e. <pm> \d '.'? \d* [ <pm> \d* ]? fails).

Example 2: If I am trying to find literals in the middle of a word, then spacing around them are important. I.e., in finding the entry Double_t Delta_phi_R_1_9_pTproj_13_dat_cent_fx3001[52] = {

grammar TOP {
    ^ .*? <word-to-find> .* ?
}
rule word-to-find {
    \w*?fx\w*
}

Will find the word. However, if the definition of the rule word-to-find is changed to : fx or \w* fx\w* or \w*fx \w* then it won't make a match.

Also, then definition '[52]' will match, while the definition 'fx[52]' will not.

Thanks for any insight. A pointer to the proper point in the documentation would help greatly! Thanks,

解决方案

can someone clarify when white space is significant in rules in Perl 6 grammars?

When :sigspace is in effect.

I'll provide a little more detail below. If you or anyone else reading this needs further details, let me know via comments and I'll expand further.

First, let's eliminate one possible source of confusion, namely the meaning of the words rule and regex in the context of Perl 6, before I provide the doc link.

The word rule may be used in either a generic sense ("the regular expression, string matching and general-purpose parsing facility of Perl 6") or as a keyword (rule). Similarly, regex may be used to mean much the same thing as the generic rule or as a keyword (regex).

With that preamble out of the way, here's a link to the :sigspace doc section.

Note that the rule keyword implicitly inserts a :sigspace such that it takes effect immediately following the first atom in the declared rule, and that the effect is lexical. See @smls's answer to another SO question, especially the first two bullet points, for detailed discussion of these two important details.

You may also find my answer to another SO question dealing with whitespace/tokenization helpful.

Hth.

这篇关于什么时候空白在Perl6语法中真的很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆