Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!husc6!seismo!munnari!otc!mikem From: mikem@otc.UUCP Newsgroups: comp.lang.c++ Subject: Re: Yacc and lex with C++; is it possible? Message-ID: <98@otc.OZ> Date: Thu, 19-Mar-87 17:56:12 EST Article-I.D.: otc.98 Posted: Thu Mar 19 17:56:12 1987 Date-Received: Sun, 22-Mar-87 23:19:00 EST References: <4475@columbia.UUCP> Organization: O.T.C. Systems Development, Australia Lines: 64 Summary: Yacc is almost ok, lex isn't. In article <4475@columbia.UUCP>, beshers@sylvester.columbia.edu (Clifford Beshers) writes: > > How well do yacc and lex work with C++? Are there major problems, minor > kludges. Must yacc and lex be re-written in C++, and, if so, has this been > done? > The C++ translator itself uses yacc, albeit in a standard "C" way, more or less. The short story is that using yacc is possible, but using lex seems too hard as it stands. I had a go, but there was just too much c-specific code produced by lex that made C++ throw up. I've almost finshed doing something that uses yacc in a "C++" way. It involves techniques like the following: class SomeParser { // ...... yylex(); yyparse(); }; You then write SomeParser::yylex() manually, (or by some other means compatible with C++), and write the yacc grammar in the normal way UNDER THE ASSUMPTION that it's within class SomeParser (which is very convenient indeed for accessing things it needs to.) I.e: Your grammar file looks like: /* file.y */ %% /* usual stuff */ %% /* rules as usual, but using C++ code */ %% #include /* the file containing the definition of SomeParser::yylex() */ The makefile then should contain something like: file.o : file.c CC -c file.c file.c : y.tab.c sed -e 's/^yyparse\([^;]*\)$$/SomeParser::yyparse\1/' y.tab.c > file.c y.tab.c : file.y yacc file.y The sed subsitution may need slight modification depending on your version of yacc, and the composition of your file structure, but you get the idea... Because yyparse has been made into SomeParser::yyparse, when it calls yylex() it will actually call SomeParser::yylex(). With a slightly more sophisticated sed script it may even be possible to have multiple parsers operating in the one program. (Useful in cases where different sources of input have to be parsed in different ways at different times...) Mike Mowbray Systems Development Overseas Telecommunications Commission (Australia) UUCP: {seismo,mcvax}!otc.oz!mikem ACSnet: mikem@otc.oz