[antlr-interest] Return Values from Tree Walker rules

Johnson, Janeen JOHNSJA at intelsat.com
Wed Jan 17 10:49:18 PST 2007


Thank you to all who work on Antlr.

I get warnings about every return value in my .g file when I generate
code, but the only thing I found about it appears at the end of this
message.  Clearly the rest of my code is useless, structured as it is,
if I should be interpretting the warning as an error message.  If the
use of return values when I get a warning is the crux of my problem,
please skip to my "Note" at the end of this message.  

If the warnings are not really errors in my case, do additional items in
the returnAST override the return values when the code gets back to the
"calling" rule?  Can this overwriting be circumvented?

Here is a portion of my print-statement-laden code's output and the
corresponding walker part of my parser.g file.  Can anyone shed some
light on what is overwriting the returned values?

First, my print statements' output from the Walker code with my comments
in << comment >>:

attribName is: STATUS				<< Strictly a value --
not involved in any calculations, but something I want to keep in the
AST -- OK >>
identifier = safe-symbol				<< Properly
parsed value  -- OK >>
q = safe-symbol  				<< From within the
bowels of the "statement" rule  -- OK >>
pinName going into status_value is 11000	<< From within the
bowels of the "statement" rule -- OK >>
value returned from statement (st2) is the	            << From a
"statement" within a "statement".  The value "the" (a token value needed
for correct parsing logic) is returned instead of my return value from
evaluating this "statement" -- not OK >>
rtnValue after safe_symbol is: unk                       << From the
bowels:  The expected rtn value and the correct value to return from the
"statement" rule even though I had an unexpected value from the
sub-statement above -- OK, I guess, although scary since I had the
erroneous value above >>
Found delimStmt and result is safe-symbol	<< Why wasn't "result"
my return value -- unk?? -- not OK >>
Exception:  java.lang.NullPointerException	<< From the java that
calls the walker -- my walker fails to return an acceptable value and
doesn't issue any clue about where it failed -- not OK  -- Is there a
code generation option to help me get some diagnostic output? What is
the syntax of that?  Is the option included in the "java antlr.Tool
parser.g" statement line or is it included somewhere else? (I've tried
every combo of placement I can think of for the -trace-related options
and never get generated code with diagnostic output so I know I'm doing
something wrong.)  >>

<partial walker code>

startRule returns [String result]
        { result = "";
        }
        : ((dls: delimStmt)+) EOF{ result = dls.toString();} ;

delimStmt returns [String dSresult]
        { dSresult = "";
          String naString = "NA";
        }
        :  ((attribName: ID COMMA)?
                { System.out.println("attribName is: " +
attribName.toString());
                }
           ) (STMTOPEN value: statement
                {
                        dSresult = value.toString();
                        System.out.println("Found delimStmt and result
is " +  value.toString());
                }
           STMTCLOSE SEMI BOOL (ID)* )
          | NA {dSresult = naString;
                System.out.println("dSresult = " + dSresult);
               }
          ;
statement returns [String rtnValue] {
        rtnValue = "";
	// within the statement rule, there are 4 or five options that
each populate rtnValue.  I refer to them in the output as the "bowels"
of the statement rule.  In the generated code, rtnValue is returned in
each case and I print out the value I've calculated in the code and it
is what I would expect to be returned.  My return value from
"statement", however, is not what I expected.

===================================================== NOTE below
=================================================
Note:  I assume that the warning I get about return values is coming
from here in the code generation code (found this by googling), but I
don't understand the ramifications.
// Warn about return value if any, but not inside syntactic predicate if
(!(grammar <javascript:searchRef('grammar')>  instanceof LexerGrammar
<javascript:searchRef('LexerGrammar')> ) && syntacticPredLevel
<javascript:searchRef('syntacticPredLevel')>  == 0 && rs
<javascript:searchRef('rs')> .block <javascript:searchRef('block')>
.returnAction <javascript:searchRef('returnAction')>  != null) { println
<javascript:searchRef('println')> ("Warning: Rule '" + rr
<javascript:searchRef('rr')> .targetRule
<javascript:searchRef('targetRule')>  + "' returns a value"); } 

If I'm totally screwed by the "ramifications" can anyone help me
off-list to learn a strategy to get the returnAST to contain what I
need?

Thanks for bearing with me.  Two previous emails were unanswered on or
off-list so if I've done or am doing something wrong in asking these
questions, please point it out -- on or off list.  There were a couple
of other questions about return values in the list archives, but they
never received any on-list answers either, so I'm at a loss.

Janeen
Intelsat is the world's largest provider of fixed satellite services.  Real-time, constant communications with people anywhere in the world is closer, by far.  

For more information about Intelsat, visit www.intelsat.com. 

 

This email message is for the sole use of the intended recipients and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Intelsat, Ltd. and its subsidiaries.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070117/43e79842/attachment.html 


More information about the antlr-interest mailing list