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

Edson Tirelli tirelli at post.com
Tue May 15 11:21:34 PDT 2007


    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:

    // $ANTLR start synpred58
    public final void synpred58_fragment() throws RecognitionException {
        //
/home/etirelli/workspace/jboss/jbossrules/drools-compiler/src/main/resources/org/drools/lang/DRL.g:1031:6:
( ( field_and | field_or ) constraint_expression[fc] )
        //
/home/etirelli/workspace/jboss/jbossrules/drools-compiler/src/main/resources/org/drools/lang/DRL.g:1031:6:
( field_and | field_or ) constraint_expression[fc]
        {
        //
/home/etirelli/workspace/jboss/jbossrules/drools-compiler/src/main/resources/org/drools/lang/DRL.g:1031:6:
( field_and | field_or )

        /** ALL THE CODE IN HERE **/

        pushFollow(FOLLOW_constraint_expression_in_synpred582720);
        constraint_expression(fc);
        _fsp--;
        if (failed) return ;

        }
    }
    // $ANTLR end synpred58

    It obviously generates the "fc cannot be resolved" error in the java
compilation. I have 2 cases like that in my grammar and I'm not sure if it
is a bug or if I should be writing something differently.

    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?

    Thank you in advance.

    Edson

-- 
  Edson Tirelli
  Software Engineer - JBoss Rules Core Developer
  Office: +55 11 3529-6000
  Mobile: +55 11 9287-5646
  JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070515/1b9cd557/attachment.html 


More information about the antlr-interest mailing list