Re: Object-oriented AST

Florian Liekweg <>
9 Apr 2006 17:22:03 -0400

          From comp.compilers

Related articles
Object-oriented AST (Thomas Weiss) (2006-04-08)
Re: Object-oriented AST (Florian Liekweg) (2006-04-09)
Re: Object-oriented AST (Chris F Clark) (2006-04-12)
Re: Object-oriented AST (Thomas Weiss) (2006-04-16)
| List of all articles for this month |

From: Florian Liekweg <>
Newsgroups: comp.compilers
Date: 9 Apr 2006 17:22:03 -0400
Organization: University of Karlsruhe, Germany
References: 06-04-055
Keywords: parse, AST
Posted-Date: 09 Apr 2006 17:22:03 EDT

Thomas Weiss wrote:
> Hello,
> [...] I am looking for an
> efficient way to design abstract syntax trees in object-oriented
> languages.

Hello, Thomas,

From your example, it seems that you are trying to turn every BNF rule
into an inheritance relationship. I would advise against doing this
in this strictness. It seems more sensible to me to assemble the
important parts of the AST (i. e. the nodes that match class decls,
methods or fields ... control flow statements (if, for, while),
statements and expressions) and create an inheritance hierarchy
over that dimension. You could have a superclass 'Statement' with
subclasses like 'ForLoop', 'IfBranch', 'WhileLoop' etc, and You
could have 'Expression' as a superclass of 'UnaryExpression' and
'BinaryExpression'. For any BNF-Rule, e. g. "A -> B | C", where
'A' stands for an item which is of importance in the AST, I'd rather
expect the 'A' node to be able to accomodate either a 'B' or a 'C'
as an attribute, not as a subclass. For non-terminals on the BNF side
which exist for technical reasons, you might not want to have an
AST node at all. It might be helpful to grab an open-source compiler
which is implemented in some OO language, and study how it does
this part. Besides other examples, there's kjc, the kaffee java

HTH, yours,
[I agree, it's OK to make each rule into a subtree, but overkill to
try to make each one into a class. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.