]> git.saurik.com Git - bison.git/blob - data/lalr1.cc
* data/c.m4 (b4_token_enums): New.
[bison.git] / data / lalr1.cc
1 m4_divert(-1)
2
3 # C++ skeleton for Bison
4
5 # Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301 USA
21
22 m4_include(b4_pkgdatadir/[c++.m4])
23
24 # We do want M4 expansion after # for CPP macros.
25 m4_changecom()
26 m4_divert(0)dnl
27 m4_if(b4_defines_flag, 0, [],
28 [@output @output_header_name@
29 b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
30 [2002, 2003, 2004, 2005])[
31 /* FIXME: This is wrong, we want computed header guards.
32 I don't know why the macros are missing now. :( */
33 #ifndef PARSER_HEADER_H
34 # define PARSER_HEADER_H
35
36 #include <string>
37 #include <iostream>
38
39 /* Using locations. */
40 #define YYLSP_NEEDED ]b4_locations_flag[
41
42 namespace yy
43 {
44 class position;
45 class location;
46 }
47
48 ]b4_token_enums(b4_tokens)[
49
50 /* Copy the first part of user declarations. */
51 ]b4_pre_prologue[
52
53 ]/* Line __line__ of lalr1.cc. */
54 b4_syncline([@oline@], [@ofile@])[
55
56 #include "stack.hh"
57 #include "location.hh"
58
59 /* Enabling traces. */
60 #ifndef YYDEBUG
61 # define YYDEBUG ]b4_debug[
62 #endif
63
64 /* Enabling verbose error messages. */
65 #ifdef YYERROR_VERBOSE
66 # undef YYERROR_VERBOSE
67 # define YYERROR_VERBOSE 1
68 #else
69 # define YYERROR_VERBOSE ]b4_error_verbose[
70 #endif
71
72 /* Enabling the token table. */
73 #ifndef YYTOKEN_TABLE
74 # define YYTOKEN_TABLE ]b4_token_table[
75 #endif
76
77 #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
78 ]m4_ifdef([b4_stype],
79 [b4_syncline([b4_stype_line], [b4_file_name])
80 union YYSTYPE b4_stype;
81 /* Line __line__ of lalr1.cc. */
82 b4_syncline([@oline@], [@ofile@])],
83 [typedef int YYSTYPE;])[
84 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
85 # define YYSTYPE_IS_DECLARED 1
86 # define YYSTYPE_IS_TRIVIAL 1
87 #endif
88
89 /* Copy the second part of user declarations. */
90 ]b4_post_prologue[
91
92 ]/* Line __line__ of lalr1.cc. */
93 b4_syncline([@oline@], [@ofile@])[
94 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
95 If N is 0, then set CURRENT to the empty location which ends
96 the previous symbol: RHS[0] (always defined). */
97
98 #ifndef YYLLOC_DEFAULT
99 # define YYLLOC_DEFAULT(Current, Rhs, N) \
100 do { \
101 if (N) \
102 { \
103 (Current).begin = (Rhs)[1].begin; \
104 (Current).end = (Rhs)[N].end; \
105 } \
106 else \
107 { \
108 (Current).begin = (Current).end = (Rhs)[0].end; \
109 } \
110 } while (0)
111 #endif
112
113 namespace yy
114 {
115 class ]b4_parser_class_name[;
116
117 template <typename P>
118 struct traits
119 {
120 };
121
122 template <>
123 struct traits<]b4_parser_class_name[>
124 {
125 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
126 typedef ]b4_int_type_for([b4_rhs])[ rhs_number_type;
127 typedef int state_type;
128 typedef YYSTYPE semantic_type;
129 typedef ]b4_location_type[ location_type;
130 };
131 }
132
133 namespace yy
134 {
135 /// A Bison parser.
136 class ]b4_parser_class_name[
137 {
138 /// Symbol semantic values.
139 typedef traits<]b4_parser_class_name[>::semantic_type semantic_type;
140 /// Symbol locations.
141 typedef traits<]b4_parser_class_name[>::location_type location_type;
142
143 public:
144 /// Build a parser object.
145 ]b4_parser_class_name[ (]b4_parse_param_decl[) :
146 yydebug_ (false),
147 yycdebug_ (&std::cerr)]b4_parse_param_cons[
148 {
149 }
150
151 virtual ~]b4_parser_class_name[ ()
152 {
153 }
154
155 /// Parse.
156 /// \returns 0 iff parsing succeeded.
157 virtual int parse ();
158
159 /// The current debugging stream.
160 std::ostream& debug_stream () const;
161 /// Set the current debugging stream.
162 void set_debug_stream (std::ostream &);
163
164 /// Type for debugging levels.
165 typedef int debug_level_type;
166 /// The current debugging level.
167 debug_level_type debug_level () const;
168 /// Set the current debugging level.
169 void set_debug_level (debug_level_type l);
170
171 private:
172 /// Report a syntax error.
173 /// \param loc where the syntax error is found.
174 /// \param msg a description of the syntax error.
175 virtual void error (const location_type& loc, const std::string& msg);
176
177 /// Generate an error message.
178 /// \param tok the look-ahead token.
179 virtual std::string yysyntax_error_ (int tok);
180
181 #if YYDEBUG
182 /// \brief Report a symbol on the debug stream.
183 /// \param yytype The token type.
184 /// \param yyvaluep Its semantic value.
185 /// \param yylocationp Its location.
186 virtual void yysymprint_ (int yytype,
187 const semantic_type* yyvaluep,
188 const location_type* yylocationp);
189 #endif /* ! YYDEBUG */
190
191
192 /// State numbers.
193 typedef traits<]b4_parser_class_name[>::state_type state_type;
194 /// State stack type.
195 typedef stack<state_type> state_stack_type;
196 /// Semantic value stack type.
197 typedef stack<semantic_type> semantic_stack_type;
198 /// location stack type.
199 typedef stack<location_type> location_stack_type;
200
201 /// The state stack.
202 state_stack_type yystate_stack_;
203 /// The semantic value stack.
204 semantic_stack_type yysemantic_stack_;
205 /// The location stack.
206 location_stack_type yylocation_stack_;
207
208 /// Internal symbol numbers.
209 typedef traits<]b4_parser_class_name[>::token_number_type token_number_type;
210 /* Tables. */
211 /// For a state, the index in \a yytable_ of its portion.
212 static const ]b4_int_type_for([b4_pact])[ yypact_[];
213 static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_;
214
215 /// For a state, default rule to reduce.
216 /// Unless\a yytable_ specifies something else to do.
217 /// Zero means the default is an error.
218 static const ]b4_int_type_for([b4_defact])[ yydefact_[];
219
220 static const ]b4_int_type_for([b4_pgoto])[ yypgoto_[];
221 static const ]b4_int_type_for([b4_defgoto])[ yydefgoto_[];
222
223 /// What to do in a state.
224 /// \a yytable_[yypact_[s]]: what to do in state \a s.
225 /// - if positive, shift that token.
226 /// - if negative, reduce the rule which number is the opposite.
227 /// - if zero, do what YYDEFACT says.
228 static const ]b4_int_type_for([b4_table])[ yytable_[];
229 static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ yytable_ninf_;
230
231 static const ]b4_int_type_for([b4_check])[ yycheck_[];
232
233 /// For a state, its accessing symbol.
234 static const ]b4_int_type_for([b4_stos])[ yystos_[];
235
236 /// For a rule, its LHS.
237 static const ]b4_int_type_for([b4_r1])[ yyr1_[];
238 /// For a rule, its RHS length.
239 static const ]b4_int_type_for([b4_r2])[ yyr2_[];
240
241 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
242 /// For a symbol, its name in clear.
243 static const char* const yytname_[];
244 #endif
245
246 #if YYERROR_VERBOSE
247 /// Convert the symbol name \a n to a form suitable for a diagnostic.
248 virtual std::string yytnamerr_ (const char *n);
249 #endif
250
251 #if YYDEBUG
252 /// A type to store symbol numbers and -1.
253 typedef traits<]b4_parser_class_name[>::rhs_number_type rhs_number_type;
254 /// A `-1'-separated list of the rules' RHS.
255 static const rhs_number_type yyrhs_[];
256 /// For each rule, the index of the first RHS symbol in \a yyrhs_.
257 static const ]b4_int_type_for([b4_prhs])[ yyprhs_[];
258 /// For each rule, its source line number.
259 static const ]b4_int_type_for([b4_rline])[ yyrline_[];
260 /// For each scanner token number, its symbol number.
261 static const ]b4_int_type_for([b4_toknum])[ yytoken_number_[];
262 /// Report on the debug stream that the rule \a r is going to be reduced.
263 virtual void yyreduce_print_ (int r);
264 /// Print the state stack on the debug stream.
265 virtual void yystack_print_ ();
266 #endif
267
268 /// Convert a scanner token number to a symbol number.
269 inline token_number_type yytranslate_ (int token);
270
271 /// \brief Reclaim the memory associated to a symbol.
272 /// \param yymsg Why this token is reclaimed.
273 /// \param yytype The symbol type.
274 /// \param yyvaluep Its semantic value.
275 /// \param yylocationp Its location.
276 inline void yydestruct_ (const char* yymsg,
277 int yytype,
278 semantic_type* yyvaluep,
279 location_type* yylocationp);
280
281 /// Pop \a n symbols the three stacks.
282 inline void yypop_ (unsigned int n = 1);
283
284 /* Constants. */
285 static const int yyeof_;
286 /* LAST_ -- Last index in TABLE_. */
287 static const int yylast_;
288 static const int yynnts_;
289 static const int yyempty_;
290 static const int yyfinal_;
291 static const int yyterror_;
292 static const int yyerrcode_;
293 static const int yyntokens_;
294 static const unsigned int yyuser_token_number_max_;
295 static const token_number_type yyundef_token_;
296
297 /* State. */
298 int yyn_;
299 int yylen_;
300 int yystate_;
301
302 /* Error handling. */
303 int yynerrs_;
304 int yyerrstatus_;
305
306 /* Debugging. */
307 int yydebug_;
308 std::ostream* yycdebug_;
309
310 ]b4_parse_param_vars[
311 };
312 }
313
314 #endif /* ! defined PARSER_HEADER_H */]
315 ])dnl
316 @output @output_parser_name@
317 b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
318 [2002, 2003, 2004, 2005])
319 m4_if(b4_prefix[], [yy], [],
320 [
321 // Take the name prefix into account.
322 #define yylex b4_prefix[]lex])
323 m4_if(b4_defines_flag, 0, [],
324 [
325 #include @output_header_name@])[
326
327 #ifndef YY_
328 # if YYENABLE_NLS
329 # if ENABLE_NLS
330 # include <libintl.h> /* FIXME: INFRINGES ON USER NAME SPACE */
331 # define YY_(msgid) dgettext ("bison-runtime", msgid)
332 # endif
333 # endif
334 # ifndef YY_
335 # define YY_(msgid) msgid
336 # endif
337 #endif
338
339 /* A pseudo ostream that takes yydebug_ into account. */
340 # define YYCDEBUG \
341 for (bool yydebugcond_ = yydebug_; yydebugcond_; yydebugcond_ = false) \
342 (*yycdebug_)
343
344 /* Enable debugging if requested. */
345 #if YYDEBUG
346
347 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
348 do { \
349 if (yydebug_) \
350 { \
351 *yycdebug_ << (Title) << ' '; \
352 yysymprint_ ((Type), (Value), (Location)); \
353 *yycdebug_ << std::endl; \
354 } \
355 } while (0)
356
357 # define YY_REDUCE_PRINT(Rule) \
358 do { \
359 if (yydebug_) \
360 yyreduce_print_ (Rule); \
361 } while (0)
362
363 # define YY_STACK_PRINT() \
364 do { \
365 if (yydebug_) \
366 yystack_print_ (); \
367 } while (0)
368
369 #else /* !YYDEBUG */
370
371 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
372 # define YY_REDUCE_PRINT(Rule)
373 # define YY_STACK_PRINT()
374
375 #endif /* !YYDEBUG */
376
377 #define YYACCEPT goto yyacceptlab
378 #define YYABORT goto yyabortlab
379 #define YYERROR goto yyerrorlab
380
381 #if YYERROR_VERBOSE
382
383 /* Return YYSTR after stripping away unnecessary quotes and
384 backslashes, so that it's suitable for yyerror. The heuristic is
385 that double-quoting is unnecessary unless the string contains an
386 apostrophe, a comma, or backslash (other than backslash-backslash).
387 YYSTR is taken from yytname. */
388 std::string
389 yy::]b4_parser_class_name[::yytnamerr_ (const char *yystr)
390 {
391 if (*yystr == '"')
392 {
393 std::string yyr = "";
394 char const *yyp = yystr;
395
396 for (;;)
397 switch (*++yyp)
398 {
399 case '\'':
400 case ',':
401 goto do_not_strip_quotes;
402
403 case '\\':
404 if (*++yyp != '\\')
405 goto do_not_strip_quotes;
406 /* Fall through. */
407 default:
408 yyr += *yyp;
409 break;
410
411 case '"':
412 return yyr;
413 }
414 do_not_strip_quotes: ;
415 }
416
417 return yystr;
418 }
419
420 #endif
421
422 #if YYDEBUG
423 /*--------------------------------.
424 | Print this symbol on YYOUTPUT. |
425 `--------------------------------*/
426
427 void
428 yy::]b4_parser_class_name[::yysymprint_ (int yytype,
429 const semantic_type* yyvaluep, const location_type* yylocationp)
430 {
431 /* Pacify ``unused variable'' warnings. */
432 (void) yyvaluep;
433 (void) yylocationp;
434 /* Backward compatibility, but should be removed eventually. */
435 std::ostream& cdebug_ = *yycdebug_;
436 (void) cdebug_;
437
438 *yycdebug_ << (yytype < yyntokens_ ? "token" : "nterm")
439 << ' ' << yytname_[yytype] << " ("
440 << *yylocationp << ": ";
441 switch (yytype)
442 {
443 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
444 [ default:
445 break;
446 }
447 *yycdebug_ << ')';
448 }
449 #endif /* ! YYDEBUG */
450
451 void
452 yy::]b4_parser_class_name[::yydestruct_ (const char* yymsg,
453 int yytype, semantic_type* yyvaluep, location_type* yylocationp)
454 {
455 /* Pacify ``unused variable'' warnings. */
456 (void) yymsg;
457 (void) yyvaluep;
458 (void) yylocationp;
459
460 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
461
462 switch (yytype)
463 {
464 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
465 default:
466 break;
467 }
468 }
469
470 void
471 yy::]b4_parser_class_name[::yypop_ (unsigned int n)
472 {
473 yystate_stack_.pop (n);
474 yysemantic_stack_.pop (n);
475 yylocation_stack_.pop (n);
476 }
477
478 std::ostream&
479 yy::]b4_parser_class_name[::debug_stream () const
480 {
481 return *yycdebug_;
482 }
483
484 void
485 yy::]b4_parser_class_name[::set_debug_stream (std::ostream& o)
486 {
487 yycdebug_ = &o;
488 }
489
490
491 yy::]b4_parser_class_name[::debug_level_type
492 yy::]b4_parser_class_name[::debug_level () const
493 {
494 return yydebug_;
495 }
496
497 void
498 yy::]b4_parser_class_name[::set_debug_level (debug_level_type l)
499 {
500 yydebug_ = l;
501 }
502
503
504 int
505 yy::]b4_parser_class_name[::parse ()
506 {
507 /* Look-ahead and look-ahead in internal form. */
508 int yylooka;
509 int yyilooka;
510
511 /// Semantic value of the look-ahead.
512 semantic_type yylval;
513 /// Location of the look-ahead.
514 location_type yylloc;
515 /// The locations where the error started and ended.
516 location yyerror_range[2];
517
518 /// $$.
519 semantic_type yyval;
520 /// @@$.
521 location_type yyloc;
522
523 int yyresult_;
524
525 YYCDEBUG << "Starting parse" << std::endl;
526
527 yynerrs_ = 0;
528 yyerrstatus_ = 0;
529
530 /* Start. */
531 yystate_ = 0;
532 yylooka = yyempty_;
533
534 ]m4_ifdef([b4_initial_action], [
535 m4_pushdef([b4_at_dollar], [yylloc])dnl
536 m4_pushdef([b4_dollar_dollar], [yylval])dnl
537 /* User initialization code. */
538 b4_initial_action
539 m4_popdef([b4_dollar_dollar])dnl
540 m4_popdef([b4_at_dollar])dnl
541 /* Line __line__ of yacc.c. */
542 b4_syncline([@oline@], [@ofile@])])dnl
543
544 [ /* Initialize the stacks. The initial state will be pushed in
545 yynewstate, since the latter expects the semantical and the
546 location values to have been already stored, initialize these
547 stacks with a primary value. */
548 yystate_stack_ = state_stack_type (0);
549 yysemantic_stack_ = semantic_stack_type (0);
550 yylocation_stack_ = location_stack_type (0);
551 yysemantic_stack_.push (yylval);
552 yylocation_stack_.push (yylloc);
553
554 /* New state. */
555 yynewstate:
556 yystate_stack_.push (yystate_);
557 YYCDEBUG << "Entering state " << yystate_ << std::endl;
558 goto yybackup;
559
560 /* Backup. */
561 yybackup:
562
563 /* Try to take a decision without look-ahead. */
564 yyn_ = yypact_[yystate_];
565 if (yyn_ == yypact_ninf_)
566 goto yydefault;
567
568 /* Read a look-ahead token. */
569 if (yylooka == yyempty_)
570 {
571 YYCDEBUG << "Reading a token: ";
572 yylooka = ]b4_c_function_call([yylex], [int],
573 [[YYSTYPE*], [&yylval]][]dnl
574 b4_location_if([, [[location*], [&yylloc]]])dnl
575 m4_ifdef([b4_lex_param], [, ]b4_lex_param))[;
576 }
577
578
579 /* Convert token to internal form. */
580 if (yylooka <= yyeof_)
581 {
582 yylooka = yyilooka = yyeof_;
583 YYCDEBUG << "Now at end of input." << std::endl;
584 }
585 else
586 {
587 yyilooka = yytranslate_ (yylooka);
588 YY_SYMBOL_PRINT ("Next token is", yyilooka, &yylval, &yylloc);
589 }
590
591 /* If the proper action on seeing token ILOOKA_ is to reduce or to
592 detect an error, take that action. */
593 yyn_ += yyilooka;
594 if (yyn_ < 0 || yylast_ < yyn_ || yycheck_[yyn_] != yyilooka)
595 goto yydefault;
596
597 /* Reduce or error. */
598 yyn_ = yytable_[yyn_];
599 if (yyn_ < 0)
600 {
601 if (yyn_ == yytable_ninf_)
602 goto yyerrlab;
603 else
604 {
605 yyn_ = -yyn_;
606 goto yyreduce;
607 }
608 }
609 else if (yyn_ == 0)
610 goto yyerrlab;
611
612 /* Accept? */
613 if (yyn_ == yyfinal_)
614 goto yyacceptlab;
615
616 /* Shift the look-ahead token. */
617 YY_SYMBOL_PRINT ("Shifting", yyilooka, &yylval, &yylloc);
618
619 /* Discard the token being shifted unless it is eof. */
620 if (yylooka != yyeof_)
621 yylooka = yyempty_;
622
623 yysemantic_stack_.push (yylval);
624 yylocation_stack_.push (yylloc);
625
626 /* Count tokens shifted since error; after three, turn off error
627 status. */
628 if (yyerrstatus_)
629 --yyerrstatus_;
630
631 yystate_ = yyn_;
632 goto yynewstate;
633
634 /*-----------------------------------------------------------.
635 | yydefault -- do the default action for the current state. |
636 `-----------------------------------------------------------*/
637 yydefault:
638 yyn_ = yydefact_[yystate_];
639 if (yyn_ == 0)
640 goto yyerrlab;
641 goto yyreduce;
642
643 /*-----------------------------.
644 | yyreduce -- Do a reduction. |
645 `-----------------------------*/
646 yyreduce:
647 yylen_ = yyr2_[yyn_];
648 /* If LEN_ is nonzero, implement the default value of the action:
649 `$$ = $1'. Otherwise, use the top of the stack.
650
651 Otherwise, the following line sets YYVAL to garbage.
652 This behavior is undocumented and Bison
653 users should not rely upon it. */
654 if (yylen_)
655 yyval = yysemantic_stack_[yylen_ - 1];
656 else
657 yyval = yysemantic_stack_[0];
658
659 {
660 slice<location_type, location_stack_type> slice (yylocation_stack_, yylen_);
661 YYLLOC_DEFAULT (yyloc, slice, yylen_);
662 }
663 YY_REDUCE_PRINT (yyn_);
664 switch (yyn_)
665 {
666 ]b4_actions[
667 default: break;
668 }
669
670 ]/* Line __line__ of lalr1.cc. */
671 b4_syncline([@oline@], [@ofile@])[
672
673 yypop_ (yylen_);
674
675 YY_STACK_PRINT ();
676
677 yysemantic_stack_.push (yyval);
678 yylocation_stack_.push (yyloc);
679
680 /* Shift the result of the reduction. */
681 yyn_ = yyr1_[yyn_];
682 yystate_ = yypgoto_[yyn_ - yyntokens_] + yystate_stack_[0];
683 if (0 <= yystate_ && yystate_ <= yylast_
684 && yycheck_[yystate_] == yystate_stack_[0])
685 yystate_ = yytable_[yystate_];
686 else
687 yystate_ = yydefgoto_[yyn_ - yyntokens_];
688 goto yynewstate;
689
690 /*------------------------------------.
691 | yyerrlab -- here on detecting error |
692 `------------------------------------*/
693 yyerrlab:
694 /* If not already recovering from an error, report this error. */
695 if (!yyerrstatus_)
696 {
697 ++yynerrs_;
698 error (yylloc, yysyntax_error_ (yyilooka));
699 }
700
701 yyerror_range[0] = yylloc;
702 if (yyerrstatus_ == 3)
703 {
704 /* If just tried and failed to reuse look-ahead token after an
705 error, discard it. */
706
707 if (yylooka <= yyeof_)
708 {
709 /* Return failure if at end of input. */
710 if (yylooka == yyeof_)
711 YYABORT;
712 }
713 else
714 {
715 yydestruct_ ("Error: discarding", yyilooka, &yylval, &yylloc);
716 yylooka = yyempty_;
717 }
718 }
719
720 /* Else will try to reuse look-ahead token after shifting the error
721 token. */
722 goto yyerrlab1;
723
724
725 /*---------------------------------------------------.
726 | yyerrorlab -- error raised explicitly by YYERROR. |
727 `---------------------------------------------------*/
728 yyerrorlab:
729
730 /* Pacify compilers like GCC when the user code never invokes
731 YYERROR and the label yyerrorlab therefore never appears in user
732 code. */
733 if (false)
734 goto yyerrorlab;
735
736 yyerror_range[0] = yylocation_stack_[yylen_ - 1];
737 yypop_ (yylen_);
738 yystate_ = yystate_stack_[0];
739 goto yyerrlab1;
740
741 /*-------------------------------------------------------------.
742 | yyerrlab1 -- common code for both syntax error and YYERROR. |
743 `-------------------------------------------------------------*/
744 yyerrlab1:
745 yyerrstatus_ = 3; /* Each real token shifted decrements this. */
746
747 for (;;)
748 {
749 yyn_ = yypact_[yystate_];
750 if (yyn_ != yypact_ninf_)
751 {
752 yyn_ += yyterror_;
753 if (0 <= yyn_ && yyn_ <= yylast_ && yycheck_[yyn_] == yyterror_)
754 {
755 yyn_ = yytable_[yyn_];
756 if (0 < yyn_)
757 break;
758 }
759 }
760
761 /* Pop the current state because it cannot handle the error token. */
762 if (yystate_stack_.height () == 1)
763 YYABORT;
764
765 yyerror_range[0] = yylocation_stack_[0];
766 yydestruct_ ("Error: popping",
767 yystos_[yystate_],
768 &yysemantic_stack_[0], &yylocation_stack_[0]);
769 yypop_ ();
770 yystate_ = yystate_stack_[0];
771 YY_STACK_PRINT ();
772 }
773
774 if (yyn_ == yyfinal_)
775 goto yyacceptlab;
776
777 yyerror_range[1] = yylloc;
778 // Using YYLLOC is tempting, but would change the location of
779 // the look-ahead. YYLOC is available though.
780 YYLLOC_DEFAULT (yyloc, yyerror_range - 1, 2);
781 yysemantic_stack_.push (yylval);
782 yylocation_stack_.push (yyloc);
783
784 /* Shift the error token. */
785 YY_SYMBOL_PRINT ("Shifting", yystos_[yyn_],
786 &yysemantic_stack_[0], &yylocation_stack_[0]);
787
788 yystate_ = yyn_;
789 goto yynewstate;
790
791 /* Accept. */
792 yyacceptlab:
793 yyresult_ = 0;
794 goto yyreturn;
795
796 /* Abort. */
797 yyabortlab:
798 yyresult_ = 1;
799 goto yyreturn;
800
801 yyreturn:
802 if (yylooka != yyeof_ && yylooka != yyempty_)
803 yydestruct_ ("Cleanup: discarding lookahead", yyilooka, &yylval, &yylloc);
804
805 while (yystate_stack_.height () != 1)
806 {
807 yydestruct_ ("Cleanup: popping",
808 yystos_[yystate_stack_[0]],
809 &yysemantic_stack_[0],
810 &yylocation_stack_[0]);
811 yypop_ ();
812 }
813
814 return yyresult_;
815 }
816
817 // Generate an error message.
818 std::string
819 yy::]b4_parser_class_name[::yysyntax_error_ (int tok)
820 {
821 std::string res;
822 #if YYERROR_VERBOSE
823 yyn_ = yypact_[yystate_];
824 if (yypact_ninf_ < yyn_ && yyn_ < yylast_)
825 {
826 /* Start YYX at -YYN if negative to avoid negative indexes in
827 YYCHECK. */
828 int yyxbegin = yyn_ < 0 ? -yyn_ : 0;
829
830 /* Stay within bounds of both yycheck and yytname. */
831 int yychecklim = yylast_ - yyn_;
832 int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_;
833 int count = 0;
834 for (int x = yyxbegin; x < yyxend; ++x)
835 if (yycheck_[x + yyn_] == x && x != yyterror_)
836 ++count;
837
838 // FIXME: This method of building the message is not compatible
839 // with internationalization. It should work like yacc.c does it.
840 // That is, first build a string that looks like this:
841 // "syntax error, unexpected %s or %s or %s"
842 // Then, invoke YY_ on this string.
843 // Finally, use the string as a format to output
844 // yytname_[tok], etc.
845 // Until this gets fixed, this message appears in English only.
846 res = "syntax error, unexpected ";
847 res += yytnamerr_ (yytname_[tok]);
848 if (count < 5)
849 {
850 count = 0;
851 for (int x = yyxbegin; x < yyxend; ++x)
852 if (yycheck_[x + yyn_] == x && x != yyterror_)
853 {
854 res += (!count++) ? ", expecting " : " or ";
855 res += yytnamerr_ (yytname_[x]);
856 }
857 }
858 }
859 else
860 #endif
861 res = YY_("syntax error");
862 return res;
863 }
864
865
866 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
867 STATE-NUM. */
868 const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) yy::b4_parser_class_name::yypact_ninf_ = b4_pact_ninf[;
869 const ]b4_int_type_for([b4_pact])[
870 yy::]b4_parser_class_name[::yypact_[] =
871 {
872 ]b4_pact[
873 };
874
875 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
876 doesn't specify something else to do. Zero means the default is an
877 error. */
878 const ]b4_int_type_for([b4_defact])[
879 yy::]b4_parser_class_name[::yydefact_[] =
880 {
881 ]b4_defact[
882 };
883
884 /* YYPGOTO[NTERM-NUM]. */
885 const ]b4_int_type_for([b4_pgoto])[
886 yy::]b4_parser_class_name[::yypgoto_[] =
887 {
888 ]b4_pgoto[
889 };
890
891 /* YYDEFGOTO[NTERM-NUM]. */
892 const ]b4_int_type_for([b4_defgoto])[
893 yy::]b4_parser_class_name[::yydefgoto_[] =
894 {
895 ]b4_defgoto[
896 };
897
898 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
899 positive, shift that token. If negative, reduce the rule which
900 number is the opposite. If zero, do what YYDEFACT says. */
901 const ]b4_int_type(b4_table_ninf, b4_table_ninf) yy::b4_parser_class_name::yytable_ninf_ = b4_table_ninf[;
902 const ]b4_int_type_for([b4_table])[
903 yy::]b4_parser_class_name[::yytable_[] =
904 {
905 ]b4_table[
906 };
907
908 /* YYCHECK. */
909 const ]b4_int_type_for([b4_check])[
910 yy::]b4_parser_class_name[::yycheck_[] =
911 {
912 ]b4_check[
913 };
914
915 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
916 symbol of state STATE-NUM. */
917 const ]b4_int_type_for([b4_stos])[
918 yy::]b4_parser_class_name[::yystos_[] =
919 {
920 ]b4_stos[
921 };
922
923 #if YYDEBUG
924 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding
925 to YYLEX-NUM. */
926 const ]b4_int_type_for([b4_toknum])[
927 yy::]b4_parser_class_name[::yytoken_number_[] =
928 {
929 ]b4_toknum[
930 };
931 #endif
932
933 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
934 const ]b4_int_type_for([b4_r1])[
935 yy::]b4_parser_class_name[::yyr1_[] =
936 {
937 ]b4_r1[
938 };
939
940 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
941 const ]b4_int_type_for([b4_r2])[
942 yy::]b4_parser_class_name[::yyr2_[] =
943 {
944 ]b4_r2[
945 };
946
947 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
948 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
949 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
950 const char*
951 const yy::]b4_parser_class_name[::yytname_[] =
952 {
953 ]b4_tname[
954 };
955 #endif
956
957 #if YYDEBUG
958 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
959 const yy::]b4_parser_class_name[::rhs_number_type
960 yy::]b4_parser_class_name[::yyrhs_[] =
961 {
962 ]b4_rhs[
963 };
964
965 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
966 YYRHS. */
967 const ]b4_int_type_for([b4_prhs])[
968 yy::]b4_parser_class_name[::yyprhs_[] =
969 {
970 ]b4_prhs[
971 };
972
973 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
974 const ]b4_int_type_for([b4_rline])[
975 yy::]b4_parser_class_name[::yyrline_[] =
976 {
977 ]b4_rline[
978 };
979
980 // Print the state stack on the debug stream.
981 void
982 yy::]b4_parser_class_name[::yystack_print_ ()
983 {
984 *yycdebug_ << "Stack now";
985 for (state_stack_type::const_iterator i = yystate_stack_.begin ();
986 i != yystate_stack_.end (); ++i)
987 *yycdebug_ << ' ' << *i;
988 *yycdebug_ << std::endl;
989 }
990
991 // Report on the debug stream that the rule \a yyrule is going to be reduced.
992 void
993 yy::]b4_parser_class_name[::yyreduce_print_ (int yyrule)
994 {
995 unsigned int yylno = yyrline_[yyrule];
996 /* Print the symbols being reduced, and their result. */
997 *yycdebug_ << "Reducing stack by rule " << yyn_ - 1
998 << " (line " << yylno << "), ";
999 for (]b4_int_type_for([b4_prhs])[ i = yyprhs_[yyn_];
1000 0 <= yyrhs_[i]; ++i)
1001 *yycdebug_ << yytname_[yyrhs_[i]] << ' ';
1002 *yycdebug_ << "-> " << yytname_[yyr1_[yyn_]] << std::endl;
1003 }
1004 #endif // YYDEBUG
1005
1006 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
1007 yy::]b4_parser_class_name[::token_number_type
1008 yy::]b4_parser_class_name[::yytranslate_ (int token)
1009 {
1010 static
1011 const token_number_type
1012 translate_table[] =
1013 {
1014 ]b4_translate[
1015 };
1016 if ((unsigned int) token <= yyuser_token_number_max_)
1017 return translate_table[token];
1018 else
1019 return yyundef_token_;
1020 }
1021
1022 const int yy::]b4_parser_class_name[::yyeof_ = 0;
1023 const int yy::]b4_parser_class_name[::yylast_ = ]b4_last[;
1024 const int yy::]b4_parser_class_name[::yynnts_ = ]b4_nterms_number[;
1025 const int yy::]b4_parser_class_name[::yyempty_ = -2;
1026 const int yy::]b4_parser_class_name[::yyfinal_ = ]b4_final_state_number[;
1027 const int yy::]b4_parser_class_name[::yyterror_ = 1;
1028 const int yy::]b4_parser_class_name[::yyerrcode_ = 256;
1029 const int yy::]b4_parser_class_name[::yyntokens_ = ]b4_tokens_number[;
1030
1031 const unsigned int yy::]b4_parser_class_name[::yyuser_token_number_max_ = ]b4_user_token_number_max[;
1032 const yy::]b4_parser_class_name[::token_number_type yy::]b4_parser_class_name[::yyundef_token_ = ]b4_undef_token_number[;
1033
1034 ]b4_epilogue
1035 dnl
1036 @output stack.hh
1037 b4_copyright([stack handling for Bison C++ parsers], [2002, 2003, 2004, 2005])[
1038
1039 #ifndef BISON_STACK_HH
1040 # define BISON_STACK_HH
1041
1042 #include <deque>
1043
1044 namespace yy
1045 {
1046 template <class T, class S = std::deque<T> >
1047 class stack
1048 {
1049 public:
1050
1051 // Hide our reversed order.
1052 typedef typename S::reverse_iterator iterator;
1053 typedef typename S::const_reverse_iterator const_iterator;
1054
1055 stack () : seq_ ()
1056 {
1057 }
1058
1059 stack (unsigned int n) : seq_ (n)
1060 {
1061 }
1062
1063 inline
1064 T&
1065 operator [] (unsigned int i)
1066 {
1067 return seq_[i];
1068 }
1069
1070 inline
1071 const T&
1072 operator [] (unsigned int i) const
1073 {
1074 return seq_[i];
1075 }
1076
1077 inline
1078 void
1079 push (const T& t)
1080 {
1081 seq_.push_front (t);
1082 }
1083
1084 inline
1085 void
1086 pop (unsigned int n = 1)
1087 {
1088 for (; n; --n)
1089 seq_.pop_front ();
1090 }
1091
1092 inline
1093 unsigned int
1094 height () const
1095 {
1096 return seq_.size ();
1097 }
1098
1099 inline const_iterator begin () const { return seq_.rbegin (); }
1100 inline const_iterator end () const { return seq_.rend (); }
1101
1102 private:
1103
1104 S seq_;
1105 };
1106
1107 /// Present a slice of the top of a stack.
1108 template <class T, class S = stack<T> >
1109 class slice
1110 {
1111 public:
1112
1113 slice (const S& stack,
1114 unsigned int range) : stack_ (stack),
1115 range_ (range)
1116 {
1117 }
1118
1119 inline
1120 const T&
1121 operator [] (unsigned int i) const
1122 {
1123 return stack_[range_ - i];
1124 }
1125
1126 private:
1127
1128 const S& stack_;
1129 unsigned int range_;
1130 };
1131 }
1132
1133 #endif // not BISON_STACK_HH]
1134 dnl
1135 @output position.hh
1136 b4_copyright([Position class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
1137
1138 /**
1139 ** \file position.hh
1140 ** Define the position class.
1141 */
1142
1143 #ifndef BISON_POSITION_HH
1144 # define BISON_POSITION_HH
1145
1146 # include <iostream>
1147 # include <string>
1148
1149 namespace yy
1150 {
1151 /// Abstract a position.
1152 class position
1153 {
1154 public:
1155 /// Initial column number.
1156 static const unsigned int initial_column = 0;
1157 /// Initial line number.
1158 static const unsigned int initial_line = 1;
1159
1160 /** \name Ctor & dtor.
1161 ** \{ */
1162 public:
1163 /// Construct a position.
1164 position () :
1165 filename (0),
1166 line (initial_line),
1167 column (initial_column)
1168 {
1169 }
1170 /** \} */
1171
1172
1173 /** \name Line and Column related manipulators
1174 ** \{ */
1175 public:
1176 /// (line related) Advance to the COUNT next lines.
1177 inline void lines (int count = 1)
1178 {
1179 column = initial_column;
1180 line += count;
1181 }
1182
1183 /// (column related) Advance to the COUNT next columns.
1184 inline void columns (int count = 1)
1185 {
1186 int leftmost = initial_column;
1187 int current = column;
1188 if (leftmost <= current + count)
1189 column += count;
1190 else
1191 column = initial_column;
1192 }
1193 /** \} */
1194
1195 public:
1196 /// File name to which this position refers.
1197 ]b4_filename_type[* filename;
1198 /// Current line number.
1199 unsigned int line;
1200 /// Current column number.
1201 unsigned int column;
1202 };
1203
1204 /// Add and assign a position.
1205 inline const position&
1206 operator+= (position& res, const int width)
1207 {
1208 res.columns (width);
1209 return res;
1210 }
1211
1212 /// Add two position objects.
1213 inline const position
1214 operator+ (const position& begin, const int width)
1215 {
1216 position res = begin;
1217 return res += width;
1218 }
1219
1220 /// Add and assign a position.
1221 inline const position&
1222 operator-= (position& res, const int width)
1223 {
1224 return res += -width;
1225 }
1226
1227 /// Add two position objects.
1228 inline const position
1229 operator- (const position& begin, const int width)
1230 {
1231 return begin + -width;
1232 }
1233
1234 /** \brief Intercept output stream redirection.
1235 ** \param ostr the destination output stream
1236 ** \param pos a reference to the position to redirect
1237 */
1238 inline std::ostream&
1239 operator<< (std::ostream& ostr, const position& pos)
1240 {
1241 if (pos.filename)
1242 ostr << *pos.filename << ':';
1243 return ostr << pos.line << '.' << pos.column;
1244 }
1245
1246 }
1247 #endif // not BISON_POSITION_HH]
1248 @output location.hh
1249 b4_copyright([Location class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
1250
1251 /**
1252 ** \file location.hh
1253 ** Define the location class.
1254 */
1255
1256 #ifndef BISON_LOCATION_HH
1257 # define BISON_LOCATION_HH
1258
1259 # include <iostream>
1260 # include <string>
1261 # include "position.hh"
1262
1263 namespace yy
1264 {
1265
1266 /// Abstract a location.
1267 class location
1268 {
1269 /** \name Ctor & dtor.
1270 ** \{ */
1271 public:
1272 /// Construct a location.
1273 location () :
1274 begin (),
1275 end ()
1276 {
1277 }
1278 /** \} */
1279
1280
1281 /** \name Line and Column related manipulators
1282 ** \{ */
1283 public:
1284 /// Reset initial location to final location.
1285 inline void step ()
1286 {
1287 begin = end;
1288 }
1289
1290 /// Extend the current location to the COUNT next columns.
1291 inline void columns (unsigned int count = 1)
1292 {
1293 end += count;
1294 }
1295
1296 /// Extend the current location to the COUNT next lines.
1297 inline void lines (unsigned int count = 1)
1298 {
1299 end.lines (count);
1300 }
1301 /** \} */
1302
1303
1304 public:
1305 /// Beginning of the located region.
1306 position begin;
1307 /// End of the located region.
1308 position end;
1309 };
1310
1311 /// Join two location objects to create a location.
1312 inline const location operator+ (const location& begin, const location& end)
1313 {
1314 location res = begin;
1315 res.end = end.end;
1316 return res;
1317 }
1318
1319 /// Add two location objects.
1320 inline const location operator+ (const location& begin, unsigned int width)
1321 {
1322 location res = begin;
1323 res.columns (width);
1324 return res;
1325 }
1326
1327 /// Add and assign a location.
1328 inline location& operator+= (location& res, unsigned int width)
1329 {
1330 res.columns (width);
1331 return res;
1332 }
1333
1334 /** \brief Intercept output stream redirection.
1335 ** \param ostr the destination output stream
1336 ** \param loc a reference to the location to redirect
1337 **
1338 ** Avoid duplicate information.
1339 */
1340 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
1341 {
1342 position last = loc.end - 1;
1343 ostr << loc.begin;
1344 if (last.filename
1345 && (!loc.begin.filename
1346 || *loc.begin.filename != *last.filename))
1347 ostr << '-' << last;
1348 else if (loc.begin.line != last.line)
1349 ostr << '-' << last.line << '.' << last.column;
1350 else if (loc.begin.column != last.column)
1351 ostr << '-' << last.column;
1352 return ostr;
1353 }
1354
1355 }
1356
1357 #endif // not BISON_LOCATION_HH]