问题
This is my grammar:
grammar test;
text: foo EOF;
foo:
'X'
|
'('
foo
')'
|
foo
'!'
|
foo
tail
;
tail: (' ' foo)+;
This is the input it perfectly parses:
X (X! (X)! (X X X)!!!) X
However, the output tree has too many tail
elements, as I explained earlier here. Is it possible to fix this?
回答1:
Thanks to @kaby76, the solution is found:
foo:
'X' tail?
|
'(' foo ')' tail?
|
foo '!' tail?
;
tail:
(
' ' 'X'
|
' ' '(' foo ')'
|
' ' foo '!'
)+
;
来源:https://stackoverflow.com/questions/64635366/how-to-make-antlr-consume-all-available-elements-using-recursion