X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/2ffbee0e0b8b8b4d3015b5af63d8da7ac28949cb..6a5ecb38fcfbcc5b75211ea2988c7cd16e73d428:/data/lalr1.cc diff --git a/data/lalr1.cc b/data/lalr1.cc index 2e6947bc..4fe3c2ad 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -170,7 +170,7 @@ b4_syncline([@oline@], [@ofile@])], b4_syncline([@oline@], [@ofile@])[ #ifndef YYLLOC_DEFAULT # define YYLLOC_DEFAULT(Current, Rhs, N) \ - Current.end = Rhs[N].end; + ((Current).end = Rhs[N].end) #endif namespace yy @@ -234,6 +234,7 @@ namespace yy virtual void lex_ (); virtual void error_ (); virtual void print_ (); + virtual void report_syntax_error_ (); /* Stacks. */ StateStack state_stack_; @@ -263,6 +264,8 @@ namespace yy static const ]b4_int_type_for([b4_rline])[ rline_[]; static const ]b4_int_type_for([b4_stos])[ stos_[]; static const ]b4_int_type_for([b4_toknum])[ token_number_[]; + virtual void reduce_print_ (int yyrule); + virtual void stack_print_ (); #endif /* Even more tables. */ @@ -286,6 +289,10 @@ namespace yy int len_; int state_; + /* Error handling. */ + int nerrs_; + int errstatus_; + /* Debugging. */ int debug_; std::ostream &cdebug_; @@ -322,20 +329,32 @@ m4_if(b4_defines_flag, 0, [], [#include @output_header_name@])[ /* Enable debugging if requested. */ #if YYDEBUG # define YYCDEBUG if (debug_) cdebug_ +# define YY_REDUCE_PRINT(Rule) \ +do { \ + if (debug_) \ + reduce_print_ (Rule); \ +} while (0) +# define YY_STACK_PRINT() \ +do { \ + if (debug_) \ + stack_print_ (); \ +} while (0) #else /* !YYDEBUG */ # define YYCDEBUG if (0) cdebug_ +# define YY_REDUCE_PRINT(Rule) +# define YY_STACK_PRINT() #endif /* !YYDEBUG */ #define YYACCEPT goto yyacceptlab #define YYABORT goto yyabortlab -#define YYERROR goto yyerrlab1 +#define YYERROR goto yyerrorlab int yy::]b4_parser_class_name[::parse () { - int nerrs = 0; - int errstatus = 0; + nerrs_ = 0; + errstatus_ = 0; /* Initialize the stacks. The initial state will be pushed in yynewstate, since the latter expects the semantical and the @@ -395,6 +414,8 @@ yy::]b4_parser_class_name[::parse () #endif } + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ n_ += ilooka_; if (n_ < 0 || last_ < n_ || check_[n_] != ilooka_) goto yydefault; @@ -433,22 +454,32 @@ yy::]b4_parser_class_name[::parse () /* Count tokens shifted since error; after three, turn off error status. */ - if (errstatus) - --errstatus; + if (errstatus_) + --errstatus_; state_ = n_; goto yynewstate; - /* Default action. */ +/*-----------------------------------------------------------. +| yydefault -- do the default action for the current state. | +`-----------------------------------------------------------*/ yydefault: n_ = defact_[state_]; if (n_ == 0) goto yyerrlab; goto yyreduce; - /* Reduce. */ +/*-----------------------------. +| yyreduce -- Do a reduction. | +`-----------------------------*/ yyreduce: len_ = r2_[n_]; + /* If LEN_ 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. + This behavior is undocumented and Bison + users should not rely upon it. */ if (len_) { yyval = semantic_stack_[len_ - 1]; @@ -460,28 +491,12 @@ yy::]b4_parser_class_name[::parse () yyloc = location_stack_[0]; } -#if YYDEBUG - if (debug_) - { - // Short files will use "unsigned char" for line numbers, - // in which case they will be output as character litterals - // by "<<". - unsigned yylno = rline_[n_]; - YYCDEBUG << "Reducing via rule " << n_ - 1 - << " (line " << yylno << "), "; - for (]b4_int_type_for([b4_prhs])[ i = prhs_[n_]; - 0 <= rhs_[i]; ++i) - YYCDEBUG << name_[rhs_[i]] << ' '; - YYCDEBUG << "-> " << name_[r1_[n_]] << std::endl; - } -#endif - if (len_) { Slice< LocationType, LocationStack > slice (location_stack_, len_); YYLLOC_DEFAULT (yyloc, slice, len_); } - + YY_REDUCE_PRINT (n_); switch (n_) { ]b4_actions[ @@ -494,16 +509,7 @@ b4_syncline([@oline@], [@ofile@])[ semantic_stack_.pop (len_); location_stack_.pop (len_); -#if YYDEBUG - if (debug_) - { - YYCDEBUG << "state stack now"; - for (StateStack::ConstIterator i = state_stack_.begin (); - i != state_stack_.end (); ++i) - YYCDEBUG << ' ' << *i; - YYCDEBUG << std::endl; - } -#endif + YY_STACK_PRINT (); semantic_stack_.push (yyval); location_stack_.push (yyloc); @@ -517,73 +523,74 @@ b4_syncline([@oline@], [@ofile@])[ state_ = defgoto_[n_ - ntokens_]; goto yynewstate; - /* Report and recover from errors. This is very incomplete. */ +/*------------------------------------. +| yyerrlab -- here on detecting error | +`------------------------------------*/ yyerrlab: /* If not already recovering from an error, report this error. */ - if (!errstatus) - { - ++nerrs; - -#if YYERROR_VERBOSE - n_ = pact_[state_]; - if (pact_ninf_ < n_ && n_ < last_) - { - message = "syntax error, unexpected "; - message += name_[ilooka_]; - { - int count = 0; - /* 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 = xbegin; x < xend; ++x) - if (check_[x + n_] == x && x != terror_) - { - message += (!count++) ? ", expecting " : " or "; - message += name_[x]; - } - } - } - } - else -#endif - message = "syntax error"; - error_ (); - } - goto yyerrlab1; + report_syntax_error_ (); - - /*----------------------------------------------------. - | yyerrlab1 -- error raised explicitly by an action. | - `----------------------------------------------------*/ - yyerrlab1: - if (errstatus == 3) + if (errstatus_ == 3) { /* If just tried and failed to reuse lookahead token after an error, discard it. */ /* Return failure if at end of input. */ - if (looka_ == eof_) - goto yyabortlab; + if (looka_ <= eof_) + { + /* If at end of input, pop the error token, + then the rest of the stack, then return failure. */ + if (looka_ == eof_) + for (;;) + { + state_stack_.pop (); + semantic_stack_.pop (); + location_stack_.pop (); + if (state_stack_.height () == 1) + YYABORT; +// YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp); +// FIXME: yydestruct (yystos[*yyssp], yyvsp]b4_location_if([, yylsp])[); + } + } + else + { #if YYDEBUG - YYCDEBUG << "Discarding token " << looka_ - << " (" << name_[ilooka_] << ")." << std::endl; + YYCDEBUG << "Discarding token " << looka_ + << " (" << name_[ilooka_] << ")." << std::endl; +// yydestruct (yytoken, &yylval]b4_location_if([, &yylloc])[); #endif - looka_ = empty_; + looka_ = empty_; + } } /* Else will try to reuse lookahead token after shifting the error token. */ + goto yyerrlab1; + + +/*---------------------------------------------------. +| yyerrorlab -- error raised explicitly by YYERROR. | +`---------------------------------------------------*/ +yyerrorlab: + +#ifdef __GNUC__ + /* Pacify GCC when the user code never invokes YYERROR and the label + yyerrorlab therefore never appears in user code. */ + if (0) + goto yyerrorlab; +#endif + + state_stack_.pop (len_); + semantic_stack_.pop (len_); + location_stack_.pop (len_); + state_ = state_stack_[0]; + goto yyerrlab1; - errstatus = 3; +/*-------------------------------------------------------------. +| yyerrlab1 -- common code for both syntax error and YYERROR. | +`-------------------------------------------------------------*/ +yyerrlab1: + errstatus_ = 3; /* Each real token shifted decrements this. */ for (;;) { @@ -601,7 +608,7 @@ b4_syncline([@oline@], [@ofile@])[ /* Pop the current state because it cannot handle the error token. */ if (state_stack_.height () == 1) - goto yyabortlab; + YYABORT; #if YYDEBUG if (debug_) @@ -625,20 +632,11 @@ b4_syncline([@oline@], [@ofile@])[ } #endif - state_ = (state_stack_.pop (), state_stack_[0]); + state_stack_.pop (); semantic_stack_.pop (); - location_stack_.pop ();; - -#if YYDEBUG - if (debug_) - { - YYCDEBUG << "Error: state stack now"; - for (StateStack::ConstIterator i = state_stack_.begin (); - i != state_stack_.end (); ++i) - YYCDEBUG << ' ' << *i; - YYCDEBUG << std::endl; - } -#endif + location_stack_.pop (); + state_ = state_stack_[0]; + YY_STACK_PRINT (); } if (n_ == final_) @@ -671,6 +669,46 @@ yy::]b4_parser_class_name[::lex_ () #endif } +/** Generate an error message, and invoke yyerror. */ +void +yy::]b4_parser_class_name[::report_syntax_error_ () +{ + /* If not already recovering from an error, report this error. */ + if (!errstatus_) + { + ++nerrs_; + +#if YYERROR_VERBOSE + n_ = pact_[state_]; + if (pact_ninf_ < n_ && n_ < last_) + { + message = "syntax error, unexpected "; + message += name_[ilooka_]; + { + int count = 0; + for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++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) + if (check_[x + n_] == x && x != terror_) + { + message += (!count++) ? ", expecting " : " or "; + message += name_[x]; + } + } + } + } + else +#endif + message = "syntax error"; + error_ (); + } +} + + /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing STATE-NUM. */ const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) yy::b4_parser_class_name::pact_ninf_ = b4_pact_ninf[; @@ -784,7 +822,33 @@ yy::]b4_parser_class_name[::rline_[] = { ]b4_rline[ }; -#endif + +/** Print the state stack from its BOTTOM up to its TOP (included). */ + +void +yy::]b4_parser_class_name[::stack_print_ () +{ + cdebug_ << "state stack now"; + for (StateStack::ConstIterator i = state_stack_.begin (); + i != state_stack_.end (); ++i) + cdebug_ << ' ' << *i; + cdebug_ << std::endl; +} + +/** Report that the YYRULE is going to be reduced. */ + +void +yy::]b4_parser_class_name[::reduce_print_ (int yyrule) +{ + unsigned int yylno = rline_[yyrule]; + /* Print the symbols being reduced, and their result. */ + cdebug_ << "Reducing via rule " << n_ - 1 << " (line " << yylno << "), "; + for (]b4_int_type_for([b4_prhs])[ i = prhs_[n_]; + 0 <= rhs_[i]; ++i) + cdebug_ << name_[rhs_[i]] << ' '; + cdebug_ << "-> " << name_[r1_[n_]] << std::endl; +} +#endif // YYDEBUG /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ yy::]b4_parser_class_name[::TokenNumberType @@ -792,12 +856,12 @@ yy::]b4_parser_class_name[::translate_ (int token) { static const TokenNumberType - translate_[] = + translate_table[] = { ]b4_translate[ }; if ((unsigned) token <= user_token_number_max_) - return translate_[token]; + return translate_table[token]; else return undef_token_; } @@ -844,16 +908,16 @@ namespace yy inline T& - operator [] (unsigned index) + operator [] (unsigned i) { - return seq_[index]; + return seq_[i]; } inline const T& - operator [] (unsigned index) const + operator [] (unsigned i) const { - return seq_[index]; + return seq_[i]; } inline @@ -899,9 +963,9 @@ namespace yy inline const T& - operator [] (unsigned index) const + operator [] (unsigned i) const { - return stack_[range_ - index]; + return stack_[range_ - i]; } private: @@ -1019,7 +1083,7 @@ namespace yy inline std::ostream& operator<< (std::ostream& ostr, const Position& pos) { - if (pos.filename != "") + if (!pos.filename.empty ()) ostr << pos.filename << ':'; return ostr << pos.line << '.' << pos.column; }