简体   繁体   中英

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

EDIT

Seeing kaby76's comment, just to make sure: you cannot just remove them from a grammar file regardless. 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 ...`
 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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