[antlr-interest] ANTLR AST vs Tree Grammar
Tommy Chheng
tommy.chheng at gmail.com
Sun Oct 25 00:46:40 PDT 2009
I'm using ANTLR to construct a small general purpose compiler. I've used
JFlex/CUP in the past but ANTLR looks like it can help generate better code
in a more streamline process. I'm reading the Language Implementation
Patterns book which has a few ANTLR examples (
http://pragprog.com/titles/tpdsl/source_code)
I'm a little confused about difference between the Tree Grammar and AST
output option in ANTLR.
In walking/tree-grammar folder of (
http://pragprog.com/titles/tpdsl/source_code), there's a lexer/parser
grammar (like VecMath.g) with a header:
options {output=AST;} // we want to create ASTs
This will just produce the parser code(VecMathLexer/VecMathParser), but not
the actual AST?
In the folder, i see a bunch of "VarNode, IntNode..". Were these AST classes
just manually created?
Then if I want to actually produce an AST tree walk output. I would have to
create something like Printer.g:
tree grammar Printer; // this grammar is a tree grammar called Printer
tokenVocab=VecMath; // use token vocabulary from VecMath.g
ASTLabelType=CommonTree; // use homogeneous CommonTree for $ID, etc.
In Printer.g, it looks like a DIFFERENT set of parsing rules? Isn't it
problematic to have 2 sets of grammar parsing rules?
Printer.g:
expr: ^('+' expr {print("+");} expr)
| ^('*' expr {print("*");} expr)
vs
VecMath.g:
expr: multExpr ('+'^ multExpr)* ; // '+' is root node
multExpr
: primary (('*'^|'.'^) primary)* // '*', '.' are roots
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091025/6d23b4b0/attachment.html
More information about the antlr-interest
mailing list