简体   繁体   English

语法:替代语法的优先顺序

[英]Grammar: Precedence of grammar alternatives

This is a very basic question about grammar alternatives. 这是关于语法选择的非常基本的问题。 If you have the following alternative: 如果您有以下选择:

Myalternative: 'a' | .;
Myalternative2: 'a' | 'b';

Would 'a' have higher priority over the '.' 'a'优先于'。' and over 'b'? 并超过“ b”?

I understand that this may also depend on the behaviour of the parser generated by this syntax but in pure theoretical grammar terms could you imagine these rules being matched in parallel ie test against 'a' and '.' 我知道这也可能取决于此语法生成的解析器的行为,但是用纯理论语法术语,您可以想象这些规则是并行匹配的,即针对“ a”和“。”进行测试。 at the same time and select the one with highest priority? 同时选择优先级最高的那个? Or is the 'a' and . 还是'a'和。 ambiguous due to the lack of precedence in grammars? 由于语法缺乏优先性而导致歧义?

The answer depends primarily on the tool you are using, and what the semantics of that tool is. 答案主要取决于您所使用的工具以及该工具的语义。 As written, this is not a context-free grammar in canonical form, and you'd need to produce that to get a theoretical answer, because only in that way can you clarify the intended semantics. 如所写,这不是规范形式的无上下文语法,并且您需要产生该语法以获得理论上的答案,因为只有这样才能阐明预期的语义。

Since the question is tagged , I'm going to guess that this is part of an Antlr lexical definition in which . 由于该问题被标记为 ,因此我猜这是Antlr词汇定义中的一部分. is a wildcard character. 是通配符。 In that case, 'a' | . 在这种情况下, 'a' | . 'a' | . means exactly the same thing as . 与...的含义完全相同 . .

Since MyAlternative matches everything that MyAlternative2 matches, and since MyAlternative comes first in the Antlr lexical definition, MyAlternative2 can never match anything. 由于MyAlternative匹配一切MyAlternative2匹配,并且由于MyAlternative至上的ANTLR的词汇定义, MyAlternative2不能匹配任何东西。 Any single character will be matched by MyAlternative (unless there is some other lexical rule which matches a longer sequence of input characters). 任何单个字符都将由MyAlternative匹配(除非还有其他一些词汇规则匹配更长的输入字符序列)。

If you put the definition of MyAlternative2 first in the grammar file, then a or b would be matched as MyAlternative2 , while any other character would be matched as MyAlternative . 如果将MyAlternative2的定义放在语法文件中的第一位,则ab将与MyAlternative2匹配,而其他任何字符将与MyAlternative匹配。

The question of precedence within alternatives is meaningless. 优先的替代品的问题是没有意义的。 It doesn't matter whether MyAlternative considers the match of an a to be a match of a or a match of . 不要紧是否MyAlternative考虑的的比赛a来是匹配a或一个匹配. . It is, in the end, a match of MyAlternative , and that symbol can only have one associated action. 最后,它是MyAlternative的匹配MyAlternative ,并且该符号只能具有一个关联的动作。

Between lexical rules, there is a precedence relationship: The first one wins. 在词汇规则之间,存在优先级关系:第一个获胜。 (More accurately, as far as I know, Antlr obeys the usual convention that the longest match wins; between two rules which both match the same longest sequence, the first one in the grammar file wins.) That is not in any way influenced by alternative bars in the rules themselves. (据我所知,更准确地说,Antlr遵循最长匹配获胜的惯例;在两个规则都匹配相同最长序列的两个规则之间,语法文件中的第一个获胜。)无论如何,这不受任何影响规则本身中的替代条。

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

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