[antlr-interest] AST + template output ???

Matthew Bowman matthew.bowman at sogotech.com
Fri Sep 28 00:28:59 PDT 2007


Just got my hands on _the_ book ... great book Ter!

However, I'm a bit confused about "Rewriting the Token Buffer..." The 
current problem that I have is with a mini-scripting language that will 
make heavy use of "aliases". For example, a built-in function can have 
the syntax "foo(x)" where x is a number. But I want someone to be able 
use an alias like "bar" such that "foo(0)" is actually what gets 
interpreted by the tree walker. Currently I have a lexer/parser grammar 
for AST construction and a tree grammar for walking/interpreting. I can 
see the power of output=template from the examples in the book, but I 
can't see how they fit in the "pipeline" of  Lexer -> TokenStream -> 
Parser -> NodeStream -> Walker. All of the examples show calling 
tokens.toString() to get the rewritten token buffer. That makes me think 
I need two sets of parsers. One to actuall "preprocess" any aliases and 
then one to parse the preprocessed input into an AST. Then finally my 
tree walker could interpret the AST. Wouldn't this require me to pretty 
much duplicate the parsing logic in the AST parser I currently have? Or 
am I completely missing the "big" picture of output=template?

Further, the book has a "Rewriting the Token Buffer with Tree Grammars" 
(Section 9.8) example that still ends up doing the same thing in the end 
- calling tokens.toString() to get the rewritten buffer. My initial 
thought would be to actually replace the token for "bar" [BAR] with the 
tokens for "foo(x)" [FOO LPAREN NUM RPAREN] but I just don't see how 
that is possible if at all.

Am I an r-tard or is what I want to accomplish not even feasible with ANTLR?

PS - If you don't have the book ... go buy it! It's totally worth it.

-- 
Matthew Bowman




More information about the antlr-interest mailing list