[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Two problems with the GLR parsers
From: |
Paul Eggert |
Subject: |
Re: Two problems with the GLR parsers |
Date: |
Fri, 15 Nov 2002 23:40:55 -0800 (PST) |
> From: Paul Hilfinger <address@hidden>
> Date: Thu, 14 Nov 2002 16:01:00 -0800
>
> Why would that give 50 as the location of error, which was pushed
> while at location 30?
Good question. This raises another question: should something like
YYLLOC_DEFAULT apply to error-token location computation too? (And I
have another question for Akim: why does src/parse-gram.y override
YYLLOC_DEFAULT? Perhaps that should be commented?)
> As the starred lines indicate, the error token is shifted 4 times!
> Fortunately, the reduction is taken only once. I'm not sure what to
> make of this, so I'll ask around before I try to make glr duplicate
> the behavior (or, of course, change input.at).
Surely the user shouldn't care how the Bison-generated parser
internally arrives at the correct actions, so long as the actions are
correct. But perhaps I am missing some subtleties about why the user
would care.
The POSIX spec for how a Yacc-generated parser is supposed to handle
the error token can be found in the "Error Handling" section of
<http://www.opengroup.org/onlinepubs/007904975/utilities/yacc.html>.
It's a bit more complicated than what the Bison manual says, e.g. it
talks about what to do at end of input. (Perhaps the Bison manual
should be updated?) Anyway, I installed the following new test case
to check that the POSIX-required error handling is being implemented
properly in this particular grammar.
2002-11-15 Paul Eggert <address@hidden>
* tests/actions.at (Actions after errors): New test case.
Index: actions.at
===================================================================
RCS file: /cvsroot/bison/bison/tests/actions.at,v
retrieving revision 1.21
retrieving revision 1.22
diff -p -u -r1.21 -r1.22
--- actions.at 15 Nov 2002 20:32:21 -0000 1.21
+++ actions.at 16 Nov 2002 07:31:35 -0000 1.22
@@ -82,6 +82,153 @@ AT_CLEANUP
+## ---------------------- ##
+## Actions after errors. ##
+## ---------------------- ##
+
+AT_SETUP([Actions after errors])
+
+
+
+AT_DATA_GRAMMAR([[input.y]],
+[[%{
+#include <stdio.h>
+#include <stdlib.h>
+
+static int yylex (void);
+static void yyerror (char const *);
+
+#define YYDEBUG 1
+%}
+%union { int ival; }
+%type <ival> 'x' ';' thing line input
+
+%%
+input:
+ /* Nothing. */
+ {
+ $$ = 0;
+ printf ("input(%d): /* Nothing */\n", $$);
+ }
+| line input /* Right recursive to load the stack so that popping at
+ EOF can be exercised. */
+ {
+ $$ = 2;
+ printf ("input(%d): line(%d) input(%d)\n", $$, $1, $2);
+ }
+;
+
+line:
+ thing thing thing ';'
+ {
+ $$ = $1;
+ printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'(%d)\n",
+ $$, $1, $2, $3, $4);
+ }
+| thing thing ';'
+ {
+ $$ = $1;
+ printf ("line(%d): thing(%d) thing(%d) ';'(%d)\n", $$, $1, $2, $3);
+ }
+| thing ';'
+ {
+ $$ = $1;
+ printf ("line(%d): thing(%d) ';'(%d)\n", $$, $1, $2);
+ }
+| error ';'
+ {
+ $$ = -1;
+ printf ("line(%d): error ';'(%d)\n", $$, $2);
+ }
+;
+
+thing:
+ 'x'
+ {
+ $$ = $1;
+ printf ("thing(%d): 'x'(%d)\n", $$, $1);
+ }
+;
+%%
+static size_t counter;
+
+static int
+yylex (void)
+{
+ static char const input[] =
+ {
+ /* Exericise the discarding of stack top and input until `error'
+ can be reduced. */
+ 'x', 'x', 'x', 'x', 'x', 'x', ';',
+
+ /* Load the stack and provoke an error that cannot be caught by
+ the grammar, to check that the stack is cleared. */
+ 'x', 'x', ';',
+ 'x', ';',
+ 'y'
+ };
+
+ if (counter < sizeof input)
+ {
+ yylval.ival = counter;
+ printf ("sending: '%c' (value = %d)\n", input[counter], yylval.ival);
+ return input[counter++];
+ }
+ else
+ {
+ printf ("sending: EOF\n");
+ return EOF;
+ }
+}
+
+static void
+yyerror (char const *msg)
+{
+ printf ("%lu: %s\n", (unsigned long int) counter, msg);
+}
+
+int
+main (void)
+{
+ yydebug = !!getenv ("YYDEBUG");
+ return yyparse ();
+}
+]])
+
+AT_CHECK([bison -o input.c input.y])
+AT_COMPILE([input])
+AT_PARSER_CHECK([./input], 1,
+[[sending: 'x' (value = 0)
+thing(0): 'x'(0)
+sending: 'x' (value = 1)
+thing(1): 'x'(1)
+sending: 'x' (value = 2)
+thing(2): 'x'(2)
+sending: 'x' (value = 3)
+4: syntax error
+sending: 'x' (value = 4)
+sending: 'x' (value = 5)
+sending: ';' (value = 6)
+line(-1): error ';'(6)
+sending: 'x' (value = 7)
+thing(7): 'x'(7)
+sending: 'x' (value = 8)
+thing(8): 'x'(8)
+sending: ';' (value = 9)
+line(7): thing(7) thing(8) ';'(9)
+sending: 'x' (value = 10)
+thing(10): 'x'(10)
+sending: ';' (value = 11)
+line(10): thing(10) ';'(11)
+sending: 'y' (value = 12)
+13: syntax error
+sending: EOF
+]])
+
+AT_CLEANUP
+
+
+
## ---------------- ##
## Exotic Dollars. ##
## ---------------- ##