Loading ...
Sorry, an error occurred while loading the content.

Forcing creation of children in an AST tree parser

Expand Messages
  • Simon Kellett
    Hi, I am trying to force the creation of children within a rule that is essentially parsing a function call: activityTimeRange: ACTIVITY^ L_paren! INSTRUMENT
    Message 1 of 3 , Mar 3, 2004
    • 0 Attachment
      Hi,

      I am trying to force the creation of children within a rule that is
      essentially parsing a function call:

      activityTimeRange: ACTIVITY^ L_paren! INSTRUMENT Comma! timeExpr
      R_paren!;

      My AST correctly shows the ACTIVITY as a root and the parens and comma
      dropped. But I would like the parameters (ie INSTRUMENT and timeExpr)
      to be *children* of node ACTIVITY.

      Reading the manual I have tried various things, but "AST Action
      Translation" about "#rule", in the last para seems to give me what I
      want: "For example, any children nodes added after the action will be
      added to the children of #rule.".

      But in the updated rule all I get is an extra "activityTimeRange" node
      and no children, only siblings !!

      activityTimeRange: ACTIVITY^ {#activityTimeRange =
      #([ACTIVITY,"activityTimeRange"], #activityTimeRange);} L_paren!
      INSTRUMENT Comma! timeExpr R_paren!;

      Any ideas/pointers as to where I am going wrong ?

      (would it be useful to have a force-to-child syntax ? (like "^" for
      force-to-root and "!" for drop-token).

      TIA, Simon
    • mzukowski@yci.com
      Show us toStringList() for the result of rule: activityTimeRange: ACTIVITY^ L_paren! INSTRUMENT Comma! timeExpr R_paren!; It should be doing exactly what you
      Message 2 of 3 , Mar 3, 2004
      • 0 Attachment
        Show us toStringList() for the result of rule:

        activityTimeRange: ACTIVITY^ L_paren! INSTRUMENT Comma! timeExpr
        R_paren!;

        It should be doing exactly what you want and I don't see why it isn't.

        Monty

        -----Original Message-----
        From: Simon Kellett [mailto:skellett@...]
        Sent: Wednesday, March 03, 2004 1:24 AM
        To: antlr-interest@yahoogroups.com
        Subject: [antlr-interest] Forcing creation of children in an AST tree parser

        Hi,

        I am trying to force the creation of children within a rule that is
        essentially parsing a function call:

        activityTimeRange: ACTIVITY^ L_paren! INSTRUMENT Comma! timeExpr
        R_paren!;

        My AST correctly shows the ACTIVITY as a root and the parens and comma
        dropped. But I would like the parameters (ie INSTRUMENT and timeExpr)
        to be *children* of node ACTIVITY.

        Reading the manual I have tried various things, but "AST Action
        Translation" about "#rule", in the last para seems to give me what I
        want: "For example, any children nodes added after the action will be
        added to the children of #rule.".

        But in the updated rule all I get is an extra "activityTimeRange" node
        and no children, only siblings !!

        activityTimeRange: ACTIVITY^ {#activityTimeRange =
        #([ACTIVITY,"activityTimeRange"], #activityTimeRange);} L_paren!
        INSTRUMENT Comma! timeExpr R_paren!;

        Any ideas/pointers as to where I am going wrong ?

        (would it be useful to have a force-to-child syntax ? (like "^" for
        force-to-root and "!" for drop-token).

        TIA, Simon




        Yahoo! Groups Links
      • Simon Kellett
        ... isn t. My rule has changed slightly (instrument is now a subrule): activityTimeRange: ACTIVITY^ L_paren! instrument Comma! timeExpr R_paren!; instrument:
        Message 3 of 3 , Mar 4, 2004
        • 0 Attachment
          --- In antlr-interest@yahoogroups.com, mzukowski@y... wrote:
          > Show us toStringList() for the result of rule:
          >
          > activityTimeRange: ACTIVITY^ L_paren! INSTRUMENT Comma! timeExpr
          > R_paren!;
          >
          > It should be doing exactly what you want and I don't see why it
          isn't.

          My rule has changed slightly (instrument is now a subrule):

          activityTimeRange: ACTIVITY^ L_paren! instrument Comma! timeExpr
          R_paren!;
          instrument: "MERIS" | "ASAR";

          and I run it

          Text i/p stream: activity (MERIS, 1234:56)
          toStringList() gives: ( activity MERIS 1234:56 )

          This is some of the generated Parser code;

          else if ((LA(1) == ACTIVITY) && (LA(2) == L_paren) && (LA(3) ==
          LITERAL_MERIS || LA(3) == LITERAL_ASAR) && (LA(4) == Comma) && (LA(5)
          == ABS_TIME || LA(5) == REL_TIME || LA(5) == ORBIT_TIME)) {
          activityTimeRange();
          astFactory->addASTChild( currentAST, returnAST );

          void LmpParser::activityTimeRange() {
          ...
          tmp39_AST = astFactory->create(LT(1));
          astFactory->makeASTRoot(currentAST, tmp39_AST);
          match(ACTIVITY);
          match(L_paren);
          instrument();
          astFactory->addASTChild( currentAST, returnAST );
          match(Comma);
          timeExpr();
          astFactory->addASTChild( currentAST, returnAST );
          match(R_paren);
          activityTimeRange_AST = currentAST.root;
          ...

          Thanks, Simon
        Your message has been successfully submitted and would be delivered to recipients shortly.