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

Passing values in tree walker

Expand Messages
  • Mike Jones
    There is an example tree walker in the documentation that shows how to return a value: class CalcTreeWalker extends TreeParser; expr returns [int r] { int a,b;
    Message 1 of 5 , Sep 3 9:20 PM
    • 0 Attachment
      There is an example tree walker in the documentation that shows how to
      return a value:

      class CalcTreeWalker extends TreeParser;

      expr returns [int r]
      {
      int a,b;
      r=0;
      }
      : #(PLUS a=expr b=expr) {r = a+b;}
      | #(STAR a=expr b=expr) {r = a*b;}
      | i:INT {r =
      Integer.parseInt(i.getText());}
      ;

      If I wanted to pass a value to expr, how would I do it?

      I tried:

      Expr (int i) returns [int r]

      but this does not appear to be valid syntax. Nothing in the
      documentation says you can do this. Is it possible?

      The reason I would like to do it is so I can easily create a list of
      things while recursively processing nodes. Otherwise I have to create
      some kind of global and manage it carefully.

      Mike
    • Terence Parr
      Hi Mike, try expr[int i] returns [int r] : ... ; ... Terence ... -- Professor Comp. Sci., University of San Francisco Creator, ANTLR Parser Generator,
      Message 2 of 5 , Sep 3 10:05 PM
      • 0 Attachment
        Hi Mike, try

        expr[int i] returns [int r] : ... ;

        :)

        Terence

        On Wednesday, September 3, 2003, at 09:20 PM, Mike Jones wrote:

        > There is an example tree walker in the documentation that shows how to
        > return a value:
        >
        > class CalcTreeWalker extends TreeParser;
        >
        > expr returns [int r]
        > {
        > int a,b;
        > r=0;
        > }
        > : #(PLUS a=expr b=expr) {r = a+b;}
        > | #(STAR a=expr b=expr) {r = a*b;}
        > | i:INT {r =
        > Integer.parseInt(i.getText());}
        > ;
        >
        > If I wanted to pass a value to expr, how would I do it?
        >
        > I tried:
        >
        > Expr (int i) returns [int r]
        >
        > but this does not appear to be valid syntax. Nothing in the
        > documentation says you can do this. Is it possible?
        >
        > The reason I would like to do it is so I can easily create a list of
        > things while recursively processing nodes. Otherwise I have to create
        > some kind of global and manage it carefully.
        >
        > Mike
        >
        >
        >
        >
        > Your use of Yahoo! Groups is subject to
        > http://docs.yahoo.com/info/terms/
        >
        >
        >
        --
        Professor Comp. Sci., University of San Francisco
        Creator, ANTLR Parser Generator, http://www.antlr.org
        Co-founder, http://www.jguru.com
        Co-founder, http://www.knowspam.net enjoy email again!
        Co-founder, http://www.peerscope.com link sharing, pure-n-simple
      • Mike Jones
        Terence, I feel stupid! Ok, so to reference it: Expr2 { int i2; } ... ; I tried that and the resulting code fails because it only calls with the AST parameter.
        Message 3 of 5 , Sep 3 10:32 PM
        • 0 Attachment
          Terence,

          I feel stupid!

          Ok, so to reference it:

          Expr2
          {
          int i2;
          }
          : expr[i2]
          ;

          I tried that and the resulting code fails because it only calls with the
          AST parameter.

          Any suggestion?

          Mike

          -----Original Message-----
          From: Terence Parr [mailto:parrt@...]
          Sent: Wednesday, September 03, 2003 11:06 PM
          To: antlr-interest@yahoogroups.com
          Subject: Re: [antlr-interest] Passing values in tree walker

          Hi Mike, try

          expr[int i] returns [int r] : ... ;

          :)

          Terence

          On Wednesday, September 3, 2003, at 09:20 PM, Mike Jones wrote:

          > There is an example tree walker in the documentation that shows how to
          > return a value:
          >
          > class CalcTreeWalker extends TreeParser;
          >
          > expr returns [int r]
          > {
          > int a,b;
          > r=0;
          > }
          > : #(PLUS a=expr b=expr) {r = a+b;}
          > | #(STAR a=expr b=expr) {r = a*b;}
          > | i:INT {r =
          > Integer.parseInt(i.getText());}
          > ;
          >
          > If I wanted to pass a value to expr, how would I do it?
          >
          > I tried:
          >
          > Expr (int i) returns [int r]
          >
          > but this does not appear to be valid syntax. Nothing in the
          > documentation says you can do this. Is it possible?
          >
          > The reason I would like to do it is so I can easily create a list of
          > things while recursively processing nodes. Otherwise I have to create
          > some kind of global and manage it carefully.
          >
          > Mike
          >
          >
          >
          >
          > Your use of Yahoo! Groups is subject to
          > http://docs.yahoo.com/info/terms/
          >
          >
          >
          --
          Professor Comp. Sci., University of San Francisco
          Creator, ANTLR Parser Generator, http://www.antlr.org
          Co-founder, http://www.jguru.com
          Co-founder, http://www.knowspam.net enjoy email again!
          Co-founder, http://www.peerscope.com link sharing, pure-n-simple






          Your use of Yahoo! Groups is subject to
          http://docs.yahoo.com/info/terms/
        • Terence Parr
          ... Hmm...must be a rule name issue...that is the way I pass arguments all the time. :) I ll bet you can check the java.g grammar from the site and you ll see
          Message 4 of 5 , Sep 3 10:34 PM
          • 0 Attachment
            On Wednesday, September 3, 2003, at 10:32 PM, Mike Jones wrote:

            > Terence,
            >
            > I feel stupid!
            >
            > Ok, so to reference it:
            >
            > Expr2
            > {
            > int i2;
            > }
            > : expr[i2]
            > ;
            >
            > I tried that and the resulting code fails because it only calls with
            > the
            > AST parameter.
            >
            > Any suggestion?

            Hmm...must be a rule name issue...that is the way I pass arguments all
            the time. :) I'll bet you can check the java.g grammar from the site
            and you'll see me building trees by passing info down into other rules.
            Could be a weird syntax gotcha too.

            Terence
            --
            Professor Comp. Sci., University of San Francisco
            Creator, ANTLR Parser Generator, http://www.antlr.org
            Co-founder, http://www.jguru.com
            Co-founder, http://www.knowspam.net enjoy email again!
            Co-founder, http://www.peerscope.com link sharing, pure-n-simple
          • Mike Jones
            Terence, I found the problem. There was a second reference to expr without the [i2] farther down in the grammar. I had not noticed that the java error was
            Message 5 of 5 , Sep 4 6:47 AM
            • 0 Attachment
              Terence,

              I found the problem. There was a second reference to expr without the
              [i2] farther down in the grammar. I had not noticed that the java error
              was related to this other expression.

              Thanks, I'm flying!

              Mike

              -----Original Message-----
              From: Terence Parr [mailto:parrt@...]
              Sent: Wednesday, September 03, 2003 11:34 PM
              To: antlr-interest@yahoogroups.com
              Subject: Re: [antlr-interest] Passing values in tree walker


              On Wednesday, September 3, 2003, at 10:32 PM, Mike Jones wrote:

              > Terence,
              >
              > I feel stupid!
              >
              > Ok, so to reference it:
              >
              > Expr2
              > {
              > int i2;
              > }
              > : expr[i2]
              > ;
              >
              > I tried that and the resulting code fails because it only calls with
              > the
              > AST parameter.
              >
              > Any suggestion?

              Hmm...must be a rule name issue...that is the way I pass arguments all
              the time. :) I'll bet you can check the java.g grammar from the site
              and you'll see me building trees by passing info down into other rules.
              Could be a weird syntax gotcha too.

              Terence
              --
              Professor Comp. Sci., University of San Francisco
              Creator, ANTLR Parser Generator, http://www.antlr.org
              Co-founder, http://www.jguru.com
              Co-founder, http://www.knowspam.net enjoy email again!
              Co-founder, http://www.peerscope.com link sharing, pure-n-simple






              Your use of Yahoo! Groups is subject to
              http://docs.yahoo.com/info/terms/
            Your message has been successfully submitted and would be delivered to recipients shortly.