[antlr-interest] problem with a simple list rewriter

Stefan Ottosson maxtefan at gmail.com
Mon Jul 20 09:25:49 PDT 2009


Hi,

I've been experimenting with a simple rewriter. The AST construction works
fine and does what I expect, but there must be something about the
interaction between Antlr and Stringtemplate that I don't understand despite
reading the Antlr book, because I can't get even simple things to do what I
want.

Here is an example I've simplified so much there's hardly anything left. I
just want to rewrite (1 2 3) into [1, 2, 3], but the elements disappear on
the way. I was under the impression that it was enough to specify the
rewrites I wanted to make and leave everything else alone with rewrite=true,
so why are the elements null?

Please help me out by explaining or pointing me to some passage in the book
or documentation. I'm very grateful for any help. Thank you.


The input:
(1 2 3)

The output:
     tree=(List 1 2 3)
     body=[null, null, null]
     []

-----------

grammar Example;
options { output=AST; }

tokens {
    List;
}

cu:
        expr+
    ;

expr:
        INT
    |   ID
    |   list
    ;

list:
        '(' expr* ')' -> ^(List expr*)
    ;

..
---------------------

tree grammar Gen;

options {
    tokenVocab=Example;
    ASTLabelType=CommonTree;
    output=template;
    rewrite=true;
}

cu:
        expr+
    ;

expr:
        INT
    |   ID
    |   list
    ;

list:
        ^(List body+=expr*)

        {
            System.out.println("body=" + $body.toString());
        }

        -> list(exprs={$body})
    ;

-----

list(exprs) ::=
<<
[<exprs; separator=", ">]
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090720/606ba3fa/attachment.html 


More information about the antlr-interest mailing list