Related articles |
---|
C Compiler in C++ stanarevic@hotmail.com (2002-05-08) |
Re: C Compiler in C++ loewis@informatik.hu-berlin.de (2002-05-12) |
Re: C Compiler in C++ dnovillo@redhat.com (Diego Novillo) (2002-05-12) |
Re: C Compiler in C++ journeyman@compilerguru.com (2002-05-12) |
Re: C Compiler in C++ thp@cs.ucr.edu (2002-05-12) |
Re: C Compiler in C++ rbates@southwind.net (Rodney M. Bates) (2002-05-13) |
Re: C Compiler in C++ lex@cc.gatech.edu (Lex Spoon) (2002-05-13) |
Re: C Compiler in C++ alexc@world.std.com (2002-05-17) |
Re: C Compiler in C++ alexc@world.std.com (2002-05-17) |
Re: C Compiler in C++ journeyman@compilerguru.com (2002-05-17) |
Re: C Compiler in C++ Bart.Vanhauwaert@nowhere.be (2002-05-17) |
Re: C Compiler in C++ joachim_d@gmx.de (Joachim Durchholz) (2002-05-23) |
Re: C Compiler in C++ lars@bearnip.com (Lars Duening) (2002-06-07) |
From: | Lex Spoon <lex@cc.gatech.edu> |
Newsgroups: | comp.compilers |
Date: | 13 May 2002 01:09:33 -0400 |
Organization: | Georgia Institute of Technology |
References: | 02-05-039 02-05-055 |
Keywords: | C, OOP |
Posted-Date: | 13 May 2002 01:09:33 EDT |
journeyman@compilerguru.com (journeyman) writes:
> On 8 May 2002 00:15:31 -0400, Nemanja Stanarevic
> <stanarevic@hotmail.com> wrote:
>
>
> >I organized the parse tree so that class PTN (Parse tree node) is a
> >super-class for all node classes. Than, I derive nodes such as
> >PTNBinaryOperation, PTNAssignment, PTNIf, PTNSequence (sequence of
> >statements), etc. Please note that in this implementation a node can
> >have 0..n children.
>
> I've found this approach is somewhat problematical. When you parse an
> expression, you have to return a pointer/reference to the base class.
> The operand of a binary expression can be any expression, not just,
> say, an assignment expression. This means you pretty much either wind
> up downcasting (casting base to derived, possibly using tag fields in
> a switch statement, defeating the whole OO paradigm), or putting
> specialized virtual functions into the base class (working around
> encapsulaion) for each tree walking pass. Either way, it can get
> pretty ugly quickly if you're not careful.
Isn't this the situation whenever you use inheritance? Is it more
often the case with parse tree nodes, that you want to know their
specific subclass?
The party line in OO design is that whenever you need to know the
precise class, you should instead send a message to the object and let
the object do something appropriate. Because different classes will
respond with different methods, the message lookup itself will do the
check for you. In the extreme, if you want to "frob" every if
statement, you can even make a message like frobIfYouAreAnIf() and
send it to the object; in the base class it does nothing, but in PTNIf
it does "this.frob()".
Lex
Return to the
comp.compilers page.
Search the
comp.compilers archives again.