[antlr-interest] Definite (but manageable) bug in antlr
Terence Parr
parrt at cs.usfca.edu
Mon Nov 19 11:38:25 PST 2007
Hi. Looks familiar. Can you try 3.1beta?
http://www.antlr.org/download/build/antlr-2007-10-29.14.tar.gz
Ter
On Nov 19, 2007, at 8:42 AM, Harmon Nine wrote:
> Hello.
>
> I’ve found a bug in antlr -- in particular, a necessary set of
> parentheses is not being generated in the output java code.
>
> Suppose you have a grammar that, in part, looks like this.
>
> statement
> : ( reference ASSIGN )=> reference ASSIGN expr
> | expr
> ;
>
> reference
> : IDENTIFIER ( {X || Y || Z}?=> paren_arglist )
> ;
>
> expr : reference
> | INTEGER
> ;
>
> In the generate Java code, the syntactic predicate “( reference
> ASSIGN )” is tested using a method “synpred1”. Due to lookahead,
> the syntactic and semantic predicates are combined into a single
> antecedent in an “if” expression:
>
> if ( synpred1() && X || Y || Z ) { … }
>
> Note the problem: the semantic predicate “X || Y || Z” needs to be
> enclosed in parentheses for the antecedent to be processed
> properly, due to the precedence of “&&” over “||”. Antlr currently
> does not generate these parentheses.
>
> A workaround is, of course, to put the parentheses in manually in
> the semantic predicate:
>
> reference
> : IDENTIFIER ( { (X || Y || Z) }?=> paren_arglist )
>
>
> NOTE: antlr WILL generate proper debug code, i.e. when executing
> antlr with the “-debug” option. This is because the debug code
> looks like this:
>
> if ( synpred1() && evalPredicate( X || Y || Z ) ) { … }
>
> This, due to the implicit grouping of “X || Y || Z” as an argument
> of the method call “evalPredicate,” evaluates the antecedent properly.
>
>
> Cheers
> -- Harmon
>
More information about the antlr-interest
mailing list