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_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_
[[]] =
32 # b4_symbol_value_template(VAL, [TYPE])
33 # -------------------------------------
34 # Same as b4_symbol_value, but used in a template method.
35 m4_copy([b4_symbol_value
], [b4_symbol_value_template
])
37 # How the semantic value is extracted when using variants.
39 # b4_symbol_value(VAL, [TYPE])
40 # ----------------------------
41 m4_define([b4_symbol_value
],
46 # b4_symbol_value_template(VAL, [TYPE])
47 # -------------------------------------
48 # Same as b4_symbol_value, but used in a template method.
49 m4_define([b4_symbol_value_template
],
51 [$
1.template as
<$
2>()],
56 # b4_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
57 # ------------------------------------------------
58 m4_define([b4_assert_if
],
59 [b4_percent_define_ifdef([[assert]], [$
1], [$
2])])
62 # b4_lhs_value([TYPE])
63 # --------------------
64 # Expansion of $<TYPE>$.
65 m4_define([b4_lhs_value
],
66 [b4_symbol_value([yylhs
.value
], [$
1])])
72 m4_define([b4_lhs_location
],
76 # b4_rhs_data(RULE-LENGTH, NUM)
77 # -----------------------------
78 # Return the data corresponding to the symbol #NUM, where the current
79 # rule has RULE-LENGTH symbols on RHS.
80 m4_define([b4_rhs_data
],
81 [yystack_@
{($
1) - ($
2)@
}])
84 # b4_rhs_state(RULE-LENGTH, NUM)
85 # -----------------------------
86 # The state corresponding to the symbol #NUM, where the current
87 # rule has RULE-LENGTH symbols on RHS.
88 m4_define([b4_rhs_state
],
89 [b4_rhs_data([$
1], [$
2]).state
])
92 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
93 # --------------------------------------
94 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
96 m4_define([b4_rhs_value
],
97 [b4_symbol_value([b4_rhs_data([$
1], [$
2]).value
], [$
3])])
100 # b4_rhs_location(RULE-LENGTH, NUM)
101 # ---------------------------------
102 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
104 m4_define([b4_rhs_location
],
105 [b4_rhs_data([$
1], [$
2]).location
])
108 # b4_symbol(NUM, FIELD)
109 # ---------------------
110 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
112 m4_define([b4_symbol
],
113 [m4_indir([b4_symbol($
1, $
2)])])
116 # b4_symbol_actions(FILENAME, LINENO,
117 # SYMBOL-TAG, SYMBOL-NUM,
118 # SYMBOL-ACTION, SYMBOL-TYPENAME)
119 # -------------------------------------------------
120 # Same as in C, but using references instead of pointers.
121 m4_define([b4_symbol_actions
],
122 [m4_pushdef([b4_dollar_dollar
],
123 [b4_symbol_value_template([yysym
.value
], [$
6])])dnl
124 m4_pushdef([b4_at_dollar
], [yysym
.location
])dnl
126 b4_syncline([$
2], [$
1])
128 b4_syncline([@oline@
], [@ofile@
])
130 m4_popdef([b4_at_dollar
])dnl
131 m4_popdef([b4_dollar_dollar
])dnl
135 # b4_symbol_case_(SYMBOL-NUM)
136 # ---------------------------
137 # Issue a "case NUM" for SYMBOL-NUM.
138 m4_define([b4_symbol_case_
],
139 [ case b4_symbol([$
1], [number
]): // b4_symbol([$1], [tag])
143 # b4_type_action_(NUMS)
144 # ---------------------
145 # Run actions for the symbol NUMS that all have the same type-name.
146 m4_define([b4_type_action_
],
147 [m4_map([b4_symbol_case_
], [$@
])[]dnl
148 b4_dollar_dollar([b4_symbol([$
1], [number
])],
149 [b4_symbol([$
1], [tag
])],
150 [b4_symbol([$
1], [type_name
])]);
156 # b4_symbol_variant(YYTYPE, YYVAL, ACTION, [ARGS])
157 # ------------------------------------------------
158 # Run some ACTION ("build", or "destroy") on YYVAL of symbol type
160 m4_define([b4_symbol_variant
],
161 [m4_pushdef([b4_dollar_dollar
],
162 [$
2.$
3<$
][3>(m4_shift3($@
))])dnl
165 m4_map([b4_type_action_
], m4_defn([b4_type_names
]))[]dnl
169 m4_popdef([b4_dollar_dollar
])dnl
173 # _b4_char_sizeof_counter
174 # -----------------------
175 # A counter used by _b4_char_sizeof_dummy to create fresh symbols.
176 m4_define([_b4_char_sizeof_counter
],
179 # _b4_char_sizeof_dummy
180 # ---------------------
181 # At each call return a new C++ identifier.
182 m4_define([_b4_char_sizeof_dummy
],
183 [m4_define([_b4_char_sizeof_counter
], m4_incr(_b4_char_sizeof_counter
))dnl
184 dummy
[]_b4_char_sizeof_counter
])
187 # b4_char_sizeof(SYMBOL-NUM)
188 # --------------------------
189 # To be mapped on the list of type names to produce:
191 # char dummy1[sizeof(type_name_1)];
192 # char dummy2[sizeof(type_name_2)];
194 # for defined type names.
195 # $3 is doubly-quoted, do not quote it again.
196 m4_define([b4_char_sizeof
],
197 [m4_ifval(b4_symbol([$
1], [type_name
]),
199 char _b4_char_sizeof_dummy@
{sizeof([b4_symbol([$
1], [type_name
])])@
}; // b4_symbol([$1], [tag])])dnl
203 m4_pushdef([b4_copyright_years
],
204 [2002, 2003, 2004, 2005, 2006, 2007, 2008])
206 m4_define([b4_parser_class_name
],
207 [b4_percent_define_get([[parser_class_name]])])
209 # The header is mandatory.
211 [b4_fatal([b4_skeleton
[: using %%defines is mandatory
]])])
213 # Backward compatibility.
214 m4_define([b4_location_constructors
])
215 m4_include(b4_pkgdatadir
/[location
.cc
])
217 # We do want M4 expansion after # for CPP macros.
221 [@
output(b4_spec_defines_file@
)@
222 b4_copyright([Skeleton interface
for Bison
LALR(1) parsers in C
++])
223 dnl FIXME
: This is wrong
, we want computed header guards
.
225 /* C++ LALR(1) parser skeleton written by Akim Demaille. */
227 #ifndef PARSER_HEADER_H
228 # define PARSER_HEADER_H
230 ]b4_percent_code_get([[requires]])[
232 ]b4_assert_if([#include <cassert>])[
242 /// A char[S] buffer to store and retrieve objects.
244 /// Sort of a variant, but does not keep track of the nature
245 /// of the stored data, since that knowledge is available
246 /// via the current state.
250 /// Whether something is contained.
253 /// Initially uninitialized.
258 /// Instantiate a \a T in here.
259 template <typename T
>
265 return *new (buffer
) T
;
268 /// Instantiate a \a T in here from \a t.
269 template <typename T
>
275 return *new (buffer
) T(t
);
278 /// Accessor to a built \a T.
279 template <typename T
>
284 return reinterpret_cast<T
&>(buffer
);
287 /// Const accessor to a built \a T (for %printer).
288 template <typename T
>
293 return reinterpret_cast<const T
&>(buffer
);
296 /// Swap the content with \a other.
297 template <typename T
>
299 swap(variant
<S
>& other
)
301 std::swap(as
<T
>(), other
.as
<T
>());
304 /// Assign the content of \a other to this.
305 /// Destroys \a other.
306 template <typename T
>
308 build(variant
<S
>& other
)
315 /// Destroy the stored \a T.
316 template <typename T
>
320 as
<T
>().~T();]b4_assert_if([
324 /// A buffer large enough to store any of the semantic values.
330 #include "location.hh"
332 /* Enabling traces. */
334 # define YYDEBUG ]b4_debug_flag[
337 /* Enabling verbose error messages. */
338 #ifdef YYERROR_VERBOSE
339 # undef YYERROR_VERBOSE
340 # define YYERROR_VERBOSE 1
342 # define YYERROR_VERBOSE ]b4_error_verbose_flag[
345 /* Enabling the token table. */
346 #ifndef YYTOKEN_TABLE
347 # define YYTOKEN_TABLE ]b4_token_table[
350 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
351 If N is 0, then set CURRENT to the empty location which ends
352 the previous symbol: RHS[0] (always defined). */
354 #ifndef YYLLOC_DEFAULT
355 # define YYLLOC_DEFAULT(Current, Rhs, N) \
359 (Current).begin = (Rhs)[1].location.begin; \
360 (Current).end = (Rhs)[N].location.end; \
364 (Current).begin = (Current).end = (Rhs)[0].location.end; \
372 class ]b4_parser_class_name
[
377 [ /// An auxiliary type to compute the largest semantic type.
379 {]m4_map([b4_char_sizeof
], m4_defn([b4_symbol_numbers
]))[
382 /// Symbol semantic values.
383 typedef variant
<sizeof(union_type
)> semantic_type
;],
384 [ /// Symbol semantic values.
386 [ union semantic_type
389 [m4_if(b4_tag_seen_flag
, 0,
390 [[ typedef int semantic_type
;]],
391 [[ typedef YYSTYPE semantic_type
;]])])])[
393 typedef YYSTYPE semantic_type
;
395 /// Symbol locations.
396 typedef ]b4_percent_define_get([[location_type]])[ location_type
;
400 ]b4_token_enums(b4_tokens
)[
403 typedef token::yytokentype token_type
;
405 /// Build a parser object.
406 ]b4_parser_class_name
[ (]b4_parse_param_decl
[);
407 virtual ~]b4_parser_class_name
[ ();
410 /// \returns 0 iff parsing succeeded.
411 virtual int parse ();
414 /// The current debugging stream.
415 std::ostream
& debug_stream () const;
416 /// Set the current debugging stream.
417 void set_debug_stream (std::ostream
&);
419 /// Type for debugging levels.
420 typedef int debug_level_type
;
421 /// The current debugging level.
422 debug_level_type
debug_level () const;
423 /// Set the current debugging level.
424 void set_debug_level (debug_level_type l
);
428 /// Report a syntax error.
429 /// \param loc where the syntax error is found.
430 /// \param msg a description of the syntax error.
431 virtual void error (const location_type
& loc
, const std::string
& msg
);
433 /// Generate an error message.
434 /// \param state the state where the error occurred.
435 /// \param tok the lookahead token.
436 virtual std::string
yysyntax_error_ (int yystate
, int tok
);
439 typedef int state_type
;
441 /// Internal symbol numbers.
442 typedef ]b4_int_type_for([b4_translate
])[ token_number_type
;
444 /// For a state, the index in \a yytable_ of its portion.
445 static const ]b4_int_type_for([b4_pact
])[ yypact_
[];
446 static const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
)[ yypact_ninf_
;
448 /// For a state, default rule to reduce.
449 /// Unless\a yytable_ specifies something else to do.
450 /// Zero means the default is an error.
451 static const ]b4_int_type_for([b4_defact
])[ yydefact_
[];
453 static const ]b4_int_type_for([b4_pgoto
])[ yypgoto_
[];
454 static const ]b4_int_type_for([b4_defgoto
])[ yydefgoto_
[];
456 /// What to do in a state.
457 /// \a yytable_[yypact_[s]]: what to do in state \a s.
458 /// - if positive, shift that token.
459 /// - if negative, reduce the rule which number is the opposite.
460 /// - if zero, do what YYDEFACT says.
461 static const ]b4_int_type_for([b4_table
])[ yytable_
[];
462 static const ]b4_int_type(b4_table_ninf
, b4_table_ninf
)[ yytable_ninf_
;
464 static const ]b4_int_type_for([b4_check
])[ yycheck_
[];
466 /// For a state, its accessing symbol.
467 static const ]b4_int_type_for([b4_stos
])[ yystos_
[];
469 /// For a rule, its LHS.
470 static const ]b4_int_type_for([b4_r1
])[ yyr1_
[];
471 /// For a rule, its RHS length.
472 static const ]b4_int_type_for([b4_r2
])[ yyr2_
[];
474 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
475 /// For a symbol, its name in clear.
476 static const char* const yytname_
[];
480 /// Convert the symbol name \a n to a form suitable for a diagnostic.
481 virtual std::string
yytnamerr_ (const char *n
);
485 /// For each rule, its source line number.
486 static const ]b4_int_type_for([b4_rline
])[ yyrline_
[];
487 /// For each scanner token number, its symbol number.
488 static const ]b4_int_type_for([b4_toknum
])[ yytoken_number_
[];
489 /// Report on the debug stream that the rule \a r is going to be reduced.
490 virtual void yy_reduce_print_ (int r
);
491 /// Print the state stack on the debug stream.
492 virtual void yystack_print_ ();
496 std::ostream
* yycdebug_
;
499 /// Convert a scanner token number \a t to a symbol number.
500 token_number_type
yytranslate_ (int t
);
502 /// A complete symbol, with its type.
503 template <typename Exact
>
504 struct symbol_base_type
506 /// Default constructor.
507 inline symbol_base_type ();
510 inline symbol_base_type (const semantic_type
& v
, const location_type
& l
);
512 /// Return this with its exact type.
513 const Exact
& self () const;
516 /// Return the type of this symbol.
517 int type_get () const;
519 /// The semantic value.
523 location_type location
;
527 /// \brief Display a symbol type, value and location.
528 /// \param yyo The output stream.
529 /// \param yysym The symbol.
530 template <typename Exact
>
531 void yy_print_ (std::ostream
& yyo
,
532 const symbol_base_type
<Exact
>& yysym
) const;
535 /// \brief Reclaim the memory associated to a symbol.
536 /// \param yymsg Why this token is reclaimed.
537 /// If null, print nothing.
538 /// \param s The symbol.
539 template <typename Exact
>
540 inline void yy_destroy_ (const char* yymsg
,
541 symbol_base_type
<Exact
>& yysym
) const;
543 /// Element of the stack: a state and its attributes.
544 struct symbol_type
: symbol_base_type
<symbol_type
>
546 /// The parent class.
547 typedef symbol_base_type
<symbol_type
> super_type
;
549 /// Default constructor.
550 inline symbol_type ();
553 inline symbol_type (int t
,
554 const semantic_type
& v
, const location_type
& l
);
559 /// Return the type corresponding to this state.
560 inline int type_get_ () const;
563 /// Element of the stack: a state and its attributes.
564 struct stack_symbol_type
: symbol_base_type
<stack_symbol_type
>
566 /// The parent class.
567 typedef symbol_base_type
<stack_symbol_type
> super_type
;
569 /// Default constructor.
570 inline stack_symbol_type ();
573 inline stack_symbol_type (state_type s
,
574 const semantic_type
& v
, const location_type
& l
);
579 /// Return the type corresponding to this state.
580 inline int type_get_ () const;
584 typedef stack
<stack_symbol_type
> stack_type
;
589 /// Push a new state on the stack.
590 /// \param m a debug message to display
591 /// if null, no trace is output.
592 /// \param s the symbol
593 /// \warning the contents of \a s.value is stolen.
594 inline void yypush_ (const char* m
, stack_symbol_type
& s
);
596 /// Push a new look ahead token on the state on the stack.
597 /// \param m a debug message to display
598 /// if null, no trace is output.
599 /// \param s the state
600 /// \param sym the symbol (for its value and location).
601 /// \warning the contents of \a s.value is stolen.
602 inline void yypush_ (const char* m
, state_type s
, symbol_type
& sym
);
604 /// Pop \a n symbols the three stacks.
605 inline void yypop_ (unsigned int n
= 1);
608 static const int yyeof_
;
609 /* LAST_ -- Last index in TABLE_. */
610 static const int yylast_
;
611 static const int yynnts_
;
612 static const int yyempty_
;
613 static const int yyfinal_
;
614 static const int yyterror_
;
615 static const int yyerrcode_
;
616 static const int yyntokens_
;
617 static const unsigned int yyuser_token_number_max_
;
618 static const token_number_type yyundef_token_
;
619 ]b4_parse_param_vars
[
624 ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
625 [b4_token_defines(b4_tokens
)
628 /* Redirection for backward compatibility. */
629 # define YYSTYPE b4_namespace_ref::b4_parser_class_name::semantic_type
632 b4_percent_code_get([[provides]])[]dnl
634 [#endif /* ! defined PARSER_HEADER_H */]
636 @
output(b4_parser_file_name@
)@
637 b4_copyright([Skeleton implementation
for Bison
LALR(1) parsers in C
++])
638 b4_percent_code_get([[top]])[]dnl
639 m4_if(b4_prefix
, [yy
], [],
641 // Take the name prefix into account.
642 #define yylex b4_prefix[]lex])[
644 /* First part of user declarations. */
645 ]b4_user_pre_prologue
648 #include "@basename(]b4_spec_defines_file[@)"]])[
650 /* User implementation prologue. */
651 ]b4_user_post_prologue
652 b4_percent_code_get
[]dnl
657 # include <libintl.h> /* FIXME: INFRINGES ON USER NAME SPACE */
658 # define YY_(msgid) dgettext ("bison-runtime", msgid)
662 # define YY_(msgid) msgid
666 /* Suppress unused-variable warnings by "using" E. */
667 #define YYUSE(e) ((void) (e))
669 /* Enable debugging if requested. */
672 /* A pseudo ostream that takes yydebug_ into account. */
673 # define YYCDEBUG if (yydebug_) (*yycdebug_)
675 # define YY_SYMBOL_PRINT(Title, Symbol) \
679 *yycdebug_ << Title << ' '; \
680 yy_print_ (*yycdebug_, Symbol); \
681 *yycdebug_ << std::endl; \
685 # define YY_REDUCE_PRINT(Rule) \
688 yy_reduce_print_ (Rule); \
691 # define YY_STACK_PRINT() \
699 # define YYCDEBUG if (false) std::cerr
700 # define YY_SYMBOL_PRINT(Title, Symbol) static_cast<void>(0)
701 # define YY_REDUCE_PRINT(Rule) static_cast<void>(0)
702 # define YY_STACK_PRINT() static_cast<void>(0)
704 #endif /* !YYDEBUG */
706 #define yyerrok (yyerrstatus_ = 0)
707 #define yyclearin (yychar = yyempty_)
709 #define YYACCEPT goto yyacceptlab
710 #define YYABORT goto yyabortlab
711 #define YYERROR goto yyerrorlab
712 #define YYRECOVERING() (!!yyerrstatus_)
717 /* Return YYSTR after stripping away unnecessary quotes and
718 backslashes, so that it's suitable for yyerror. The heuristic is
719 that double-quoting is unnecessary unless the string contains an
720 apostrophe, a comma, or backslash (other than backslash-backslash).
721 YYSTR is taken from yytname. */
723 ]b4_parser_class_name
[::yytnamerr_ (const char *yystr
)
727 std::string yyr
= "";
728 char const *yyp
= yystr
;
735 goto do_not_strip_quotes
;
739 goto do_not_strip_quotes
;
748 do_not_strip_quotes
: ;
756 /// Build a parser object.
757 ]b4_parser_class_name::b4_parser_class_name
[ (]b4_parse_param_decl
[)]m4_ifset([b4_parse_param
], [
760 ]m4_ifset([b4_parse_param
], [ ], [ :])[yydebug_ (false),
761 yycdebug_ (&std::cerr
)]m4_ifset([b4_parse_param
], [,])[
762 #endif]b4_parse_param_cons[
766 ]b4_parser_class_name::~b4_parser_class_name
[ ()
776 template <typename Exact
>
777 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type ()
783 template <typename Exact
>
784 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::symbol_base_type (const semantic_type
& v
, const location_type
& l
)
790 template <typename Exact
>
792 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::self () const
794 return static_cast<const Exact
&>(*this);
797 template <typename Exact
>
799 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::self ()
801 return static_cast<Exact
&>(*this);
804 template <typename Exact
>
806 ]b4_parser_class_name
[::symbol_base_type
<Exact
>::type_get () const
808 return self ().type_get_ ();
812 ]b4_parser_class_name
[::symbol_type::symbol_type ()
818 ]b4_parser_class_name
[::symbol_type::symbol_type (int t
,
819 const semantic_type
& v
, const location_type
& l
)
826 ]b4_parser_class_name
[::symbol_type::type_get_ () const
831 // stack_symbol_type.
832 ]b4_parser_class_name
[::stack_symbol_type::stack_symbol_type ()
838 ]b4_parser_class_name
[::stack_symbol_type::stack_symbol_type (state_type s
,
839 const semantic_type
& v
, const location_type
& l
)
846 ]b4_parser_class_name
[::stack_symbol_type::type_get_ () const
848 return yystos_
[state
];
852 template <typename Exact
>
854 ]b4_parser_class_name
[::yy_destroy_ (const char* yymsg
,
855 symbol_base_type
<Exact
>& yysym
) const
857 int yytype
= yysym
.type_get ();
860 YY_SYMBOL_PRINT (yymsg
, yysym
);
865 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_destructors
]))[
871 b4_symbol_variant([[yytype]], [[yysym
.value
]], [[template destroy]])])[
875 template <typename Exact
>
877 ]b4_parser_class_name
[::yy_print_ (std::ostream
& yyo
,
878 const symbol_base_type
<Exact
>& yysym
) const
880 int yytype
= yysym
.type_get ();
881 yyo
<< (yytype
< yyntokens_
? "token" : "nterm")
882 << ' ' << yytname_
[yytype
] << " ("
883 << yysym
.location
<< ": ";
886 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_printers
]))dnl
895 ]b4_parser_class_name
[::yypush_ (const char* m
, state_type s
,
899 YY_SYMBOL_PRINT (m
, sym
);
901 [[ yystack_
.push (stack_symbol_type (s
, semantic_type(), sym
.location
));
902 ]b4_symbol_variant([[yystos_
[s
]]], [[yystack_
[0].value
]],
903 [build
], [sym
.value
])],
904 [ yystack_
.push (stack_symbol_type (s
, sym
.value
, sym
.location
));])[
908 ]b4_parser_class_name
[::yypush_ (const char* m
, stack_symbol_type
& s
)
911 YY_SYMBOL_PRINT (m
, s
);
913 [[ yystack_
.push (stack_symbol_type (s
.state
, semantic_type(), s
.location
));
914 ]b4_symbol_variant([[yystos_
[s
.state
]]], [[yystack_
[0].value
]],
915 [build
], [s
.value
])],
916 [ yystack_
.push (s
);])[
920 ]b4_parser_class_name
[::yypop_ (unsigned int n
)
927 ]b4_parser_class_name
[::debug_stream () const
933 ]b4_parser_class_name
[::set_debug_stream (std::ostream
& o
)
939 ]b4_parser_class_name
[::debug_level_type
940 ]b4_parser_class_name
[::debug_level () const
946 ]b4_parser_class_name
[::set_debug_level (debug_level_type l
)
953 ]b4_parser_class_name
[::parse ()
955 /// Coded type of the lookahead.
956 int yychar
= yyempty_
;
963 /* Error handling. */
965 int yyerrstatus_
= 0;
967 /// The lookahead symbol.
970 /// The locations where the error started and ended.
971 stack_symbol_type yyerror_range
[2];
974 stack_symbol_type yylhs
;
976 /// The return value of parse().
979 YYCDEBUG
<< "Starting parse" << std::endl
;
981 ]m4_ifdef([b4_initial_action
], [
982 m4_pushdef([b4_at_dollar
], [yyla
.location
])dnl
983 m4_pushdef([b4_dollar_dollar
], [yyla
.value
])dnl
984 /* User initialization code. */
985 b4_user_initial_action
986 m4_popdef([b4_dollar_dollar
])dnl
987 m4_popdef([b4_at_dollar
])])dnl
989 [ /* Initialize the stack. The initial state will be set in
990 yynewstate, since the latter expects the semantical and the
991 location values to have been already stored, initialize these
992 stacks with a primary value. */
993 yystack_
= stack_type (0);
994 yypush_ (0, 0, yyla
);
996 // A new state was pushed on the stack.
997 // Invariant: yystate == yystack_[0].state, i.e.,
998 // yystate was just pushed onto the state stack.
1000 YYCDEBUG
<< "Entering state " << yystate
<< std::endl
;
1003 if (yystate
== yyfinal_
)
1011 /* Try to take a decision without lookahead. */
1012 yyn
= yypact_
[yystate
];
1013 if (yyn
== yypact_ninf_
)
1016 /* Read a lookahead token. */
1017 if (yychar
== yyempty_
)
1019 YYCDEBUG
<< "Reading a token: ";
1020 yychar
= ]b4_c_function_call([yylex
], [int],
1021 [[YYSTYPE
*], [&yyla
.value
]][]dnl
1022 b4_locations_if([, [[location
*], [&yyla
.location
]]])dnl
1023 m4_ifdef([b4_lex_param
], [, ]b4_lex_param
))[;
1027 /* Convert token to internal form. */
1028 if (yychar
<= yyeof_
)
1030 yychar
= yyla
.type
= yyeof_
;
1031 YYCDEBUG
<< "Now at end of input." << std::endl
;
1035 yyla
.type
= yytranslate_ (yychar
);
1036 YY_SYMBOL_PRINT ("Next token is", yyla
);
1039 /* If the proper action on seeing token YYLA.TYPE is to reduce or
1040 to detect an error, take that action. */
1042 if (yyn
< 0 || yylast_
< yyn
|| yycheck_
[yyn
] != yyla
.type
)
1045 /* Reduce or error. */
1046 yyn
= yytable_
[yyn
];
1049 if (yyn
== 0 || yyn
== yytable_ninf_
)
1055 /* Discard the token being shifted. */
1058 /* Count tokens shifted since error; after three, turn off error
1063 /* Shift the lookahead token. */
1065 yypush_ ("Shifting", yystate
, yyla
);
1068 /*-----------------------------------------------------------.
1069 | yydefault -- do the default action for the current state. |
1070 `-----------------------------------------------------------*/
1072 yyn
= yydefact_
[yystate
];
1077 /*-----------------------------.
1078 | yyreduce -- Do a reduction. |
1079 `-----------------------------*/
1081 yylen
= yyr2_
[yyn
];]b4_variant_if([
1082 /* Variants are always initialized to an empty instance of the
1083 correct type. The default $$=$1 rule is NOT applied when using
1085 ]b4_symbol_variant([[yyr1_@
{yyn@
}]], [yylhs
.value
], [build
])[],[
1086 /* If YYLEN is nonzero, implement the default value of the action:
1087 `$$ = $1'. Otherwise, use the top of the stack.
1089 Otherwise, the following line sets YYLHS.VALUE to garbage.
1090 This behavior is undocumented and Bison
1091 users should not rely upon it. */
1093 yylhs
.value
= yystack_@
{yylen
- 1@
}.value
;
1095 yylhs
.value
= yystack_@
{0@
}.value
;])[
1097 // Compute the default @@$.
1099 slice
<stack_symbol_type
, stack_type
> slice (yystack_
, yylen
);
1100 YYLLOC_DEFAULT (yylhs
.location
, slice
, yylen
);
1103 // Perform the reduction.
1104 YY_REDUCE_PRINT (yyn
);
1111 // Compute post-reduction state.
1113 yystate
= yypgoto_
[yyn
- yyntokens_
] + yystack_
[yylen
].state
;
1114 if (0 <= yystate
&& yystate
<= yylast_
1115 && yycheck_
[yystate
] == yystack_
[yylen
].state
)
1116 yystate
= yytable_
[yystate
];
1118 yystate
= yydefgoto_
[yyn
- yyntokens_
];
1119 yylhs
.state
= yystate
;
1120 YY_SYMBOL_PRINT ("-> $$ =", yylhs
);
1122 // Destroy the lhs symbols.
1123 for (int i
= 0; i
< yylen
; ++i
)
1124 // Destroy a variant which value may have be swapped with
1125 // yylhs.value. The value of yylhs.value (hence maybe one of
1126 // these lhs symbols) depends on what does the default
1127 // contruction for this type. In the case of pointers for
1128 // instance, nothing is done, so the value is junk. Therefore
1129 // do not try to report the content in the debug trace, it's
1130 // junk. Hence yymsg = 0. Besides, that keeps exactly the same
1131 // traces as with the other Bison skeletons.
1132 yy_destroy_ (0, yystack_
[i
]);]])[
1138 /* Shift the result of the reduction. */
1142 /*------------------------------------.
1143 | yyerrlab -- here on detecting error |
1144 `------------------------------------*/
1146 /* If not already recovering from an error, report this error. */
1150 error (yyla
.location
, yysyntax_error_ (yystate
, yyla
.type
));
1153 yyerror_range
[0].location
= yyla
.location
;
1154 if (yyerrstatus_
== 3)
1156 /* If just tried and failed to reuse lookahead token after an
1157 error, discard it. */
1159 if (yychar
<= yyeof_
)
1161 /* Return failure if at end of input. */
1162 if (yychar
== yyeof_
)
1167 yy_destroy_ ("Error: discarding", yyla
);
1172 /* Else will try to reuse lookahead token after shifting the error
1177 /*---------------------------------------------------.
1178 | yyerrorlab -- error raised explicitly by YYERROR. |
1179 `---------------------------------------------------*/
1182 /* Pacify compilers like GCC when the user code never invokes
1183 YYERROR and the label yyerrorlab therefore never appears in user
1188 yyerror_range
[0].location
= yystack_
[yylen
- 1].location
;
1189 /* Do not reclaim the symbols of the rule which action triggered
1193 yystate
= yystack_
[0].state
;
1196 /*-------------------------------------------------------------.
1197 | yyerrlab1 -- common code for both syntax error and YYERROR. |
1198 `-------------------------------------------------------------*/
1200 yyerrstatus_
= 3; /* Each real token shifted decrements this. */
1202 stack_symbol_type error_token
;
1205 yyn
= yypact_
[yystate
];
1206 if (yyn
!= yypact_ninf_
)
1209 if (0 <= yyn
&& yyn
<= yylast_
&& yycheck_
[yyn
] == yyterror_
)
1211 yyn
= yytable_
[yyn
];
1217 // Pop the current state because it cannot handle the error token.
1218 if (yystack_
.size () == 1)
1221 yyerror_range
[0].location
= yystack_
[0].location
;
1222 yy_destroy_ ("Error: popping", yystack_
[0]);
1224 yystate
= yystack_
[0].state
;
1228 yyerror_range
[1].location
= yyla
.location
;
1229 YYLLOC_DEFAULT (error_token
.location
, (yyerror_range
- 1), 2);
1231 /* Shift the error token. */
1232 error_token
.state
= yystate
= yyn
;
1233 yypush_ ("Shifting", error_token
);
1248 if (yychar
!= yyempty_
)
1249 yy_destroy_ ("Cleanup: discarding lookahead", yyla
);
1251 /* Do not reclaim the symbols of the rule which action triggered
1252 this YYABORT or YYACCEPT. */
1254 while (yystack_
.size () != 1)
1256 yy_destroy_ ("Cleanup: popping", yystack_
[0]);
1263 // Generate an error message.
1265 ]b4_parser_class_name
[::yysyntax_error_ (int yystate
, int]dnl
1266 b4_error_verbose_if([ tok
])[)
1271 int yyn
= yypact_
[yystate
];
1272 if (yypact_ninf_
< yyn
&& yyn
<= yylast_
)
1274 /* Start YYX at -YYN if negative to avoid negative indexes in
1276 int yyxbegin
= yyn
< 0 ? -yyn
: 0;
1278 /* Stay within bounds of both yycheck and yytname. */
1279 int yychecklim
= yylast_
- yyn
+ 1;
1280 int yyxend
= yychecklim
< yyntokens_
? yychecklim
: yyntokens_
;
1282 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
1283 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
)
1286 // FIXME: This method of building the message is not compatible
1287 // with internationalization. It should work like yacc.c does it.
1288 // That is, first build a string that looks like this:
1289 // "syntax error, unexpected %s or %s or %s"
1290 // Then, invoke YY_ on this string.
1291 // Finally, use the string as a format to output
1292 // yytname_[tok], etc.
1293 // Until this gets fixed, this message appears in English only.
1294 res
= "syntax error, unexpected ";
1295 res
+= yytnamerr_ (yytname_
[tok
]);
1299 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
1300 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
)
1302 res
+= (!count
++) ? ", expecting " : " or ";
1303 res
+= yytnamerr_ (yytname_
[x
]);
1309 res
= YY_("syntax error");
1314 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
1316 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) b4_parser_class_name::yypact_ninf_
= b4_pact_ninf
[;
1317 ]b4_table_define([pact
], [b4_pact
])[;
1319 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
1320 doesn't specify something else to do. Zero means the default is an
1322 ]b4_table_define([defact
], [b4_defact
])[;
1324 /* YYPGOTO[NTERM-NUM]. */
1325 ]b4_table_define([pgoto
], [b4_pgoto
])[;
1327 /* YYDEFGOTO[NTERM-NUM]. */
1328 ]b4_table_define([defgoto
], [b4_defgoto
])[;
1330 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
1331 positive, shift that token. If negative, reduce the rule which
1332 number is the opposite. If zero, do what YYDEFACT says. */
1333 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) b4_parser_class_name::yytable_ninf_
= b4_table_ninf
[;
1334 ]b4_table_define([table
], [b4_table
])[;
1337 ]b4_table_define([check
], [b4_check
])[;
1339 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
1340 symbol of state STATE-NUM. */
1341 ]b4_table_define([stos
], [b4_stos
])[;
1344 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding
1346 ]b4_table_define([token_number
], [b4_toknum
])[;
1349 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
1350 ]b4_table_define([r1
], [b4_r1
])[;
1352 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
1353 ]b4_table_define([r2
], [b4_r2
])[;
1355 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
1356 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
1357 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
1359 const ]b4_parser_class_name
[::yytname_
[] =
1366 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
1367 ]b4_table_define([rline
], [b4_rline
])[;
1369 // Print the state stack on the debug stream.
1371 ]b4_parser_class_name
[::yystack_print_ ()
1373 *yycdebug_
<< "Stack now";
1374 for (stack_type::const_iterator
1375 i
= yystack_
.begin (),
1376 i_end
= yystack_
.end ();
1378 *yycdebug_
<< ' ' << i
->state
;
1379 *yycdebug_
<< std::endl
;
1382 // Report on the debug stream that the rule \a yyrule is going to be reduced.
1384 ]b4_parser_class_name
[::yy_reduce_print_ (int yyrule
)
1386 unsigned int yylno
= yyrline_
[yyrule
];
1387 int yynrhs
= yyr2_
[yyrule
];
1388 /* Print the symbols being reduced, and their result. */
1389 *yycdebug_
<< "Reducing stack by rule " << yyrule
- 1
1390 << " (line " << yylno
<< "):" << std::endl
;
1391 /* The symbols being reduced. */
1392 for (int yyi
= 0; yyi
< yynrhs
; yyi
++)
1393 YY_SYMBOL_PRINT (" $" << yyi
+ 1 << " =",
1394 ]b4_rhs_data(yynrhs
, yyi
+ 1)[);
1398 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
1399 ]b4_parser_class_name
[::token_number_type
1400 ]b4_parser_class_name
[::yytranslate_ (int t
)
1403 const token_number_type
1408 if ((unsigned int) t
<= yyuser_token_number_max_
)
1409 return translate_table
[t
];
1411 return yyundef_token_
;
1414 const int ]b4_parser_class_name
[::yyeof_
= 0;
1415 const int ]b4_parser_class_name
[::yylast_
= ]b4_last
[;
1416 const int ]b4_parser_class_name
[::yynnts_
= ]b4_nterms_number
[;
1417 const int ]b4_parser_class_name
[::yyempty_
= -2;
1418 const int ]b4_parser_class_name
[::yyfinal_
= ]b4_final_state_number
[;
1419 const int ]b4_parser_class_name
[::yyterror_
= 1;
1420 const int ]b4_parser_class_name
[::yyerrcode_
= 256;
1421 const int ]b4_parser_class_name
[::yyntokens_
= ]b4_tokens_number
[;
1423 const unsigned int ]b4_parser_class_name
[::yyuser_token_number_max_
= ]b4_user_token_number_max
[;
1424 const ]b4_parser_class_name
[::token_number_type
]b4_parser_class_name
[::yyundef_token_
= ]b4_undef_token_number
[;
1426 ]b4_namespace_close
[
1429 @
output(b4_dir_prefix
[]stack
.hh@
)@
1430 b4_copyright([Stack handling
for Bison parsers in C
++])[
1432 #ifndef BISON_STACK_HH
1433 # define BISON_STACK_HH
1438 template <class T
, class S
= std::deque
<T
> >
1443 // Hide our reversed order.
1444 typedef typename
S::reverse_iterator iterator
;
1445 typedef typename
S::const_reverse_iterator const_iterator
;
1451 stack (unsigned int n
) : seq_ (n
)
1457 operator [] (unsigned int i
)
1464 operator [] (unsigned int i
) const
1473 seq_
.push_front (t
);
1478 pop (unsigned int n
= 1)
1485 typename
S::size_type
1488 return seq_
.size ();
1491 inline const_iterator
begin () const { return seq_
.rbegin (); }
1492 inline const_iterator
end () const { return seq_
.rend (); }
1495 /// The wrapped container.
1499 /// Present a slice of the top of a stack.
1500 template <class T
, class S
= stack
<T
> >
1505 slice (const S
& stack
,
1506 unsigned int range
) : stack_ (stack
),
1513 operator [] (unsigned int i
) const
1515 return stack_
[range_
- i
];
1521 unsigned int range_
;
1523 ]b4_namespace_close
[
1525 #endif // not BISON_STACK_HH[]dnl
1528 m4_popdef([b4_copyright_years
])dnl