Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!rutgers!bellcore!u1100a!krohn From: krohn@u1100a.UUCP (Eric Krohn) Newsgroups: comp.lang.c++ Subject: Re: Using OO Languages for Compilers/Interpreters Message-ID: <2145@u1100a.UUCP> Date: 14 Apr 89 15:07:03 GMT References: <16915@cup.portal.com> <3690007@wdl1.UUCP> Reply-To: krohn@u1100a.UUCP (Eric Krohn) Organization: Bell Communications Research, Piscataway, NJ Lines: 52 In article <3690007@wdl1.UUCP> rme@wdl1.UUCP (Richard M Emberson) writes: ] ] The yacc++ I produced from yacc yields instances of a class ] (called yacc) defined in a c++ header file. The grammar tables ] are passed in at instance creation. Thus multiple instances can ] exist in the same program and at the same time. At least in System V yaccs, you also need different versions of yyparse since the rule actions get interpolated into a giant switch statement at the bottom of yyparse. A fine use for virtual functions. (I seem to recall that the first UNIX machine I used (v6 or v7) had a separate function for the actions; so I believe that yyparse could be inherited but the auxiliary function would have to be virtual.) I have an abstract Yacc class that looks like: class Yacc { public: int yydebug; /* set to 1 to get debugging */ int yychar; /* current input token number */ YYSTYPE yylval; /* current token value */ FILE *yyin; /* input file */ LexLocation current; /* current file name and line number */ Yacc (FILE *input, long linenumber, const char *filename); virtual int yyparse (); virtual int yylex (); virtual int yyerror (const char *msg); long yylineno () { return (current.linenumber()); } }; This class depends on a straightforward sed script to make all the yacc tables static, rename yyparse to be a member function, and omit the global definitions of yychar and yydebug. C++ automagically takes care of the rest. Multiple instances of a class derived from Yacc will run the same parser and actions. Different derived classes can run completely different parsers and actions. I originally wrote this class with the expectation of having one or more yacc sub-parsers to handle some ambiguities in the topmost parser, but I have not yet resorted to this because I've been able to patch things up in the lexer. -- -- Eric J. Krohn krohn@ctt.ctt.bellcore.com or {bcr,bellcore}!u1100a!krohn Bell Communications Research, 444 Hoes Ln, Piscataway, NJ 08854