From 39912f52635a7b7709b6a890a41e066340a89111 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 20 Oct 2002 11:20:15 +0000 Subject: [PATCH] * src/output.c (prepare): Move the definition of `tokens_number', `nterms_number', `undef_token_number', `user_token_number_max' to... (prepare_tokens): Here. (prepare_tokens): Rename as... (prepare_symbols): this. (prepare): Move the definition of `rules_number' to... (prepare_rules): here. (prepare): Move the definition of `last', `final_state_number', `states_number' to... (prepare_states): here. * data/yacc.c, data/lalr1.cc, data/glr.c: Normalize `>' into `<'. --- ChangeLog | 15 +++++++++++++++ data/glr.c | 27 ++++++++++++++------------- data/lalr1.cc | 7 ++++--- data/yacc.c | 13 +++++++------ src/output.c | 40 +++++++++++++++++++--------------------- src/tables.h | 6 ++++-- 6 files changed, 63 insertions(+), 45 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc5035c2..860eecc7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2002-10-20 Akim Demaille + + * src/output.c (prepare): Move the definition of `tokens_number', + `nterms_number', `undef_token_number', `user_token_number_max' + to... + (prepare_tokens): Here. + (prepare_tokens): Rename as... + (prepare_symbols): this. + (prepare): Move the definition of `rules_number' to... + (prepare_rules): here. + (prepare): Move the definition of `last', `final_state_number', + `states_number' to... + (prepare_states): here. + * data/yacc.c, data/lalr1.cc, data/glr.c: Normalize `>' into `<'. + 2002-10-20 Akim Demaille * src/tables.h, src/tables.c, src/output.c: Comment changes. diff --git a/data/glr.c b/data/glr.c index 94e033cd..e631cb97 100644 --- a/data/glr.c +++ b/data/glr.c @@ -231,6 +231,7 @@ static YYLTYPE yyloc_default; /* YYFINAL -- State number of the termination state. */ #define YYFINAL ]b4_final_state_number[ +/* YYLAST -- Last index in YYTABLE. */ #define YYLAST ]b4_last[ /* YYNTOKENS -- Number of terminals. */ @@ -739,7 +740,7 @@ yyLRgotoState (yyStateNum yystate, yySymbol yylhs) static inline bool yyisShiftAction (int yyaction) { - return yyaction > 0; + return 0 < yyaction; } static inline bool @@ -825,14 +826,14 @@ yyexpandGLRStack (yyGLRStack* yystack) size_t yysize, yynewSize; size_t yyn; yysize = yystack->yynextFree - yystack->yyitems; - if (yysize >= YYMAXDEPTH) + if (YYMAXDEPTH <= yysize) yyFail (yystack, "parsing stack overflow (%d items)", yysize); yynewSize = 2*yysize; - if (yynewSize > YYMAXDEPTH) + if (YYMAXDEPTH < yynewSize) yynewSize = YYMAXDEPTH; yyinitGLRStack (&yynewStack, yynewSize); for (yyp0 = yystack->yyitems, yyp1 = yynewStack.yyitems, yyn = yysize; - yyn > 0; + 0 < yyn; yyn -= 1, yyp0 += 1, yyp1 += 1) { *yyp1 = *yyp0; @@ -1024,7 +1025,7 @@ yydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule, int yyi; yyGLRState* yys; yyGLRStackItem yyrhsVals[YYMAXRHS]; - for (yyi = yynrhs-1, yys = yystack->yytops.yystates[yyk]; yyi >= 0; + for (yyi = yynrhs-1, yys = yystack->yytops.yystates[yyk]; 0 <= yyi; yyi -= 1, yys = yys->yypred) { assert (yys->yypred != NULL); @@ -1078,7 +1079,7 @@ yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, YYDPRINTF ((stderr, "Reducing stack %d by rule %d (line %d),", yyk, yyrule - 1, yyrline[yyrule])); /* Print the symbols being reduced, and their result. */ - for (yyi = yyprhs[yyrule]; yyrhs[yyi] >= 0; yyi++) + for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++) YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi])); YYFPRINTF (stderr, " -> %s\n", yytokenName (yyr1[yyrule])); } @@ -1099,7 +1100,7 @@ yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, yyStateNum yynewLRState; for (yys = yystack->yytops.yystates[yyk], yyn = yyrhsLength (yyrule); - yyn > 0; yyn -= 1) + 0 < yyn; yyn -= 1) { yys = yys->yypred; assert (yys != NULL); @@ -1222,7 +1223,7 @@ yypreference (yySemanticOption* y0, yySemanticOption* y1) return 0; if (p0 < p1) return 3; - if (p0 > p1) + if (p1 < p0) return 2; return 0; } @@ -1235,7 +1236,7 @@ static YYRESULTTAG yyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack) { YYRESULTTAG yyflag; - if (yyn > 0) + if (0 < yyn) { assert (yys->yypred != NULL); yyflag = yyresolveStates (yys->yypred, yyn-1, yystack); @@ -1263,7 +1264,7 @@ yyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack, yynrhs = yyrhsLength (yyopt->yyrule); YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack)); - for (yyi = yynrhs-1, yys = yyopt->yystate; yyi >= 0; + for (yyi = yynrhs-1, yys = yyopt->yystate; 0 <= yyi; yyi -= 1, yys = yys->yypred) { assert (yys->yypred != NULL); @@ -1294,14 +1295,14 @@ static void yyreportTree (yySemanticOption* yyx, int yyindent) yyGLRState* yys; yyGLRState* yystates[YYMAXRHS]; - for (yyi = yynrhs, yys = yyx->yystate; yyi > 0; yyi -= 1, yys = yys->yypred) + for (yyi = yynrhs, yys = yyx->yystate; 0 < yyi; yyi -= 1, yys = yys->yypred) yystates[yyi] = yys; if (yys == NULL) yystates[0] = &YYLEFTMOST_STATE; else yystates[0] = yys; - if (yys->yyposn+1 > yyx->yystate->yyposn) + if (yyx->yystate->yyposn < yys->yyposn + 1) YYFPRINTF (stderr, "%*s%s -> \n", yyindent, "", yytokenName (yylhsNonterm (yyx->yyrule)), yyx->yyrule); @@ -1735,7 +1736,7 @@ yyparse (YYPARSE_PARAM_ARG) yytoken = YYEMPTY; yyposn += 1; yyglrShift (&yystack, 0, yyaction, yyposn, yylval, yyllocp); - if (yystack.yyerrState > 0) + if (0 < yystack.yyerrState) yystack.yyerrState -= 1; YYDPRINTF ((stderr, "Entering state %d\n", yystack.yytops.yystates[0]->yylrState)); diff --git a/data/lalr1.cc b/data/lalr1.cc index 22e8c090..96325b0b 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -257,6 +257,7 @@ namespace yy /* Constants. */ static const int eof_; + /* LAST_ -- Last index in TABLE_. */ static const int last_; static const int nnts_; static const int empty_; @@ -374,7 +375,7 @@ yy::b4_parser_class_name::parse () } n_ += ilooka_; - if (n_ < 0 || n_ > last_ || check_[[n_]] != ilooka_) + if (n_ < 0 || last_ < n_ || check_[[n_]] != ilooka_) goto yydefault; /* Reduce or error. */ @@ -442,7 +443,7 @@ yy::b4_parser_class_name::parse () YYCDEBUG << "Reducing via rule " << n_ - 1 << " (line " << rline_[[n_]] << "), "; for (b4_int_type_for([b4_prhs]) i = prhs_[[n_]]; - rhs_[[i]] >= 0; ++i) + 0 <= rhs_[[i]]; ++i) YYCDEBUG << name_[[rhs_[i]]] << ' '; YYCDEBUG << "-> " << name_[[r1_[n_]]] << std::endl; } @@ -483,7 +484,7 @@ yy::b4_parser_class_name::parse () /* Shift the result of the reduction. */ n_ = r1_[[n_]]; state_ = pgoto_[[n_ - ntokens_]] + state_stack_[[0]]; - if (state_ >= 0 && state_ <= last_ && check_[[state_]] == state_stack_[[0]]) + if (0 <= state_ && state_ <= last_ && check_[[state_]] == state_stack_[[0]]) state_ = table_[[state_]]; else state_ = defgoto_[[n_ - ntokens_]]; diff --git a/data/yacc.c b/data/yacc.c index 75c194f5..b13702da 100644 --- a/data/yacc.c +++ b/data/yacc.c @@ -319,6 +319,7 @@ b4_location_if( /* YYFINAL -- State number of the termination state. */ #define YYFINAL b4_final_state_number +/* YYLAST -- Last index in YYTABLE. */ #define YYLAST b4_last /* YYNTOKENS -- Number of terminals. */ @@ -811,7 +812,7 @@ yyparse (YYPARSE_PARAM_ARG) yysetstate: *yyssp = yystate; - if (yyssp >= yyss + yystacksize - 1) + if (yyss + yystacksize - 1 <= yyssp) { /* Get the current used size of the three stacks, in elements. */ YYSIZE_T yysize = yyssp - yyss + 1; @@ -843,10 +844,10 @@ yyparse (YYPARSE_PARAM_ARG) goto yyoverflowlab; # else /* Extend the stack our own way. */ - if (yystacksize >= YYMAXDEPTH) + if (YYMAXDEPTH <= yystacksize) goto yyoverflowlab; yystacksize *= 2; - if (yystacksize > YYMAXDEPTH) + if (YYMAXDEPTH < yystacksize) yystacksize = YYMAXDEPTH; { @@ -872,7 +873,7 @@ yyparse (YYPARSE_PARAM_ARG) YYDPRINTF ((stderr, "Stack size increased to %lu\n", (unsigned long int) yystacksize)); - if (yyssp >= yyss + yystacksize - 1) + if (yyss + yystacksize - 1 <= yyssp) YYABORT; } @@ -1005,7 +1006,7 @@ yyreduce: yyn - 1, yyrline[yyn]); /* Print the symbols being reduced, and their result. */ - for (yyi = yyprhs[yyn]; yyrhs[yyi] >= 0; yyi++) + for (yyi = yyprhs[yyn]; 0 <= yyrhs[yyi]; yyi++) YYFPRINTF (stderr, "%s ", yytname[yyrhs[yyi]]); YYFPRINTF (stderr, " -> %s\n", yytname[yyr1[yyn]]); } @@ -1126,7 +1127,7 @@ yyerrlab1: /* Pop the error token. */ YYPOPSTACK; /* Pop the rest of the stack. */ - while (yyssp > yyss) + while (yyss < yyssp) { YYDPRINTF ((stderr, "Error: popping ")); YYDSYMPRINT ((stderr, diff --git a/src/output.c b/src/output.c index 41961a5d..1edf854f 100644 --- a/src/output.c +++ b/src/output.c @@ -107,19 +107,25 @@ GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t) GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t) -/*-----------------------------------------------------------------. -| Prepare the muscles related to the tokens: translate, tname, and | -| toknum. | -`-----------------------------------------------------------------*/ +/*------------------------------------------------------------------. +| Prepare the muscles related to the symbols: translate, tname, and | +| toknum. | +`------------------------------------------------------------------*/ static void -prepare_tokens (void) +prepare_symbols (void) { + MUSCLE_INSERT_INT ("tokens_number", ntokens); + MUSCLE_INSERT_INT ("nterms_number", nvars); + MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number); + MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number); + muscle_insert_symbol_number_table ("translate", token_translations, token_translations[0], 1, max_user_token_number + 1); + /* tname -- token names. */ { int i; int j = 0; @@ -215,6 +221,8 @@ prepare_rules (void) muscle_insert_short_table ("dprec", dprec, 0, 0, nrules); muscle_insert_short_table ("merger", merger, 0, 0, nrules); + MUSCLE_INSERT_INT ("rules_number", nrules); + free (rhs); free (prhs); free (rline); @@ -238,6 +246,10 @@ prepare_states (void) values[i] = states[i]->accessing_symbol; muscle_insert_symbol_number_table ("stos", values, 0, 1, nstates); + + MUSCLE_INSERT_INT ("last", high); + MUSCLE_INSERT_INT ("final_state_number", final_state->number); + MUSCLE_INSERT_INT ("states_number", nstates); } @@ -449,7 +461,7 @@ prepare_actions (void) that case. Nevertheless, it seems even better to be able to use the GLR skeletons even without the non-deterministic tables. */ muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table, - conflict_table[0], 1, high+1); + conflict_table[0], 1, high + 1); muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list, conflict_list[0], 1, conflict_list_cnt); } @@ -566,20 +578,6 @@ prepare (void) MUSCLE_INSERT_STRING ("output_parser_name", parser_file_name); MUSCLE_INSERT_STRING ("output_header_name", spec_defines_file); - /* Symbols. */ - MUSCLE_INSERT_INT ("tokens_number", ntokens); - MUSCLE_INSERT_INT ("nterms_number", nvars); - MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number); - MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number); - - /* Rules. */ - MUSCLE_INSERT_INT ("rules_number", nrules); - - /* States. */ - MUSCLE_INSERT_INT ("last", high); - MUSCLE_INSERT_INT ("final_state_number", final_state->number); - MUSCLE_INSERT_INT ("states_number", nstates); - /* User Code. */ obstack_1grow (&pre_prologue_obstack, 0); obstack_1grow (&post_prologue_obstack, 0); @@ -609,7 +607,7 @@ output (void) { obstack_init (&format_obstack); - prepare_tokens (); + prepare_symbols (); prepare_rules (); prepare_states (); prepare_actions (); diff --git a/src/tables.h b/src/tables.h index 27ff7b54..92b4605e 100644 --- a/src/tables.h +++ b/src/tables.h @@ -85,8 +85,10 @@ default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise, YYTABLE[P+I] should be used. - YYFINAL = the state number of the termination state. YYFLAG = most - negative short int. Used to flag ?? */ + YYFINAL = the state number of the termination state. + + YYLAST ( = high) the number of the last element of YYTABLE, i.e., + sizeof (YYTABLE) - 1. */ extern int nvectors; -- 2.45.2