[comp.lang.c] Interfacing yacc/lex with C++

burkhard@software.org (Neil Burkhard) (09/20/90)

I'm trying to use yacc/lex in a program I'm developing in C++
using Apollo's C++ system. Can anybody shed some light on
how I can have the yacc action routines invoke C++ routines? Not
obvious how I can have the compiler figure out the proper names since
C++ mangles them. Note: everything is in C++ (including 'main')
except yacc/lex stuff.

Thanks in advance for any helpful comments.
--
---------------------------------------------------------------
Neil Burkhard                        Internet:  burkhard@software.org
Software Productivity Consortium     Phone:     703-742-7154
2214 Rock Hill Road                  
Herndon, VA  22070

"Just when you think you're on easy street, someone rips up
 the pavement"

aardvark@cooper.cooper.EDU (Rob Horn ) (09/20/90)

interfacing C++ with Yacc is messy, but easily done.
Yacc itself produces C code with the users actions imbedded in it
unchanged. Yacc does not care if your actions are valid C

since C++ is a superset of C, the Yacc/Lex generated code can be compiled 
with the C++ compiler. example:

%token SPAM MOOSE FOOBAR . . .
	ferret	: SPAM MOOSE
			{ // C++ code }
		| SPAM SPAM
			{ // C++ code }
		;
%%

Yacc will completely ignore what the actions you give it are and will put
them unchanged into the file it produces.
The version of Yacc I have used produces an old style C code, and thus the 
C++ compiler will print out a bunch of horrible warnings 
"warning: yyparse() old style declaration. type int assumed. " and such,
but will produce no errors, provided the actions are acceptable C++ code.

rich@Rice.edu (Richard Murphey) (09/20/90)

In article <1635@software.software.org> burkhard@software.org (Neil Burkhard) writes:
   I'm trying to use yacc/lex in a program I'm developing in C++
   using Apollo's C++ system. Can anybody shed some light on
   how I can have the yacc action routines invoke C++ routines? Not
   obvious how I can have the compiler figure out the proper names since
   C++ mangles them. Note: everything is in C++ (including 'main')
   except yacc/lex stuff.

I have the beginnings of an extension language written in g++, bison
and flex (free C++, yacc and lex workalikes).  The sources are on
qed.rice.edu (128.42.4.38) in /pub/interp-1.9.tar.Z and are
distributed under the GPL.  The texinfo sources for the documentation
are included.

The bison (or yacc) output is compiled with g++; all you need is a
define or two to take into account the differences in I/O.

Any comments or suggestions on interp-1.9 are welcome!  Rich

olson@sax.cs.uiuc.edu (Bob Olson) (09/20/90)

I have a perl script that will create a c++ class from yacc or lex output.
It is not yet entirely solid, but it does seem to work (I'll be
working on it more when my project actually gets to the point at which
the parser'll be used).

It is available for anonymous ftp on a.cs.uiuc.edu in
tmp/olson/gen.shar.

--bob