[antlr-interest] Ok I got a working lexer and parser as per the tutorial how do I make it actually generate something?

Gavin Lambert antlr at mirality.co.nz
Fri Sep 5 00:53:15 PDT 2008


At 09:25 5/09/2008, Jarrod Roberson wrote:
>What I am having a hard time finding is information on how to 
>make it actually ACT on what I have parsed.

There are several different ways to act in the parser.

The simplest is to simply put actions directly into the parser:

expr : a=term { SetInitialTerm($a.value); }
   ( (o=PLUS | o=MINUS) b=term { PerformOperation($o, $b.value); } 
)*

term returns [int value]: ...;

While simple (in that there are no other files to worry about), 
this can sometimes lead to

Other options are to use "output=template" and then specify 
StringTemplate outputs for each rule, or to use "output=AST" and 
add tree construction operators or rewrites (and optionally write 
a tree walker grammar that in turn uses direct actions or 
StringTemplate to do things).

Examples of all of these approaches are shown in the examples 
archive on the downloads page, and you can find quick references 
to most of the functionality in the Wiki.  For a detailed 
explanation you'll probably want to get the ANTLR book, though.



More information about the antlr-interest mailing list