Re: Building C/C++ compilers

"Robert J. Simpson" <>
17 Dec 2004 00:33:24 -0500

          From comp.compilers

Related articles
Building C/C++ compilers (Sigurd Lerstad) (2004-12-16)
Re: Building C/C++ compilers (Vidar Hokstad) (2004-12-17)
Re: Building C/C++ compilers (Robert J. Simpson) (2004-12-17)
Re: Building C/C++ compilers (David Lindauer) (2004-12-17)
| List of all articles for this month |

From: "Robert J. Simpson" <>
Newsgroups: comp.compilers
Date: 17 Dec 2004 00:33:24 -0500
Organization: Eurofeeds
References: 04-12-073
Keywords: C++, parse
Posted-Date: 17 Dec 2004 00:33:24 EST

"Sigurd Lerstad" <> wrote

> I'm trying to make a C/C++ compiler.
> In the phase to produce Abstract Syntax Tree (AST) from the source. C/C++
> has a difficult case that has an amiguity
> g * h
> this can mean declare a variable pointer to g named h, or the variable g
> multiplied with h;
> In the above mentioned book, Appel first builds a syntax tree and then
> does
> semantic analysis, i.e. type checking etc.
> but it seems to me that because of the above ambiguity, ast building and
> semantic analysis must be performed at the same time ?

I would advise you to do symbol lookup before parsing. That way you will get
a TYPENAME token or an IDENTIFIER token.

> C++ makes it even more difficult, consider this example:
> class myclass
> {
> int method()
> {
> g* h;
> }
> typedef int g;
> }
> Now, g is declared later in the source, which complicates things even
> more.

Yes, you have to consider the function body to occur 'after' the
declarations. I don't know if it even possible to do this with
bison. You could parse it as 'g times h' and then manually convert
when you find g is a type. My preferred way of doing this is to use a
two pass approach. The first pass can be very crude e.g. just match
'{' and '}' or you can do a full parse and discard the result.

> Does anyone have any pointers to me overcoming these two issues,
> also on the light of using bison as the parser.

Personally I never use yacc or bison where the language is
defined. They are very useful when developing a language but it's such
a small proportion of the total work in writing a compiler and it
really restricts what you can do.


Post a followup to this message

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