[antlr-interest] Re: output code generation in c++

Andy Tripp antlr at jazillian.com
Tue Aug 1 09:25:29 PDT 2006


Scott Amort wrote:

> Hi,
>
> dotnet fr wrote:
>
>> In the antlr steps :
>> -create the grammar file (parse the file and generate AST)
>> -generate code in c++.
>> Now I want to use my AST to generate ouput code (for example .hpp, cpp)
>> Is it available in Antlr 2.7.6 ? Is it possible ?
>
>
> This is slightly different.  If I understand you correctly, you are 
> attempting to convert some language construct to C++?  Certainly it is 
> possible, but not in any automatic way.  Your options would be to: (1) 
> create a TreeWalker and output your C++ code to stream or file from 
> within the various nodes; or (2) take the AST generated by your 
> grammar and manually walk it, outputting in a similar way to 1.
>
> Of course, this option does not require that your actual 
> lexer/parser/treewalker be C++ code.  You could set ANTLR to use a C#, 
> Java or Python target, and then make use of the StringTemplate code 
> available now.
>
> Here are a couple of links to discussion for and against using a 
> manual approach versus StringTemplate:
>
> http://www.antlr.org/article/1136917339929/stringTemplate.html
> http://www.codegeneration.net/tiki-read_article.php?articleId=77


See also this article about the pros and cons (ok, mostly cons) of 
treewalkers vs walking an AST "by hand":
http://www.jazillian.com/articles/treewalkers.html

>
> Best,
> Scott
>



More information about the antlr-interest mailing list