3
votes

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")

Interpretation

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.

Break

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
could you post your complete parser file? (What's constructor, call, indexer, for instance?) I'm not an ANTLR Guru, but I've built things like that in the past (compilers - using Lex/Yacc for the Parser part - to be precise), and I might be able to help you.Dr.Kameleon
I truly believe those are irrelevant to the question, but I will add them to the question.Lazlo
Just an idea (it may seem stupid, but I'd try it if I were you) : create another element superroot : PAREN_OPEN root PAREN_CLOSE and see what happens. (I hoped I didn't mess up the syntax but you get my point...)Dr.Kameleon
or basically : super : PAREN_OPEN root PAREN_CLOSE | rootDr.Kameleon
I could have added | '(' root ')' instead of | '(' value ')', but that would only allow other root elements to be parenthesized, ant not things such as (3 < d()).e.Lazlo

1 Answers

1
votes

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:

enter image description here

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

enter image description here

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.