How to parse the root of the hierarchy in parentheses?

I am trying to parse values ​​using ANTLR. Here is the relevant part of my grammar:

root : IDENTIFIER | SELF | literal | constructor | call | indexer;

hierarchy : root (SUB^ (IDENTIFIER | call | indexer))*;

factor  : hierarchy ((MULT^ | DIV^ | MODULO^) hierarchy)*;

sum : factor ((PLUS^ | MINUS^) factor)*;

comparison  : sum (comparison_operator^ sum)*;

value   : comparison | '(' value ')';

I will not describe each token or rule, as their name fully explains their role. This grammar works well and compiles, allowing me to analyze using valuethings like:

a.b[c(5).d[3] * e()] < e("f")

Interpretation

The only thing left to recognize the values ​​is to have the roots in parentheses. For instance:

(a.b).c
(3 < d()).e
...

Naively and without much expectation, I tried to add the following alternative to my rule root:

root : ... | '(' value ')';

This, however, breaks the rule valuedue to non-LL (*) ism:

rule value has non-LL(*) decision due to recursive rule invocations reachable
from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using 
backtrack=true option.

Break

ANTLR Reference, . , , , , ANTLR , .

?

: :

parameter : type IDENTIFIER -> ^(PARAMETER ^(type IDENTIFIER));

constructor : NEW type PAREN_OPEN (arguments+=value (SEPARATOR arguments+=value)*)? PAREN_CLOSE -> ^(CONSTRUCTOR type ^(ARGUMENTS $arguments*)?);

call : IDENTIFIER PAREN_OPEN (values+=value (SEPARATOR values+=value)*)? PAREN_CLOSE -> ^(CALL IDENTIFIER ^(ARGUMENTS $values*)?);

indexer : IDENTIFIER INDEX_START (values+=value (SEPARATOR values+=value)*)? INDEX_END -> ^(INDEXER IDENTIFIER ^(ARGUMENTS $values*));
+3
1

'(' value ')' value root:

root : IDENTIFIER | SELF | literal | constructor | call | indexer | '(' value ')';

...

value : comparison;

(a.b).c :

enter image description here

(3 < d()).e :

enter image description here

, , , AST:

root : IDENTIFIER | SELF | literal | constructor | call | indexer | '('! value ')'!;

, List += . :

call 
 : IDENTIFIER PAREN_OPEN (values+=value (SEPARATOR values+=value)*)? PAREN_CLOSE 
   -> ^(CALL IDENTIFIER ^(ARGUMENTS $values*)?)
 ;

:

call 
 : IDENTIFIER PAREN_OPEN (value (SEPARATOR value)*)? PAREN_CLOSE 
   -> ^(CALL IDENTIFIER ^(ARGUMENTS value*)?)
 ;

, ( !). , (a) 1 2 value:

value 
 : comparison    // alternative 1
 | '(' value ')' // alternative 2
 ;

: a comparison ( 1) (a), root, '(' value ')'. 2 ! : "" , .

+1

All Articles