简体   繁体   English

这种钉语法有什么问题?

[英]what is wrong with this peg grammar?

the following grammar (from RFC 2396): 以下语法(来自RFC 2396):

domainlabel = 'a' / ('a' ('a' / '-')* 'a')

cannot parse this: 无法解析此:

aa

why? 为什么?

Because PEG is not BNF. 因为PEG不是BNF。 The use of / rather than the usual BNF alternation operator | 使用/而不是通常的BNF交替运算符| (as seen in RFC 2396) was a deliberate attempt to avoid confusion (although it doesn't help that older standards such as RFC 822 also used / ). (如RFC 2396所示)是为了避免混淆而进行的有意尝试(尽管RFC 822等较旧的标准也使用/并没有帮助)。

In a PEG, / is the " ordered -choice" operator . 在PEG中, /有序选择”运算符 Unlike the BNF alternation operator, / is not symmetrical. 与BNF交替运算符不同, /不对称。 If the first alternative succeeds, it is accepted. 如果第一种选择成功,则将被接受。 Only if the first alternative fails does the PEG backtrack and try the second alternative. 仅当第一种方法失败时,PEG才会回溯并尝试第二种方法。

So when 'a' / ('a' ('a' / '-')* 'a') is applied to aa , the first alternative successfully absorbs the first a and the second alternative is never attempted. 因此,当将'a' / ('a' ('a' / '-')* 'a')应用于aa ,第一个选择成功吸收了第一个a ,而从未尝试过第二个选择。 The fact that the parse subsequently fails to match the entire string does not matter; 解析随后无法匹配整个字符串的事实无关紧要; / only backtracks if matching the first alternative itself fails, not if some subsequent part of the parse fails. /仅当匹配第一个替代项本身失败时才回溯,而不是如果解析的某些后续部分失败时则回溯。

In short, if you use PEGs, you need to be careful to write your alternations in the correct order. 简而言之,如果您使用PEG,则需要注意以正确的顺序书写替换内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM