[antlr-interest] Problem with return values in ANTLR 3

Dr. Kocher, Hartmut h.kocher at pharmatechnik.de
Wed Dec 21 03:01:19 PST 2005


I try to return a boolean value from a rule (instead of the usual
template).

 

Here's the simple rule:

 

checkedState returns [boolean result]

            : 'Checked' '=' ('true' {$result = true; } |'false') ';'

            ;

 

 

Below's the code that's getting generated (ANTLR 3, EA7) (stripped down
to some interesting parts):

 

My thought's on this:

1)       The result class seems to be OK. The Boolean field is added.
Maybe it should be public as well...

2)       The local variable "result" either has the wrong type or is not
needed at all.

3)       Instead of "result = true" I would have expected that
"retval.result = true" would be generated.

4)       Instead of returning "result", it should return "retval" as
usual.

 

Any ideas?

 

BTW: How would one access the result value in another rule?
$checkedState.result ???

 

public static class checkedState_return extends RuleReturnScope {

        boolean result;

        public StringTemplate st;

        public StringTemplate getTemplate() { return st; }

    };

 

    // $ANTLR start checkedState

    public checkedState_return checkedState() throws
RecognitionException {   

 

        checkedState_return retval = new checkedState_return();

        retval.start = (Token)input.LT(1);

        checkedState_return result;

 

 

...

            switch (alt4) {

                case 1 :

                    // C:\XTplus\GUI\ANTLR\GuiParser.g:29:26: 'true'

                    {

 

                    match(input,15,FOLLOW_15_in_checkedState178);

 

                    result = true; 

 

 

                    // TEMPLATE REWRITE

 

 

                    }

                    break;

...

        return result;

 

    }

    // $ANTLR end checkedState

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20051221/fb8c78bf/attachment-0001.html


More information about the antlr-interest mailing list