Re: Object Oriented Compiler Design Problem

Dennis Brueni <brueni@ipass.net>
13 Sep 1998 22:29:00 -0400

          From comp.compilers

Related articles
Object Oriented Compiler Design Problem mayurnaik@my-dejanews.com (1998-09-05)
Re: Object Oriented Compiler Design Problem qjackson@wave.home.com (Quinn Tyler Jackson) (1998-09-13)
Re: Object Oriented Compiler Design Problem qjackson@wave.home.com (Quinn Tyler Jackson) (1998-09-13)
Re: Object Oriented Compiler Design Problem dwight@pentasoft.com (1998-09-13)
Object Oriented Compiler Design Problem dboucher@locus.ca (Dominique Boucher) (1998-09-13)
Re: Object Oriented Compiler Design Problem brueni@ipass.net (Dennis Brueni) (1998-09-13)
Re: Object Oriented Compiler Design Problem mikee@cetasoft.cog (1998-09-13)
Re: Object Oriented Compiler Design Problem jucie@uol.com.br (JuciÍ Dias Andrade) (1998-09-13)
Re: Object Oriented Compiler Design Problem danwang+news@cs.princeton.edu (Daniel C. Wang) (1998-09-18)
| List of all articles for this month |

From: Dennis Brueni <brueni@ipass.net>
Newsgroups: comp.compilers
Date: 13 Sep 1998 22:29:00 -0400
Organization: Compilers Central
References: 98-09-019
Keywords: OOP, design

mayurnaik@my-dejanews.com wrote:
>
> Consider the production rules:
>
> #1 term : term '+' primary { ??? }
>
> [...]
>
> In production rule #1, I have to add two symbols. If BOTH are constants, I
> have to compute the result (which will be a pointer to an object of class
> constant) and return it. Else, I have to generate Intermediate Code and
> return a Compiler Generated temporary name (which will be a pointer to an
> object of class variable)
>
> The function symbol* add(symbol*, symbol*) cannot be made a virtual function
> of class symbol, since it is a friend function. But, the moment it is a
> friend function, it does not know whether the symbols to be added are
> Constants or Variables


There are ways to formulate this in a fully OO fashion, tho, you might
want to think through what implications such an approach might have on
your ability to do later code optimizations, etc.


First thing to fix: don't use friend relationships.


Second thing. You need to make the 'ability to be added' a property
of your 'symbol' class. For example, class 'symbol' could have a pure
virtual method for evaluating a symbol, and another for adding it to
another symbol.


          // returns 0 if 'this' may be evaluated, nonzero otherwise
          virtual int valueOf(int &v) const = 0;


          // add 'this' to 's1' and return new symbol
          virtual symbol *addTo(const symbol *s1) const = 0;


addTo is more powerful than the 'add' function you were going after,
because it knows the type of 'this' and based on the result of valueOf,
can decide whether the addition involves a constant of variable.


The disadvantage to this approach is that it can grow quite intricate
when you scale it up to the tons of operators present in the C language.


Anyway, hope this helps,


--Dennis B.
--


Post a followup to this message

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