[antlr-interest] problem with including code at a begining of a rule.

Raphael ma_boite_a_spam at yahoo.fr
Mon Dec 5 02:19:29 PST 2005


Dear all,

In my grammar (see below) I need to include come javacode a the begining 
of a rule, ie :
rule :
{ code...}
  alternative1 {code alternative1}
| alternative2  {code alternative1}
...
;

the problem is : this code declares an identifier which is accessed in 
all alternative, but in generated java code, the code is inserted in the 
first alternative code, not at the begining of the procedure. Thus I can 
not access this identifier in alternatives{2,n}. The most surprising is 
that in this same files other rules which use code inclusion at the 
begining works well. Antlr version is 2.7.6 (use of eclipse plug in)

Real grammar and generated java codes are included below (hope they are 
readable in this way)

bodyParam:
{
     List<FetchBodysectionPartSpecifier> partSpecifs = new 
ArrayList<FetchBodysectionPartSpecifier>();
}

            |  (BODY^      LBRACKET! ( arg = partSpecifier 
{partSpecifs.add(arg.getText());} )+ RBRACKET! (LT! partialSection GT!)?)
                          { params.BODYSECTION = true;
                              params.needAllMessageStepThrueParser=true;
                            }
            | (BODY_PEEK^ LBRACKET! ( arg = partSpecifier 
{partSpecifs.add(arg.getText());} )+ RBRACKET! (LT! partialSection GT!)?)
                          { params.BODYSECTION = true;
                            params.PEEK = true;   
                            params.needAllMessageStepThrueParser=true;
                            }
            | (BODY^ |     BODY_PEEK^ {params.PEEK = true;} )      
LBRACKET! RBRACKET! (LT! partialSection GT!)? { params.FULL_BODY=true;}
            ;

---------------------------------------------------------

public final void bodyParam() throws RecognitionException, 
TokenStreamException {
       
       
try {      // for error handling
 if ((_tokenSet_0.member(LA(1))) && (_tokenSet_1.member(LA(2))) && 
(_tokenSet_2.member(LA(3))) && (_tokenSet_3.member(LA(4))) && 
(_tokenSet_4.member(LA(5))) && (_tokenSet_5.member(LA(6))))
{
    List<FetchBodysectionPartSpecifier> partSpecifs = new 
ArrayList<FetchBodysectionPartSpecifier>();
} else if ((LA(1)==BODY) && (LA(2)==LBRACKET) && 
(LA(3)==Dot||LA(3)==PartNumber) && (_tokenSet_7.member(LA(4))) && 
(_tokenSet_8.member(LA(5))) && (_tokenSet_9.member(LA(6)))) {
                {
................


More information about the antlr-interest mailing list