Object Oriented Compiler Design Problem

Dominique Boucher <dboucher@locus.ca>
13 Sep 1998 22:28:21 -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: Dominique Boucher <dboucher@locus.ca>
Newsgroups: comp.compilers
Date: 13 Sep 1998 22:28:21 -0400
Organization: Compilers Central
References: 98-09-019
Keywords: OOP, design

mayurnaik@my-dejanews.com wrote:
> 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


The problem here is that you need multiple dispatch if you don't want
to use RTTI or union with a type field. Here is a possible solution
with a very low overhead (only one virtual method lookup).


First, make 'add' a pure virtual function that takes only one
parameter, and define two other virtual functions:


class constant;
class variable;


class symbol
{
    // ...
    virtual symbol* add(symbol*) = 0;


    virtual symbol* addToConstant(constant*) = 0;
    virtual symbol* addToVariable(variable*) = 0;
    // ...
};


Now, in class 'constant', implement these virtual functions:


class constant : public symbol
{
    // ...
    virtual symbol* add(symbol* sym)
        { return sym->addToConstant(this); }
    virtual symbol* addToConstant (constant* cst)
        { return new constant(cst->val + this->val); } // constant folding
    virtual symbol* addToVariable (variable* var)
        { /* ... gen intermediate code and return temporary var. */ }


    //...
};


You need to do the same in class 'variable':


class variable : public symbol
{
    // ...
    virtual symbol* add(symbol* sym)
        { return sym->addToVariable(this); }
    virtual symbol* addToConstant (constant* cst)
        { /* ... gen intermediate code and return temporary var. */ }
    virtual symbol* addToVariable (variable* var)
        { /* ... gen intermediate code and return temporary var. */ }


    //...
};


The first production rule now becomes:


#1 term : term '+' primary { $$ = $1->add($2); }




The only problem with this solution is that class 'symbol' on the its
subclass, but this can certainly by solved using an intermediate class
(a la Visitor pattern).


Hope this helps!


--Dominique
+---------------------------------+----------------------------+
| Dominique Boucher, M.Sc. |Email: dboucher@locus.ca |
| Software Designer |Tel : (514) 954-3804 |
| Locus Dialogue Corporation |Fax : (514) 954-3805 |
--


Post a followup to this message

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