Using a grammar parser for Python and constructing files from the tree
Question: I have a custom made grammar for an interpreted language and I am looking for
advice on a parser which will create a tree which I can query. From the
structure I would like to be able to generate code in the interpreted
language. Most grammar parsers that I have seen validate already existing
code. The second part of my question is should the grammar be abstracted to
the point that the Python code will substitute symbols in the tree for actual
code terminology? Ideally, I would love be be able to query a root symbol and
have returned all the symbols which fall under that root and so forth all the
way to a terminal symbol.

Any advice on this process or my vocabulary regarding it would be very
helpful. Thank you.


Answer: I'd use ANTLR. Version 3 (current) supports [generating Python
code](http://www.antlr.org/wiki/display/ANTLR3/Antlr3PythonTarget). It will
generate an Abstract Syntax Tree (AST) automatically during parsing, which you
can then traverse. An important part of this will be annotating your grammar
with which tokens are to be treated as subtrees (e.g. operators).

