Path: utzoo!utgpu!watserv1!watmath!att!ima!esegue!compilers-sender From: jar@florida.eng.ileaf.com (Jim Roskind x5570) Newsgroups: comp.compilers Subject: Two pass compiler using YACC? Keywords: yacc, parse Message-ID: <9009030104.AA15456@florida.io.uucp> Date: 3 Sep 90 01:04:55 GMT Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: jar@florida.eng.ileaf.com (Jim Roskind x5570) Organization: Compilers Central Lines: 49 Approved: compilers@esegue.segue.boston.ma.us > From: wsineel@info.win.tue.nl (e.vriezekolk) > > We are working on a compiler, using yacc. The compiler will be two-pass, > and we have a different .y file for both passes. Each .y file is > translated by yacc to pass.1.C and pass.2.C (we are using C++). > > The problems come during link-time, for ld, obviously, complains about > multiple defined symbols (such as yylval and yyparse). > > This must be a traditional problem. How is it solved? IMHO the cleanest solution lies in use of the C preprocessor (as do so many solutions :-) ). Suppose that the only multiply defined external were yyparse and yyerror. Create a file called "pass1.h" containing the lines: #define yyparse pass1_yyparse #define yyerror pass1_yyerror Then simply #include this file into the initial section of your yacc grammar. This placement will cause the #include directive to appear sufficiently early in the y.tab.c file that *all* references to yyparse will be translated appropriately. External calls to the first pass should use the new name. > [I hope the grammars for the two passes are the same, and just the { } > actions are different. ... -John] I actually have examples where the grammar are distinct phases of processing, and have very little connection. For example, in parsing C, there is 1) the preprocessing parser, 2) the preprocessing expression evaluation parser, and 3) the C syntax parser. I actually implement each of the phases using a yacc based parser, and hence the grammars are quite distinct. In the most general case, when the phases of translation are very "orthogonal", there is a need for multiple distinct parsers, and the trick for making yacc work in such an environment is quite useful. (It also applies to multiple flex/lex based lexers). Jim Roskind Independent Consultant (407)729-4348 or (617)577-9813 x5570 jar@hq.ileaf.com or ...!uunet!leafusa!jar -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.