[antlr-interest] AST rewrite rules issue with -> ^('+' $elts+)
Damien
damien.internet at gmail.com
Wed Jan 16 03:06:22 PST 2008
Hello,
I'm writing a grammar for a calculator.
I have the following working:
mulExpr : atom ('*' (atom))+ -> ^('*' atom+);
which is equivalent to the following, also working:
mulExpr : elts+=atom ('*' (elts+=atom))+ -> ^('*' $elts+);
I have an issue with the following:
addExpr : (elts+=mulExpr | elts+=atom) ('+' (elts+=mulExpr |
elts+=atom))* -> ^('+' $elts+);
If I don't comment out the rewite rule above, the AST is empty.
Why is this wrong?
Thanks a lot
Complete grammar:
grammar Expr;
options { output=AST; backtrack=true; } //k=2;
prog : stmt+;
stmt : expr (NEWLINE | EOF);
expr : addExpr;
parExpr : '(' e=expr ')' -> $e
;
addExpr : //'-'? (mulExpr | atom) (('+' | '-') (mulExpr | atom))*
(elts+=mulExpr | elts+=atom) ('+' (elts+=mulExpr | elts+=atom))*
-> ^('+' $elts+)
;
mulExpr : atom ('*' (atom))+ -> ^('*' atom+)
;
functionExpr
: ID '(' expr (',' expr)* ')' -> ^(ID expr+)
;
atom : INT | ID | functionExpr | parExpr
;
ID : ('a'..'z')+;
INT : '0'..'9'+;
NEWLINE : '\r'? '\n';
WS : (' ')+ {skip();};
More information about the antlr-interest
mailing list