[antlr-interest] How do you include a constant string in an AST?

Benjamin S Wolf jokeserver at gmail.com
Tue May 29 19:17:35 PDT 2012


On Tue, May 29, 2012 at 3:49 PM, Chris McConnell <chrimc at microsoft.com> wrote:
> I'm actually generating C++, but I have a syntax to AST grammar which I can easily debug in AntlrWorks and a tree processing grammar which connects to the C++ generator.  This allows me to easily debug the grammar using AntlrWorks.  If I have to drop into the target grammar, I will end up with multiple generator methods and just introduce specific AST nodes for this case.  I was hoping for some way of doing it without having to drop into the target language.  From the documentation it seemed like I could do this:
>
> tokens {ItemID ;}
>
> linkItemID: linkPredicate '(' PROPERTY ',' string ')' -> ^(linkPredicate PROPERTY ^(Eq ^(PropCollection ItemID[')', "ItemID"]) string));
>

I believe anywhere you use the Token[arg1, arg2] rewrite syntax, arg1
has to be a token reference. Java is giving you an error because it's
interpreting ')' as a char. You might be able to use $')' but if not,
try naming the token match with eg. a=')' and use ItemID[$a, ...].

My rules that use this syntax actually only used named tokens, eg.
BAR[$FOO, "0"].


More information about the antlr-interest mailing list