[antlr-interest] Definite (but manageable) bug in antlr

Harmon Nine hnine at isis.vanderbilt.edu
Mon Nov 19 08:42:14 PST 2007


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

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071119/4cb822d9/attachment-0001.html 


More information about the antlr-interest mailing list