yacc/bison error recovery

Mark-Jan Nederhof <nederhof@dfki.de>
20 Dec 2001 00:33:36 -0500

          From comp.compilers

Related articles
yacc/bison error recovery nederhof@dfki.de (Mark-Jan Nederhof) (2001-12-20)
| List of all articles for this month |

From: Mark-Jan Nederhof <nederhof@dfki.de>
Newsgroups: comp.compilers
Date: 20 Dec 2001 00:33:36 -0500
Organization: DFKI Saarbruecken GmbH, D 66123 Saarbruecken
Keywords: yacc, parse, errors
Posted-Date: 20 Dec 2001 00:33:36 EST

Suppose my grammar is:

s : e { printf("%d\n", $1); }

                : 'x' { $$ = 0; }
                | 'x' '-' e { $$ = $3 + 1;}
                | error '-' e { $$ = $3 + 1;}

I.e. input consists of a list of identifiers 'x' separated by hyphens,
and the program should count and print the number of hyphens. The
error rule states that there may be incorrect identifiers, but
regardless we expect the number of hyphens to be counted.

For the input "x-x-y-x-x-xy-y-y-x-x", which contains some incorrect
identifiers, I would expect output "9", i.e. the number of hyphens,
but instead the parser prints two (!) numbers, viz. "5" and "4",
resp. How can this be? Is the parser implicitly restarted in the
middle of the input? I only call yyparse() once.

It seems to me that incorrect identifier "y" is dealt with as
expected, but something strange happens when encountering "xy". If I
add more occurrences of "xy" to the input string, the parser prints
even more numbers (adding up to the total number of hyphens in the
input). "yx" is treated just like "y", i.e. in the expected way of
reducing to "error".

PS yacc and bison behave exactly the same here.
Thanks in advance,
  Mark-Jan Nederhof
[When yacc is in its error state it pops states off the stack until it
can resynchronize. Looks like it popped all the way up to the root.

Post a followup to this message

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