[antlr-interest] How to suppress tokens with rewrite rules

Simon Woods simon.woods at virgin.net
Thu Apr 3 14:33:50 PDT 2008


OK. Sorted  ... perhaps not the best way (as my lack of knowledge means 
that I'm unaware whether there are better ways to do this) but it seems 
to work...

forstatement
    : ^(FOR ID)
      -> {%{$ID.text}}
    ;

getstatement
    : ^(GET (s+=ID)+)
      -> listcolumns(columns={$s})
//      -> template(ids={$s}) "<ids; separator=\",\">" in-line version

The definitive ref arrived today!


> Hi again
>
> In trying to learn antlr, I've been trying to find out the best way to 
> remove certain tokens through rewrite, but I'm not sure of the best way.
>
> The tree grammar will receive a forstatement "FOR tableX" and a 
> getstatement "GET columnX,columnY ..."
>
> I'm simply trying to suppress the FOR and GET keywords
>
> I have tried with a tree grammar/string template
>
> ===Tree Grammar ===
>
> mainstatement
>     : forstatement getstatement
>       -> 
> translate(forClause={$forstatement.text},getClause={$getstatement.text})
>     ;
>    
> forstatement
>     : ^(FOR ID)
>       -> striptoken(text={$ID.text})
>     ;
>
> getstatement
>     : ^(GET ID+)
>       -> striptoken(text={$ID.text})
>     ;   
>
>
> ==== STG ====
>
> group QL2SQL;
>
> translate(forClause, getClause) ::= <<
> SELECT <getClause; separator=","> FROM <forClause>
>  >>
>
> striptoken(text) ::= <<
> <text>
>  >>
>
> Now I'm *sure* this isn't the best way, nor does it work for the GET ID+ 
> rule as I only get the last get entry in the comma separate value list 
> of columns ...
>
> Thanks again
>
> Simon



More information about the antlr-interest mailing list