C Compiler in C++

stanarevic@hotmail.com (Nemanja Stanarevic)
8 May 2002 00:15:31 -0400

          From comp.compilers

Related articles
C Compiler in C++ stanarevic@hotmail.com (2002-05-08)
Re: C Compiler in C++ loewis@informatik.hu-berlin.de (2002-05-12)
Re: C Compiler in C++ dnovillo@redhat.com (Diego Novillo) (2002-05-12)
Re: C Compiler in C++ journeyman@compilerguru.com (2002-05-12)
Re: C Compiler in C++ thp@cs.ucr.edu (2002-05-12)
Re: C Compiler in C++ rbates@southwind.net (Rodney M. Bates) (2002-05-13)
Re: C Compiler in C++ lex@cc.gatech.edu (Lex Spoon) (2002-05-13)
[6 later articles]
| List of all articles for this month |
From: stanarevic@hotmail.com (Nemanja Stanarevic)
Newsgroups: comp.compilers
Date: 8 May 2002 00:15:31 -0400
Organization: http://groups.google.com/
Keywords: C, question
Posted-Date: 08 May 2002 00:15:31 EDT

Dear colleagues -
I am writing a complete C compiler in C++ as an academic exercise and
I need an advice related to the design of parse tree data structure
for C declarations.


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.


However, I have no idea how to go about representing C variable
declarations in the parse tree. The problem is that C grammar itself
is very loose on rules for variable declarations. For example: extern
int const unsigned volatile static foo; is syntactically correct, but
semantically doesn't make sense. Also, int const const const const
usigned bar; is both syntactically and semantically correct, but
should emit a warning about multiple const qualifiers.


It almost looks like that I have to have a parse tree node class for
each production in C grammar related to declarations (such as:
declaration, declaration_list, declaration_specifiers,
init_declarator_list, etc.). Once this parse tree is generated, I
would have to go trough that structure and derive declarations in
terms of typing system that I have in place (class hierarchy to
support basic types, pointers to types, structs, unions, arrays of
types, etc.) and report errors/warnings. But this is so terribly ugly
- there is way too many parse tree nodes that need to be defined, and
it seems redundant since I already have type hierarchy in place.


Does anyone know of a good way (or generally accepted way) of
representing the parse tree for C declarations using a similar class
hierarchy?


Thanks,
Nemanja Stanarevic


Post a followup to this message

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