Home > Archive > Compilers > March 2005 > Parsing C++ Declarations
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Parsing C++ Declarations
|
|
| HalfWayMan 2005-03-18, 3:59 am |
| 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.
Perhaps I'm doing it all wrong. It would be a shame.
Yours,
Blake.
| |
| Brian Inglis 2005-03-20, 4:02 pm |
| On 18 Mar 2005 00:47:53 -0500 in comp.compilers, "HalfWayMan"
<blake.rain@gmail.com> wrote:
>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.
>
>Perhaps I'm doing it all wrong. It would be a shame.
Studying an implementation of cdecl and c++decl, which enumerates all
the parts, but does not define all the properties, may help:
ftp://sunsite.unc.edu/pub/Linux/dev...decl-2.5.tar.gz but
don't forget about such things as incomplete types and tentative
definitions. Some scanning of comp.std.c for thread topics about
definitions and declarations may also help. And you could look at the
GCC front ends for C and C++.
--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a
b[dot]ca)
fake address use address above to reply
| |
| DrDiettrich 2005-03-20, 4:02 pm |
| HalfWayMan wrote:
>
> 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)
identifier.
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
directions.
However you implement your internal representation, make it strictly
sequential!
HTH
DoDi
|
|
|
|
|