AST Construction

Mathias edited this page Jan 26, 2011 · 5 revisions
Clone this wiki locally

Contrary to the parse tree, which is very closely tied to the grammar by its direct relation to the grammar rules, the Abstract Syntax Tree you might want to construct for your language heavily depends on your exact project needs. This is why parboiled takes a very open and flexible approach to supporting it.
There are absolutely no restrictions on the type of your AST nodes. parboiled does provide a number of immutable and mutable base classes you might choose to use, however, there is nothing that forces you to do so. Take a look at the org.parboiled.trees package to get started.

Typically you would use the parser value stack as the “work bench” for constructing your AST. If you work with parboiled for Java you should take a look at the Calculators examples to get an understanding of how this might be done.
For parboiled for Scala the JSON Parser examples are worth a look.