[antlr-interest] Qes: How to control AST- Combine multi-rules into one

Johnicholas Hines johnicholas.hines at gmail.com
Fri Jun 16 10:24:45 PDT 2006


I'm an antlr newbie, but perhaps this walker (intended to run with the
calc example in the antlr examples folder) would suit your needs. NB:
I have no idea if this compiles or runs.

class CalcTreeWalker extends TreeParser;
expr returns [String r]
{
        float a,b;
        r= "";
}
        :       #(PLUS a=expr b=expr)   { r = a+b; }
        |       #(STAR a=expr b=expr)   { r = a+b; }
        |       i:INT                   { r = i->getText(); }
        ;

On 6/16/06, Charlie Yang <Cyang at infobal.com> wrote:
> Hi, Johnicholas Hines:
>
> Thanks for your kind reply.
> I considered using tree walker to seperate actions from parser/grammar.
> However, I'm quite new with Antlr and all examples i found about treewalker
> are almost similar with parser. so I did not understand why do samething
> twice. Now I get it  but, still, fail get a good example to start with. do
> you by chance have an example/docs about treewalker for me? your help will
> be very appreciated.
>
> Charlie
>
>
>
> -----Original Message-----
> From: Johnicholas Hines [mailto:johnicholas.hines at gmail.com]
> Sent: Thursday, June 15, 2006 6:51 PM
> To: Charlie Yang
> Subject: Re: [antlr-interest] Qes: How to control AST- Combine multi-rules
> into one
>
>
> Could you perhaps first parse the expression and then walk the tree
> generated?
>
> The AST that antlr builds is not N-ary; it is some finite number-ary,
> depending only on your grammar.
>
> If you print out the AST that antlr is building for you, you will see
> it is actually binary or whatever, and then you can easily write the
> treeparser in that language.
>
> Johnicholas
>
> On 6/15/06, Charlie Yang <Cyang at infobal.com> wrote:
> > Hi, All, I get a problem with AST handling:
> >
> > for parsing a general function call like:
> >        ......
> >        Foo("first", "2ndpara", 3);
> >        ......
> >
> > I can use rules like:
> >        ...
> >        functioncall_paras :
> >                    OPEN_PAREN  (one_argu ( COMMA one_argu )*)?
> CLOSE_PAREN!
> >        ...
> >
> > the rule works fine.
> >
> > However, i don't know how to concatenate all argus text together in the
> AST.
> > I want all argument combine to single leaf node rather than one argu on
> leaf
> > node.
> > I can use "oa" and oc.getText() to get first argument's text. but how to
> get
> > the rest of "ob" , can between 0 to n?
> >
> >  functioncall_paras! :
> >    OPEN_PAREN!  ( oa:one_argu ( COMMA ob:one_argu )*)? CLOSE_PAREN!
> >    {
> >        String rtn="";
> >        if(#oa!=null){
> >                rtn = "(" + #ae.getText() ;
> >                if(#ob != null){
> >                        //????????? Qestion Here ????????????
> >                        rtn=rtn +","+#ae2.getText();
> >                }
> >
> >        }
> >        oa.setText(rtn);
> >        #functioncall_paras=#oa;
> >    }
> >    ;
> >
> > Thanks,
> >
> > Charlie
> >
>


More information about the antlr-interest mailing list