Re: HELP: How to parse forward references in C++ classes

kanze@gabi-soft.fr (J. Kanze)
11 Apr 1996 23:41:55 -0400

          From comp.compilers

Related articles
HELP: How to parse forward references in C++ classes tft@fi.muni.cz (Tomas Telecky) (1996-04-10)
Re: HELP: How to parse forward references in C++ classes jsgray@ix.netcom.com (1996-04-11)
Re: HELP: How to parse forward references in C++ classes kanze@gabi-soft.fr (1996-04-11)
| List of all articles for this month |

From: kanze@gabi-soft.fr (J. Kanze)
Newsgroups: comp.compilers
Date: 11 Apr 1996 23:41:55 -0400
Organization: GABI Software, Sarl.
References: 96-04-060
Keywords: C++, parse

Tomas Telecky <tft@fi.muni.cz> writes:


|> I am trying to write some kind of C++ syntactical analyzer using the
|> Roskind's C++ bison grammar. ...


|> There are, however, forward references in C++ classes definitions
|> possible, like


|> 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.


Despite the moderator's comment concerning guessing/backtracking, in
this particular case, I'd just stash away the function body (probably
as a string of preprocessor tokens) until I hit the end of the class,
and scan it there. You have to defer part of the processing until
this point anyway.


The only real disadvantage I can see with this scheme is that any
error messages may appear out of order. Again, however, this must be
true for semantic errors anyway, so a little more shouldn't hurt.


Of course, this doesn't mean that there aren't other places in the C++
grammar where you might want backtracking.
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
--


Post a followup to this message

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