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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to