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