[antlr-interest] Code generation problem?

Ronald Albury ronalbury at gmail.com
Wed Jan 25 14:31:26 PST 2012


Please help!

I believe the following section of grammar is valid:

stateSection returns [State result] :
    STATE ztate = stateAttrs
    {
        $result = ztate;
    }
    ( screen = screenSection
    {
        $result.setScreen(screen);
    })?
    ( menu = menuSection
    {
        $result.setMenu(menu);
    })?
   ( periodic = periodicSection
   {
       $result.setPeriodic(periodic);
   })?
   ENDSTATE
   ;

However, there is an issue with the generated code (I have removed some of
the blank lines).  It creates variables for result, ztate, and screen, but
does not correctly generate code for menu and periodic:

  public final State stateSection() throws RecognitionException {
        State result = null;
        State ztate =null;
        Screen screen =null;
        try {
            // C:\\csc\\P\\Pet\\Pet.g:121:37: ( STATE ztate= stateAttrs
(screen= screenSection )? (menu= menuSection )? (periodic= periodicSection
)? ENDSTATE )
            // C:\\csc\\P\\Pet\\Pet.g:122:2: STATE ztate= stateAttrs
(screen= screenSection )? (menu= menuSection )? (periodic= periodicSection
)? ENDSTATE
            {
            match(input,STATE,FOLLOW_STATE_in_stateSection935);
            pushFollow(FOLLOW_stateAttrs_in_stateSection941);
            ztate=stateAttrs();
            state._fsp--;
                 result = ztate;

            // C:\\csc\\P\\Pet\\Pet.g:126:2: (screen= screenSection )?
            int alt2=2;
            int LA2_0 = input.LA(1);
            if ( (LA2_0==SCREEN) ) {
                alt2=1;
            }
            switch (alt2) {
                case 1 :
                    // C:\\csc\\P\\Pet\\Pet.g:126:4: screen= screenSection
                    {
                    pushFollow(FOLLOW_screenSection_in_stateSection953);
                    screen=screenSection();
                    state._fsp--;
                      result.setScreen(screen);
                    }
                    break;
            }

            // C:\\csc\\P\\Pet\\Pet.g:130:2: (menu= menuSection )?
            int alt3=2;
            int LA3_0 = input.LA(1);
            if ( (LA3_0==MENU) ) {
                alt3=1;
            }
            switch (alt3) {
                case 1 :
                    // C:\\csc\\P\\Pet\\Pet.g:130:4: menu= menuSection
                    {
                    pushFollow(FOLLOW_menuSection_in_stateSection968);
                    menuSection();
                    state._fsp--;
                      result.setMenu(menu);

                    }
                    break;
            }
            // C:\\csc\\P\\Pet\\Pet.g:134:2: (periodic= periodicSection )?
            int alt4=2;
            int LA4_0 = input.LA(1);
            if ( (LA4_0==PERIODIC) ) {
                alt4=1;
            }
            switch (alt4) {
                case 1 :
                    // C:\\csc\\P\\Pet\\Pet.g:134:4: periodic=
periodicSection
                    {
                    pushFollow(FOLLOW_periodicSection_in_stateSection983);
                    periodicSection();
                    state._fsp--;
                      result.setPeriodic(periodic);

                    }
                    break;
            }
            match(input,ENDSTATE,FOLLOW_ENDSTATE_in_stateSection992);
            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
         // do for sure before leaving
        }
        return result;
    }
    // $ANTLR end "stateSection"


More information about the antlr-interest mailing list