Re: C Compiler in C++

Lex Spoon <>
13 May 2002 01:09:33 -0400

          From comp.compilers

Related articles
C Compiler in C++ (2002-05-08)
Re: C Compiler in C++ (2002-05-12)
Re: C Compiler in C++ (Diego Novillo) (2002-05-12)
Re: C Compiler in C++ (2002-05-12)
Re: C Compiler in C++ (2002-05-12)
Re: C Compiler in C++ (Rodney M. Bates) (2002-05-13)
Re: C Compiler in C++ (Lex Spoon) (2002-05-13)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (2002-05-17)
Re: C Compiler in C++ (Joachim Durchholz) (2002-05-23)
Re: C Compiler in C++ (Lars Duening) (2002-06-07)
| List of all articles for this month |

From: Lex Spoon <>
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) writes:

> On 8 May 2002 00:15:31 -0400, Nemanja Stanarevic
> <> 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()".


Post a followup to this message

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