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