Re: Recursive-descent C parser WANTED (Brian Bliss)
Thu, 27 Feb 92 22:02:41 GMT

          From comp.compilers

Related articles
Re: Recursive-descent C parser WANTED (1992-02-27)
Re: Recursive-descent C parser WANTED (1992-02-27)
Re: Recursive-descent C parser WANTED (1992-02-27)
Re: Recursive-descent C parser WANTED (1992-03-04)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Brian Bliss)
Keywords: yacc, design, errors
Organization: UIUC Center for Supercomputing Research and Development
References: <> 92-02-126
Date: Thu, 27 Feb 92 22:02:41 GMT

In article <1342@dms.UUCP> albaugh@dms.UUCP (Mike Albaugh) writes:
> Anyway, while such programs [Yacc/Lex/Bison/Flex] are "cool", they
>leave much to be desired in the area of error recovery, just the area
>where it is pretty simple to get a naive recursive descent parser to help
>a lot.

Here's a trick that I use with bison and yacc for better error recovery:
first I #define a macro, call it MYSETJMP, which saves the internal state
of the parser, i.e., all the stack indeces, and current state number (but
not the lookahead token), performs a setjmp, and restores the states if
the return code is nonzero.

Before any statement is parsed, I invoke the macro, saving the state of
the parser. if an error is encountered while parsing a statement and I
want to recover by scanning to the next statement, I do a longjump back to
the point where the state was saved, and scan for the next semicolon or
other recognizable construct.

You'll have to figure out exactly what needs to be saved as far as the
parser state goes (not that hard), construct the grammar so that the
internal stacks never shrink past the point where the state was saved (not
hard) and possibly add a few consructs so that the semiocolon or other
recognizable construct can be considered a statement by itself, or exist
outside the scope of a block, such that the mechanism which throws away
tokens until a recognizable construct is found leaves the parser in a
state where it expects said construct (i.e. figure out when to clear or
reset yychar), and generally, get your fingers dirty with yacc internals,
but creating an error recovery mechanism that works as well as what is
found in recursive-decent parsers can be done with a little creative
run-time stack manipulation.


Post a followup to this message

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