[antlr-interest] Why doesn't this work?

John B. Brodie jbb at acm.org
Sun Jun 24 12:02:46 PDT 2007


Tim Gleason asked (in part):
>I'm sorry for bothering you guys.  I've been translating a grammar
>from a book into ANTLR and I can't seem to get passed a problem with
>expressions and parenthesis.  At the bottom of included a subset of
>the grammar that demonstrates the problem.
>
>For some reason the parser gets confused when it sees '('.  It doesn't
>seem to know if it is grouping an expression or the parameters of a
>function call.  But I don't /think/ the grammar is ambiguous.
>
>The simplest case that fails is the input to the 'statement' rule:
>
>foo();
>
>It will fail to parse at rule 'expression'.  But if I remove the line
>marked with an asterisk, it parses fine.  I don't understand how one
>affects the other.
>
>Any help would be greatly appreciated!
>...snipped...

I can not explain why. But it would seem that your predicate in the
expression rule is causing something to go awry. If I replace your
expression rule with:

expression
        :       functionCall ( ('*'|'/') functionCall )*
        ;

then all of the strings "foo;", "foo*2;", "(foo);", and "foo();" are
parsed without complaint.

Hope this helps...
    -jbb


More information about the antlr-interest mailing list