[antlr-interest] Newbie questions: How to traverse the AST ...
Andy Tripp
antlr at jazillian.com
Fri Dec 29 09:28:37 PST 2006
Agnisys wrote:
> Hi,
> I need to pick up useful pieces of information from a 3rd party file. The file format is simple
> field based with possible quoted text. I've written the lexer and parser for the file (.g). I
> would have used Perl but decided to try Antlr in the hope that when the 3rd party changes the
> format I would have to do little to accommodate the changes.
>
> I have a couple of questions related to this :
>
> 1. What is the standard idiom to traverse the AST ? Any examples would help. How do I get to
> the relevant information in the Java code?
>
The simplest way to traverse the AST is something like:
AST child = parent.getFirstChild();
while (child != null) {
// do your work
child = child.getNextSibling();
}
Here's an example that traverses an AST to print pretty Java code:
http://www.jazillian.com/antlr/emitter.html
The more precise/complex/official way to traverse an AST is with an
ANTLR "treewalker"...you define a "grammar" for
the structure of the AST, and then add actions to that grammar. See the
ANTLR documentation for details. Here's a treewalker that also
pretty-prints Java code:
http://antlr.org/share/1101058268251/java.tree.g
> 2. Is there an AWK mode that simply splits the input into fields?
>
No, I don't think so, but I'm sure an ANTLR lexer that just splits the
input into whitespace-separated fields
would be quite trivial :) Sound to me like ANTLR might be overkill for
what you need to do. ANTLR (and other
tools) are really geared toward getting a deep understanding of the
structure and meaning of a non-trivial language.
If you can do what you need in Perl or AWK then you probably should. Or
use some very simple Java.
> 3. How to deal with quoted comments that can contain elements from the language itself.
> For example :
> ABC="123 456=789"
> The '=' should be skipped inside the double quote.
>
Here, you can embedd ANTLR "actions" inside your lexer.
> 4. Is the antlr-interest group postings search-able on any site?
>
I just use "site:antlr.org whatever" at google.
> Thanks for any pointers for any of these questions. This would enable me to move forward.
> Anupam.
>
> P.S. Anxiously waiting for the Antlr book. I found this to be very helpful:
> http://www.cs.usfca.edu/~parrt/course/652/lectures/antlr-lexers.html
>
>
>
>
More information about the antlr-interest
mailing list