[antlr-interest] Bug in synpreds or error writing the grammar?

Terence Parr parrt at cs.usfca.edu
Tue May 15 18:15:55 PDT 2007


On May 15, 2007, at 11:21 AM, Edson Tirelli wrote:

>
>     All,
>
>     I searched the list archives but found no similar problem  
> report, so I would appreciate your advice.
>     In my grammar, rules automatically generate synpreds. Although,  
> for 2 of them, the generated code presents a compilation error.
>     Snippet:
>
>
>                 (
>                     (
>                     field_and { fc.addRestriction(new  
> RestrictionConnectiveDescr(RestrictionConnectiveDescr.AND)); }
>                     |
>                     field_or { fc.addRestriction(new  
> RestrictionConnectiveDescr(RestrictionConnectiveDescr.OR)); }
>                     )
>                     constraint_expression[fc]
>                 )*
>
>     The above is part of a rule. "fc" is a variable defined  
> previously in that rule. The generated code contains a synpred58  
> and a synpred58_fragment methods for the above lines. The problem  
> is that the synpred58_fragment is making a call to  
> constraint_expression, using the fc variable that is out of the  
> scope for synpred58_fragment:

Yeah, this is an issue.  The fragment will execute w/o actions and so  
fc won't be valid value anyway.  I'm not sure how to make this  
easier.  In the book i recommend what you do next or to use a dynamic  
rule scope to make values available to invoked rules.

>     As a workaround, I added the following lines to the  
> @parser::members {} section:
>
>     // THE FOLLOWING LINES ARE DUMMY ATTRIBUTES TO WORK AROUND THE  
> PROBLEM
>     private BaseDescr from = null;
>     private FieldConstraintDescr fc = null;
>
>     Obviously, with such dummy attributes in the generated Parser  
> class, the code compiles fine and the parser works just fine.
>
>     So, any ideas?

Not sure how to get around in general.  I try to use synpreds and  
autobacktracking infrequently in general, but here's another good  
reason. ;)

Ter



More information about the antlr-interest mailing list