[antlr-interest] Beginner questions

Damien damien.internet at gmail.com
Mon Jan 7 08:25:59 PST 2008


Hello,

I'm new to Antlr but I have already bought the book and done some 
reading and practicing using ANTLRWorks 1.1.5.
I have written a simple grammar (for simple maths, including function 
calls) which compiles. The grammar requires the option "backtrack=true".
I have 3 questions:

1) My grammar runs fine on the input "d+f(-2)" but the interpreter fails 
with "FailedPredicateException". Is "backtrack=true" impossible in 
interpreted mode?

2) Using the debugger on the input "d+f(-2)", the part of the parse tree 
from atom > functionExpr > .. appears in double, the one on the left 
being green. I can't find what that means.

3) I'm using java as the target language. I can't find how the access 
the parse tree, only the AST with the option output=AST.


Thank you,

damien


grammar Expr;

options { output=AST; k=2; backtrack=true; }

prog    :    stmt+;

stmt    :    expr NEWLINE
    ;
   
expr    :    addExpr;

addExpr    :    '-'? (atom | mulExpr) (('+' | '-') (atom | mulExpr))*
    ;
   
mulExpr    :    atom (('*' | '/') atom)+
    ;
   
parExpr    :    '(' expr ')'
    ;

functionExpr
    :    ID '(' expr (',' 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