You may find that if you reverse the order of the '+' and '-' alternatives in the failing version of your rule, you may find that the minus case works and the plus case fails, i.e. the opposite of what you are seeing now. If this hypothesis is correct, then you are probably seeing a quirky known issue in the antlr interpreter. (I'm sorry, I do not recall the bug number.) The first version of your rule works, apparently because the alternatives are "protected" by parens. I hope this helps.
Kyle On Sat, Jan 1, 2011 at 9:11 AM, Stefan Misch <[email protected]> wrote: > Hi, > > I'm using my holdidays to get more in touch with ANTLR. As I now also have > the book I'm working through the samples in chapter 3 using C# as target > language. I noticed a difference between AntlrWorks 1.4.2 interpreter and > the code it generated. > > Using the first grammar sample Expr.g without any actions the interpreter > can parse the simple expression "5-3" and build a graph. If I augment the > grammar with actions the interpreter fails with > "MismatchedTokenException(12!=4)" - token 12 is minus '-', 4 is NEWLINE. He > seems to be unhappy with the slightly altered rule syntax for "expr" in the > augmented version. If I use the same syntax for describing rule "expr" in > the simpler non-augmented grammar the AntlrWorks interpreter fails, too. > > The interpreter works with this rule for "expr": > > expr: multExpr (('+'|'-') multExpr)* > ; > > but fails with this: > > expr: multExpr > ( '+' multExpr > | '-' multExpr > )* > ; > > Please note that it can interprete "5+3" with both ways, just "5-3" fails. > Please also note that the ExprParser.cs file generated for the augmented > grammar with actions works, i.e. the correct result is printed, so the > possible bug is limited to AntlrWorks. > > Thanks, > Stefan > > Here is my complete grammar for Expr.g: > > grammar Expr; > > options { > language=CSharp3; > } > > public prog > : statement+ > ; > > statement > : expr NEWLINE > | assignment NEWLINE > | NEWLINE > ; > > assignment > : ID '=' expr > ; > > expr > : multExpr (('+'|'-') multExpr)* > ; > > multExpr > : atom (('*'|'/') atom)* > ; > > atom > : INT > | ID > | LPAREN expr RPAREN > ; > > INT : ('0'..'9')+; > ID : ('a'..'z'|'A'..'Z'|'_') > ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*; > LPAREN : '('; > RPAREN : ')'; > NEWLINE : '\r'? '\n'; > WS : (' '|'\t')+ {Skip();}; > > > > List: http://www.antlr.org/mailman/listinfo/antlr-interest > Unsubscribe: > http://www.antlr.org/mailman/options/antlr-interest/your-email-address > List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
