Related articles |
---|
Translating to Intermediate Representation kcraft@mailexcite.com (Keith Craft) (2000-12-06) |
From: | "Keith Craft" <kcraft@mailexcite.com> |
Newsgroups: | comp.compilers |
Date: | 6 Dec 2000 00:09:20 -0500 |
Organization: | Compilers Central |
Keywords: | code |
Posted-Date: | 06 Dec 2000 00:09:20 EST |
Hi,
I'm trying to build a lexer/yaccer using flex/bison that will input
C/C++ code and output a translation to an intermediate representation of an
experimental language. I think I've built my symbol tables properly, and I
have the C/C++ grammar I intend to use. Basically, I'm not sure of the way
in which to go about doing the actual translation, emitting new intermediate
code, and building the abstract syntax tree. So far, for instance, I have
(in my bison grammar):
exp: exp '+' exp {$<exp>$ = new Expresssion($1, $3, ADD_TYPE);} ... and so
on for all arithmetic expressions.
Expression is a class which takes two Expressions and the type of operation
as an argument. Thus far, I believe this to be correct. Unfortunately, I'm
stuck as to how to proceed in similar fashion for functions, loops, and
other types of statements. Here's what they look like in my grammar:
program:
| element program;
element: declaration
| EXTERN fun_prot ';'
| fun_prot ';'
| function ;
decl:
| declaration decl ;
stmts:
| statement stmts ;
declaration: std_type variable ne_var_list ';' ;
std_type: VOID {{$<msym>$ = getStatType(0, false);} // size = 0; name =
s_int0
| INT {{$<msym>$ = getStatType(32, false);} // size = 32;
name = s_int32
| CHAR {{$<msym>$ = getStatType(8, false);} // size = 0;
name = s_int8
| FLOAT {{$<msym>$ = getStatType(32, true);} // size = 32
;
ne_var_list:
| ',' variable ne_var_list;
variable: ptr C_STRING array ;
ptr:
| '*' ;
array:
| '[' C_INTEGER ']' array ;
fun_prot: std_type ptr C_STRING '(' arg_list ;
arg_list: ')'
| arg ne_arg_list ;
ne_arg_list: ')'
| ',' ELLIPSIS ')'
| ',' arg ne_arg_list ;
arg: std_type ptr string ;
string:
| C_STRING
function: fun_prot '{' decl stmts '}';
statement: assign ';'
| cond
| loop
| fcall ';'
| RETURN arith_exp ';' ;
assign: loperation C_STRING array
| C_STRING array roperation ;
loperation: OP_PP
| OP_MM ;
roperation: OP_PP
| OP_MM
| OP_EQ rhs ;
rhs: assign
| fcall
| arith_exp ;
cond: IF '(' bool_exp ')' '{' decl stmts '}'
else_part ;
else_part:
| ELSE '{' decl stmts '}'
loop: for
| while
| do_while ;
for: FOR '(' assign ';' bool_exp ';' assign ')'
'{' decl stmts '}' ;
while: WHILE '(' bool_exp ')' '{' decl stmts '}' ;
do_while: DO '{' decl stmts '}' WHILE '(' bool_exp ')'
';' ;
fcall: C_STRING '(' parlist ')'
parlist:
| par ne_par_list ;
ne_par_list:
| ',' par ne_par_list ;
par: ptr C_STRING array
| constant ;
constant: C_INTEGER
| C_FLOAT
| Q_STRING ;
exp: exp '+' exp {$<exp>$ = new Expresssion($1, $3, ADD_TYPE);}
....
and then, in a separate C file, the definition of Expression:
class expr
{
public:
expr(int e_t, expr *n1, expr *n2, int t)
{
expr_type = e_t;
type = t;
nd1 = n1;
nd2 = n2;
}
expr(int e_t, expr* n1, int t)
{
expr_type = e_t;
type = t;
nd1 = n1;
nd2 = NULL;
}
private:
int expr_type;
int type; // variables, constanst, expression, assign, or function call
expr *nd1, *nd2;
};
How would I define a function class? Statement class?
Here's my symbol table:
class mysmtab
{
public:
msymtab(int s)
{
size = s;
char temparr[10];
sprintf(temparr, "%d", s);
name = "sint";
name += temparr;
}
msymtab *find(string fstring)
{
msymtab * temp;
msymtab *found;
msymtab *rptr = NULL;
temp = c_tables;
while (temp != NULL)
{
if ( (fstring == temp->name) && (temp->isActive))
{
found = temp->name;
}
}
rptr = found;
return found;
}
msymtab *findstatic(int size)
{
msymtab *temp;
temp = st_table;
while (temp != NULL)
{
if ( (size == temp->size) )
return temp;
}
}
private:
string name; // Id of variable
int type; // enum for array type, signed integer, float, array, function
int size; // 0, 8, 32
int scope;
bool isActive;
float value; // value for constants
string strconst; // value for constants
bool arglist; // Does have ...?
bool externflag; // If extern function (1) static type table
list<msymtab *> ar_sizes;
msymtab *base_ptr; // What "Type" a var is
msymtab *st_table; // Static type table
msymtab *ct_table; // Const type table
msymtab *mv_table; // Experimental Language Variable Tables
msymtab *c_tables;
msymtab *next;
}
How does this look? What am I missing?
Thanks in advance for all your help -- it comes much appreciated.
-Keith
Return to the
comp.compilers page.
Search the
comp.compilers archives again.