[antlr-interest] Token/TOKENS Rewrite Bug
ANTLR Mailing List
jstpierre-antlr at mecheye.net
Sun Jan 27 15:48:08 PST 2008
Hello there. I'm not sure if this has been fixed, but I have a simple
grammar that ANTLR (in 3.0 or some dev version) generates incorrectly in
Java. It's an easy manual fix, but I'd thought I'll let you know.
Here's my grammar, it's a simple variation of the Expr grammar:
grammar ExprTree;
options {
// language=ActionScript;
output=AST;
ASTLabelType=CommonTree; // type of $stat.tree ref etc...
}
tokens {
SET_VARIABLE = 'SetVariable';
GET_VARIABLE = 'GetVariable';
PUSH = 'Push';
PLUS = '+';
MINUS = '-';
STAR = '*';
SLASH = '/';
PERCENT = '%';
}
prog: ( stat { System.out.println ( $stat.tree.toStringTree ( ) ); } )+ ;
stat: expr NEWLINE -> expr
| ID '=' expr NEWLINE -> ^(ID expr SET_VARIABLE)
| NEWLINE ->
;
expr: v1=multExpr (op=(PLUS|MINUS) v2=multExpr)? -> ^($v1 ($v2 $op)?);
multExpr
: v1=atom (op=(STAR|SLASH|PERCENT) v2=atom)? -> ^($v1 ($v2 $op)?);
atom: INT
| ID -> ^(ID GET_VARIABLE)
| '('! expr ')'!
;
ID : ('a'..'z'|'A'..'Z')+ ;
INT : '0'..'9'+ ;
NEWLINE: ';' ;
WS : (' '| '\t')+ { $channel=HIDDEN; } ;
The problem happens in expr and multExpr; the rewrite of $op. It gets caught
up in PLUS or MINUS (or STAR, SLASH or PERCENT) that it forgets to set op.
Inside the alternative switch statement that sets op, we have this:
switch (alt3) {
case 1 :
// ExprTree.g:25:26: PLUS
{
PLUS9=Token(input.LT(1));
matchStream(input,PLUS,FOLLOW_PLUS_in_expr198);
stream_PLUS.add(PLUS9);
}
break;
case 2 :
// ExprTree.g:25:31: MINUS
{
MINUS10=Token(input.LT(1));
matchStream(input,MINUS,FOLLOW_MINUS_in_expr200);
stream_MINUS.add(MINUS10);
}
break;
}
It sets PLUS9 and MINUS10 without setting op, but op is referenced when
doing the rewrite. The op is just a simple fix of setting op at the same
time it sets PLUS9 (or MINUS10).
Also, Terr, I am interested in developing the ActionScript target (I have
already talked to George Scott). What would I need to do to get my
credentials added?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080127/fe5ff253/attachment.html
More information about the antlr-interest
mailing list