How to parse a parenthesized hierarchy root?

2020-07-23 05:56发布

问题:

I'm trying to parse values with ANTLR. Here's 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 won't describe each token or rule since their name is quite explanatory of their role. This grammar works well and compiles, allowing me to parse, using value, things such as:

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

The only thing left for value recognition is to be able to have parenthesized hierarchy roots. For instance:

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

Naively, and without much expectations, I tried adding the following alternative to my root rule:

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

This however breaks the value rule due 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.

Even after reading most of The Definitive ANTLR Reference, I still don't understand these errors. However, what I do understand is that, upon seeing a parenthesis opening, ANTLR cannot know if it's looking at the beginning of a parenthesized value, or at the beginning of a parenthesized root.

How can I clearly define the behavior of parenthesized hierarchy root?

Edit: As requested, the additional rules:

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*));

回答1:

Remove '(' value ')' from value and place it in root:

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

...

value : comparison;

Now (a.b).c will result in the following parse:

And (3 < d()).e in:

Of course, you'll probably want to omit the parenthesis from the AST:

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

Also, you don't need to append tokens in a List using += in your parser rules. The following:

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

can be rewritten into:

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

EDIT

Your main problem is the fact that certain input can be parsed in two (or more!) ways. For example, the input (a) could be parsed by alternative 1 and 2 of your value rule:

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

Run through your parser rules: a comparison (alternative 1) can match (a) because it matches the root rule, which in its turn matches '(' value ')'. But that is also what alternative 2 matches! And there you have it: the parser "sees" for one input, two different parses and reports about this ambiguity.



标签: antlr