Antlr, rule which could result the same as another

88 views Asked by At

For a compilation project, my group and me are defining a grammar with Antlr. We have currently a problem with theses rules :

expr: ...
| lvalue expr3 expr2
|  ID '('exprList')' expr2
|... ;


lvalue: ID lvalue2;

lvalue2: '.' ID lvalue2
    | '[' expr ']' lvalue2
    | ;

As you can see, lvalue can result in ID, which lead to a non-LL() grammar. So my question is: how can we modify the grammar so as to make it LL() without permitting extra stuff.

Thank you in advance !

1

There are 1 answers

1
Mike Lischke On

Indeed you can refactor the rules, but on the other hand it's not a left recursive construct you use there, so your conclusion this would no longer be LL(k) is not correct. All you have are two alternatives that start with the same token. Factoring that out is, as said, one way to overcome that situation. Another one is to simply increase the lookahead, which allows the parser to also inspect following tokens to decide which alt to take.

You can either set the lookahead value in the grammar options:

options {
  k = 2;
}

or in specific rules to only increase the value for that rule:

expr options { k = 2; }: ...
| lvalue expr3 expr2
|  ID '('exprList')' expr2
|... ;

Note: ANTLR 4 doesn't have this kind of problem as it always has unlimited lookahead.