Related articles |
---|
getting bison.simple to report valid tokens on parse errors... beaty@longs.LANCE.ColoState.Edu (1990-03-05) |
Re: getting bison.simple to report valid tokens on parse errors... beaty@longs.LANCE.ColoState.Edu (1990-03-13) |
Date: | Mon, 5 Mar 90 16:38:01 MST |
From: | beaty@longs.LANCE.ColoState.Edu |
Keywords: | parse,errors,yacc |
Newsgroups: | comp.compilers,gnu.utils.bug |
I spent some time working on the generic bison parser so that it would
report which tokens are valid when a parse error occurs. i am aware of
some of the inadequacies of this method, but it works well and gives
lots of useful information. The basis of this change can be found on
page 183 in "Introduction to Compiler Construction with UNIX" by
Schreiner and Friedman.
This patch is independent of the grammar so should be useful for any
bison description. I've used it on a machine description language and
a dataflow graphics language. The following is a diff to feed to
'patch' for bison.simple.
----------------------------------------------------------------------
*** bison.simple Mon Mar 5 10:49:48 1990
--- bison.simple.new Mon Mar 5 10:58:19 1990
***************
*** 383,388
{
++yynerrs;
yyerror("parse error");
}
if (yyerrstatus == 3)
--- 383,404 -----
{
++yynerrs;
yyerror("parse error");
+ if ((yyn = yypact[yystate]) > YYFLAG && yyn < YYLAST)
+ {
+ int x, count;
+ count = 0;
+ for (x = 0; x < (sizeof(yytname) / sizeof(char *)); x++)
+ {
+ if (yycheck[x + yyn] == x)
+ {
+ printf ("%s %s",
+ count == 0 ? "expecting:" : " or",
+ yytname[x]);
+ count++;
+ }
+ }
+ printf ("%s", count > 0 ? "\n" : "");
+ }
}
if (yyerrstatus == 3)
----------------------------------------------------------------------
with the following lines in my lex description:
----------------------------------------------------------------------
int line_number
.
.
.
%%
.
.
.
\n { line_number++; }
----------------------------------------------------------------------
I also use the following yyerror() to add some information to the
error messages:
----------------------------------------------------------------------
yyerror (s)
char *s;
{
extern int line_number;
extern char *yytext;
fprintf (stderr, "%s on line number %d, ", s, line_number+1);
fprintf (stderr, "%s unexpected\n", yytext);
}
----------------------------------------------------------------------
Hope this helps someone out.
steve beaty@longs.lance.colostate.edu
Return to the
comp.compilers page.
Search the
comp.compilers archives again.