Xref: utzoo comp.lang.c++:8821 gnu.g++:1018 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!milano!czech!krohn From: krohn@czech.sw.mcc.com (Eric Krohn) Newsgroups: comp.lang.c++,gnu.g++ Subject: Re: yacc/lex experience... Message-ID: <4049@czech.sw.mcc.com> Date: 6 Aug 90 16:20:05 GMT References: <3006@zipeecs.umich.edu> Reply-To: krohn@czech.sw.mcc.com (Eric Krohn) Organization: MCC, Austin, Texas Lines: 33 In article <3006@zipeecs.umich.edu> bagchi@dip.eecs.umich.edu (Ranjan Bagchi) writes: ] Does anyone have any experience with using yacc & lex in ] conjunction with c++/g++? I've got a yyparse function which I would ] like to make a member function of class say...foo. ] I'm assuming that everything could be done with a sed script, but ] I really don't know about all the functions which need name-changes. I have turned yyparse into a member function for two different programs. Among the advantages are turning the global variables used to communicate with yyparse into member variables, and access to members variables and functions within the yacc actions. With a little doctoring of the y.tab.c, you can have multiple coexisting parsers, each within its own class. The definition of yyparse must have its name changed to member function syntax (Parser::yyparse, for example). It is wise to make yychar, yylval, yydebug, yylex(), and yyerror() into member variables/functions also. Have your sed (or perl) script delete the global definitions/forward declarations of yychar, yylval, yyparse, yydebug, and yyerrflag. If you want multiple parsers, or if you like data hiding, make all the other global yy* names static. With a little care, your script will be able to process output from the different UNIX yaccs, the PD Berkeley yacc, and bison. Some things to watch for are: * The call to getenv from Berkeley yacc (fixed in a recent release). * The calls to free in SunOS 4.0 yacc (Sun broke with AT&T cfront tradition and reverted to declaring void free (char *) instead of void free (void *)). Having used hand coded lexers, I cannot say anything offhand about wrapping lex output in a C++ class. -- Eric J. Krohn krohn@sw.mcc.com