[comp.lang.c++] Using LEX & YACC with c++

smith-josh@CS.YALE.EDU (Josh Smith) (08/08/89)

Here is a summary of the responses to my question about whether
it is possible to use lex and yacc with C++.

The consensus seems to be that it can be done without much difficulty.

smb@ulysses.att.com says
> Yes, but the stack cannot be of a type that contains any constructors.
I'm not sure which stack he means, but it doesn't sound like a problem
one runs into often.

Leo Breebart (leo@duttnph.tudelft.nl) warns that
> [Lex] throws spurious "#include <stdio.h>" into your code, which
> screws up an inclusion of <stream.hxx> you're almost certain to want.
and recommends flex&bison (bison is from GNU) instead of lex&yacc.

I have found that lex.yy.c (the lex output file) contains one
"#include <stdio.h>"; it worked fine when I changed this to 
"#include <stream.h>".  There may be other circumstances in which
more references to <stdio.h> are included, but I imagine that one
could put a call to awk or sed in the makefile to automatically
change from <stdio.h> to <stream.h> in any case.

Breebart also points out that it's easy to keep the analyzer in C,
which is what he has done.

C. K. Tan (tan@cs.wisc.edu) points out that you get a lot of warnings during
compilation (and believe me, he's right) because of the differences in the
way arguments to functions are declared.  I suppose Dan Schonfeld's
suggestion (below) would clear them up.

Dan Schonfeld (dbs@mtunk.att.com until August 18;
schonfeld-dan@cs.yale.edu thereafter) says that
> The only modification that I had to make was with the
> function definitions. I'm using AT&T's cfront 2.0 which is based off Ansi C,
> and the yacc function definitions are non-Ansi C, so I had to put in a couple
> of prototypes here and there. Also, I had to change some of the external names
> that yacc produces because of conflicts with C++ libraries (I believe the
> conflict arose from the Strings library, but I'm not positive). I just use
> sed and change every occurence of 'yy' to 'YY' and 'U' to 'YyYyU' and have
> no problems. With other versions of C++ you probably will have to make similar
> patches.

Bill Gibbons (bgibbons@apple.com) warns that
> [Y]ou have to avoid the // comment style, since YACC doesn't understand it.

I got other messages saying "yes," "proceed with confidence," etc..

	Josh Smith
	smith-josh@cs.yale.edu