[comp.lang.c++] Use of yacc/bison and g++

gordon@stats.ucl.ac.UK (08/24/88)

[I am posting for a friend, so excuse me if the message is a little
"second-hand".] Should it be possible to include C++ statements in a
yacc file, "compile" with yacc (or better bison?) and compile the
result with g++?
Gordon Joly.

Surface mail: Dr. G.C.Joly, Department of Statistical Science,
      University College London, Gower Street, LONDON WC1E 6BT, U.K.
E-mail:  					   | Tel: +44 1 387 7050
 JANET (U.K. national network) gcj@uk.ac.ucl.stats |      extension 3636
         (Arpa/Internet form: gcj@stats.ucl.ac.uk) | FAX: +44 1 387 8057
Relays: ARPA,EAN: @nss.cs.ucl.ac.uk                |
	CSNET: %nss.cs.ucl.ac.uk@relay.cs.net      |
        BITNET: %ukacrl.bitnet@cunyvm.cuny.edu, @ac.uk
        EARN: @ukacrl.bitnet, @AC.UK
By uucp/Usenet: ....!uunet!mcvax!ukc!stats.ucl.ac.uk!gcj

travis@mojo.columbia.edu (Travis Lee Winfrey) (08/30/88)

In article <8808240932.AA08805@prep.ai.mit.edu> gordon%stats.ucl.ac.uk@NSS.CS.UCL.AC.UK writes:
>Should it be possible to include C++ statements in a
>yacc file, "compile" with yacc (or better bison?) and compile the
>result with g++?

Yes, it *should* be possible.  I've been doing that all summer with Yacc/CC.  I
haven't used bison/g++, only because of the bugs and general flakiness of g++.
(spare me the flames, I'm sure it will become a fine compiler.)  The action
statements in yacc are only translated, with $$, $1, etc., being rewritten
appropriately.  Getting the typing right can be a mild problem.  Generally, it
works exactly as you would expect it to.

The biggest problems I had were with CC, not with g++, and they both involved
static initializers:

1) when I used the default main() in liby.a, main() wasn't compiled by CC, so
_main() wasn't called from main(), so any static initializers of variables were
*not* called.  This causes subtle bugs in your programs if you don't use
streams IO, and immediate core dumps if you do.

2) peripherally related to YACC, I had problems with an initial yylex() which
used cin, didn't use cout, and dumped core immediately on trying to read a
character.  This is easy to repeat, as 

	#include <stream.h>
	main()
	{ char ch;
	  cin >> ch ; 
	}

dumps core with our streams library.  If any reference to cout is made, even if
it can never be called (e.g., "if ( x && ! x ) cout.put(x);"), then the program
doesn't dump core.  CC always failed, but g++ handled it correctly.

I tracked this down to the tied_to field of the cin structure, which contains
the address of cout.  cout wasn't initialized because it was never called.  I
didn't follow the bug into the cout routines, because I could patch it here:
since tied_to was not called if it was null, the fix was just to add "(void)
cin.tie(0);" to the program.  This bug has probably been fixed in later
versions of streams, but it's a pretty special case.

t
Arpa:	travis@mojo.cs.columbia.edu 	Bitnet: travis@cu20b
Usenet: rutgers!columbia!travis        Dragnet: "just the facts, ma'am"
USMail:	483 Mudd, Columbia Univ., NYC 10025   Phone: 212-280-8091