1 # C++ skeleton for Bison
3 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 # Free Software Foundation, Inc.
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.
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.
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/>.
19 m4_include(b4_pkgdatadir
/[c
++.m4
])
21 # b4_subtract(LHS, RHS)
22 # ---------------------
23 # Evaluate LHS - RHS if they are integer literals, otherwise expand
25 m4_define([b4_subtract
],
26 [m4_bmatch([$
1$
2], [^[0123456789]*$
],
34 # Join with comma, skipping empty arguments.
35 # b4_args calls itself recursively until it sees the first non-empty
36 # argument, then calls _b4_args which prepends each non-empty argument
42 [$
1[]_$
0(m4_shift($@
))],
43 [$
0(m4_shift($@
))])])])
45 # _b4_args(ARGS1, ...)
46 # --------------------
50 [m4_ifval([$
1], [, $
1])[]$
0(m4_shift($@
))])])
53 # b4_table_define(TABLE-NAME, CONTENT)
54 # ------------------------------------
55 # Define "parser::yy<TABLE-NAME>_" which contents is CONTENT.
56 m4_define([b4_table_define
],
57 [const b4_int_type_for([$
2])
58 b4_parser_class_name::yy$
1_
[[]] =
64 # b4_symbol_value_template(VAL, [TYPE])
65 # -------------------------------------
66 # Same as b4_symbol_value, but used in a template method.
67 m4_copy([b4_symbol_value
], [b4_symbol_value_template
])
69 # How the semantic value is extracted when using variants.
71 # b4_symbol_value(VAL, [TYPE])
72 # ----------------------------
73 m4_define([b4_symbol_value
],
78 # b4_symbol_value_template(VAL, [TYPE])
79 # -------------------------------------
80 # Same as b4_symbol_value, but used in a template method.
81 m4_define([b4_symbol_value_template
],
83 [$
1.template as
<$
2>()],
88 # b4_lex_symbol_if([IF-YYLEX-RETURNS-A-COMPLETE-SYMBOL], [IF-NOT])
89 # ----------------------------------------------------------------
90 m4_define([b4_lex_symbol_if
],
91 [b4_percent_define_ifdef([[lex_symbol]], [$
1], [$
2])])
94 # b4_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
95 # ------------------------------------------------
96 m4_define([b4_assert_if
],
97 [b4_percent_define_ifdef([[assert]], [$
1], [$
2])])
100 # b4_lhs_value([TYPE])
101 # --------------------
102 # Expansion of $<TYPE>$.
103 m4_define([b4_lhs_value
],
104 [b4_symbol_value([yylhs
.value
], [$
1])])
110 m4_define([b4_lhs_location
],
114 # b4_rhs_data(RULE-LENGTH, NUM)
115 # -----------------------------
116 # Return the data corresponding to the symbol #NUM, where the current
117 # rule has RULE-LENGTH symbols on RHS.
118 m4_define([b4_rhs_data
],
119 [yystack_@
{b4_subtract($@
)@
}])
122 # b4_rhs_state(RULE-LENGTH, NUM)
123 # -----------------------------
124 # The state corresponding to the symbol #NUM, where the current
125 # rule has RULE-LENGTH symbols on RHS.
126 m4_define([b4_rhs_state
],
127 [b4_rhs_data([$
1], [$
2]).state
])
130 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
131 # --------------------------------------
132 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
134 m4_define([b4_rhs_value
],
135 [b4_symbol_value([b4_rhs_data([$
1], [$
2]).value
], [$
3])])
138 # b4_rhs_location(RULE-LENGTH, NUM)
139 # ---------------------------------
140 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
142 m4_define([b4_rhs_location
],
143 [b4_rhs_data([$
1], [$
2]).location
])
146 # b4_symbol(NUM, FIELD)
147 # ---------------------
148 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
149 # undefined. If FIELD = id, prepend the prefix.
150 m4_define([b4_symbol
],
151 [m4_if([$
2], [id
], [b4_percent_define_get([token
.prefix
])])dnl
152 m4_indir([b4_symbol($
1, $
2)])])
155 # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
156 # -------------------------------------------
157 # If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
158 # Otherwise an error.
159 m4_define([b4_symbol_if
],
160 [m4_case(b4_symbol([$
1], [$
2]),
163 [m4_fatal([$
0: field $
2 of $
1 is
not a Boolean
:] b4_symbol([$
1], [$
2]))])])
166 # b4_symbol_actions(FILENAME, LINENO,
167 # SYMBOL-TAG, SYMBOL-NUM,
168 # SYMBOL-ACTION, SYMBOL-TYPENAME)
169 # -------------------------------------------------
170 # Same as in C, but using references instead of pointers.
171 m4_define([b4_symbol_actions
],
172 [m4_pushdef([b4_dollar_dollar
],
173 [b4_symbol_value_template([yysym
.value
], [$
6])])dnl
174 m4_pushdef([b4_at_dollar
], [yysym
.location
])dnl
176 b4_syncline([$
2], [$
1])
178 b4_syncline([@oline@
], [@ofile@
])
180 m4_popdef([b4_at_dollar
])dnl
181 m4_popdef([b4_dollar_dollar
])dnl
185 # b4_symbol_case_(SYMBOL-NUM)
186 # ---------------------------
187 # Issue a "case NUM" for SYMBOL-NUM.
188 m4_define([b4_symbol_case_
],
189 [ case b4_symbol([$
1], [number
]): // b4_symbol([$1], [tag])
193 # b4_type_action_(NUMS)
194 # ---------------------
195 # Run actions for the symbol NUMS that all have the same type-name.
196 # Skip NUMS that have no type-name.
197 m4_define([b4_type_action_
],
198 [b4_symbol_if([$
1], [has_type
],
199 [m4_map([b4_symbol_case_
], [$@
])[]dnl
200 b4_dollar_dollar([b4_symbol([$
1], [number
])],
201 [b4_symbol([$
1], [tag
])],
202 [b4_symbol([$
1], [type
])]);
208 # b4_symbol_constructor_declaration_(SYMBOL-NUMBERS)
209 # ----------------------------------------------------
210 # Declare the overloaded version of make_symbol for the (common) type of
211 # these SYMBOL-NUMBERS. Use at class-level.
212 m4_define([b4_symbol_constructor_declaration_
],
213 [ template <token_type
>
214 static inline symbol_type
215 make_symbol (b4_args(b4_symbol_if([$
1], [has_type
],
216 [const b4_symbol([$
1], [type
])& v
]),
217 b4_locations_if([const location_type
& l
])));
221 # b4_symbol_constructor_declarations
222 # ----------------------------------
223 # Declare the overloaded versions of make_symbol for all the value types.
224 # Use at class-level.
225 m4_define([b4_symbol_constructor_declarations
],
227 // Declaration of make_symbol for each value type.
228 m4_map([b4_symbol_constructor_declaration_
], m4_defn([b4_type_names
]))])])
232 # b4_symbol_constructor_specialization_(SYMBOL-NUMBER)
233 # ----------------------------------------------------
234 # Declare the specialization of make_symbol for this each SYMBOL-NUMBER.
235 # Specializations cannot be declared at class-level, this must be done
236 # at namespace-level.
237 m4_define([b4_symbol_constructor_specialization_
],
238 [b4_symbol_if([$
1], [is_token
], [b4_symbol_if([$
1], [has_id
],
241 b4_parser_class_name::symbol_type
242 b4_parser_class_name::make_symbol
<b4_parser_class_name::token::b4_symbol([$
1], [id
])> (dnl
243 b4_args(b4_symbol_if([$
1], [has_type
],
244 [const b4_symbol([$
1], [type
])& v
]),
245 b4_locations_if([const location_type
& l
])));
249 # b4_symbol_constructor_specializations
250 # -------------------------------------
251 # Declare specializations of make_symbol.
252 m4_define([b4_symbol_constructor_specializations
],
254 // Specializations of make_symbol for each symbol type.
255 m4_map([b4_symbol_constructor_specialization_
],
256 m4_defn([b4_symbol_numbers
]))])dnl
261 # b4_symbol_constructor_definition_(SYMBOL-NUMBER)
262 # ------------------------------------------------
263 # Define make_symbol for this SYMBOL-NUMBER.
264 m4_define([b4_symbol_constructor_definition_
],
265 [b4_symbol_if([$
1], [is_token
], [b4_symbol_if([$
1], [has_id
],
267 b4_parser_class_name::symbol_type
268 b4_parser_class_name::make_symbol
<b4_parser_class_name::token::b4_symbol([$
1], [id
])> (dnl
269 b4_args(b4_symbol_if([$
1], [has_type
],
270 [const b4_symbol([$
1], [type
])& v
]),
271 b4_locations_if([const location_type
& l
])))
273 return symbol_type (b4_args([yytranslate_ (token::b4_symbol([$
1], [id
]))],
274 b4_symbol_if([$
1], [has_type
], [v
]),
275 b4_locations_if([l
])));
281 # b4_symbol_constructor_definitions
282 # ----------------------------------
283 # Define the overloaded versions of make_symbol for all the value types.
284 m4_define([b4_symbol_constructor_definitions
],
285 [[ // symbol_base_type.
286 template <typename Exact
>
287 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type ()
288 : value()]b4_locations_if([
293 template <typename Exact
>
294 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type (const location_type
& l
)
300 template <typename Exact
>
301 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type (]b4_args(
302 [const semantic_type
& v
],
303 b4_locations_if([const location_type
& l
]))[)
304 : value(v
)]b4_locations_if([
309 template <typename Exact
>
311 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::self () const
313 return static_cast<const Exact
&>(*this);
316 template <typename Exact
>
318 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::self ()
320 return static_cast<Exact
&>(*this);
323 template <typename Exact
>
325 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::type_get () const
327 return self ().type_get_ ();
331 ]b4_parser_class_name
[::symbol_type::symbol_type ()
337 ]b4_parser_class_name
[::symbol_type::symbol_type (]b4_args(
339 b4_locations_if([const location_type
& l
]))[)
340 : super_type (]b4_locations_if([l
])[)
345 ]b4_parser_class_name
[::symbol_type::symbol_type (]b4_args(
347 [const semantic_type
& v
],
348 b4_locations_if([const location_type
& l
]))[)
349 : super_type (v
]b4_locations_if([, l
])[)
355 ]b4_parser_class_name
[::symbol_type::type_get_ () const
360 ]b4_parser_class_name
[::token_type
361 ]b4_parser_class_name
[::symbol_type::token () const
363 // YYTOKNUM[NUM] -- (External) token number corresponding to the
364 // (internal) symbol number NUM (which must be that of a token). */
366 const ]b4_int_type_for([b4_toknum
])[
371 return static_cast<token_type
> (yytoken_number_
[type
]);
376 [ // Implementation of make_symbol for each symbol type.
377 m4_map([b4_symbol_constructor_definition_
], m4_defn([b4_symbol_numbers
]))])])
380 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
381 # ------------------------------------------------
382 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
384 m4_define([b4_symbol_variant
],
385 [m4_pushdef([b4_dollar_dollar
],
386 [$
2.$
3<$
][3>(m4_shift3($@
))])dnl
389 m4_map([b4_type_action_
], m4_defn([b4_type_names
]))[]dnl
393 m4_popdef([b4_dollar_dollar
])dnl
397 # _b4_char_sizeof_counter
398 # -----------------------
399 # A counter used by _b4_char_sizeof_dummy to create fresh symbols.
400 m4_define([_b4_char_sizeof_counter
],
403 # _b4_char_sizeof_dummy
404 # ---------------------
405 # At each call return a new C++ identifier.
406 m4_define([_b4_char_sizeof_dummy
],
407 [m4_define([_b4_char_sizeof_counter
], m4_incr(_b4_char_sizeof_counter
))dnl
408 dummy
[]_b4_char_sizeof_counter
])
411 # b4_char_sizeof_(SYMBOL-NUM)
412 # ---------------------------
413 # A comment describing this symbol.
414 m4_define([b4_char_sizeof_
],
415 [ // b4_symbol([$1], [tag])
418 # b4_char_sizeof(SYMBOL-NUMS)
419 # --------------------------
420 # To be mapped on the list of type names to produce:
422 # char dummy1[sizeof(type_name_1)];
423 # char dummy2[sizeof(type_name_2)];
425 # for defined type names.
426 m4_define([b4_char_sizeof
],
427 [b4_symbol_if([$
1], [has_type
],
429 m4_map([b4_char_sizeof_
], [$@
])dnl
430 char _b4_char_sizeof_dummy@
{sizeof([b4_symbol([$
1], [type
])])@
};
434 # b4_yytranslate_definition
435 # -------------------------
436 # Define yytranslate_. Sometimes we want it in the header file,
437 # sometimes the cc file suffices.
438 m4_define([b4_yytranslate_definition
],
439 [[ // Symbol number corresponding to token number t.
440 ]b4_parser_class_name
[::token_number_type
441 ]b4_parser_class_name
[::yytranslate_ (]b4_lex_symbol_if([token_type
],
445 const token_number_type
450 const unsigned int user_token_number_max_
= ]b4_user_token_number_max
[;
451 const token_number_type undef_token_
= ]b4_undef_token_number
[;
453 if (static_cast<int>(t
) <= yyeof_
)
455 else if (static_cast<unsigned int> (t
) <= user_token_number_max_
)
456 return translate_table
[t
];
463 m4_pushdef([b4_copyright_years
],
464 [2002, 2003, 2004, 2005, 2006, 2007, 2008])
466 m4_define([b4_parser_class_name
],
467 [b4_percent_define_get([[parser_class_name]])])
469 # The header is mandatory.
471 [b4_fatal([b4_skeleton
[: using %%defines is mandatory
]])])
474 [# Backward compatibility.
475 m4_define([b4_location_constructors
])
476 m4_include(b4_pkgdatadir
/[location
.cc
])])
478 # We do want M4 expansion after # for CPP macros.
481 @
output(b4_spec_defines_file@
)@
482 b4_copyright([Skeleton interface
for Bison
LALR(1) parsers in C
++])
483 dnl FIXME
: This is wrong
, we want computed header guards
.
485 /* C++ LALR(1) parser skeleton written by Akim Demaille. */
487 #ifndef PARSER_HEADER_H
488 # define PARSER_HEADER_H
490 ]b4_percent_code_get([[requires]])[
492 ]b4_assert_if([#include <cassert>])[
498 ]b4_locations_if([ class position
;
502 /// A char[S] buffer to store and retrieve objects.
504 /// Sort of a variant, but does not keep track of the nature
505 /// of the stored data, since that knowledge is available
506 /// via the current state.
510 /// Whether something is contained.
513 /// Empty construction.
515 variant ()]b4_assert_if([
519 /// Instantiate a \a T in here.
520 template <typename T
>
526 return *new (buffer
) T
;
529 /// Instantiate a \a T in here from \a t.
530 template <typename T
>
536 return *new (buffer
) T(t
);
539 /// Construct and fill.
540 template <typename T
>
542 variant (const T
& t
)]b4_assert_if([
548 /// Accessor to a built \a T.
549 template <typename T
>
554 return reinterpret_cast<T
&>(buffer
);
557 /// Const accessor to a built \a T (for %printer).
558 template <typename T
>
563 return reinterpret_cast<const T
&>(buffer
);
566 /// Swap the content with \a other.
567 template <typename T
>
569 swap(variant
<S
>& other
)
571 std::swap(as
<T
>(), other
.as
<T
>());
574 /// Assign the content of \a other to this.
575 /// Destroys \a other.
576 template <typename T
>
578 build(variant
<S
>& other
)
585 /// Destroy the stored \a T.
586 template <typename T
>
590 as
<T
>().~T();]b4_assert_if([
594 /// A buffer large enough to store any of the semantic values.
600 ]b4_locations_if([#include "location.hh"])[
602 /* Enabling traces. */
604 # define YYDEBUG ]b4_debug_flag[
607 /* Enabling verbose error messages. */
608 #ifdef YYERROR_VERBOSE
609 # undef YYERROR_VERBOSE
610 # define YYERROR_VERBOSE 1
612 # define YYERROR_VERBOSE ]b4_error_verbose_flag[
615 /* Enabling the token table. */
616 #ifndef YYTOKEN_TABLE
617 # define YYTOKEN_TABLE ]b4_token_table[
620 ]b4_locations_if([dnl
621 [/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
622 If N is 0, then set CURRENT to the empty location which ends
623 the previous symbol: RHS[0] (always defined). */
625 #ifndef YYLLOC_DEFAULT
626 # define YYLLOC_DEFAULT(Current, Rhs, N) \
630 (Current).begin = (Rhs)[1].location.begin; \
631 (Current).end = (Rhs)[N].location.end; \
635 (Current).begin = (Current).end = (Rhs)[0].location.end; \
643 class ]b4_parser_class_name
[
648 [ /// An auxiliary type to compute the largest semantic type.
650 {]m4_map([b4_char_sizeof
], m4_defn([b4_type_names
]))[};
652 /// Symbol semantic values.
653 typedef variant
<sizeof(union_type
)> semantic_type
;],
654 [ /// Symbol semantic values.
656 [ union semantic_type
659 [m4_if(b4_tag_seen_flag
, 0,
660 [[ typedef int semantic_type
;]],
661 [[ typedef YYSTYPE semantic_type
;]])])])[
663 typedef YYSTYPE semantic_type
;
664 #endif]b4_locations_if([
665 /// Symbol locations.
666 typedef b4_percent_define_get([[location_type]]) location_type
;])[
670 ]b4_token_enums(b4_tokens
)[
673 typedef token::yytokentype token_type
;
675 /// Build a parser object.
676 ]b4_parser_class_name
[ (]b4_parse_param_decl
[);
677 virtual ~]b4_parser_class_name
[ ();
680 /// \returns 0 iff parsing succeeded.
681 virtual int parse ();
684 /// The current debugging stream.
685 std::ostream
& debug_stream () const;
686 /// Set the current debugging stream.
687 void set_debug_stream (std::ostream
&);
689 /// Type for debugging levels.
690 typedef int debug_level_type
;
691 /// The current debugging level.
692 debug_level_type
debug_level () const;
693 /// Set the current debugging level.
694 void set_debug_level (debug_level_type l
);
698 /// Report a syntax error.]b4_locations_if([
699 /// \param loc where the syntax error is found.])[
700 /// \param msg a description of the syntax error.
701 virtual void error (]b4_locations_if([const location_type
& loc
, ])[const std::string
& msg
);
703 /// Generate an error message.
704 /// \param state the state where the error occurred.
705 /// \param tok the lookahead token.
706 virtual std::string
yysyntax_error_ (int yystate
, int tok
);
709 typedef int state_type
;
711 /// Internal symbol numbers.
712 typedef ]b4_int_type_for([b4_translate
])[ token_number_type
;
714 /// For a state, the index in \a yytable_ of its portion.
715 static const ]b4_int_type_for([b4_pact
])[ yypact_
[];
716 static const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
)[ yypact_ninf_
;
718 /// For a state, default rule to reduce.
719 /// Unless\a yytable_ specifies something else to do.
720 /// Zero means the default is an error.
721 static const ]b4_int_type_for([b4_defact
])[ yydefact_
[];
723 static const ]b4_int_type_for([b4_pgoto
])[ yypgoto_
[];
724 static const ]b4_int_type_for([b4_defgoto
])[ yydefgoto_
[];
726 /// What to do in a state.
727 /// \a yytable_[yypact_[s]]: what to do in state \a s.
728 /// - if positive, shift that token.
729 /// - if negative, reduce the rule which number is the opposite.
730 /// - if zero, do what YYDEFACT says.
731 static const ]b4_int_type_for([b4_table
])[ yytable_
[];
732 static const ]b4_int_type(b4_table_ninf
, b4_table_ninf
)[ yytable_ninf_
;
734 static const ]b4_int_type_for([b4_check
])[ yycheck_
[];
736 /// For a state, its accessing symbol.
737 static const ]b4_int_type_for([b4_stos
])[ yystos_
[];
739 /// For a rule, its LHS.
740 static const ]b4_int_type_for([b4_r1
])[ yyr1_
[];
741 /// For a rule, its RHS length.
742 static const ]b4_int_type_for([b4_r2
])[ yyr2_
[];
744 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
745 /// For a symbol, its name in clear.
746 static const char* const yytname_
[];
747 #endif]b4_error_verbose_if([
749 /// Convert the symbol name \a n to a form suitable for a diagnostic.
750 static std::string
yytnamerr_ (const char *n
);])[
753 /// For each rule, its source line number.
754 static const ]b4_int_type_for([b4_rline
])[ yyrline_
[];
755 /// Report on the debug stream that the rule \a r is going to be reduced.
756 virtual void yy_reduce_print_ (int r
);
757 /// Print the state stack on the debug stream.
758 virtual void yystack_print_ ();
762 std::ostream
* yycdebug_
;
765 /// Convert a scanner token number \a t to a symbol number.
766 static inline token_number_type
yytranslate_ (]b4_lex_symbol_if([token_type
], [int])[ t
);
768 /// A complete symbol, with its type.
769 template <typename Exact
>
770 struct symbol_base_type
772 /// Default constructor.
773 inline symbol_base_type ();
775 /// Constructor.]b4_locations_if([
776 inline symbol_base_type (const location_type
& l
)])[;
777 inline symbol_base_type (]b4_args(
778 [const semantic_type
& v
],
779 b4_locations_if([const location_type
& l
]))[);
781 /// Return this with its exact type.
782 const Exact
& self () const;
785 /// Return the type of this symbol.
786 int type_get () const;
788 /// The semantic value.
789 semantic_type value
;]b4_locations_if([
792 location_type location
;])[
796 /// \brief Display a symbol type, value and location.
797 /// \param yyo The output stream.
798 /// \param yysym The symbol.
799 template <typename Exact
>
800 void yy_print_ (std::ostream
& yyo
,
801 const symbol_base_type
<Exact
>& yysym
) const;
804 /// \brief Reclaim the memory associated to a symbol.
805 /// \param yymsg Why this token is reclaimed.
806 /// If null, print nothing.
807 /// \param s The symbol.
808 template <typename Exact
>
809 inline void yy_destroy_ (const char* yymsg
,
810 symbol_base_type
<Exact
>& yysym
) const;
813 /// Element of the stack: a state and its attributes.
814 struct symbol_type
: symbol_base_type
<symbol_type
>
816 /// The parent class.
817 typedef symbol_base_type
<symbol_type
> super_type
;
819 /// Default constructor.
820 inline symbol_type ();
823 inline symbol_type (]b4_args([int t
],
824 [const semantic_type
& v
],
825 b4_locations_if([const location_type
& l
]))[);
827 inline symbol_type (]b4_args([int t
],
828 b4_locations_if([const location_type
& l
]))[);
833 /// Return the type corresponding to this state.
834 inline int type_get_ () const;
837 inline token_type
token () const;
840 ]b4_symbol_constructor_declarations
[
843 /// Element of the stack: a state and its attributes.
844 struct stack_symbol_type
: symbol_base_type
<stack_symbol_type
>
846 /// The parent class.
847 typedef symbol_base_type
<stack_symbol_type
> super_type
;
849 /// Default constructor.
850 inline stack_symbol_type ();
853 inline stack_symbol_type (]b4_args([state_type s
],
854 [const semantic_type
& v
],
855 b4_locations_if([const location_type
& l
]))[);
860 /// Return the type corresponding to this state.
861 inline int type_get_ () const;
865 typedef stack
<stack_symbol_type
> stack_type
;
870 /// Push a new state on the stack.
871 /// \param m a debug message to display
872 /// if null, no trace is output.
873 /// \param s the symbol
874 /// \warning the contents of \a s.value is stolen.
875 inline void yypush_ (const char* m
, stack_symbol_type
& s
);
877 /// Push a new look ahead token on the state on the stack.
878 /// \param m a debug message to display
879 /// if null, no trace is output.
880 /// \param s the state
881 /// \param sym the symbol (for its value and location).
882 /// \warning the contents of \a s.value is stolen.
883 inline void yypush_ (const char* m
, state_type s
, symbol_type
& sym
);
885 /// Pop \a n symbols the three stacks.
886 inline void yypop_ (unsigned int n
= 1);
892 yylast_
= ]b4_last
[, //< Last index in yytable_.
893 yynnts_
= ]b4_nterms_number
[, //< Number of nonterminal symbols.
895 yyfinal_
= ]b4_final_state_number
[, //< Termination state number.
898 yyntokens_
= ]b4_tokens_number
[, //< Number of tokens.
901 ]b4_parse_param_vars
[
904 ]b4_lex_symbol_if([b4_yytranslate_definition
])[
905 ]b4_symbol_constructor_specializations
[
906 ]b4_lex_symbol_if([b4_symbol_constructor_definitions
])[
909 ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
910 [b4_token_defines(b4_tokens
)
913 /* Redirection for backward compatibility. */
914 # define YYSTYPE b4_namespace_ref::b4_parser_class_name::semantic_type
917 b4_percent_code_get([[provides]])[]dnl
919 [#endif /* ! defined PARSER_HEADER_H */]
920 @
output(b4_parser_file_name@
)@
921 b4_copyright([Skeleton implementation
for Bison
LALR(1) parsers in C
++])
922 b4_percent_code_get([[top]])[]dnl
923 m4_if(b4_prefix
, [yy
], [],
925 // Take the name prefix into account.
926 #define yylex b4_prefix[]lex])[
928 /* First part of user declarations. */
929 ]b4_user_pre_prologue
[
931 #include "@basename(]b4_spec_defines_file[@)"
933 /* User implementation prologue. */
934 ]b4_user_post_prologue
935 b4_percent_code_get
[]dnl
940 # include <libintl.h> /* FIXME: INFRINGES ON USER NAME SPACE */
941 # define YY_(msgid) dgettext ("bison-runtime", msgid)
945 # define YY_(msgid) msgid
949 /* Suppress unused-variable warnings by "using" E. */
950 #define YYUSE(e) ((void) (e))
952 /* Enable debugging if requested. */
955 /* A pseudo ostream that takes yydebug_ into account. */
956 # define YYCDEBUG if (yydebug_) (*yycdebug_)
958 # define YY_SYMBOL_PRINT(Title, Symbol) \
962 *yycdebug_ << Title << ' '; \
963 yy_print_ (*yycdebug_, Symbol); \
964 *yycdebug_ << std::endl; \
968 # define YY_REDUCE_PRINT(Rule) \
971 yy_reduce_print_ (Rule); \
974 # define YY_STACK_PRINT() \
982 # define YYCDEBUG if (false) std::cerr
983 # define YY_SYMBOL_PRINT(Title, Symbol) static_cast<void>(0)
984 # define YY_REDUCE_PRINT(Rule) static_cast<void>(0)
985 # define YY_STACK_PRINT() static_cast<void>(0)
987 #endif /* !YYDEBUG */
989 #define yyerrok (yyerrstatus_ = 0)
990 #define yyclearin (yyempty = true)
992 #define YYACCEPT goto yyacceptlab
993 #define YYABORT goto yyabortlab
994 #define YYERROR goto yyerrorlab
995 #define YYRECOVERING() (!!yyerrstatus_)
997 ]b4_namespace_open
[]b4_error_verbose_if([[
999 /* Return YYSTR after stripping away unnecessary quotes and
1000 backslashes, so that it's suitable for yyerror. The heuristic is
1001 that double-quoting is unnecessary unless the string contains an
1002 apostrophe, a comma, or backslash (other than backslash-backslash).
1003 YYSTR is taken from yytname. */
1005 ]b4_parser_class_name
[::yytnamerr_ (const char *yystr
)
1009 std::string yyr
= "";
1010 char const *yyp
= yystr
;
1017 goto do_not_strip_quotes
;
1021 goto do_not_strip_quotes
;
1030 do_not_strip_quotes
: ;
1037 /// Build a parser object.
1038 ]b4_parser_class_name::b4_parser_class_name
[ (]b4_parse_param_decl
[)]m4_ifset([b4_parse_param
], [
1041 ]m4_ifset([b4_parse_param
], [ ], [ :])[yydebug_ (false),
1042 yycdebug_ (&std::cerr
)]m4_ifset([b4_parse_param
], [,])[
1043 #endif]b4_parse_param_cons[
1047 ]b4_parser_class_name::~b4_parser_class_name
[ ()
1056 ]b4_lex_symbol_if([], [b4_symbol_constructor_definitions
])[
1058 // stack_symbol_type.
1059 ]b4_parser_class_name
[::stack_symbol_type::stack_symbol_type ()
1065 ]b4_parser_class_name
[::stack_symbol_type::stack_symbol_type (]b4_args(
1067 [const semantic_type
& v
],
1068 b4_locations_if([const location_type
& l
]))[)
1069 : super_type (v
]b4_locations_if([, l
])[)
1075 ]b4_parser_class_name
[::stack_symbol_type::type_get_ () const
1077 return yystos_
[state
];
1081 template <typename Exact
>
1083 ]b4_parser_class_name
[::yy_destroy_ (const char* yymsg
,
1084 symbol_base_type
<Exact
>& yysym
) const
1086 int yytype
= yysym
.type_get ();
1089 YY_SYMBOL_PRINT (yymsg
, yysym
);
1094 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_destructors
]))[
1100 b4_symbol_variant([[yytype]], [[yysym
.value
]], [[template destroy]])])[
1104 template <typename Exact
>
1106 ]b4_parser_class_name
[::yy_print_ (std::ostream
& yyo
,
1107 const symbol_base_type
<Exact
>& yysym
) const
1109 int yytype
= yysym
.type_get ();
1110 yyo
<< (yytype
< yyntokens_
? "token" : "nterm")
1111 << ' ' << yytname_
[yytype
] << " ("]b4_locations_if([
1112 << yysym
.location
<< ": "])[;
1115 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_printers
]))[
1124 ]b4_parser_class_name
[::yypush_ (const char* m
, state_type s
,
1128 YY_SYMBOL_PRINT (m
, sym
);
1130 [[ yystack_
.push (stack_symbol_type (]b4_args(
1133 b4_locations_if([sym
.location
]))[));
1134 ]b4_symbol_variant([[yystos_
[s
]]], [[yystack_
[0].value
]],
1135 [build
], [sym
.value
])],
1136 [[ yystack_
.push (stack_symbol_type (]b4_args(
1139 b4_locations_if([sym
.location
]))[));]])[
1143 ]b4_parser_class_name
[::yypush_ (const char* m
, stack_symbol_type
& s
)
1146 YY_SYMBOL_PRINT (m
, s
);
1148 [[ yystack_
.push (stack_symbol_type (]b4_args(
1151 b4_locations_if([s
.location
]))[));
1152 ]b4_symbol_variant([[yystos_
[s
.state
]]], [[yystack_
[0].value
]],
1153 [build
], [s
.value
])],
1154 [ yystack_
.push (s
);])[
1158 ]b4_parser_class_name
[::yypop_ (unsigned int n
)
1165 ]b4_parser_class_name
[::debug_stream () const
1171 ]b4_parser_class_name
[::set_debug_stream (std::ostream
& o
)
1177 ]b4_parser_class_name
[::debug_level_type
1178 ]b4_parser_class_name
[::debug_level () const
1184 ]b4_parser_class_name
[::set_debug_level (debug_level_type l
)
1191 ]b4_parser_class_name
[::parse ()
1193 /// Whether yyla contains a lookahead.
1194 bool yyempty
= true;
1201 /* Error handling. */
1203 int yyerrstatus_
= 0;
1205 /// The lookahead symbol.
1206 symbol_type yyla
;]b4_locations_if([[
1208 /// The locations where the error started and ended.
1209 stack_symbol_type yyerror_range
[2];]])[
1212 stack_symbol_type yylhs
;
1214 /// The return value of parse().
1217 YYCDEBUG
<< "Starting parse" << std::endl
;
1219 ]m4_ifdef([b4_initial_action
], [
1220 m4_pushdef([b4_at_dollar
], [yyla
.location
])dnl
1221 m4_pushdef([b4_dollar_dollar
], [yyla
.value
])dnl
1222 /* User initialization code. */
1223 b4_user_initial_action
1224 m4_popdef([b4_dollar_dollar
])dnl
1225 m4_popdef([b4_at_dollar
])])dnl
1227 [ /* Initialize the stack. The initial state will be set in
1228 yynewstate, since the latter expects the semantical and the
1229 location values to have been already stored, initialize these
1230 stacks with a primary value. */
1231 yystack_
= stack_type (0);
1232 yypush_ (0, 0, yyla
);
1234 // A new state was pushed on the stack.
1235 // Invariant: yystate == yystack_[0].state, i.e.,
1236 // yystate was just pushed onto the state stack.
1238 YYCDEBUG
<< "Entering state " << yystate
<< std::endl
;
1241 if (yystate
== yyfinal_
)
1249 /* Try to take a decision without lookahead. */
1250 yyn
= yypact_
[yystate
];
1251 if (yyn
== yypact_ninf_
)
1254 /* Read a lookahead token. */
1257 YYCDEBUG
<< "Reading a token: ";
1259 [ yyla
= b4_c_function_call([yylex
], [symbol_type
],
1260 m4_ifdef([b4_lex_param
], b4_lex_param
));],
1261 [ yyla
.type
= yytranslate_ (b4_c_function_call([yylex
], [int],
1262 [[YYSTYPE
*], [&yyla
.value
]][]dnl
1263 b4_locations_if([, [[location
*], [&yyla
.location
]]])dnl
1264 m4_ifdef([b4_lex_param
], [, ]b4_lex_param
)));])[
1267 YY_SYMBOL_PRINT ("Next token is", yyla
);
1269 /* If the proper action on seeing token YYLA.TYPE is to reduce or
1270 to detect an error, take that action. */
1272 if (yyn
< 0 || yylast_
< yyn
|| yycheck_
[yyn
] != yyla
.type
)
1275 /* Reduce or error. */
1276 yyn
= yytable_
[yyn
];
1279 if (yyn
== 0 || yyn
== yytable_ninf_
)
1285 /* Discard the token being shifted. */
1288 /* Count tokens shifted since error; after three, turn off error
1293 /* Shift the lookahead token. */
1295 yypush_ ("Shifting", yystate
, yyla
);
1298 /*-----------------------------------------------------------.
1299 | yydefault -- do the default action for the current state. |
1300 `-----------------------------------------------------------*/
1302 yyn
= yydefact_
[yystate
];
1307 /*-----------------------------.
1308 | yyreduce -- Do a reduction. |
1309 `-----------------------------*/
1311 yylen
= yyr2_
[yyn
];]b4_variant_if([
1312 /* Variants are always initialized to an empty instance of the
1313 correct type. The default $$=$1 rule is NOT applied when using
1315 ]b4_symbol_variant([[yyr1_@
{yyn@
}]], [yylhs
.value
], [build
])[],[
1316 /* If YYLEN is nonzero, implement the default value of the action:
1317 `$$ = $1'. Otherwise, use the top of the stack.
1319 Otherwise, the following line sets YYLHS.VALUE to garbage.
1320 This behavior is undocumented and Bison
1321 users should not rely upon it. */
1323 yylhs
.value
= yystack_@
{yylen
- 1@
}.value
;
1325 yylhs
.value
= yystack_@
{0@
}.value
;])[
1326 ]b4_locations_if([dnl
1328 // Compute the default @@$.
1330 slice
<stack_symbol_type
, stack_type
> slice (yystack_
, yylen
);
1331 YYLLOC_DEFAULT (yylhs
.location
, slice
, yylen
);
1334 // Perform the reduction.
1335 YY_REDUCE_PRINT (yyn
);
1342 // Compute post-reduction state.
1344 yystate
= yypgoto_
[yyn
- yyntokens_
] + yystack_
[yylen
].state
;
1345 if (0 <= yystate
&& yystate
<= yylast_
1346 && yycheck_
[yystate
] == yystack_
[yylen
].state
)
1347 yystate
= yytable_
[yystate
];
1349 yystate
= yydefgoto_
[yyn
- yyntokens_
];
1350 yylhs
.state
= yystate
;
1351 YY_SYMBOL_PRINT ("-> $$ =", yylhs
);
1353 // Destroy the lhs symbols.
1354 for (int i
= 0; i
< yylen
; ++i
)
1355 // Destroy a variant which value may have be swapped with
1356 // yylhs.value. The value of yylhs.value (hence maybe one of
1357 // these lhs symbols) depends on what does the default
1358 // contruction for this type. In the case of pointers for
1359 // instance, nothing is done, so the value is junk. Therefore
1360 // do not try to report the content in the debug trace, it's
1361 // junk. Hence yymsg = 0. Besides, that keeps exactly the same
1362 // traces as with the other Bison skeletons.
1363 yy_destroy_ (0, yystack_
[i
]);]])[
1369 /* Shift the result of the reduction. */
1373 /*------------------------------------.
1374 | yyerrlab -- here on detecting error |
1375 `------------------------------------*/
1377 /* If not already recovering from an error, report this error. */
1381 error (]b4_args(b4_locations_if([yyla
.location
]),
1382 [yysyntax_error_ (yystate
, yyla
.type
)])[);
1386 yyerror_range
[0].location
= yyla
.location
;]])[
1387 if (yyerrstatus_
== 3)
1389 /* If just tried and failed to reuse lookahead token after an
1390 error, discard it. */
1392 /* Return failure if at end of input. */
1393 if (yyla
.type
== yyeof_
)
1397 yy_destroy_ ("Error: discarding", yyla
);
1402 /* Else will try to reuse lookahead token after shifting the error
1407 /*---------------------------------------------------.
1408 | yyerrorlab -- error raised explicitly by YYERROR. |
1409 `---------------------------------------------------*/
1412 /* Pacify compilers like GCC when the user code never invokes
1413 YYERROR and the label yyerrorlab therefore never appears in user
1419 yyerror_range
[0].location
= yystack_
[yylen
- 1].location
;]])[
1420 /* Do not reclaim the symbols of the rule which action triggered
1424 yystate
= yystack_
[0].state
;
1427 /*-------------------------------------------------------------.
1428 | yyerrlab1 -- common code for both syntax error and YYERROR. |
1429 `-------------------------------------------------------------*/
1431 yyerrstatus_
= 3; /* Each real token shifted decrements this. */
1433 stack_symbol_type error_token
;
1436 yyn
= yypact_
[yystate
];
1437 if (yyn
!= yypact_ninf_
)
1440 if (0 <= yyn
&& yyn
<= yylast_
&& yycheck_
[yyn
] == yyterror_
)
1442 yyn
= yytable_
[yyn
];
1448 // Pop the current state because it cannot handle the error token.
1449 if (yystack_
.size () == 1)
1452 yyerror_range
[0].location
= yystack_
[0].location
;]])[
1453 yy_destroy_ ("Error: popping", yystack_
[0]);
1455 yystate
= yystack_
[0].state
;
1459 yyerror_range
[1].location
= yyla
.location
;
1460 YYLLOC_DEFAULT (error_token
.location
, (yyerror_range
- 1), 2);]])[
1462 /* Shift the error token. */
1463 error_token
.state
= yystate
= yyn
;
1464 yypush_ ("Shifting", error_token
);
1480 yy_destroy_ ("Cleanup: discarding lookahead", yyla
);
1482 /* Do not reclaim the symbols of the rule which action triggered
1483 this YYABORT or YYACCEPT. */
1485 while (yystack_
.size () != 1)
1487 yy_destroy_ ("Cleanup: popping", yystack_
[0]);
1494 // Generate an error message.
1496 ]b4_parser_class_name
[::yysyntax_error_ (]dnl
1497 b4_error_verbose_if([int yystate
, int yytoken
],
1500 std::string yyres
;]b4_error_verbose_if([[
1501 int yyn
= yypact_
[yystate
];
1502 if (yypact_ninf_
< yyn
&& yyn
<= yylast_
)
1504 /* Start YYX at -YYN if negative to avoid negative indexes in
1506 int yyxbegin
= yyn
< 0 ? -yyn
: 0;
1508 /* Stay within bounds of both yycheck and yytname. */
1509 int yychecklim
= yylast_
- yyn
+ 1;
1510 int yyxend
= yychecklim
< yyntokens_
? yychecklim
: yyntokens_
;
1512 // Number of "expected" tokens.
1515 enum { YYERROR_VERBOSE_ARGS_MAXIMUM
= 5 };
1516 // Arguments of yyformat.
1517 char const *yyarg
[YYERROR_VERBOSE_ARGS_MAXIMUM
];
1518 yyarg
[yycount
++] = yytname_
[yytoken
];
1519 for (int yyx
= yyxbegin
; yyx
< yyxend
; ++yyx
)
1520 if (yycheck_
[yyx
+ yyn
] == yyx
&& yyx
!= yyterror_
)
1522 if (yycount
== YYERROR_VERBOSE_ARGS_MAXIMUM
)
1528 yyarg
[yycount
++] = yytname_
[yyx
];
1531 char const* yyformat
= 0;
1534 #define YYCASE_(N, S) \
1538 YYCASE_(1, YY_("syntax error, unexpected %s"));
1539 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1540 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1541 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1542 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1547 for (char const* yyp
= yyformat
; *yyp
; ++yyp
)
1548 if (yyp
[0] == '%' && yyp
[1] == 's' && yyi
< yycount
)
1550 yyres
+= yytnamerr_ (yyarg
[yyi
++]);
1558 [ yyres
= YY_("syntax error");
1563 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
1565 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) b4_parser_class_name::yypact_ninf_
= b4_pact_ninf
[;
1566 ]b4_table_define([pact
], [b4_pact
])[;
1568 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
1569 doesn't specify something else to do. Zero means the default is an
1571 ]b4_table_define([defact
], [b4_defact
])[;
1573 /* YYPGOTO[NTERM-NUM]. */
1574 ]b4_table_define([pgoto
], [b4_pgoto
])[;
1576 /* YYDEFGOTO[NTERM-NUM]. */
1577 ]b4_table_define([defgoto
], [b4_defgoto
])[;
1579 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
1580 positive, shift that token. If negative, reduce the rule which
1581 number is the opposite. If zero, do what YYDEFACT says. */
1582 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) b4_parser_class_name::yytable_ninf_
= b4_table_ninf
[;
1583 ]b4_table_define([table
], [b4_table
])[;
1586 ]b4_table_define([check
], [b4_check
])[;
1588 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
1589 symbol of state STATE-NUM. */
1590 ]b4_table_define([stos
], [b4_stos
])[;
1592 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
1593 ]b4_table_define([r1
], [b4_r1
])[;
1595 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
1596 ]b4_table_define([r2
], [b4_r2
])[;
1598 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
1599 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
1600 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
1602 const ]b4_parser_class_name
[::yytname_
[] =
1609 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
1610 ]b4_table_define([rline
], [b4_rline
])[;
1612 // Print the state stack on the debug stream.
1614 ]b4_parser_class_name
[::yystack_print_ ()
1616 *yycdebug_
<< "Stack now";
1617 for (stack_type::const_iterator
1618 i
= yystack_
.begin (),
1619 i_end
= yystack_
.end ();
1621 *yycdebug_
<< ' ' << i
->state
;
1622 *yycdebug_
<< std::endl
;
1625 // Report on the debug stream that the rule \a yyrule is going to be reduced.
1627 ]b4_parser_class_name
[::yy_reduce_print_ (int yyrule
)
1629 unsigned int yylno
= yyrline_
[yyrule
];
1630 int yynrhs
= yyr2_
[yyrule
];
1631 /* Print the symbols being reduced, and their result. */
1632 *yycdebug_
<< "Reducing stack by rule " << yyrule
- 1
1633 << " (line " << yylno
<< "):" << std::endl
;
1634 /* The symbols being reduced. */
1635 for (int yyi
= 0; yyi
< yynrhs
; yyi
++)
1636 YY_SYMBOL_PRINT (" $" << yyi
+ 1 << " =",
1637 ]b4_rhs_data(yynrhs
, yyi
+ 1)[);
1641 ]b4_lex_symbol_if([], [b4_yytranslate_definition
])[
1642 ]b4_namespace_close
[
1645 @
output(b4_dir_prefix
[]stack
.hh@
)@
1646 b4_copyright([Stack handling
for Bison parsers in C
++])[
1648 #ifndef BISON_STACK_HH
1649 # define BISON_STACK_HH
1654 template <class T
, class S
= std::deque
<T
> >
1659 // Hide our reversed order.
1660 typedef typename
S::reverse_iterator iterator
;
1661 typedef typename
S::const_reverse_iterator const_iterator
;
1667 stack (unsigned int n
) : seq_ (n
)
1673 operator [] (unsigned int i
)
1680 operator [] (unsigned int i
) const
1689 seq_
.push_front (t
);
1694 pop (unsigned int n
= 1)
1701 typename
S::size_type
1704 return seq_
.size ();
1707 inline const_iterator
begin () const { return seq_
.rbegin (); }
1708 inline const_iterator
end () const { return seq_
.rend (); }
1711 /// The wrapped container.
1715 /// Present a slice of the top of a stack.
1716 template <class T
, class S
= stack
<T
> >
1721 slice (const S
& stack
,
1722 unsigned int range
) : stack_ (stack
),
1729 operator [] (unsigned int i
) const
1731 return stack_
[range_
- i
];
1737 unsigned int range_
;
1739 ]b4_namespace_close
[
1741 #endif // not BISON_STACK_HH[]dnl
1744 m4_popdef([b4_copyright_years
])dnl