From: Akim Demaille Date: Tue, 21 Feb 2012 10:51:37 +0000 (+0100) Subject: glr: fix ambiguity reports. X-Git-Tag: v2.7.90~502 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/a6b2f4fc020fe6e464f4a445a7851825d3d57e4d glr: fix ambiguity reports. Fix a regression introduced in commit 783aa653f4ca70a75919c8516b950494c612cbfe. * tests/glr-regression.at (Ambiguity reports): New. * data/glr.c (yyreportTree): Fix an offset error. --- diff --git a/data/glr.c b/data/glr.c index 9b525f47..148af2b6 100644 --- a/data/glr.c +++ b/data/glr.c @@ -1727,10 +1727,10 @@ yyreportTree (yySemanticOption* yyx, int yyindent) { if (yystates[yyi-1]->yyposn+1 > yystates[yyi]->yyposn) YYFPRINTF (stderr, "%*s%s \n", yyindent+2, "", - yytokenName (yystos[yystates[yyi-1]->yylrState])); + yytokenName (yystos[yystates[yyi]->yylrState])); else YYFPRINTF (stderr, "%*s%s \n", yyindent+2, "", - yytokenName (yystos[yystates[yyi-1]->yylrState]), + yytokenName (yystos[yystates[yyi]->yylrState]), (unsigned long int) (yystates[yyi-1]->yyposn + 1), (unsigned long int) yystates[yyi]->yyposn); } diff --git a/tests/glr-regression.at b/tests/glr-regression.at index a9bff580..bc32c901 100644 --- a/tests/glr-regression.at +++ b/tests/glr-regression.at @@ -1788,3 +1788,120 @@ glr-regr18.y:26.18-24: previous declaration ]) AT_CLEANUP + + +## ------------------- ## +## Ambiguity reports. ## +## ------------------- ## + +AT_SETUP([Ambiguity reports]) + +AT_DATA_GRAMMAR([input.y], +[[ +%{ + #include + #include + static void yyerror (char const *); + static int yylex (void); +%} + +%debug +%glr-parser + +%% +start: + 'a' b 'c' d +| 'a' b 'c' d +; +b: 'b'; +d: /* nada. */; +%% + +static int +yylex (void) +{ + static char const input[] = "abc"; + static size_t toknum; + if (! (toknum < sizeof input)) + abort (); + return input[toknum++]; +} + +static void +yyerror (char const *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + yydebug = 1; + return !!yyparse (); +} +]]) + +AT_BISON_CHECK([[-o input.c input.y]], 0, [], +[input.y: conflicts: 1 reduce/reduce +]) +AT_COMPILE([input]) + +AT_PARSER_CHECK([[./input]], 1, [], +[Starting parse +Entering state 0 +Reading a token: Next token is token 'a' () +Shifting token 'a' () +Entering state 1 +Reading a token: Next token is token 'b' () +Shifting token 'b' () +Entering state 3 +Reducing stack 0 by rule 3 (line 25): + $1 = token 'b' () +-> $$ = nterm b () +Entering state 4 +Reading a token: Next token is token 'c' () +Shifting token 'c' () +Entering state 6 +Reducing stack 0 by rule 4 (line 26): +-> $$ = nterm d () +Entering state 7 +Reading a token: Now at end of input. +Stack 0 Entering state 7 +Now at end of input. +Splitting off stack 1 from 0. +Reduced stack 1 by rule #2; action deferred. Now in state 2. +Stack 1 Entering state 2 +Now at end of input. +Reduced stack 0 by rule #1; action deferred. Now in state 2. +Merging stack 0 into stack 1. +Stack 1 Entering state 2 +Now at end of input. +Removing dead stacks. +Rename stack 1 -> 0. +On stack 0, shifting token $end () +Stack 0 now in state #5 +Ambiguity detected. +Option 1, + start -> + 'a' + b + 'c' + d + +Option 2, + start -> + 'a' + b + 'c' + d + +syntax is ambiguous +Cleanup: popping token $end () +Cleanup: popping unresolved nterm start () +Cleanup: popping nterm d () +Cleanup: popping token 'c' () +Cleanup: popping nterm b () +Cleanup: popping token 'a' () +]) + +AT_CLEANUP