Re: Get allowed-next-tokens in yacc?

Vladimir Alexiev <vladimir@cs.ualberta.ca>
7 Mar 1998 22:41:41 -0500

          From comp.compilers

Related articles
Get allowed-next-tokens in yacc? rfaulds@avici.com (R Faulds) (1998-03-03)
Re: Get allowed-next-tokens in yacc? vitale@esat.kuleuven.ac.be (Maurizio Vitale) (1998-03-06)
Re: Get allowed-next-tokens in yacc? vladimir@cs.ualberta.ca (Vladimir Alexiev) (1998-03-07)
| List of all articles for this month |
From: Vladimir Alexiev <vladimir@cs.ualberta.ca>
Newsgroups: comp.compilers
Date: 7 Mar 1998 22:41:41 -0500
Organization: University of Alberta, Computing Science
References: 98-03-005
Keywords: yacc

"R Faulds" <rfaulds@avici.com> writes:


> 1) Is there a way (without hacking y.tab.c) to get the list of allowed
> tokens for some (any) point in the parse process?


Sure. This would be yacc-dependent though.


> >show interface<help-key>
> <num> - numeric input required
> ALL - display all


This sounds good, but for some tokens (the "syntactic noise" tokens)
you can't give sensible help. Eg '(' may mean signify the beginning of
an "argument list" or a "nested expression", and ALL may mean "all
interfaces or "all widgets", etc.


> 2) When I'm parsing down a specific rule, how do I use error to localize
> the error handling?


> >set interface address <CR>
> trap the error at the rule's action code so I could issue
> "numeric required, choose an interface between [1..7]".


This is related to the previous point. If you can figure out the set
of allowed tokens, you can make a sensible error message.


> [Honestly, I wouldn't use yacc. Getting the actual list of allowed tokens
> at each point is really hard due to the state and table compaction that
> yacc does. And the error recovery is no prize-winner. -John]


I don't know what compaction yacc does, but I'd suggest you use bison.
Look at the skeleton file bison.simple, after a "#ifdef
YYERROR_VERBOSE" label. If that is defined, bison calls yyerror with
eg "parse error, expecting `foo' or `bar' or `'+'' or `NUM' or
'ALL'". It's a simple matter to enrich this with more detailed
explanations taken from the table containing the help texts. Note that
some of the expected tokens could be non-terminals.
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.