Object Oriented Compiler Design Problem

mayurnaik@my-dejanews.com
5 Sep 1998 01:20:58 -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)
[2 later articles]
| List of all articles for this month |

From: mayurnaik@my-dejanews.com
Newsgroups: comp.compilers
Date: 5 Sep 1998 01:20:58 -0400
Organization: Deja News - The Leader in Internet Discussion
Keywords: OOP, design, comment

Hi,
I am developing a C Compiler using C++ as the host language. I am facing an
Object Oriented Design Problem


A part of the design is as under:


class symbol {
// data structures for representing the type specifier,
// declarators, etc. of a 'C' data type e.g. int, char**,
// etc.
public:
// ...
};




A 'C' variable is represented as under:


class variable : public symbol {
char* name;
// ...
};


Considering only integer constants for the moment, an integer
constant in C is represented as under:


class constant : public symbol {
int val;
// ...
};




The attributes associated with the grammar symbols presented below are
as under:


variable --> char*
constant --> int
primary --> symbol*
term --> symbol*


Consider the production rules:


#1 term : term '+' primary { ??? }


#2 | primary { $$ = $1; }


;


#3 primary : variable { $$ = new variable($1.name); }


#4 | constant { $$ = new constant($1.val); }


;




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


I want a suitable Object Oriented Solution to this problem. I want to avoid
the solution using 'a union with a type field'. Also, would RTTI be a
suitable solution? Does it not impose a large overhead?


Thanks


Mayur Naik


alternate e-mail: fd95005@bits-pilani.ac.in
[I suspect a union with a type field is it. RTTI in this case would amount to
the same thing. -John]
--


Post a followup to this message

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