[antlr-interest] ANTLRWorks: Why do these rules behave differently in the embedded interpreter?

kferrio at gmail.com kferrio at gmail.com
Sun Dec 13 16:52:06 PST 2009


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091213/43447067/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 271 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20091213/43447067/attachment.bin 


More information about the antlr-interest mailing list