Related articles |
---|
I need answer to a simple YACC question. hp-sdd!najmi@hpcuhb.HP.COM (1989-02-03) |
Re: I need answer to a simple YACC question. djones@megatest.uucp (1989-02-07) |
Re: I need answer to a simple YACC question. oakhill!epsilon!steve@cs.utexas.edu (1989-02-16) |
Re: I need answer to a simple YACC question. megatest!djones@decwrl.dec.com (1989-02-24) |
Re: I need answer to a simple YACC question. megatest!djones@decwrl.dec.com (1989-02-24) |
From: | djones@megatest.uucp (Dave Jones) |
Newsgroups: | comp.compilers |
Date: | 7 Feb 89 04:24:52 GMT |
References: | <3281@ima.ima.isc.com> |
Organization: | Megatest Corporation, San Jose, Ca |
> 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.
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.