Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!snorkelwacker!spdcc!esegue!compilers-sender From: bart@videovax.tv.tek.com (Bart Massey) Newsgroups: comp.compilers Subject: Re: Two pass compiler using YACC? Keywords: C, yacc, parse, design Message-ID: <6029@videovax.tv.tek.com> Date: 25 Aug 90 04:41:58 GMT References: <1364@svin02.info.win.tue.nl> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: Bart Massey Organization: Tektronix TV Measurement Systems, Beaverton OR Lines: 42 Approved: compilers@esegue.segue.boston.ma.us In article <1364@svin02.info.win.tue.nl> wsineel@info.win.tue.nl (e.vriezekolk) writes: > We are working on a compiler, using yacc. The compiler will be two-pass, > and we have a different .y file for both passes. ... > The problems come during link-time, for ld, obviously, complains about > multiple defined symbols (such as yylval and yyparse). ... > [I hope the grammars for the two passes are the same, and just the { } > actions are different. I always do the obvious thing, if(pass1)... else ... > in the action routines ... > it is a big problem to keep the two files in > sync when you make a grammar change. -John] Hmmm. It seems to me to depend a lot on what is meant by "two-pass". In the traditional usage, where the second pass is merely used to resolve forward references, I'd agree completely. However, I have heard the phrase "two-pass" used to describe compilers where the first pass outputs a very different intermediate form which the second pass parses. In this case, one could legitimately want two entirely different grammars. There are several ways to accomplish this, none of them trivial. One is to get a copy of GNU BISON and use the "semantic parsers" feature, which does what you want. It needs some slight debugging, however, and is really hard to figure out. Another is to get a copy of Berkeley YACC, and hack it up to do the right thing, which would probably be easy, and would probably be greatly appreciated by others. The third, and by far the most traditional, is to write a sed script which hacks the YACC output so that all the names are unique. This is usually some baroque variant on replacing "yy" with some more unique prefix. Finally, and this is my favorite, you can just hose the intermediate form through a pipe to a separate program. This opens up a lot of other neat possibilities... It really is a shame that none of the 3 parser generators mentioned above generates modular parsers very well. Ah well. Hope this helps, Bart Massey ..tektronix!videovax.tv.tek.com!bart ..tektronix!reed.bitnet!bart -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.