X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/8901f32e4aa568b05cbf31f7697f9cd98408f81e..e9b0834e184ae9e5263c2fb3a915df60019d25e6:/data/lalr1.cc diff --git a/data/lalr1.cc b/data/lalr1.cc index dc321881..93c30057 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -46,19 +46,50 @@ m4_define([b4_assert_if], [b4_percent_define_ifdef([[assert]], [$1], [$2])]) +# b4_lhs_value([TYPE]) +# -------------------- +# Expansion of $$. +m4_define([b4_lhs_value], +[b4_symbol_value([yylhs.value], [$1])]) + + +# b4_lhs_location() +# ----------------- +# Expansion of @$. +m4_define([b4_lhs_location], +[yylhs.location]) + + +# b4_rhs_data(RULE-LENGTH, NUM) +# ----------------------------- +# Return the data corresponding to the symbol #NUM, where the current +# rule has RULE-LENGTH symbols on RHS. +m4_define([b4_rhs_data], + [yystack_@{($1) - ($2)@}]) + + +# b4_rhs_state(RULE-LENGTH, NUM) +# ----------------------------- +# The state corresponding to the symbol #NUM, where the current +# rule has RULE-LENGTH symbols on RHS. +m4_define([b4_rhs_state], + [b4_rhs_data([$1], [$2]).state]) + + # b4_rhs_value(RULE-LENGTH, NUM, [TYPE]) # -------------------------------------- # Expansion of $NUM, where the current rule has RULE-LENGTH # symbols on RHS. m4_define([b4_rhs_value], - [b4_symbol_value([yystack_@{($1) - ($2)@}.value], [$3])]) + [b4_symbol_value([b4_rhs_data([$1], [$2]).value], [$3])]) + # b4_rhs_location(RULE-LENGTH, NUM) # --------------------------------- # Expansion of @NUM, where the current rule has RULE-LENGTH symbols # on RHS. m4_define([b4_rhs_location], -[yystack_@{($1) - ($2)@}.location]) + [b4_rhs_data([$1], [$2]).location]) # b4_symbol_actions(FILENAME, LINENO, @@ -427,10 +458,6 @@ m4_ifdef([b4_stype], #endif #if YYDEBUG - /// A `-1'-separated list of the rules' RHS. - static const ]b4_int_type_for([b4_rhs])[ yyrhs_[]; - /// For each rule, the index of the first RHS symbol in \a yyrhs_. - static const ]b4_int_type_for([b4_prhs])[ yyprhs_[]; /// For each rule, its source line number. static const ]b4_int_type_for([b4_rline])[ yyrline_[]; /// For each scanner token number, its symbol number. @@ -485,9 +512,11 @@ m4_ifdef([b4_stype], stack_type yystack_; /// Push a new state on the stack. - /// \warning the contents of \a v is stolen. - inline void yypush_ (state_type s, - semantic_type& v, const location_type& l); + /// \param m a debug message to display + /// if null, no trace is output. + /// \param s the symbol + /// \warning the contents of \a s.value is stolen. + inline void yypush_ (const char* m, data_type& s); /// Pop \a n symbols the three stacks. inline void yypop_ (unsigned int n = 1); @@ -725,13 +754,15 @@ b4_percent_code_get[]dnl } void - ]b4_parser_class_name[::yypush_ (state_type s, - semantic_type& v, const location_type& l) + ]b4_parser_class_name[::yypush_ (const char* m, data_type& s) { + if (m) + YY_SYMBOL_PRINT (m, yystos_[s.state], s.value, s.location); ]b4_variant_if( [[ yystack_.push (data_type (s, semantic_type(), l)); - ]b4_symbol_variant([[yystos_[s]]], [[yystack_[0].value]], [build], [v])], -[ yystack_.push (data_type (s, v, l));])[ + ]b4_symbol_variant([[yystos_[s]]], [[yystack_[0].value]], + [build], [s.value])], +[ yystack_.push (s);])[ } void @@ -783,25 +814,23 @@ b4_percent_code_get[]dnl int yynerrs_ = 0; int yyerrstatus_ = 0; - /// Semantic value of the lookahead. - semantic_type yylval; - /// Location of the lookahead. - location_type yylloc; + /// The lookahead symbol. + data_type yyla; + /// The locations where the error started and ended. data_type yyerror_range[2]; - /// $$. - semantic_type yyval; - /// @@$. - location_type yyloc; + /// $$ and @@$. + data_type yylhs; + /// The return value of parse(). int yyresult; YYCDEBUG << "Starting parse" << std::endl; ]m4_ifdef([b4_initial_action], [ -m4_pushdef([b4_at_dollar], [yylloc])dnl -m4_pushdef([b4_dollar_dollar], [yylval])dnl +m4_pushdef([b4_at_dollar], [yyla.location])dnl +m4_pushdef([b4_dollar_dollar], [yyla.value])dnl /* User initialization code. */ b4_user_initial_action m4_popdef([b4_dollar_dollar])dnl @@ -812,7 +841,7 @@ m4_popdef([b4_at_dollar])])dnl location values to have been already stored, initialize these stacks with a primary value. */ yystack_ = stack_type (0); - yypush_ (yystate, yylval, yylloc); + yypush_ (0, yyla); // A new state was pushed on the stack. // Invariant: yystate == yystack_[0].state, i.e., @@ -839,8 +868,8 @@ m4_popdef([b4_at_dollar])])dnl { YYCDEBUG << "Reading a token: "; yychar = ]b4_c_function_call([yylex], [int], - [[YYSTYPE*], [&yylval]][]dnl -b4_locations_if([, [[location*], [&yylloc]]])dnl + [[YYSTYPE*], [&yyla.value]][]dnl +b4_locations_if([, [[location*], [&yyla.location]]])dnl m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; } @@ -854,7 +883,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; else { yytoken = yytranslate_ (yychar); - YY_SYMBOL_PRINT ("Next token is", yytoken, yylval, yylloc); + YY_SYMBOL_PRINT ("Next token is", yytoken, yyla.value, yyla.location); } /* If the proper action on seeing token YYTOKEN is to reduce or to @@ -868,14 +897,11 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; if (yyn <= 0) { if (yyn == 0 || yyn == yytable_ninf_) - goto yyerrlab; + goto yyerrlab; yyn = -yyn; goto yyreduce; } - /* Shift the lookahead token. */ - YY_SYMBOL_PRINT ("Shifting", yytoken, yylval, yylloc); - /* Discard the token being shifted. */ yychar = yyempty_; @@ -884,8 +910,9 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; if (yyerrstatus_) --yyerrstatus_; - yystate = yyn; - yypush_ (yystate, yylval, yylloc); + /* Shift the lookahead token. */ + yyla.state = yystate = yyn; + yypush_ ("Shifting", yyla); goto yynewstate; /*-----------------------------------------------------------. @@ -905,22 +932,22 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; /* Variants are always initialized to an empty instance of the correct type. The default $$=$1 rule is NOT applied when using variants */ - ]b4_symbol_variant([[yyr1_@{yyn@}]], [yyval], [build])[],[ + ]b4_symbol_variant([[yyr1_@{yyn@}]], [yylhs.value], [build])[],[ /* If YYLEN is nonzero, implement the default value of the action: `$$ = $1'. Otherwise, use the top of the stack. - Otherwise, the following line sets YYVAL to garbage. + Otherwise, the following line sets YYLHS.VALUE to garbage. This behavior is undocumented and Bison users should not rely upon it. */ if (yylen) - yyval = yystack_@{yylen - 1@}.value; + yylhs.value = yystack_@{yylen - 1@}.value; else - yyval = yystack_@{0@}.value;])[ + yylhs.value = yystack_@{0@}.value;])[ // Compute the default @@$. { slice slice (yystack_, yylen); - YYLLOC_DEFAULT (yyloc, slice, yylen); + YYLLOC_DEFAULT (yylhs.location, slice, yylen); } // Perform the reduction. @@ -931,18 +958,27 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; default: break; } - YY_SYMBOL_PRINT ("-> $$ =", yyr1_[yyn], yyval, yyloc); + // Compute post-reduction state. + yyn = yyr1_[yyn]; + yystate = yypgoto_[yyn - yyntokens_] + yystack_[yylen].state; + if (0 <= yystate && yystate <= yylast_ + && yycheck_[yystate] == yystack_[yylen].state) + yystate = yytable_[yystate]; + else + yystate = yydefgoto_[yyn - yyntokens_]; + yylhs.state = yystate; + YY_SYMBOL_PRINT ("-> $$ =", yyn, yylhs.value, yylhs.location); ]b4_variant_if([[ // Destroy the lhs symbols. for (int i = 0; i < yylen; ++i) - // Destroy a variant which value may have be swapped with yyval. - // The value of yyval (hence maybe one of these lhs symbols) - // depends on what does the default contruction for this type. - // In the case of pointers for instance, nothing is done, so the - // value is junk. Therefore do not try to report the content in - // the debug trace, it's junk. Hence yymsg = 0. Besides, that - // keeps exactly the same traces as with the other Bison - // skeletons. + // Destroy a variant which value may have be swapped with + // yylhs.value. The value of yylhs.value (hence maybe one of + // these lhs symbols) depends on what does the default + // contruction for this type. In the case of pointers for + // instance, nothing is done, so the value is junk. Therefore + // do not try to report the content in the debug trace, it's + // junk. Hence yymsg = 0. Besides, that keeps exactly the same + // traces as with the other Bison skeletons. yydestruct_ (0, yystos_[yystack_[i].state], yystack_[i].value, yystack_[i].location);]])[ @@ -952,14 +988,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; YY_STACK_PRINT (); /* Shift the result of the reduction. */ - yyn = yyr1_[yyn]; - yystate = yypgoto_[yyn - yyntokens_] + yystack_[0].state; - if (0 <= yystate && yystate <= yylast_ - && yycheck_[yystate] == yystack_[0].state) - yystate = yytable_[yystate]; - else - yystate = yydefgoto_[yyn - yyntokens_]; - yypush_ (yystate, yyval, yyloc); + yypush_ (0, yylhs); goto yynewstate; /*------------------------------------. @@ -970,10 +999,10 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; if (!yyerrstatus_) { ++yynerrs_; - error (yylloc, yysyntax_error_ (yystate, yytoken)); + error (yyla.location, yysyntax_error_ (yystate, yytoken)); } - yyerror_range[0].location = yylloc; + yyerror_range[0].location = yyla.location; if (yyerrstatus_ == 3) { /* If just tried and failed to reuse lookahead token after an @@ -987,7 +1016,8 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; } else { - yydestruct_ ("Error: discarding", yytoken, yylval, yylloc); + yydestruct_ ("Error: discarding", + yytoken, yyla.value, yyla.location); yychar = yyempty_; } } @@ -1021,45 +1051,42 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; `-------------------------------------------------------------*/ yyerrlab1: yyerrstatus_ = 3; /* Each real token shifted decrements this. */ + { + data_type error_token; + for (;;) + { + yyn = yypact_[yystate]; + if (yyn != yypact_ninf_) + { + yyn += yyterror_; + if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_) + { + yyn = yytable_[yyn]; + if (0 < yyn) + break; + } + } - for (;;) - { - yyn = yypact_[yystate]; - if (yyn != yypact_ninf_) - { - yyn += yyterror_; - if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_) - { - yyn = yytable_[yyn]; - if (0 < yyn) - break; - } - } - - // Pop the current state because it cannot handle the error token. - if (yystack_.size () == 1) - YYABORT; - - yyerror_range[0].location = yystack_[0].location; - yydestruct_ ("Error: popping", - yystos_[yystate], - yystack_[0].value, yystack_[0].location); - yypop_ (); - yystate = yystack_[0].state; - YY_STACK_PRINT (); - } - - yyerror_range[1].location = yylloc; - // Using YYLLOC is tempting, but would change the location of - // the lookahead. YYLOC is available though. - YYLLOC_DEFAULT (yyloc, (yyerror_range - 1), 2); - - /* Shift the error token. */ - YY_SYMBOL_PRINT ("Shifting", yystos_[yyn], - yystack_[0].value, yystack_[0].location); - - yystate = yyn; - yypush_ (yystate, yylval, yyloc); + // Pop the current state because it cannot handle the error token. + if (yystack_.size () == 1) + YYABORT; + + yyerror_range[0].location = yystack_[0].location; + yydestruct_ ("Error: popping", + yystos_[yystate], + yystack_[0].value, yystack_[0].location); + yypop_ (); + yystate = yystack_[0].state; + YY_STACK_PRINT (); + } + + yyerror_range[1].location = yyla.location; + YYLLOC_DEFAULT (error_token.location, (yyerror_range - 1), 2); + + /* Shift the error token. */ + error_token.state = yystate = yyn; + yypush_ ("Shifting", error_token); + } goto yynewstate; /* Accept. */ @@ -1074,7 +1101,8 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; yyreturn: if (yychar != yyempty_) - yydestruct_ ("Cleanup: discarding lookahead", yytoken, yylval, yylloc); + yydestruct_ ("Cleanup: discarding lookahead", + yytoken, yyla.value, yyla.location); /* Do not reclaim the symbols of the rule which action triggered this YYABORT or YYACCEPT. */ @@ -1193,13 +1221,6 @@ b4_error_verbose_if([ tok])[) #endif #if YYDEBUG - /* YYRHS -- A `-1'-separated list of the rules' RHS. */ - ]b4_table_define([rhs], [b4_rhs])[; - - /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in - YYRHS. */ - ]b4_table_define([prhs], [b4_prhs])[; - /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ ]b4_table_define([rline], [b4_rline])[; @@ -1228,7 +1249,7 @@ b4_error_verbose_if([ tok])[) /* The symbols being reduced. */ for (int yyi = 0; yyi < yynrhs; yyi++) YY_SYMBOL_PRINT (" $" << yyi + 1 << " =", - yyrhs_[yyprhs_[yyrule] + yyi], + ]yystos_@{b4_rhs_state(yynrhs, yyi + 1)@}[, ]b4_rhs_value(yynrhs, yyi + 1)[, ]b4_rhs_location(yynrhs, yyi + 1)[); }