Hi,

This question is so rudimentary that I am almost embarrassed to ask.  But since I almost never try to use ANTLRWorks for my parsers, I'll risk injuring my pride in exchange for learning something.
If I paste the Expr.g *verbatim* from 
http://www.antlr.org/works/help/tutorial/content/Expr.g into ANTLRWorks 1.3.1 
and feed it the following test input:

3+1
3-1

both run (via the Run menu) fine and produce the expected numerical outputs.  But for the same test input, the ANTLRWorks interpreter produces the expected parse tree for only 3+1 and gives a MisMatchedTokenException on the '-' in 3-1.  If I reverse the '+' and '-' alternatives in rule expr, the results are also reversed: it's the second alternative that goes bad in the ANTLRWorks interpreter. 
Thinking this might have something to do with the embedded actions which the 
interpreter does not understand, I stripped them all out.  That leaves us 
with the following rule, for which the interpreter runs without error on our test 
input:

expr
    :   multExpr
        (           ( '+' multExpr | '-' multExpr )         )*
    ;

So I figured I was right about actions causing problems.  But wait.  
Let's dig deeper.  This second rule

expr
    :   multExpr
        (           ( '+' multExpr ) | ( '-' multExpr )         )*
    ;

works in the interpreter as expected for the first alternative (used for 3+1) but produces a MisMatchedTokenException for the second alternative (used for 3-1).
And better yet, this third rule

expr
    :   multExpr
        (           ( ( '+' multExpr ) | ( '-' multExpr ) )
        )*
    ;

works great in the interpreter for both 3+1 and 3-1, just like the first rule 
does.

All three rules actually run (from the Run menu) as expected.  Of course, running them 
isn't very interesting with the actions stripped out, but they do run without error.  
So I suspect that they would all produce equally viable parsers outside ANTLRWorks, but I have 
not checked.  Have I stumbled onto an issue with the interpreter embedded in 
ANTLRWorks, or have I done something silly?  (Or both?)

Thanks,
Kyle

Attachment: signature.asc
Description: OpenPGP digital signature

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to