[antlr-interest] disambiguating predicates / wrong decision code
Davis, Alan
adavis at ti.com
Wed Jul 21 09:21:57 PDT 2010
I'm trying to use a disambiguating semantic predicate to differentiate expressions involving different-class identifiers. For example "A1+A2" is an "expr" while "V1+V2" is a "vexpr". The problem is ANTLR seems to incorrectly predict a valid sentence, resulting in a syntax error, even though there is no warning about an ambiguity or anything.
Here is the grammar:
-----------------------------
grammar vexpr;
options { language = C; }
program : stat+ ;
stat : vid '=' rhs ';'
;
rhs : vid // handles V2
| vexpr // handles V2 + V3
| expr // handles(?) A1 + A2
;
vexpr : vid '+' vid
;
expr: id
| id '+' id
;
// IDs that start with 'V'
vid : { *(LT(1)->getText(LT(1))->chars) == 'V' }? ID
;
// IDs that don't start with 'V'
id : { *(LT(1)->getText(LT(1))->chars) != 'V' }? ID
;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
WS : (' ' | '\t' | '\r' | '\n')+ { $channel=HIDDEN; }
;
------------------
(The asymmetry in handling atoms is an artifact of the cutdown; if I move vid from rhs to vexpr this problem goes away).
The parser cannot parse the following input: V1 = A1 + A2 ;
When I look at the generated code for rhs (below), it seems to predict alternative 2 (vexpr) if LA(2) is '+' (8). But '+' in that position is not sufficient to disambiguate alternative 2 from alternative 3!
This seems like a bug -- or am I missing something.
Here is the decision code (reformatted for brevity):
switch ( LA(1) )
{
case ID:
{
if ( (LA(2) == 8) ) // '+'
alt2=2; // vexpr
else if ( (( *(LT(1)->getText(LT(1))->chars) == 'V' )) )
alt2=1; // vid
else if ( (( *(LT(1)->getText(LT(1))->chars) != 'V' )) )
alt2=3; // expr
else
{
CONSTRUCTEX();
...
- Alan Davis
Texas Instruments
More information about the antlr-interest
mailing list