Related articles |
---|
Object-oriented AST dev@weiss.nom.fr (Thomas Weiss) (2006-04-08) |
Re: Object-oriented AST liekweg@gmx.de (Florian Liekweg) (2006-04-09) |
Re: Object-oriented AST cfc@shell01.TheWorld.com (Chris F Clark) (2006-04-12) |
Re: Object-oriented AST dev@weiss.nom.fr (Thomas Weiss) (2006-04-16) |
From: | Florian Liekweg <liekweg@gmx.de> |
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
compiler.
HTH, yours,
FLorian
[I agree, it's OK to make each rule into a subtree, but overkill to
try to make each one into a class. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.