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

Robin Davies rerdavies at rogers.com
Mon Jun 25 05:43:56 PDT 2007


These two alternatives are in the wrong order:

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

When the first alternative matches functionCall, the rule is done, and no 
attempt is made to match ('*'|'/').




----- Original Message ----- 
From: "John B. Brodie" <jbb at acm.org>
To: <tgleason at gmail.com>
Cc: <antlr-interest at antlr.org>
Sent: Sunday, June 24, 2007 3:02 PM
Subject: Re: [antlr-interest] Why doesn't this work?


>
> 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