X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/5348bfbe2923560fe0e03a486466a738a392626c..0ced3098fd97e1483b4ac938bdce99bfc7942539:/data/lalr1.cc?ds=sidebyside diff --git a/data/lalr1.cc b/data/lalr1.cc index cb1274d6..4fe3c2ad 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -234,6 +234,7 @@ namespace yy virtual void lex_ (); virtual void error_ (); virtual void print_ (); + virtual void report_syntax_error_ (); /* Stacks. */ StateStack state_stack_; @@ -264,6 +265,7 @@ namespace yy 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. */ @@ -287,6 +289,10 @@ namespace yy int len_; int state_; + /* Error handling. */ + int nerrs_; + int errstatus_; + /* Debugging. */ int debug_; std::ostream &cdebug_; @@ -328,9 +334,15 @@ 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 @@ -341,8 +353,8 @@ do { \ 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 @@ -442,8 +454,8 @@ 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; @@ -497,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); @@ -525,46 +528,9 @@ b4_syncline([@oline@], [@ofile@])[ `------------------------------------*/ yyerrlab: /* If not already recovering from an error, report this error. */ - if (!errstatus) - { - ++nerrs; + report_syntax_error_ (); -#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 x1 = xbegin; x1 < xend; ++x1) - if (check_[x1 + n_] == x1 && x1 != terror_) - { - message += (!count++) ? ", expecting " : " or "; - message += name_[x1]; - } - } - } - } - else -#endif - message = "syntax error"; - error_ (); - } - - if (errstatus == 3) + if (errstatus_ == 3) { /* If just tried and failed to reuse lookahead token after an error, discard it. */ @@ -607,6 +573,13 @@ b4_syncline([@oline@], [@ofile@])[ `---------------------------------------------------*/ 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_); @@ -617,7 +590,7 @@ yyerrorlab: | yyerrlab1 -- common code for both syntax error and YYERROR. | `-------------------------------------------------------------*/ yyerrlab1: - errstatus = 3; /* Each real token shifted decrements this. */ + errstatus_ = 3; /* Each real token shifted decrements this. */ for (;;) { @@ -663,17 +636,7 @@ yyerrlab1: semantic_stack_.pop (); location_stack_.pop (); state_ = state_stack_[0]; - -#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 + YY_STACK_PRINT (); } if (n_ == final_) @@ -706,6 +669,45 @@ 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. */ @@ -821,12 +823,23 @@ yy::]b4_parser_class_name[::rline_[] = ]b4_rline[ }; +/** 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) { - int yyi; unsigned int yylno = rline_[yyrule]; /* Print the symbols being reduced, and their result. */ cdebug_ << "Reducing via rule " << n_ - 1 << " (line " << yylno << "), ";