Re: HELP: How to parse forward references in C++ classes (Jan Gray)
11 Apr 1996 23:40:12 -0400

          From comp.compilers

Related articles
HELP: How to parse forward references in C++ classes (Tomas Telecky) (1996-04-10)
Re: HELP: How to parse forward references in C++ classes (1996-04-11)
Re: HELP: How to parse forward references in C++ classes (1996-04-11)
| List of all articles for this month |

From: (Jan Gray)
Newsgroups: comp.compilers
Date: 11 Apr 1996 23:40:12 -0400
Organization: Netcom
References: 96-04-060
Keywords: C++, parse

In 96-04-060 Tomas Telecky <> writes:
> [how do I handle forward refs to possibly typedef'ed names when parsing
> C++ ? e.g. ...]
>class X { X(){ i = 0; } int i; };
>The "i" identifier is used before it is declared in class X (at least
>lexically). The lexer must return correct value for "i" though.

To do this correctly in general is much, much, much harder than you
might think. Getting the grammar approximately right is perhaps 10%
of the design effort. If you need to do a good job at this, I
recommend you start with an existing, working front end source base.

Your particular scenario is relatively easy, though. Most compilers
use some variation on buffering the post-preprocessed tokens of the
bodies of inline member functions and then parsing them after the
entire class definition has been seen, but before anything following
the class definition is seen.

Don't forget nested classes' inline member functions, and also that
inline member function definitions can themselves contain local class
definitions with their own nested classes and inline member functions
and so on. You might be happier simply not handling such cases. Many
constructions which are possible in C++ are also incredibly rare in
practice (e.g. found only in validation suites.)

Don't forget to tag your tokens with their original line numbers so
diagnostics refer to the correct line, rather than the last line of
the class definition.

The rest of this etc., it is quite a challenge for a lexical analyzer to
correctly determine whether identifiers are types or not.

What a mess is C++. Incredible, daunting, unnecessary complexity.
Darn thing is, I use a comfortable subset of it daily.

Jan Gray
Redmond, WA

(Former coimplementor of an incremental C++ compiler. Improbable but

Post a followup to this message

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