|Parsing C++ Declarations email@example.com (HalfWayMan) (2005-03-18)|
|Re: Parsing C++ Declarations Brian.Inglis@SystematicSW.ab.ca (Brian Inglis) (2005-03-20)|
|Re: Parsing C++ Declarations firstname.lastname@example.org (DrDiettrich) (2005-03-20)|
|Date:||20 Mar 2005 11:13:13 -0500|
|Posted-Date:||20 Mar 2005 11:13:13 EST|
> I have been writing compilers for some time now, but one thing that
> always seems to bug me is parsing of C and C++ declarations. Perhaps
> it's not the parsing that I have the problem with, more the
> representation of the declaration afterwards. In a current project I
> am representing declarations as a list of either specifiers or
> declarators. However, this leads to problems and holes in type
> comparisons and conversion. I was wondering if any of you knew of an
> elegent way of representing this information.
In my own parser I separate declarations into 3 parts:
1) common prefix, up to the first "(", "*" (etc.) or identifier.
2) central declarator, up to and including the (possibly missing)
3) postfix (arrays, argument lists)
The central part is constructed recursively in case of parentheses,
after a ")" the 3 parts of the finished inner level are merged into
the outer level parts. This merge is a bit tricky, I'm used to and I'm
actually using Pascal declaration style internally. The resulting
internal type definition is based on a sequence of modifiers like
pointer-to, array-of, procedure-returning, etc., in a sequential
representation of the recursive C definition.
This sequential representation allows to expand used typedefs by
replacing the typedef name, at the end of a sequential definition, by
its own seqential representation. For type comparison, conversion, and
other purposes, my sequential definitions can be parsed in both
However you implement your internal representation, make it strictly
Return to the
Search the comp.compilers archives again.