[antlr-interest] Re: Understanding the Java Grammar,
or Kelcy Grammer
Xue Yong Zhi
seclib at seclib.com
Wed Jan 25 11:43:52 PST 2006
sporty wrote:
> I read the Java grammar included w/ antlr, after trying to deal with mathematical equations. I successfully got a parser in the form, similar to the java one, to work:
>
> eplus : NUMBER ((PLUS^|MINUS^) NUMBER)* ;
> etimes : eplus ((TIMES^|DIV^) eplus)* ;
>
> I can wholeheartedly accept that it works, but I do not understand why as yet. I would expect and equation of the form 1*2+3*4+5 not to parse. After it parses 1*2+3, I would expect it not to get to 4, since the token after 3 to be a *, and eplus specifies +'s and -'s to follow.
>
I suggest you run the code in a debugger to see how the parser works.
In short, if there is no "*"(I am talking things like (TIMES^|DIV^)
eplus)*) in the rules, sure it won't. But in your case, when the parser
sees TIMES after 3, it is back to the 'etimes' rules.
--
Xue Yong Zhi
http://seclib.blogspot.com
More information about the antlr-interest
mailing list