简体   繁体   English

Antlr 奇怪的括号语法

[英]Antlr weird parentheses syntax

在此处输入图像描述

Cant understand this round bracket meaning.无法理解这个圆括号的含义。 Its not necessary to write it, but sometimes it can produce left-recursion error.不必写,但有时会产生左递归错误。 Where should we use it in grammar rules?我们应该在语法规则的什么地方使用它?

Its not necessary to write it,没必要写,

That is correct, it is not necessary.这是正确的,没有必要。 Just remove them.只需删除它们。

but sometimes it can produce left-recursion error.但有时它会产生左递归错误。

If that really is the case, you can open an issue here: https://github.com/antlr/antlr4/issues如果确实如此,您可以在这里打开一个问题: https://github.com/antlr/antlr4/issues

EDIT编辑

Seeing kaby76's comment, just to make sure: you cannot just remove them from a grammar file regardless.看到 kaby76 的评论,只是为了确保:无论如何,您不能只将它们从语法文件中删除。 They can be removed from your example rule.它们可以从您的示例规则中删除。

When used like this:像这样使用时:

rule
 : ID '=' ( NUMBER | STRING ) // match either `ID '=' NUMBER` 
                              //           or `ID '=' STRING`
 ;

they cannot be removed because removing them wold result in:它们不能被移除,因为移除它们会导致:

rule
 : ID '=' NUMBER | STRING // match either `ID '=' NUMBER` 
                          //           or `STRING`
 ;

Or with repetition:或重复:

rule
 : ( ID STRING )+ // match: `ID STRING ID STRING ID STRING ...`
 ;

and this:和这个:

rule
 : ID STRING+ // match: `ID STRING STRING STRING ...`
 ;

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

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