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
])
22 # b4_table_define(TABLE-NAME, CONTENT)
23 # ------------------------------------
24 # Define "parser::yy<TABLE-NAME>_" which contents is CONTENT.
25 m4_define([b4_table_define
],
26 [const b4_int_type_for([$
2])
27 b4_parser_class_name::yy$
1_
[[]] =
33 # b4_symbol_value_template(VAL, [TYPE])
34 # -------------------------------------
35 # Same as b4_symbol_value, but used in a template method.
36 m4_copy([b4_symbol_value
], [b4_symbol_value_template
])
38 # How the semantic value is extracted when using variants.
40 # b4_symbol_value(VAL, [TYPE])
41 # ----------------------------
42 m4_define([b4_symbol_value
],
47 # b4_symbol_value_template(VAL, [TYPE])
48 # -------------------------------------
49 # Same as b4_symbol_value, but used in a template method.
50 m4_define([b4_symbol_value_template
],
52 [$
1.template as
< $
2 >()],
57 # b4_lex_symbol_if([IF-YYLEX-RETURNS-A-COMPLETE-SYMBOL], [IF-NOT])
58 # ----------------------------------------------------------------
59 m4_define([b4_lex_symbol_if
],
60 [b4_percent_define_ifdef([[lex_symbol]], [$
1], [$
2])])
63 # b4_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
64 # ------------------------------------------------
65 m4_define([b4_assert_if
],
66 [b4_percent_define_ifdef([[assert]], [$
1], [$
2])])
69 # b4_lhs_value([TYPE])
70 # --------------------
71 # Expansion of $<TYPE>$.
72 m4_define([b4_lhs_value
],
73 [b4_symbol_value([yylhs
.value
], [$
1])])
79 m4_define([b4_lhs_location
],
83 # b4_rhs_data(RULE-LENGTH, NUM)
84 # -----------------------------
85 # Return the data corresponding to the symbol #NUM, where the current
86 # rule has RULE-LENGTH symbols on RHS.
87 m4_define([b4_rhs_data
],
88 [yystack_@
{b4_subtract($@
)@
}])
91 # b4_rhs_state(RULE-LENGTH, NUM)
92 # -----------------------------
93 # The state corresponding to the symbol #NUM, where the current
94 # rule has RULE-LENGTH symbols on RHS.
95 m4_define([b4_rhs_state
],
96 [b4_rhs_data([$
1], [$
2]).state
])
99 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
100 # --------------------------------------
101 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
103 m4_define([b4_rhs_value
],
104 [b4_symbol_value([b4_rhs_data([$
1], [$
2]).value
], [$
3])])
107 # b4_rhs_location(RULE-LENGTH, NUM)
108 # ---------------------------------
109 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
111 m4_define([b4_rhs_location
],
112 [b4_rhs_data([$
1], [$
2]).location
])
115 # b4_symbol_(NUM, FIELD)
116 # ----------------------
117 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
119 m4_define([b4_symbol_
],
120 [m4_indir([b4_symbol($
1, $
2)])])
123 # b4_symbol(NUM, FIELD)
124 # ---------------------
125 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
126 # undefined. If FIELD = id, prepend the prefix.
127 m4_define([b4_symbol
],
129 [id
], [m4_do([b4_percent_define_get([token
.prefix
])],
130 [b4_symbol_([$
1], [id
])])],
134 # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
135 # -------------------------------------------
136 # If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
137 # Otherwise an error.
138 m4_define([b4_symbol_if
],
139 [m4_case(b4_symbol([$
1], [$
2]),
142 [m4_fatal([$
0: field $
2 of $
1 is
not a Boolean
:] b4_symbol([$
1], [$
2]))])])
145 # b4_symbol_actions(FILENAME, LINENO,
146 # SYMBOL-TAG, SYMBOL-NUM,
147 # SYMBOL-ACTION, SYMBOL-TYPENAME)
148 # -------------------------------------------------
149 # Same as in C, but using references instead of pointers.
150 m4_define([b4_symbol_actions
],
151 [m4_pushdef([b4_dollar_dollar
],
152 [b4_symbol_value_template([yysym
.value
], [$
6])])dnl
153 m4_pushdef([b4_at_dollar
], [yysym
.location
])dnl
155 b4_syncline([$
2], [$
1])
157 b4_syncline([@oline@
], [@ofile@
])
159 m4_popdef([b4_at_dollar
])dnl
160 m4_popdef([b4_dollar_dollar
])dnl
164 # b4_symbol_case_(SYMBOL-NUM)
165 # ---------------------------
166 # Issue a "case NUM" for SYMBOL-NUM.
167 m4_define([b4_symbol_case_
],
168 [ case b4_symbol([$
1], [number
]): // b4_symbol([$1], [tag])
172 # b4_type_action_(NUMS)
173 # ---------------------
174 # Run actions for the symbol NUMS that all have the same type-name.
175 # Skip NUMS that have no type-name.
176 m4_define([b4_type_action_
],
177 [b4_symbol_if([$
1], [has_type
],
178 [m4_map([b4_symbol_case_
], [$@
])[]dnl
179 b4_dollar_dollar([b4_symbol([$
1], [number
])],
180 [b4_symbol([$
1], [tag
])],
181 [b4_symbol([$
1], [type
])]);
187 # b4_symbol_constructor_declaration_(SYMBOL-NUMBER)
188 # -------------------------------------------------
189 # Declare the overloaded version of make_symbol for the (common) type of
190 # these SYMBOL-NUMBERS. Use at class-level.
191 m4_define([b4_symbol_constructor_declaration_
],
192 [b4_symbol_if([$
1], [is_token
], [b4_symbol_if([$
1], [has_id
],
195 make_
[]b4_symbol_([$
1], [id
]) (dnl
196 b4_args(b4_symbol_if([$
1], [has_type
],
197 [const b4_symbol([$
1], [type
])& v
]),
198 b4_locations_if([const location_type
& l
])));
203 # b4_symbol_constructor_declarations
204 # ----------------------------------
205 # Declare symbol constructors for all the value types.
206 # Use at class-level.
207 m4_define([b4_symbol_constructor_declarations
],
209 // Symbol constructors declarations.
210 m4_map([b4_symbol_constructor_declaration_
], m4_defn([b4_symbol_numbers
]))])])
214 # b4_symbol_constructor_definition_(SYMBOL-NUMBER)
215 # ------------------------------------------------
216 # Define symbol constructor for this SYMBOL-NUMBER.
217 m4_define([b4_symbol_constructor_definition_
],
218 [b4_symbol_if([$
1], [is_token
], [b4_symbol_if([$
1], [has_id
],
219 [ b4_parser_class_name::symbol_type
220 b4_parser_class_name::make_
[]b4_symbol_([$
1], [id
]) (dnl
221 b4_args(b4_symbol_if([$
1], [has_type
],
222 [const b4_symbol([$
1], [type
])& v
]),
223 b4_locations_if([const location_type
& l
])))
225 return symbol_type (b4_args([yytranslate_ (token::b4_symbol([$
1], [id
]))],
226 b4_symbol_if([$
1], [has_type
], [v
]),
227 b4_locations_if([l
])));
233 # b4_symbol_constructor_definitions
234 # ----------------------------------
235 # Define the overloaded versions of make_symbol for all the value types.
236 m4_define([b4_symbol_constructor_definitions
],
237 [[ // symbol_base_type.
238 template <typename Exact
>
239 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type ()
240 : value()]b4_locations_if([
245 template <typename Exact
>
246 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type (const location_type
& l
)
252 template <typename Exact
>
253 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type (]b4_args(
254 [const semantic_type
& v
],
255 b4_locations_if([const location_type
& l
]))[)
256 : value(v
)]b4_locations_if([
261 template <typename Exact
>
263 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::self () const
265 return static_cast<const Exact
&>(*this);
268 template <typename Exact
>
270 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::self ()
272 return static_cast<Exact
&>(*this);
275 template <typename Exact
>
277 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::type_get () const
279 return self ().type_get_ ();
283 ]b4_parser_class_name
[::symbol_type::symbol_type ()
289 ]b4_parser_class_name
[::symbol_type::symbol_type (]b4_args(
291 b4_locations_if([const location_type
& l
]))[)
292 : super_type (]b4_locations_if([l
])[)
297 ]b4_parser_class_name
[::symbol_type::symbol_type (]b4_args(
299 [const semantic_type
& v
],
300 b4_locations_if([const location_type
& l
]))[)
301 : super_type (v
]b4_locations_if([, l
])[)
307 ]b4_parser_class_name
[::symbol_type::type_get_ () const
312 ]b4_parser_class_name
[::token_type
313 ]b4_parser_class_name
[::symbol_type::token () const
315 // YYTOKNUM[NUM] -- (External) token number corresponding to the
316 // (internal) symbol number NUM (which must be that of a token). */
318 const ]b4_int_type_for([b4_toknum
])[
323 return static_cast<token_type
> (yytoken_number_
[type
]);
328 [ // Implementation of make_symbol for each symbol type.
329 m4_map([b4_symbol_constructor_definition_
], m4_defn([b4_symbol_numbers
]))])])
332 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
333 # ------------------------------------------------
334 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
336 m4_define([b4_symbol_variant
],
337 [m4_pushdef([b4_dollar_dollar
],
338 [$
2.$
3< $
][3 >(m4_shift3($@
))])dnl
341 m4_map([b4_type_action_
], m4_defn([b4_type_names
]))[]dnl
345 m4_popdef([b4_dollar_dollar
])dnl
349 # _b4_char_sizeof_counter
350 # -----------------------
351 # A counter used by _b4_char_sizeof_dummy to create fresh symbols.
352 m4_define([_b4_char_sizeof_counter
],
355 # _b4_char_sizeof_dummy
356 # ---------------------
357 # At each call return a new C++ identifier.
358 m4_define([_b4_char_sizeof_dummy
],
359 [m4_define([_b4_char_sizeof_counter
], m4_incr(_b4_char_sizeof_counter
))dnl
360 dummy
[]_b4_char_sizeof_counter
])
363 # b4_char_sizeof_(SYMBOL-NUM)
364 # ---------------------------
365 # A comment describing this symbol.
366 m4_define([b4_char_sizeof_
],
367 [ // b4_symbol([$1], [tag])
370 # b4_char_sizeof(SYMBOL-NUMS)
371 # --------------------------
372 # To be mapped on the list of type names to produce:
374 # char dummy1[sizeof(type_name_1)];
375 # char dummy2[sizeof(type_name_2)];
377 # for defined type names.
378 m4_define([b4_char_sizeof
],
379 [b4_symbol_if([$
1], [has_type
],
381 m4_map([b4_char_sizeof_
], [$@
])dnl
382 char _b4_char_sizeof_dummy@
{sizeof([b4_symbol([$
1], [type
])])@
};
386 # b4_yytranslate_definition
387 # -------------------------
388 # Define yytranslate_. Sometimes we want it in the header file,
389 # sometimes the cc file suffices.
390 m4_define([b4_yytranslate_definition
],
391 [[ // Symbol number corresponding to token number t.
392 ]b4_parser_class_name
[::token_number_type
393 ]b4_parser_class_name
[::yytranslate_ (]b4_lex_symbol_if([token_type
],
397 const token_number_type
402 const unsigned int user_token_number_max_
= ]b4_user_token_number_max
[;
403 const token_number_type undef_token_
= ]b4_undef_token_number
[;
405 if (static_cast<int>(t
) <= yyeof_
)
407 else if (static_cast<unsigned int> (t
) <= user_token_number_max_
)
408 return translate_table
[t
];
415 m4_pushdef([b4_copyright_years
],
416 [2002, 2003, 2004, 2005, 2006, 2007, 2008])
418 m4_define([b4_parser_class_name
],
419 [b4_percent_define_get([[parser_class_name]])])
421 # The header is mandatory.
423 [b4_fatal([b4_skeleton
[: using %%defines is mandatory
]])])
426 [# Backward compatibility.
427 m4_define([b4_location_constructors
])
428 m4_include(b4_pkgdatadir
/[location
.cc
])])
430 # We do want M4 expansion after # for CPP macros.
433 @
output(b4_spec_defines_file@
)@
434 b4_copyright([Skeleton interface
for Bison
LALR(1) parsers in C
++])
435 dnl FIXME
: This is wrong
, we want computed header guards
.
437 /* C++ LALR(1) parser skeleton written by Akim Demaille. */
439 #ifndef PARSER_HEADER_H
440 # define PARSER_HEADER_H
442 ]b4_percent_code_get([[requires]])[
444 ]b4_assert_if([#include <cassert>])[
450 ]b4_locations_if([ class position
;
454 /// A char[S] buffer to store and retrieve objects.
456 /// Sort of a variant, but does not keep track of the nature
457 /// of the stored data, since that knowledge is available
458 /// via the current state.
462 /// Whether something is contained.
465 /// Empty construction.
467 variant ()]b4_assert_if([
471 /// Instantiate a \a T in here.
472 template <typename T
>
478 return *new (buffer
) T
;
481 /// Instantiate a \a T in here from \a t.
482 template <typename T
>
488 return *new (buffer
) T(t
);
491 /// Construct and fill.
492 template <typename T
>
494 variant (const T
& t
)]b4_assert_if([
500 /// Accessor to a built \a T.
501 template <typename T
>
506 return reinterpret_cast<T
&>(buffer
);
509 /// Const accessor to a built \a T (for %printer).
510 template <typename T
>
515 return reinterpret_cast<const T
&>(buffer
);
518 /// Swap the content with \a other.
519 template <typename T
>
521 swap(variant
<S
>& other
)
523 std::swap(as
<T
>(), other
.as
<T
>());
526 /// Assign the content of \a other to this.
527 /// Destroys \a other.
528 template <typename T
>
530 build(variant
<S
>& other
)
537 /// Destroy the stored \a T.
538 template <typename T
>
542 as
<T
>().~T();]b4_assert_if([
546 /// A buffer large enough to store any of the semantic values.
552 ]b4_locations_if([#include "location.hh"])[
554 /* Enabling traces. */
556 # define YYDEBUG ]b4_debug_flag[
559 /* Enabling verbose error messages. */
560 #ifdef YYERROR_VERBOSE
561 # undef YYERROR_VERBOSE
562 # define YYERROR_VERBOSE 1
564 # define YYERROR_VERBOSE ]b4_error_verbose_flag[
567 /* Enabling the token table. */
568 #ifndef YYTOKEN_TABLE
569 # define YYTOKEN_TABLE ]b4_token_table[
572 ]b4_locations_if([dnl
573 [/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
574 If N is 0, then set CURRENT to the empty location which ends
575 the previous symbol: RHS[0] (always defined). */
577 #ifndef YYLLOC_DEFAULT
578 # define YYLLOC_DEFAULT(Current, Rhs, N) \
582 (Current).begin = (Rhs)[1].location.begin; \
583 (Current).end = (Rhs)[N].location.end; \
587 (Current).begin = (Current).end = (Rhs)[0].location.end; \
595 class ]b4_parser_class_name
[
600 [ /// An auxiliary type to compute the largest semantic type.
602 {]m4_map([b4_char_sizeof
], m4_defn([b4_type_names
]))[};
604 /// Symbol semantic values.
605 typedef variant
<sizeof(union_type
)> semantic_type
;],
606 [ /// Symbol semantic values.
608 [ union semantic_type
611 [m4_if(b4_tag_seen_flag
, 0,
612 [[ typedef int semantic_type
;]],
613 [[ typedef YYSTYPE semantic_type
;]])])])[
615 typedef YYSTYPE semantic_type
;
616 #endif]b4_locations_if([
617 /// Symbol locations.
618 typedef b4_percent_define_get([[location_type]]) location_type
;])[
622 ]b4_token_enums(b4_tokens
)[
625 typedef token::yytokentype token_type
;
627 /// Build a parser object.
628 ]b4_parser_class_name
[ (]b4_parse_param_decl
[);
629 virtual ~]b4_parser_class_name
[ ();
632 /// \returns 0 iff parsing succeeded.
633 virtual int parse ();
636 /// The current debugging stream.
637 std::ostream
& debug_stream () const;
638 /// Set the current debugging stream.
639 void set_debug_stream (std::ostream
&);
641 /// Type for debugging levels.
642 typedef int debug_level_type
;
643 /// The current debugging level.
644 debug_level_type
debug_level () const;
645 /// Set the current debugging level.
646 void set_debug_level (debug_level_type l
);
650 /// Report a syntax error.]b4_locations_if([
651 /// \param loc where the syntax error is found.])[
652 /// \param msg a description of the syntax error.
653 virtual void error (]b4_locations_if([const location_type
& loc
, ])[const std::string
& msg
);
655 /// Generate an error message.
656 /// \param state the state where the error occurred.
657 /// \param tok the lookahead token.
658 virtual std::string
yysyntax_error_ (int yystate
, int tok
);
661 typedef int state_type
;
663 /// Internal symbol numbers.
664 typedef ]b4_int_type_for([b4_translate
])[ token_number_type
;
666 /// For a state, the index in \a yytable_ of its portion.
667 static const ]b4_int_type_for([b4_pact
])[ yypact_
[];
668 static const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
)[ yypact_ninf_
;
670 /// For a state, default rule to reduce.
671 /// Unless\a yytable_ specifies something else to do.
672 /// Zero means the default is an error.
673 static const ]b4_int_type_for([b4_defact
])[ yydefact_
[];
675 static const ]b4_int_type_for([b4_pgoto
])[ yypgoto_
[];
676 static const ]b4_int_type_for([b4_defgoto
])[ yydefgoto_
[];
678 /// What to do in a state.
679 /// \a yytable_[yypact_[s]]: what to do in state \a s.
680 /// - if positive, shift that token.
681 /// - if negative, reduce the rule which number is the opposite.
682 /// - if zero, do what YYDEFACT says.
683 static const ]b4_int_type_for([b4_table
])[ yytable_
[];
684 static const ]b4_int_type(b4_table_ninf
, b4_table_ninf
)[ yytable_ninf_
;
686 static const ]b4_int_type_for([b4_check
])[ yycheck_
[];
688 /// For a state, its accessing symbol.
689 static const ]b4_int_type_for([b4_stos
])[ yystos_
[];
691 /// For a rule, its LHS.
692 static const ]b4_int_type_for([b4_r1
])[ yyr1_
[];
693 /// For a rule, its RHS length.
694 static const ]b4_int_type_for([b4_r2
])[ yyr2_
[];
696 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
697 /// For a symbol, its name in clear.
698 static const char* const yytname_
[];
699 #endif]b4_error_verbose_if([
701 /// Convert the symbol name \a n to a form suitable for a diagnostic.
702 static std::string
yytnamerr_ (const char *n
);])[
705 /// For each rule, its source line number.
706 static const ]b4_int_type_for([b4_rline
])[ yyrline_
[];
707 /// Report on the debug stream that the rule \a r is going to be reduced.
708 virtual void yy_reduce_print_ (int r
);
709 /// Print the state stack on the debug stream.
710 virtual void yystack_print_ ();
714 std::ostream
* yycdebug_
;
717 /// Convert a scanner token number \a t to a symbol number.
718 static inline token_number_type
yytranslate_ (]b4_lex_symbol_if([token_type
], [int])[ t
);
720 /// A complete symbol, with its type.
721 template <typename Exact
>
722 struct symbol_base_type
724 /// Default constructor.
725 inline symbol_base_type ();
727 /// Constructor.]b4_locations_if([
728 inline symbol_base_type (const location_type
& l
)])[;
729 inline symbol_base_type (]b4_args(
730 [const semantic_type
& v
],
731 b4_locations_if([const location_type
& l
]))[);
733 /// Return this with its exact type.
734 const Exact
& self () const;
737 /// Return the type of this symbol.
738 int type_get () const;
740 /// The semantic value.
741 semantic_type value
;]b4_locations_if([
744 location_type location
;])[
748 /// \brief Display a symbol type, value and location.
749 /// \param yyo The output stream.
750 /// \param yysym The symbol.
751 template <typename Exact
>
752 void yy_print_ (std::ostream
& yyo
,
753 const symbol_base_type
<Exact
>& yysym
) const;
756 /// \brief Reclaim the memory associated to a symbol.
757 /// \param yymsg Why this token is reclaimed.
758 /// If null, print nothing.
759 /// \param s The symbol.
760 template <typename Exact
>
761 inline void yy_destroy_ (const char* yymsg
,
762 symbol_base_type
<Exact
>& yysym
) const;
765 /// Element of the stack: a state and its attributes.
766 struct symbol_type
: symbol_base_type
<symbol_type
>
768 /// The parent class.
769 typedef symbol_base_type
<symbol_type
> super_type
;
771 /// Default constructor.
772 inline symbol_type ();
775 inline symbol_type (]b4_args([int t
],
776 [const semantic_type
& v
],
777 b4_locations_if([const location_type
& l
]))[);
779 inline symbol_type (]b4_args([int t
],
780 b4_locations_if([const location_type
& l
]))[);
785 /// Return the type corresponding to this state.
786 inline int type_get_ () const;
789 inline token_type
token () const;
792 ]b4_symbol_constructor_declarations
[
795 /// Element of the stack: a state and its attributes.
796 struct stack_symbol_type
: symbol_base_type
<stack_symbol_type
>
798 /// The parent class.
799 typedef symbol_base_type
<stack_symbol_type
> super_type
;
801 /// Default constructor.
802 inline stack_symbol_type ();
805 inline stack_symbol_type (]b4_args([state_type s
],
806 [const semantic_type
& v
],
807 b4_locations_if([const location_type
& l
]))[);
812 /// Return the type corresponding to this state.
813 inline int type_get_ () const;
817 typedef stack
<stack_symbol_type
> stack_type
;
822 /// Push a new state on the stack.
823 /// \param m a debug message to display
824 /// if null, no trace is output.
825 /// \param s the symbol
826 /// \warning the contents of \a s.value is stolen.
827 inline void yypush_ (const char* m
, stack_symbol_type
& s
);
829 /// Push a new look ahead token on the state on the stack.
830 /// \param m a debug message to display
831 /// if null, no trace is output.
832 /// \param s the state
833 /// \param sym the symbol (for its value and location).
834 /// \warning the contents of \a s.value is stolen.
835 inline void yypush_ (const char* m
, state_type s
, symbol_type
& sym
);
837 /// Pop \a n symbols the three stacks.
838 inline void yypop_ (unsigned int n
= 1);
844 yylast_
= ]b4_last
[, //< Last index in yytable_.
845 yynnts_
= ]b4_nterms_number
[, //< Number of nonterminal symbols.
847 yyfinal_
= ]b4_final_state_number
[, //< Termination state number.
850 yyntokens_
= ]b4_tokens_number
[, //< Number of tokens.
853 ]b4_parse_param_vars
[
856 ]b4_lex_symbol_if([b4_yytranslate_definition
])[
857 ]b4_lex_symbol_if([b4_symbol_constructor_definitions
])[
860 ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
861 [b4_token_defines(b4_tokens
)
864 /* Redirection for backward compatibility. */
865 # define YYSTYPE b4_namespace_ref::b4_parser_class_name::semantic_type
868 b4_percent_code_get([[provides]])[]dnl
870 [#endif /* ! defined PARSER_HEADER_H */]
871 @
output(b4_parser_file_name@
)@
872 b4_copyright([Skeleton implementation
for Bison
LALR(1) parsers in C
++])
873 b4_percent_code_get([[top]])[]dnl
874 m4_if(b4_prefix
, [yy
], [],
876 // Take the name prefix into account.
877 #define yylex b4_prefix[]lex])[
879 /* First part of user declarations. */
880 ]b4_user_pre_prologue
[
882 #include "@basename(]b4_spec_defines_file[@)"
884 /* User implementation prologue. */
885 ]b4_user_post_prologue
886 b4_percent_code_get
[]dnl
891 # include <libintl.h> /* FIXME: INFRINGES ON USER NAME SPACE */
892 # define YY_(msgid) dgettext ("bison-runtime", msgid)
896 # define YY_(msgid) msgid
900 /* Suppress unused-variable warnings by "using" E. */
901 #define YYUSE(e) ((void) (e))
903 /* Enable debugging if requested. */
906 /* A pseudo ostream that takes yydebug_ into account. */
907 # define YYCDEBUG if (yydebug_) (*yycdebug_)
909 # define YY_SYMBOL_PRINT(Title, Symbol) \
913 *yycdebug_ << Title << ' '; \
914 yy_print_ (*yycdebug_, Symbol); \
915 *yycdebug_ << std::endl; \
919 # define YY_REDUCE_PRINT(Rule) \
922 yy_reduce_print_ (Rule); \
925 # define YY_STACK_PRINT() \
933 # define YYCDEBUG if (false) std::cerr
934 # define YY_SYMBOL_PRINT(Title, Symbol) static_cast<void>(0)
935 # define YY_REDUCE_PRINT(Rule) static_cast<void>(0)
936 # define YY_STACK_PRINT() static_cast<void>(0)
938 #endif /* !YYDEBUG */
940 #define yyerrok (yyerrstatus_ = 0)
941 #define yyclearin (yyempty = true)
943 #define YYACCEPT goto yyacceptlab
944 #define YYABORT goto yyabortlab
945 #define YYERROR goto yyerrorlab
946 #define YYRECOVERING() (!!yyerrstatus_)
948 ]b4_namespace_open
[]b4_error_verbose_if([[
950 /* Return YYSTR after stripping away unnecessary quotes and
951 backslashes, so that it's suitable for yyerror. The heuristic is
952 that double-quoting is unnecessary unless the string contains an
953 apostrophe, a comma, or backslash (other than backslash-backslash).
954 YYSTR is taken from yytname. */
956 ]b4_parser_class_name
[::yytnamerr_ (const char *yystr
)
960 std::string yyr
= "";
961 char const *yyp
= yystr
;
968 goto do_not_strip_quotes
;
972 goto do_not_strip_quotes
;
981 do_not_strip_quotes
: ;
988 /// Build a parser object.
989 ]b4_parser_class_name::b4_parser_class_name
[ (]b4_parse_param_decl
[)]m4_ifset([b4_parse_param
], [
992 ]m4_ifset([b4_parse_param
], [ ], [ :])[yydebug_ (false),
993 yycdebug_ (&std::cerr
)]m4_ifset([b4_parse_param
], [,])[
994 #endif]b4_parse_param_cons[
998 ]b4_parser_class_name::~b4_parser_class_name
[ ()
1007 ]b4_lex_symbol_if([], [b4_symbol_constructor_definitions
])[
1009 // stack_symbol_type.
1010 ]b4_parser_class_name
[::stack_symbol_type::stack_symbol_type ()
1016 ]b4_parser_class_name
[::stack_symbol_type::stack_symbol_type (]b4_args(
1018 [const semantic_type
& v
],
1019 b4_locations_if([const location_type
& l
]))[)
1020 : super_type (v
]b4_locations_if([, l
])[)
1026 ]b4_parser_class_name
[::stack_symbol_type::type_get_ () const
1028 return yystos_
[state
];
1032 template <typename Exact
>
1034 ]b4_parser_class_name
[::yy_destroy_ (const char* yymsg
,
1035 symbol_base_type
<Exact
>& yysym
) const
1037 int yytype
= yysym
.type_get ();
1040 YY_SYMBOL_PRINT (yymsg
, yysym
);
1045 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_destructors
]))[
1051 b4_symbol_variant([[yytype]], [[yysym
.value
]], [[template destroy]])])[
1055 template <typename Exact
>
1057 ]b4_parser_class_name
[::yy_print_ (std::ostream
& yyo
,
1058 const symbol_base_type
<Exact
>& yysym
) const
1060 int yytype
= yysym
.type_get ();
1061 yyo
<< (yytype
< yyntokens_
? "token" : "nterm")
1062 << ' ' << yytname_
[yytype
] << " ("]b4_locations_if([
1063 << yysym
.location
<< ": "])[;
1066 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_printers
]))[
1075 ]b4_parser_class_name
[::yypush_ (const char* m
, state_type s
,
1079 YY_SYMBOL_PRINT (m
, sym
);
1081 [[ yystack_
.push (stack_symbol_type (]b4_args(
1084 b4_locations_if([sym
.location
]))[));
1085 ]b4_symbol_variant([[yystos_
[s
]]], [[yystack_
[0].value
]],
1086 [build
], [sym
.value
])],
1087 [[ yystack_
.push (stack_symbol_type (]b4_args(
1090 b4_locations_if([sym
.location
]))[));]])[
1094 ]b4_parser_class_name
[::yypush_ (const char* m
, stack_symbol_type
& s
)
1097 YY_SYMBOL_PRINT (m
, s
);
1099 [[ yystack_
.push (stack_symbol_type (]b4_args(
1102 b4_locations_if([s
.location
]))[));
1103 ]b4_symbol_variant([[yystos_
[s
.state
]]], [[yystack_
[0].value
]],
1104 [build
], [s
.value
])],
1105 [ yystack_
.push (s
);])[
1109 ]b4_parser_class_name
[::yypop_ (unsigned int n
)
1116 ]b4_parser_class_name
[::debug_stream () const
1122 ]b4_parser_class_name
[::set_debug_stream (std::ostream
& o
)
1128 ]b4_parser_class_name
[::debug_level_type
1129 ]b4_parser_class_name
[::debug_level () const
1135 ]b4_parser_class_name
[::set_debug_level (debug_level_type l
)
1142 ]b4_parser_class_name
[::parse ()
1144 /// Whether yyla contains a lookahead.
1145 bool yyempty
= true;
1152 /* Error handling. */
1154 int yyerrstatus_
= 0;
1156 /// The lookahead symbol.
1157 symbol_type yyla
;]b4_locations_if([[
1159 /// The locations where the error started and ended.
1160 stack_symbol_type yyerror_range
[2];]])[
1163 stack_symbol_type yylhs
;
1165 /// The return value of parse().
1168 YYCDEBUG
<< "Starting parse" << std::endl
;
1170 ]m4_ifdef([b4_initial_action
], [
1171 m4_pushdef([b4_at_dollar
], [yyla
.location
])dnl
1172 m4_pushdef([b4_dollar_dollar
], [yyla
.value
])dnl
1173 /* User initialization code. */
1174 b4_user_initial_action
1175 m4_popdef([b4_dollar_dollar
])dnl
1176 m4_popdef([b4_at_dollar
])])dnl
1178 [ /* Initialize the stack. The initial state will be set in
1179 yynewstate, since the latter expects the semantical and the
1180 location values to have been already stored, initialize these
1181 stacks with a primary value. */
1182 yystack_
= stack_type (0);
1183 yypush_ (0, 0, yyla
);
1185 // A new state was pushed on the stack.
1186 // Invariant: yystate == yystack_[0].state, i.e.,
1187 // yystate was just pushed onto the state stack.
1189 YYCDEBUG
<< "Entering state " << yystate
<< std::endl
;
1192 if (yystate
== yyfinal_
)
1200 /* Try to take a decision without lookahead. */
1201 yyn
= yypact_
[yystate
];
1202 if (yyn
== yypact_ninf_
)
1205 /* Read a lookahead token. */
1208 YYCDEBUG
<< "Reading a token: ";
1210 [ yyla
= b4_c_function_call([yylex
], [symbol_type
],
1211 m4_ifdef([b4_lex_param
], b4_lex_param
));],
1212 [ yyla
.type
= yytranslate_ (b4_c_function_call([yylex
], [int],
1213 [[YYSTYPE
*], [&yyla
.value
]][]dnl
1214 b4_locations_if([, [[location
*], [&yyla
.location
]]])dnl
1215 m4_ifdef([b4_lex_param
], [, ]b4_lex_param
)));])[
1218 YY_SYMBOL_PRINT ("Next token is", yyla
);
1220 /* If the proper action on seeing token YYLA.TYPE is to reduce or
1221 to detect an error, take that action. */
1223 if (yyn
< 0 || yylast_
< yyn
|| yycheck_
[yyn
] != yyla
.type
)
1226 /* Reduce or error. */
1227 yyn
= yytable_
[yyn
];
1230 if (yyn
== 0 || yyn
== yytable_ninf_
)
1236 /* Discard the token being shifted. */
1239 /* Count tokens shifted since error; after three, turn off error
1244 /* Shift the lookahead token. */
1246 yypush_ ("Shifting", yystate
, yyla
);
1249 /*-----------------------------------------------------------.
1250 | yydefault -- do the default action for the current state. |
1251 `-----------------------------------------------------------*/
1253 yyn
= yydefact_
[yystate
];
1258 /*-----------------------------.
1259 | yyreduce -- Do a reduction. |
1260 `-----------------------------*/
1262 yylen
= yyr2_
[yyn
];]b4_variant_if([
1263 /* Variants are always initialized to an empty instance of the
1264 correct type. The default $$=$1 rule is NOT applied when using
1266 ]b4_symbol_variant([[yyr1_@
{yyn@
}]], [yylhs
.value
], [build
])[],[
1267 /* If YYLEN is nonzero, implement the default value of the action:
1268 `$$ = $1'. Otherwise, use the top of the stack.
1270 Otherwise, the following line sets YYLHS.VALUE to garbage.
1271 This behavior is undocumented and Bison
1272 users should not rely upon it. */
1274 yylhs
.value
= yystack_@
{yylen
- 1@
}.value
;
1276 yylhs
.value
= yystack_@
{0@
}.value
;])[
1277 ]b4_locations_if([dnl
1279 // Compute the default @@$.
1281 slice
<stack_symbol_type
, stack_type
> slice (yystack_
, yylen
);
1282 YYLLOC_DEFAULT (yylhs
.location
, slice
, yylen
);
1285 // Perform the reduction.
1286 YY_REDUCE_PRINT (yyn
);
1293 // Compute post-reduction state.
1295 yystate
= yypgoto_
[yyn
- yyntokens_
] + yystack_
[yylen
].state
;
1296 if (0 <= yystate
&& yystate
<= yylast_
1297 && yycheck_
[yystate
] == yystack_
[yylen
].state
)
1298 yystate
= yytable_
[yystate
];
1300 yystate
= yydefgoto_
[yyn
- yyntokens_
];
1301 yylhs
.state
= yystate
;
1302 YY_SYMBOL_PRINT ("-> $$ =", yylhs
);
1304 // Destroy the lhs symbols.
1305 for (int i
= 0; i
< yylen
; ++i
)
1306 // Destroy a variant which value may have be swapped with
1307 // yylhs.value. The value of yylhs.value (hence maybe one of
1308 // these lhs symbols) depends on what does the default
1309 // contruction for this type. In the case of pointers for
1310 // instance, nothing is done, so the value is junk. Therefore
1311 // do not try to report the content in the debug trace, it's
1312 // junk. Hence yymsg = 0. Besides, that keeps exactly the same
1313 // traces as with the other Bison skeletons.
1314 yy_destroy_ (0, yystack_
[i
]);]])[
1320 /* Shift the result of the reduction. */
1324 /*------------------------------------.
1325 | yyerrlab -- here on detecting error |
1326 `------------------------------------*/
1328 /* If not already recovering from an error, report this error. */
1332 error (]b4_args(b4_locations_if([yyla
.location
]),
1333 [yysyntax_error_ (yystate
, yyla
.type
)])[);
1337 yyerror_range
[0].location
= yyla
.location
;]])[
1338 if (yyerrstatus_
== 3)
1340 /* If just tried and failed to reuse lookahead token after an
1341 error, discard it. */
1343 /* Return failure if at end of input. */
1344 if (yyla
.type
== yyeof_
)
1348 yy_destroy_ ("Error: discarding", yyla
);
1353 /* Else will try to reuse lookahead token after shifting the error
1358 /*---------------------------------------------------.
1359 | yyerrorlab -- error raised explicitly by YYERROR. |
1360 `---------------------------------------------------*/
1363 /* Pacify compilers like GCC when the user code never invokes
1364 YYERROR and the label yyerrorlab therefore never appears in user
1370 yyerror_range
[0].location
= yystack_
[yylen
- 1].location
;]])[
1371 /* Do not reclaim the symbols of the rule which action triggered
1375 yystate
= yystack_
[0].state
;
1378 /*-------------------------------------------------------------.
1379 | yyerrlab1 -- common code for both syntax error and YYERROR. |
1380 `-------------------------------------------------------------*/
1382 yyerrstatus_
= 3; /* Each real token shifted decrements this. */
1384 stack_symbol_type error_token
;
1387 yyn
= yypact_
[yystate
];
1388 if (yyn
!= yypact_ninf_
)
1391 if (0 <= yyn
&& yyn
<= yylast_
&& yycheck_
[yyn
] == yyterror_
)
1393 yyn
= yytable_
[yyn
];
1399 // Pop the current state because it cannot handle the error token.
1400 if (yystack_
.size () == 1)
1403 yyerror_range
[0].location
= yystack_
[0].location
;]])[
1404 yy_destroy_ ("Error: popping", yystack_
[0]);
1406 yystate
= yystack_
[0].state
;
1410 yyerror_range
[1].location
= yyla
.location
;
1411 YYLLOC_DEFAULT (error_token
.location
, (yyerror_range
- 1), 2);]])[
1413 /* Shift the error token. */
1414 error_token
.state
= yystate
= yyn
;
1415 yypush_ ("Shifting", error_token
);
1431 yy_destroy_ ("Cleanup: discarding lookahead", yyla
);
1433 /* Do not reclaim the symbols of the rule which action triggered
1434 this YYABORT or YYACCEPT. */
1436 while (yystack_
.size () != 1)
1438 yy_destroy_ ("Cleanup: popping", yystack_
[0]);
1445 // Generate an error message.
1447 ]b4_parser_class_name
[::yysyntax_error_ (]dnl
1448 b4_error_verbose_if([int yystate
, int yytoken
],
1451 std::string yyres
;]b4_error_verbose_if([[
1452 int yyn
= yypact_
[yystate
];
1453 if (yypact_ninf_
< yyn
&& yyn
<= yylast_
)
1455 /* Start YYX at -YYN if negative to avoid negative indexes in
1457 int yyxbegin
= yyn
< 0 ? -yyn
: 0;
1459 /* Stay within bounds of both yycheck and yytname. */
1460 int yychecklim
= yylast_
- yyn
+ 1;
1461 int yyxend
= yychecklim
< yyntokens_
? yychecklim
: yyntokens_
;
1463 // Number of "expected" tokens.
1466 enum { YYERROR_VERBOSE_ARGS_MAXIMUM
= 5 };
1467 // Arguments of yyformat.
1468 char const *yyarg
[YYERROR_VERBOSE_ARGS_MAXIMUM
];
1469 yyarg
[yycount
++] = yytname_
[yytoken
];
1470 for (int yyx
= yyxbegin
; yyx
< yyxend
; ++yyx
)
1471 if (yycheck_
[yyx
+ yyn
] == yyx
&& yyx
!= yyterror_
)
1473 if (yycount
== YYERROR_VERBOSE_ARGS_MAXIMUM
)
1479 yyarg
[yycount
++] = yytname_
[yyx
];
1482 char const* yyformat
= 0;
1485 #define YYCASE_(N, S) \
1489 YYCASE_(1, YY_("syntax error, unexpected %s"));
1490 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1491 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1492 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1493 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1498 for (char const* yyp
= yyformat
; *yyp
; ++yyp
)
1499 if (yyp
[0] == '%' && yyp
[1] == 's' && yyi
< yycount
)
1501 yyres
+= yytnamerr_ (yyarg
[yyi
++]);
1509 [ yyres
= YY_("syntax error");
1514 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
1516 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) b4_parser_class_name::yypact_ninf_
= b4_pact_ninf
[;
1517 ]b4_table_define([pact
], [b4_pact
])[;
1519 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
1520 doesn't specify something else to do. Zero means the default is an
1522 ]b4_table_define([defact
], [b4_defact
])[;
1524 /* YYPGOTO[NTERM-NUM]. */
1525 ]b4_table_define([pgoto
], [b4_pgoto
])[;
1527 /* YYDEFGOTO[NTERM-NUM]. */
1528 ]b4_table_define([defgoto
], [b4_defgoto
])[;
1530 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
1531 positive, shift that token. If negative, reduce the rule which
1532 number is the opposite. If zero, do what YYDEFACT says. */
1533 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) b4_parser_class_name::yytable_ninf_
= b4_table_ninf
[;
1534 ]b4_table_define([table
], [b4_table
])[;
1537 ]b4_table_define([check
], [b4_check
])[;
1539 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
1540 symbol of state STATE-NUM. */
1541 ]b4_table_define([stos
], [b4_stos
])[;
1543 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
1544 ]b4_table_define([r1
], [b4_r1
])[;
1546 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
1547 ]b4_table_define([r2
], [b4_r2
])[;
1549 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
1550 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
1551 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
1553 const ]b4_parser_class_name
[::yytname_
[] =
1560 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
1561 ]b4_table_define([rline
], [b4_rline
])[;
1563 // Print the state stack on the debug stream.
1565 ]b4_parser_class_name
[::yystack_print_ ()
1567 *yycdebug_
<< "Stack now";
1568 for (stack_type::const_iterator
1569 i
= yystack_
.begin (),
1570 i_end
= yystack_
.end ();
1572 *yycdebug_
<< ' ' << i
->state
;
1573 *yycdebug_
<< std::endl
;
1576 // Report on the debug stream that the rule \a yyrule is going to be reduced.
1578 ]b4_parser_class_name
[::yy_reduce_print_ (int yyrule
)
1580 unsigned int yylno
= yyrline_
[yyrule
];
1581 int yynrhs
= yyr2_
[yyrule
];
1582 /* Print the symbols being reduced, and their result. */
1583 *yycdebug_
<< "Reducing stack by rule " << yyrule
- 1
1584 << " (line " << yylno
<< "):" << std::endl
;
1585 /* The symbols being reduced. */
1586 for (int yyi
= 0; yyi
< yynrhs
; yyi
++)
1587 YY_SYMBOL_PRINT (" $" << yyi
+ 1 << " =",
1588 ]b4_rhs_data(yynrhs
, yyi
+ 1)[);
1592 ]b4_lex_symbol_if([], [b4_yytranslate_definition
])[
1593 ]b4_namespace_close
[
1596 @
output(b4_dir_prefix
[]stack
.hh@
)@
1597 b4_copyright([Stack handling
for Bison parsers in C
++])[
1599 #ifndef BISON_STACK_HH
1600 # define BISON_STACK_HH
1605 template <class T
, class S
= std::deque
<T
> >
1610 // Hide our reversed order.
1611 typedef typename
S::reverse_iterator iterator
;
1612 typedef typename
S::const_reverse_iterator const_iterator
;
1618 stack (unsigned int n
) : seq_ (n
)
1624 operator [] (unsigned int i
)
1631 operator [] (unsigned int i
) const
1640 seq_
.push_front (t
);
1645 pop (unsigned int n
= 1)
1652 typename
S::size_type
1655 return seq_
.size ();
1658 inline const_iterator
begin () const { return seq_
.rbegin (); }
1659 inline const_iterator
end () const { return seq_
.rend (); }
1662 /// The wrapped container.
1666 /// Present a slice of the top of a stack.
1667 template <class T
, class S
= stack
<T
> >
1672 slice (const S
& stack
,
1673 unsigned int range
) : stack_ (stack
),
1680 operator [] (unsigned int i
) const
1682 return stack_
[range_
- i
];
1688 unsigned int range_
;
1690 ]b4_namespace_close
[
1692 #endif // not BISON_STACK_HH[]dnl
1695 m4_popdef([b4_copyright_years
])dnl