Re: Parsing C++ Declarations

DrDiettrich <drdiettrich@compuserve.de>
20 Mar 2005 11:13:13 -0500

          From comp.compilers

Related articles
Parsing C++ Declarations blake.rain@gmail.com (HalfWayMan) (2005-03-18)
Re: Parsing C++ Declarations Brian.Inglis@SystematicSW.ab.ca (Brian Inglis) (2005-03-20)
Re: Parsing C++ Declarations drdiettrich@compuserve.de (DrDiettrich) (2005-03-20)
| List of all articles for this month |

From: DrDiettrich <drdiettrich@compuserve.de>
Newsgroups: comp.compilers
Date: 20 Mar 2005 11:13:13 -0500
Organization: Compilers Central
References: 05-03-069
Keywords: C++, parse
Posted-Date: 20 Mar 2005 11:13:13 EST

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



Post a followup to this message

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