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

Simon Woods simon.woods at virgin.net
Thu Apr 3 10:45:42 PDT 2008


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