[antlr-interest] unexpected token: null
lignucius
subscribeshit at gmx.net
Thu May 8 06:14:55 PDT 2003
hi all
i always get the error specified in the subject.
but i don't see the mistake i made apparently! maybe you do?
the code:
remark: the string i passed to the lexer was "sin(4)". no CR or LF or
EOF
after that. a couple lines in code are for testing purpose only.
class TestCalcParser extends Parser;
options { buildAST=true; }
imaginaryTokenDefinitions :
SIGN_MINUS
SIGN_PLUS
;
expr : sumExpr ;
sumExpr : prodExpr ((PLUS^|MINUS^) prodExpr)* ;
prodExpr : powExpr ((MUL^|DIV^|MOD^) powExpr)* ;
powExpr : funcExpr (POW^ funcExpr)? ;
funcExpr : (SIN^ | COS^ | TAN^ | SQRT^ | LN^ | LG^ | LD^)? signExpr ;
signExpr : ( m:MINUS^ {#m.setType(SIGN_MINUS);} | p:PLUS^
{#p.setType(SIGN_PLUS);} )? atom ;
atom : DBL | LPAREN^ expr RPAREN! | VAR ;
class TestCalcLexer extends Lexer;
options { k=5; }
NEWLINE : ( "\r\n" // DOS
'\r' // MAC
'\n' // Unix
)
{ newline();
$setType(Token.SKIP);
}
;
PLUS : '+' ;
MINUS : '-' ;
MUL : '*' ;
DIV : '/' ;
MOD : '%' ;
POW : '^' ;
LPAREN : '(' { System.out.println("found ("); } ;
RPAREN : ')' { System.out.println("found )"); } ;
protected SIN : "sin" ;
protected COS : "cos" ;
protected TAN : "tan" ;
protected SQRT : "sqrt" ;
protected LN : "ln" ;
protected LG : "lg" ;
protected LD : "ld" ;
protected E : "e" ;
protected PI : "pi" ;
protected UCGLYPH : 'A'..'Z' ;
protected LCGLYPH : 'a'..'z' ;
protected VAR : (UCGLYPH | LCGLYPH)+;
TXT : (SIN) => SIN { $setType(SIN); System.out.println("found SIN");
}
| (COS) => COS { $setType(COS); }
| (TAN) => TAN { $setType(TAN); }
| (SQRT) => SQRT { $setType(SQRT); }
| (LN) => LN { $setType(LN); }
| (LG) => LG { $setType(LG); }
| (LD) => LD { $setType(LD); }
| (E) => E { $setType(E); }
| (PI) => PI { $setType(PI); }
| (VAR) => VAR { $setType(VAR); System.out.println("found VAR"); }
;
protected DIGIT : '0'..'9' ;
protected POINT : '.' ;
DBL : (DIGIT)+(POINT (DIGIT)+)? { System.out.println("found DBL");
} ;
{import java.lang.Math;}
class TestCalcTreeWalker extends TreeParser;
expr returns [double r]
{ double a,b; r=0; }
: #(PLUS a=expr b=expr) { r=a+b; }
| #(MINUS a=expr b=expr) { r=a-b; }
| #(MUL a=expr b=expr) { r=a*b; }
| #(DIV a=expr b=expr) { r=a/b; }
| #(MOD a=expr b=expr) { r=a%b; }
| #(POW a=expr b=expr) { r=Math.pow(a,b); }
| #(LPAREN a=expr) { r=a; }
| #(SIGN_MINUS a=expr) { r=-1*a; }
| #(SIGN_PLUS a=expr) { if(a<0) r=0-a; else r=a; }
| #(SIN a=expr) { r=Math.sin(a); }
| #(COS a=expr) { r=Math.cos(a); }
| #(TAN a=expr) { r=Math.tan(a); }
| #(SQRT a=expr) { r=Math.sqrt(a); }
| #(LN a=expr) { r=Math.log(a); }
| #(LG a=expr) { r=Math.log(a)/Math.log(10); }
| #(LD a=expr) { r=Math.log(a)/Math.log(2); }
| E { r=Math.E; }
| PI { r=Math.PI; }
| v:VAR { System.out.println(v.getText()); }
| i:DBL { r=Double.parseDouble(i.getText()); }
;
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list