|C Compiler in C++ email@example.com (2002-05-08)|
|Re: C Compiler in C++ firstname.lastname@example.org (2002-05-12)|
|Re: C Compiler in C++ email@example.com (Diego Novillo) (2002-05-12)|
|Re: C Compiler in C++ firstname.lastname@example.org (2002-05-12)|
|Re: C Compiler in C++ email@example.com (2002-05-12)|
|Re: C Compiler in C++ firstname.lastname@example.org (Rodney M. Bates) (2002-05-13)|
|Re: C Compiler in C++ email@example.com (Lex Spoon) (2002-05-13)|
|[6 later articles]|
|From:||firstname.lastname@example.org (Nemanja Stanarevic)|
|Date:||8 May 2002 00:15:31 -0400|
|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
Return to the
Search the comp.compilers archives again.