From: Akim Demaille Date: Sun, 2 Mar 2003 13:39:44 +0000 (+0000) Subject: * data/yacc.c, data/glr.c, data/lal1.cc: Use similar code to X-Git-Tag: BISON-2_0~367 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/c47495657bb2f35012e35668a2c6c149e1bed3ac * data/yacc.c, data/glr.c, data/lal1.cc: Use similar code to generate verbose error messages. Use the number of tokens as an upper bound in yytname, as it cannot be a non terminal. --- diff --git a/ChangeLog b/ChangeLog index 7b5df644..67ab551d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2003-03-02 Akim Demaille + + * data/yacc.c, data/glr.c, data/lal1.cc: Use similar code to + generate verbose error messages. + Use the number of tokens as an upper bound in yytname, as it + cannot be a non terminal. + 2003-03-02 Akim Demaille * tests/regression.at (_AT_DATA_DANCER_Y): Fix the expected error diff --git a/data/glr.c b/data/glr.c index d2cdd77b..86a6fd0e 100644 --- a/data/glr.c +++ b/data/glr.c @@ -613,14 +613,14 @@ yytokenName (yySymbol yytoken) /** Fill in YYVSP[YYLOW1 .. YYLOW0-1] from the chain of states starting * at YYVSP[YYLOW0].yystate.yypred. Leaves YYVSP[YYLOW1].yystate.yypred - * containing the pointer to the next state in the chain. Assumes + * containing the pointer to the next state in the chain. Assumes * YYLOW1 < YYLOW0. For convenience, returns YYLOW1. */ static int -yyfill (yyGLRStackItem* yyvsp, int yylow0, int yylow1) +yyfill (yyGLRStackItem* yyvsp, int yylow0, int yylow1) { yyGLRState* s; int i; - s = yyvsp[yylow0].yystate.yypred; + s = yyvsp[yylow0].yystate.yypred; for (i = yylow0-1; i >= yylow1; i -= 1) { if (! s->yyresolved) @@ -1022,7 +1022,7 @@ yyglrShift (yyGLRStack* yystack, int yyk, yyStateNum yylrState, size_t yyposn, } /** Shift stack #K of YYSTACK, to a new state corresponding to LR - * state YYLRSTATE, at input position YYPOSN, with the (unresolved) + * state YYLRSTATE, at input position YYPOSN, with the (unresolved) * semantic value of YYRHS under the action for YYRULE. */ static inline void yyglrShiftDefer (yyGLRStack* yystack, int yyk, yyStateNum yylrState, @@ -1597,29 +1597,28 @@ yyreportSyntaxError (yyGLRStack* yystack, #if YYERROR_VERBOSE yySymbol* const yytokenp = yystack->yytokenp; int yyn; - const char* yyprefix; yyn = yypact[yystack->yytops.yystates[0]->yylrState]; if (YYPACT_NINF < yyn && yyn < YYLAST) { size_t yysize = 0; + const char* yyprefix; char* yymsg; int yyx; /* Start YYX at -YYN if negative to avoid negative indexes in YYCHECK. */ - int yyxbase = yyn < 0 ? -yyn : 0; + int yyxbegin = yyn < 0 ? -yyn : 0; /* Stay within bounds of both yycheck and yytname. */ int yychecklim = YYLAST - yyn; - int yynsyms = sizeof (yytname) / sizeof (yytname[0]); - int yyxlim = yychecklim < yynsyms ? yychecklim : yynsyms; + int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; int yycount = 0; yyprefix = ", expecting "; - for (yyx = yyxbase; yyx < yyxlim && yycount < 5; yyx += 1) + for (yyx = yyxbegin; yyx < yyxend; ++yyx) if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) { - yysize += strlen (yytokenName (yyx)) + strlen (yyprefix); + yysize += strlen (yyprefix) + strlen (yytokenName (yyx)); yycount += 1; if (yycount == 5) { @@ -1640,7 +1639,7 @@ yyreportSyntaxError (yyGLRStack* yystack, if (yycount < 5) { yyprefix = ", expecting "; - for (yyx = yyxbase; yyx < yyxlim; yyx += 1) + for (yyx = yyxbegin; yyx < yyxend; ++yyx) if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) { sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx)); diff --git a/data/lalr1.cc b/data/lalr1.cc index 1d1b9cff..34398cc1 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -498,13 +498,19 @@ b4_syncline([@oline@], [@ofile@])[ message += name_[ilooka_]; { int count = 0; - for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x) + /* Start YYX at -YYN if negative to avoid negative indexes in + YYCHECK. */ + int xbegin = n_ < 0 ? -n_ : 0; + /* Stay within bounds of both yycheck and yytname. */ + int checklim = last_ - n_; + int xend = checklim < ntokens_ ? checklim : ntokens_; + for (int x = xbegin; x < xend; ++x) if (check_[x + n_] == x && x != terror_) ++count; if (count < 5) { count = 0; - for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x) + for (int x = xbegin; x < xend; ++x) if (check_[x + n_] == x && x != terror_) { message += (!count++) ? ", expecting " : " or "; diff --git a/data/yacc.c b/data/yacc.c index fca98e54..eb0b7b30 100644 --- a/data/yacc.c +++ b/data/yacc.c @@ -1038,32 +1038,30 @@ yyerrlab: { YYSIZE_T yysize = 0; int yytype = YYTRANSLATE (yychar); + const char* yyprefix; char *yymsg; int yyx; /* Start YYX at -YYN if negative to avoid negative indexes in YYCHECK. */ - int yyxbase = yyn < 0 ? -yyn : 0; + int yyxbegin = yyn < 0 ? -yyn : 0; /* Stay within bounds of both yycheck and yytname. */ int yychecklim = YYLAST - yyn; - int yynsyms = sizeof (yytname) / sizeof (yytname[0]); - int yyxlim = yychecklim < yynsyms ? yychecklim : yynsyms; + int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; int yycount = 0; - for (yyx = yyxbase; yyx < yyxlim; yyx++) + yyprefix = ", expecting "; + for (yyx = yyxbegin; yyx < yyxend; ++yyx) if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) { - yysize += (! yycount - ? sizeof (", expecting ") - 1 - : sizeof (" or ") - 1); - yysize += yystrlen (yytname[yyx]); - yycount++; + yysize += yystrlen (yyprefix) + yystrlen (yytname [yyx]); + yycount += 1; if (yycount == 5) { yysize = 0; break; - } + } } yysize += (sizeof ("syntax error, unexpected ") + yystrlen (yytname[yytype])); @@ -1075,14 +1073,13 @@ yyerrlab: if (yycount < 5) { - yycount = 0; - for (yyx = yyxbase; yyx < yyxlim; yyx++) + yyprefix = ", expecting "; + for (yyx = yyxbegin; yyx < yyxend; ++yyx) if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) { - const char *yyq = ! yycount ? ", expecting " : " or "; - yyp = yystpcpy (yyp, yyq); + yyp = yystpcpy (yyp, yyprefix); yyp = yystpcpy (yyp, yytname[yyx]); - yycount++; + yyprefix = " or "; } } yyerror (]b4_yyerror_args[yymsg);