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

Alexey Demakov demakov at ispras.ru
Mon Dec 5 02:32:07 PST 2005


1. Declare variables before colon.
2. Initialize them after colon.
3. If you need initialization for all alternatives, wrap them by (  )

rule[ rule parameters] returns [rule return variable]
{ local variable declarations }
:
{ local variable initialization - for all alternatives}
( 
    { local variable initialization - for alt1 only}
    alt1 
  | { local variable initialization - for alt2 only}
    alt2 
)
;
    
All code after colon is executed only if not in guessing mode,
so all initialization and other code should be after colon.
But for correct java code, local variable declarations
used in more than one code fragment should be declared before colon.

Regards,
Alexey

-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com



----- Original Message ----- 
From: "Raphael" <ma_boite_a_spam at yahoo.fr>
To: <antlr-interest at antlr.org>
Sent: Monday, December 05, 2005 1:19 PM
Subject: [antlr-interest] problem with including code at a begining of arule.


> 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