Path: utzoo!utgpu!watmath!clyde!ima!compilers-sender From: djones@megatest.uucp (Dave Jones) Newsgroups: comp.compilers Subject: Re: I need answer to a simple YACC question. Message-ID: <3328@ima.ima.isc.com> Date: 7 Feb 89 04:24:52 GMT References: <3281@ima.ima.isc.com> Sender: compilers-sender@ima.ima.isc.com Reply-To: djones@megatest.uucp (Dave Jones) Organization: Megatest Corporation, San Jose, Ca Lines: 45 Approved: compilers@ima.UUCP > I know I can look at the y.output (on HPUX anyways) to > find the productions in each state's LALR(1) items. However > I want to find the lookahead set for a given production > for a given state. > > In other words I need to know how I can get the lookahead portion > of the LALR(1) item by looking at YACC generated files. > Thanks in advance for any help. > > Farrukh Najmi. Tain't easy. And if you could do it, you probably would not have what you wanted anyway. Do you want to calculate these for error- messages? (If so, you might ought to have said so.) You see, which tokens can next be accepted depends not just on the state which is on the top of the stack, but potenitally on other states deep underneath. That's because yacc tables are LALR(1) (with default productions as well), not the space-consuming canonical LR(1) tables. So if you had a list of all tokens which could be shifted when a given state was on top of the stack, it might have entries which would not be legal as a next token in an input file which had blocked the parser with a syntax error. But be of good cheer! If you only need the information at runtime, for error-messages, you can hack the parser, yyparse() or its genotype, yaccpar, so that when it blocks, it will first save a copy of the stack, then run in simulation on each possible token until it either shifts or blocks. If it shifts it, remember the token. In any case, restore the stack and try again, until you've tested all the tokens. Then report, "legal tokens are...", and spew out all the ones you have remembered. Dave J. P.S. I'm writing a compiler using yacc, even as we speak, and I just figured this out on the fly. I think I'm going to incorporate it! Thanks for the inspiration. -- Send compilers articles to ima!compilers or, in a pinch, to Levine@YALE.EDU Plausible paths are { decvax | harvard | yale | bbn}!ima Please send responses to the originator of the message -- I cannot forward mail accidentally sent back to compilers. Meta-mail to ima!compilers-request