]> git.saurik.com Git - bison.git/blob - data/lalr1.cc
2c38cdf3adf6340132a90bed2e8721fe5bf5fb50
[bison.git] / data / lalr1.cc
1 # C++ skeleton for Bison
2
3 # Copyright (C) 2002-2015 Free Software Foundation, Inc.
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 m4_include(b4_pkgdatadir/[c++.m4])
19
20 # api.value.type=variant is valid.
21 m4_define([b4_value_type_setup_variant])
22
23 # b4_integral_parser_table_declare(TABLE-NAME, CONTENT, COMMENT)
24 # --------------------------------------------------------------
25 # Declare "parser::yy<TABLE-NAME>_" whose contents is CONTENT.
26 m4_define([b4_integral_parser_table_declare],
27 [m4_ifval([$3], [b4_comment([$3], [ ])
28 ])dnl
29 static const b4_int_type_for([$2]) yy$1_[[]];dnl
30 ])
31
32 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
33 # -------------------------------------------------------------
34 # Define "parser::yy<TABLE-NAME>_" whose contents is CONTENT.
35 m4_define([b4_integral_parser_table_define],
36 [ const b4_int_type_for([$2])
37 b4_parser_class_name::yy$1_[[]] =
38 {
39 $2
40 };dnl
41 ])
42
43 # b4_symbol_value_template(VAL, [TYPE])
44 # -------------------------------------
45 # Same as b4_symbol_value, but used in a template method. It makes
46 # a difference when using variants. Note that b4_value_type_setup_union
47 # overrides b4_symbol_value, so we must override it again.
48 m4_copy([b4_symbol_value], [b4_symbol_value_template])
49 m4_append([b4_value_type_setup_union],
50 [m4_copy_force([b4_symbol_value_union], [b4_symbol_value_template])])
51
52 # b4_lhs_value([TYPE])
53 # --------------------
54 # Expansion of $<TYPE>$.
55 m4_define([b4_lhs_value],
56 [b4_symbol_value([yylhs.value], [$1])])
57
58
59 # b4_lhs_location()
60 # -----------------
61 # Expansion of @$.
62 m4_define([b4_lhs_location],
63 [yylhs.location])
64
65
66 # b4_rhs_data(RULE-LENGTH, NUM)
67 # -----------------------------
68 # Return the data corresponding to the symbol #NUM, where the current
69 # rule has RULE-LENGTH symbols on RHS.
70 m4_define([b4_rhs_data],
71 [yystack_@{b4_subtract($@)@}])
72
73
74 # b4_rhs_state(RULE-LENGTH, NUM)
75 # ------------------------------
76 # The state corresponding to the symbol #NUM, where the current
77 # rule has RULE-LENGTH symbols on RHS.
78 m4_define([b4_rhs_state],
79 [b4_rhs_data([$1], [$2]).state])
80
81
82 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
83 # --------------------------------------
84 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
85 # symbols on RHS.
86 m4_define([b4_rhs_value],
87 [b4_symbol_value([b4_rhs_data([$1], [$2]).value], [$3])])
88
89
90 # b4_rhs_location(RULE-LENGTH, NUM)
91 # ---------------------------------
92 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
93 # on RHS.
94 m4_define([b4_rhs_location],
95 [b4_rhs_data([$1], [$2]).location])
96
97
98 # b4_symbol_action(SYMBOL-NUM, KIND)
99 # ----------------------------------
100 # Run the action KIND (destructor or printer) for SYMBOL-NUM.
101 # Same as in C, but using references instead of pointers.
102 m4_define([b4_symbol_action],
103 [b4_symbol_if([$1], [has_$2],
104 [m4_pushdef([b4_symbol_value], m4_defn([b4_symbol_value_template]))[]dnl
105 b4_dollar_pushdef([yysym.value],
106 b4_symbol_if([$1], [has_type],
107 [m4_dquote(b4_symbol([$1], [type]))]),
108 [yysym.location])dnl
109 b4_symbol_case_([$1])
110 b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"])
111 b4_symbol([$1], [$2])
112 b4_syncline([@oline@], [@ofile@])
113 break;
114
115 m4_popdef([b4_symbol_value])[]dnl
116 b4_dollar_popdef[]dnl
117 ])])
118
119
120 # b4_lex
121 # ------
122 # Call yylex.
123 m4_define([b4_lex],
124 [b4_token_ctor_if(
125 [b4_function_call([yylex],
126 [symbol_type], m4_ifdef([b4_lex_param], b4_lex_param))],
127 [b4_function_call([yylex], [int],
128 [b4_api_PREFIX[STYPE*], [&yyla.value]][]dnl
129 b4_locations_if([, [[location*], [&yyla.location]]])dnl
130 m4_ifdef([b4_lex_param], [, ]b4_lex_param))])])
131
132
133 m4_pushdef([b4_copyright_years],
134 [2002-2015])
135
136 m4_define([b4_parser_class_name],
137 [b4_percent_define_get([[parser_class_name]])])
138
139 b4_bison_locations_if([# Backward compatibility.
140 m4_define([b4_location_constructors])
141 m4_include(b4_pkgdatadir/[location.cc])])
142 m4_include(b4_pkgdatadir/[stack.hh])
143 b4_variant_if([m4_include(b4_pkgdatadir/[variant.hh])])
144
145 # b4_shared_declarations
146 # ----------------------
147 # Declaration that might either go into the header (if --defines)
148 # or open coded in the parser body.
149 m4_define([b4_shared_declarations],
150 [b4_percent_code_get([[requires]])[
151 ]b4_parse_assert_if([# include <cassert>])[
152 # include <vector>
153 # include <iostream>
154 # include <stdexcept>
155 # include <string>]b4_defines_if([[
156 # include "stack.hh"
157 ]b4_bison_locations_if([[# include "location.hh"]])])[
158 ]b4_variant_if([b4_variant_includes])[
159
160 ]b4_attribute_define[
161 ]b4_YYDEBUG_define[
162
163 ]b4_namespace_open[
164
165 ]b4_defines_if([],
166 [b4_stack_define
167 b4_bison_locations_if([b4_position_define
168 b4_location_define])])[
169
170 ]b4_variant_if([b4_variant_define])[
171
172 /// A Bison parser.
173 class ]b4_parser_class_name[
174 {
175 public:
176 ]b4_public_types_declare[
177 /// Build a parser object.
178 ]b4_parser_class_name[ (]b4_parse_param_decl[);
179 virtual ~]b4_parser_class_name[ ();
180
181 /// Parse.
182 /// \returns 0 iff parsing succeeded.
183 virtual int parse ();
184
185 #if ]b4_api_PREFIX[DEBUG
186 /// The current debugging stream.
187 std::ostream& debug_stream () const YY_ATTRIBUTE_PURE;
188 /// Set the current debugging stream.
189 void set_debug_stream (std::ostream &);
190
191 /// Type for debugging levels.
192 typedef int debug_level_type;
193 /// The current debugging level.
194 debug_level_type debug_level () const YY_ATTRIBUTE_PURE;
195 /// Set the current debugging level.
196 void set_debug_level (debug_level_type l);
197 #endif
198
199 /// Report a syntax error.]b4_locations_if([[
200 /// \param loc where the syntax error is found.]])[
201 /// \param msg a description of the syntax error.
202 virtual void error (]b4_locations_if([[const location_type& loc, ]])[const std::string& msg);
203
204 /// Report a syntax error.
205 void error (const syntax_error& err);
206
207 private:
208 /// This class is not copyable.
209 ]b4_parser_class_name[ (const ]b4_parser_class_name[&);
210 ]b4_parser_class_name[& operator= (const ]b4_parser_class_name[&);
211
212 /// State numbers.
213 typedef int state_type;
214
215 /// Generate an error message.
216 /// \param yystate the state where the error occurred.
217 /// \param yytoken the lookahead token type, or yyempty_.
218 virtual std::string yysyntax_error_ (state_type yystate,
219 symbol_number_type yytoken) const;
220
221 /// Compute post-reduction state.
222 /// \param yystate the current state
223 /// \param yysym the nonterminal to push on the stack
224 state_type yy_lr_goto_state_ (state_type yystate, int yysym);
225
226 /// Whether the given \c yypact_ value indicates a defaulted state.
227 /// \param yyvalue the value to check
228 static bool yy_pact_value_is_default_ (int yyvalue);
229
230 /// Whether the given \c yytable_ value indicates a syntax error.
231 /// \param yyvalue the value to check
232 static bool yy_table_value_is_error_ (int yyvalue);
233
234 static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_;
235 static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ yytable_ninf_;
236
237 /// Convert a scanner token number \a t to a symbol number.
238 static token_number_type yytranslate_ (]b4_token_ctor_if([token_type], [int])[ t);
239
240 // Tables.
241 ]b4_parser_tables_declare[]b4_error_verbose_if([
242
243 /// Convert the symbol name \a n to a form suitable for a diagnostic.
244 static std::string yytnamerr_ (const char *n);])[
245
246 ]b4_token_table_if([], [[#if ]b4_api_PREFIX[DEBUG]])[
247 /// For a symbol, its name in clear.
248 static const char* const yytname_[];
249 ]b4_token_table_if([[#if ]b4_api_PREFIX[DEBUG]])[
250 ]b4_integral_parser_table_declare([rline], [b4_rline],
251 [[YYRLINE[YYN] -- Source line where rule number YYN was defined.]])[
252 /// Report on the debug stream that the rule \a r is going to be reduced.
253 virtual void yy_reduce_print_ (int r);
254 /// Print the state stack on the debug stream.
255 virtual void yystack_print_ ();
256
257 // Debugging.
258 int yydebug_;
259 std::ostream* yycdebug_;
260
261 /// \brief Display a symbol type, value and location.
262 /// \param yyo The output stream.
263 /// \param yysym The symbol.
264 template <typename Base>
265 void yy_print_ (std::ostream& yyo, const basic_symbol<Base>& yysym) const;
266 #endif
267
268 /// \brief Reclaim the memory associated to a symbol.
269 /// \param yymsg Why this token is reclaimed.
270 /// If null, print nothing.
271 /// \param yysym The symbol.
272 template <typename Base>
273 void yy_destroy_ (const char* yymsg, basic_symbol<Base>& yysym) const;
274
275 private:
276 /// Type access provider for state based symbols.
277 struct by_state
278 {
279 /// Default constructor.
280 by_state ();
281
282 /// The symbol type as needed by the constructor.
283 typedef state_type kind_type;
284
285 /// Constructor.
286 by_state (kind_type s);
287
288 /// Copy constructor.
289 by_state (const by_state& other);
290
291 /// Steal the symbol type from \a that.
292 void move (by_state& that);
293
294 /// The (internal) type number (corresponding to \a state).
295 /// \a empty when empty.
296 symbol_number_type type_get () const;
297
298 /// The state number used to denote an empty symbol.
299 enum { empty = 0 };
300
301 /// The state.
302 /// \a empty when empty.
303 state_type state;
304 };
305
306 /// "Internal" symbol: element of the stack.
307 struct stack_symbol_type : basic_symbol<by_state>
308 {
309 /// Superclass.
310 typedef basic_symbol<by_state> super_type;
311 /// Construct an empty symbol.
312 stack_symbol_type ();
313 /// Steal the contents from \a sym to build this.
314 stack_symbol_type (state_type s, symbol_type& sym);
315 /// Assignment, needed by push_back.
316 stack_symbol_type& operator= (const stack_symbol_type& that);
317 };
318
319 /// Stack type.
320 typedef stack<stack_symbol_type> stack_type;
321
322 /// The stack.
323 stack_type yystack_;
324
325 /// Push a new state on the stack.
326 /// \param m a debug message to display
327 /// if null, no trace is output.
328 /// \param s the symbol
329 /// \warning the contents of \a s.value is stolen.
330 void yypush_ (const char* m, stack_symbol_type& s);
331
332 /// Push a new look ahead token on the state on the stack.
333 /// \param m a debug message to display
334 /// if null, no trace is output.
335 /// \param s the state
336 /// \param sym the symbol (for its value and location).
337 /// \warning the contents of \a s.value is stolen.
338 void yypush_ (const char* m, state_type s, symbol_type& sym);
339
340 /// Pop \a n symbols the three stacks.
341 void yypop_ (unsigned int n = 1);
342
343 /// Constants.
344 enum
345 {
346 yyeof_ = 0,
347 yylast_ = ]b4_last[, ///< Last index in yytable_.
348 yynnts_ = ]b4_nterms_number[, ///< Number of nonterminal symbols.
349 yyempty_ = -2,
350 yyfinal_ = ]b4_final_state_number[, ///< Termination state number.
351 yyterror_ = 1,
352 yyerrcode_ = 256,
353 yyntokens_ = ]b4_tokens_number[ ///< Number of tokens.
354 };
355
356 ]b4_parse_param_vars[
357 };
358
359 ]b4_token_ctor_if([b4_yytranslate_define
360 b4_public_types_define])[
361 ]b4_namespace_close[
362
363 ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
364 [b4_token_defines
365
366 #ifndef ]b4_api_PREFIX[STYPE
367 // Redirection for backward compatibility.
368 # define ]b4_api_PREFIX[STYPE b4_namespace_ref::b4_parser_class_name::semantic_type
369 #endif
370 ])[
371 ]b4_percent_code_get([[provides]])[
372 ]])
373
374 b4_defines_if(
375 [b4_output_begin([b4_spec_defines_file])
376 b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++])
377 [
378 /**
379 ** \file ]b4_spec_defines_file[
380 ** Define the ]b4_namespace_ref[::parser class.
381 */
382
383 // C++ LALR(1) parser skeleton written by Akim Demaille.
384
385 ]b4_cpp_guard_open([b4_spec_defines_file])[
386 ]b4_shared_declarations[
387 ]b4_cpp_guard_close([b4_spec_defines_file])
388 b4_output_end()
389 ])
390
391
392 b4_output_begin([b4_parser_file_name])
393 b4_copyright([Skeleton implementation for Bison LALR(1) parsers in C++])
394 b4_percent_code_get([[top]])[]dnl
395 m4_if(b4_prefix, [yy], [],
396 [
397 // Take the name prefix into account.
398 #define yylex b4_prefix[]lex])[
399
400 // First part of user declarations.
401 ]b4_user_pre_prologue[
402
403 ]b4_null_define[
404
405 ]b4_defines_if([[#include "@basename(]b4_spec_defines_file[@)"]],
406 [b4_shared_declarations])[
407
408 // User implementation prologue.
409 ]b4_user_post_prologue[
410 ]b4_percent_code_get[
411
412 #ifndef YY_
413 # if defined YYENABLE_NLS && YYENABLE_NLS
414 # if ENABLE_NLS
415 # include <libintl.h> // FIXME: INFRINGES ON USER NAME SPACE.
416 # define YY_(msgid) dgettext ("bison-runtime", msgid)
417 # endif
418 # endif
419 # ifndef YY_
420 # define YY_(msgid) msgid
421 # endif
422 #endif
423
424 ]b4_locations_if([dnl
425 [#define YYRHSLOC(Rhs, K) ((Rhs)[K].location)
426 ]b4_yylloc_default_define])[
427
428 // Suppress unused-variable warnings by "using" E.
429 #define YYUSE(E) ((void) (E))
430
431 // Enable debugging if requested.
432 #if ]b4_api_PREFIX[DEBUG
433
434 // A pseudo ostream that takes yydebug_ into account.
435 # define YYCDEBUG if (yydebug_) (*yycdebug_)
436
437 # define YY_SYMBOL_PRINT(Title, Symbol) \
438 do { \
439 if (yydebug_) \
440 { \
441 *yycdebug_ << Title << ' '; \
442 yy_print_ (*yycdebug_, Symbol); \
443 *yycdebug_ << std::endl; \
444 } \
445 } while (false)
446
447 # define YY_REDUCE_PRINT(Rule) \
448 do { \
449 if (yydebug_) \
450 yy_reduce_print_ (Rule); \
451 } while (false)
452
453 # define YY_STACK_PRINT() \
454 do { \
455 if (yydebug_) \
456 yystack_print_ (); \
457 } while (false)
458
459 #else // !]b4_api_PREFIX[DEBUG
460
461 # define YYCDEBUG if (false) std::cerr
462 # define YY_SYMBOL_PRINT(Title, Symbol) YYUSE(Symbol)
463 # define YY_REDUCE_PRINT(Rule) static_cast<void>(0)
464 # define YY_STACK_PRINT() static_cast<void>(0)
465
466 #endif // !]b4_api_PREFIX[DEBUG
467
468 #define yyerrok (yyerrstatus_ = 0)
469 #define yyclearin (yyempty = true)
470
471 #define YYACCEPT goto yyacceptlab
472 #define YYABORT goto yyabortlab
473 #define YYERROR goto yyerrorlab
474 #define YYRECOVERING() (!!yyerrstatus_)
475
476 ]b4_namespace_open[]b4_error_verbose_if([[
477
478 /* Return YYSTR after stripping away unnecessary quotes and
479 backslashes, so that it's suitable for yyerror. The heuristic is
480 that double-quoting is unnecessary unless the string contains an
481 apostrophe, a comma, or backslash (other than backslash-backslash).
482 YYSTR is taken from yytname. */
483 std::string
484 ]b4_parser_class_name[::yytnamerr_ (const char *yystr)
485 {
486 if (*yystr == '"')
487 {
488 std::string yyr = "";
489 char const *yyp = yystr;
490
491 for (;;)
492 switch (*++yyp)
493 {
494 case '\'':
495 case ',':
496 goto do_not_strip_quotes;
497
498 case '\\':
499 if (*++yyp != '\\')
500 goto do_not_strip_quotes;
501 // Fall through.
502 default:
503 yyr += *yyp;
504 break;
505
506 case '"':
507 return yyr;
508 }
509 do_not_strip_quotes: ;
510 }
511
512 return yystr;
513 }
514 ]])[
515
516 /// Build a parser object.
517 ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
518 :])[
519 #if ]b4_api_PREFIX[DEBUG
520 ]m4_ifset([b4_parse_param], [ ], [ :])[yydebug_ (false),
521 yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
522 #endif]b4_parse_param_cons[
523 {}
524
525 ]b4_parser_class_name::~b4_parser_class_name[ ()
526 {}
527
528
529 /*---------------.
530 | Symbol types. |
531 `---------------*/
532
533 ]b4_token_ctor_if([], [b4_public_types_define])[
534
535 // by_state.
536 inline
537 ]b4_parser_class_name[::by_state::by_state ()
538 : state (empty)
539 {}
540
541 inline
542 ]b4_parser_class_name[::by_state::by_state (const by_state& other)
543 : state (other.state)
544 {}
545
546 inline
547 void
548 ]b4_parser_class_name[::by_state::move (by_state& that)
549 {
550 state = that.state;
551 that.state = empty;
552 }
553
554 inline
555 ]b4_parser_class_name[::by_state::by_state (state_type s)
556 : state (s)
557 {}
558
559 inline
560 ]b4_parser_class_name[::symbol_number_type
561 ]b4_parser_class_name[::by_state::type_get () const
562 {
563 return state == empty ? 0 : yystos_[state];
564 }
565
566 inline
567 ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type ()
568 {}
569
570
571 inline
572 ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type (state_type s, symbol_type& that)
573 : super_type (s]b4_locations_if([, that.location])[)
574 {
575 ]b4_variant_if([b4_symbol_variant([that.type_get ()],
576 [value], [move], [that.value])],
577 [[value = that.value;]])[
578 // that is emptied.
579 that.type = empty;
580 }
581
582 inline
583 ]b4_parser_class_name[::stack_symbol_type&
584 ]b4_parser_class_name[::stack_symbol_type::operator= (const stack_symbol_type& that)
585 {
586 state = that.state;
587 ]b4_variant_if([b4_symbol_variant([that.type_get ()],
588 [value], [copy], [that.value])],
589 [[value = that.value;]])[]b4_locations_if([
590 location = that.location;])[
591 return *this;
592 }
593
594
595 template <typename Base>
596 inline
597 void
598 ]b4_parser_class_name[::yy_destroy_ (const char* yymsg, basic_symbol<Base>& yysym) const
599 {
600 if (yymsg)
601 YY_SYMBOL_PRINT (yymsg, yysym);]b4_variant_if([], [
602
603 // User destructor.
604 b4_symbol_actions([destructor], [yysym.type_get ()])])[
605 }
606
607 #if ]b4_api_PREFIX[DEBUG
608 template <typename Base>
609 void
610 ]b4_parser_class_name[::yy_print_ (std::ostream& yyo,
611 const basic_symbol<Base>& yysym) const
612 {
613 std::ostream& yyoutput = yyo;
614 YYUSE (yyoutput);
615 symbol_number_type yytype = yysym.type_get ();
616 yyo << (yytype < yyntokens_ ? "token" : "nterm")
617 << ' ' << yytname_[yytype] << " ("]b4_locations_if([
618 << yysym.location << ": "])[;
619 ]b4_symbol_actions([printer])[
620 yyo << ')';
621 }
622 #endif
623
624 inline
625 void
626 ]b4_parser_class_name[::yypush_ (const char* m, state_type s, symbol_type& sym)
627 {
628 stack_symbol_type t (s, sym);
629 yypush_ (m, t);
630 }
631
632 inline
633 void
634 ]b4_parser_class_name[::yypush_ (const char* m, stack_symbol_type& s)
635 {
636 if (m)
637 YY_SYMBOL_PRINT (m, s);
638 yystack_.push (s);
639 }
640
641 inline
642 void
643 ]b4_parser_class_name[::yypop_ (unsigned int n)
644 {
645 yystack_.pop (n);
646 }
647
648 #if ]b4_api_PREFIX[DEBUG
649 std::ostream&
650 ]b4_parser_class_name[::debug_stream () const
651 {
652 return *yycdebug_;
653 }
654
655 void
656 ]b4_parser_class_name[::set_debug_stream (std::ostream& o)
657 {
658 yycdebug_ = &o;
659 }
660
661
662 ]b4_parser_class_name[::debug_level_type
663 ]b4_parser_class_name[::debug_level () const
664 {
665 return yydebug_;
666 }
667
668 void
669 ]b4_parser_class_name[::set_debug_level (debug_level_type l)
670 {
671 yydebug_ = l;
672 }
673 #endif // ]b4_api_PREFIX[DEBUG
674
675 inline ]b4_parser_class_name[::state_type
676 ]b4_parser_class_name[::yy_lr_goto_state_ (state_type yystate, int yysym)
677 {
678 int yyr = yypgoto_[yysym - yyntokens_] + yystate;
679 if (0 <= yyr && yyr <= yylast_ && yycheck_[yyr] == yystate)
680 return yytable_[yyr];
681 else
682 return yydefgoto_[yysym - yyntokens_];
683 }
684
685 inline bool
686 ]b4_parser_class_name[::yy_pact_value_is_default_ (int yyvalue)
687 {
688 return yyvalue == yypact_ninf_;
689 }
690
691 inline bool
692 ]b4_parser_class_name[::yy_table_value_is_error_ (int yyvalue)
693 {
694 return yyvalue == yytable_ninf_;
695 }
696
697 int
698 ]b4_parser_class_name[::parse ()
699 {
700 // State.
701 int yyn;
702 /// Length of the RHS of the rule being reduced.
703 int yylen = 0;
704
705 // Error handling.
706 int yynerrs_ = 0;
707 int yyerrstatus_ = 0;
708
709 /// Whether yyla contains a lookahead.
710 bool yyempty = true;
711
712 /// The lookahead symbol.
713 symbol_type yyla;]b4_locations_if([[
714
715 /// The locations where the error started and ended.
716 stack_symbol_type yyerror_range[3];]])[
717
718 /// The return value of parse ().
719 int yyresult;
720
721 // FIXME: This shoud be completely indented. It is not yet to
722 // avoid gratuitous conflicts when merging into the master branch.
723 try
724 {
725 YYCDEBUG << "Starting parse" << std::endl;
726
727 ]m4_ifdef([b4_initial_action], [
728 b4_dollar_pushdef([yyla.value], [], [yyla.location])dnl
729 // User initialization code.
730 b4_user_initial_action
731 b4_dollar_popdef])[]dnl
732
733 [ /* Initialize the stack. The initial state will be set in
734 yynewstate, since the latter expects the semantical and the
735 location values to have been already stored, initialize these
736 stacks with a primary value. */
737 yystack_.clear ();
738 yypush_ (YY_NULLPTR, 0, yyla);
739
740 // A new symbol was pushed on the stack.
741 yynewstate:
742 YYCDEBUG << "Entering state " << yystack_[0].state << std::endl;
743
744 // Accept?
745 if (yystack_[0].state == yyfinal_)
746 goto yyacceptlab;
747
748 goto yybackup;
749
750 // Backup.
751 yybackup:
752
753 // Try to take a decision without lookahead.
754 yyn = yypact_[yystack_[0].state];
755 if (yy_pact_value_is_default_ (yyn))
756 goto yydefault;
757
758 // Read a lookahead token.
759 if (yyempty)
760 {
761 YYCDEBUG << "Reading a token: ";
762 try
763 {]b4_token_ctor_if([[
764 symbol_type yylookahead (]b4_lex[);
765 yyla.move (yylookahead);]], [[
766 yyla.type = yytranslate_ (]b4_lex[);]])[
767 }
768 catch (const syntax_error& yyexc)
769 {
770 error (yyexc);
771 goto yyerrlab1;
772 }
773 yyempty = false;
774 }
775 YY_SYMBOL_PRINT ("Next token is", yyla);
776
777 /* If the proper action on seeing token YYLA.TYPE is to reduce or
778 to detect an error, take that action. */
779 yyn += yyla.type_get ();
780 if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yyla.type_get ())
781 goto yydefault;
782
783 // Reduce or error.
784 yyn = yytable_[yyn];
785 if (yyn <= 0)
786 {
787 if (yy_table_value_is_error_ (yyn))
788 goto yyerrlab;
789 yyn = -yyn;
790 goto yyreduce;
791 }
792
793 // Discard the token being shifted.
794 yyempty = true;
795
796 // Count tokens shifted since error; after three, turn off error status.
797 if (yyerrstatus_)
798 --yyerrstatus_;
799
800 // Shift the lookahead token.
801 yypush_ ("Shifting", yyn, yyla);
802 goto yynewstate;
803
804 /*-----------------------------------------------------------.
805 | yydefault -- do the default action for the current state. |
806 `-----------------------------------------------------------*/
807 yydefault:
808 yyn = yydefact_[yystack_[0].state];
809 if (yyn == 0)
810 goto yyerrlab;
811 goto yyreduce;
812
813 /*-----------------------------.
814 | yyreduce -- Do a reduction. |
815 `-----------------------------*/
816 yyreduce:
817 yylen = yyr2_[yyn];
818 {
819 stack_symbol_type yylhs;
820 yylhs.state = yy_lr_goto_state_(yystack_[yylen].state, yyr1_[yyn]);]b4_variant_if([
821 /* Variants are always initialized to an empty instance of the
822 correct type. The default '$$ = $1' action is NOT applied
823 when using variants. */
824 b4_symbol_variant([[yyr1_@{yyn@}]], [yylhs.value], [build])], [
825 /* If YYLEN is nonzero, implement the default value of the
826 action: '$$ = $1'. Otherwise, use the top of the stack.
827
828 Otherwise, the following line sets YYLHS.VALUE to garbage.
829 This behavior is undocumented and Bison users should not rely
830 upon it. */
831 if (yylen)
832 yylhs.value = yystack_@{yylen - 1@}.value;
833 else
834 yylhs.value = yystack_@{0@}.value;])[
835 ]b4_locations_if([dnl
836 [
837 // Compute the default @@$.
838 {
839 slice<stack_symbol_type, stack_type> slice (yystack_, yylen);
840 YYLLOC_DEFAULT (yylhs.location, slice, yylen);
841 }]])[
842
843 // Perform the reduction.
844 YY_REDUCE_PRINT (yyn);
845 try
846 {
847 switch (yyn)
848 {
849 ]b4_user_actions[
850 default:
851 break;
852 }
853 }
854 catch (const syntax_error& yyexc)
855 {
856 error (yyexc);
857 YYERROR;
858 }
859 YY_SYMBOL_PRINT ("-> $$ =", yylhs);
860 yypop_ (yylen);
861 yylen = 0;
862 YY_STACK_PRINT ();
863
864 // Shift the result of the reduction.
865 yypush_ (YY_NULLPTR, yylhs);
866 }
867 goto yynewstate;
868
869 /*--------------------------------------.
870 | yyerrlab -- here on detecting error. |
871 `--------------------------------------*/
872 yyerrlab:
873 // If not already recovering from an error, report this error.
874 if (!yyerrstatus_)
875 {
876 ++yynerrs_;
877 error (]b4_join(b4_locations_if([yyla.location]),
878 [[yysyntax_error_ (yystack_[0].state,
879 yyempty ? yyempty_ : yyla.type_get ())]])[);
880 }
881
882 ]b4_locations_if([[
883 yyerror_range[1].location = yyla.location;]])[
884 if (yyerrstatus_ == 3)
885 {
886 /* If just tried and failed to reuse lookahead token after an
887 error, discard it. */
888
889 // Return failure if at end of input.
890 if (yyla.type_get () == yyeof_)
891 YYABORT;
892 else if (!yyempty)
893 {
894 yy_destroy_ ("Error: discarding", yyla);
895 yyempty = true;
896 }
897 }
898
899 // Else will try to reuse lookahead token after shifting the error token.
900 goto yyerrlab1;
901
902
903 /*---------------------------------------------------.
904 | yyerrorlab -- error raised explicitly by YYERROR. |
905 `---------------------------------------------------*/
906 yyerrorlab:
907
908 /* Pacify compilers like GCC when the user code never invokes
909 YYERROR and the label yyerrorlab therefore never appears in user
910 code. */
911 if (false)
912 goto yyerrorlab;]b4_locations_if([[
913 yyerror_range[1].location = yystack_[yylen - 1].location;]])[
914 /* Do not reclaim the symbols of the rule whose action triggered
915 this YYERROR. */
916 yypop_ (yylen);
917 yylen = 0;
918 goto yyerrlab1;
919
920 /*-------------------------------------------------------------.
921 | yyerrlab1 -- common code for both syntax error and YYERROR. |
922 `-------------------------------------------------------------*/
923 yyerrlab1:
924 yyerrstatus_ = 3; // Each real token shifted decrements this.
925 {
926 stack_symbol_type error_token;
927 for (;;)
928 {
929 yyn = yypact_[yystack_[0].state];
930 if (!yy_pact_value_is_default_ (yyn))
931 {
932 yyn += yyterror_;
933 if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_)
934 {
935 yyn = yytable_[yyn];
936 if (0 < yyn)
937 break;
938 }
939 }
940
941 // Pop the current state because it cannot handle the error token.
942 if (yystack_.size () == 1)
943 YYABORT;
944 ]b4_locations_if([[
945 yyerror_range[1].location = yystack_[0].location;]])[
946 yy_destroy_ ("Error: popping", yystack_[0]);
947 yypop_ ();
948 YY_STACK_PRINT ();
949 }
950 ]b4_locations_if([[
951 yyerror_range[2].location = yyla.location;
952 YYLLOC_DEFAULT (error_token.location, yyerror_range, 2);]])[
953
954 // Shift the error token.
955 error_token.state = yyn;
956 yypush_ ("Shifting", error_token);
957 }
958 goto yynewstate;
959
960 // Accept.
961 yyacceptlab:
962 yyresult = 0;
963 goto yyreturn;
964
965 // Abort.
966 yyabortlab:
967 yyresult = 1;
968 goto yyreturn;
969
970 yyreturn:
971 if (!yyempty)
972 yy_destroy_ ("Cleanup: discarding lookahead", yyla);
973
974 /* Do not reclaim the symbols of the rule whose action triggered
975 this YYABORT or YYACCEPT. */
976 yypop_ (yylen);
977 while (1 < yystack_.size ())
978 {
979 yy_destroy_ ("Cleanup: popping", yystack_[0]);
980 yypop_ ();
981 }
982
983 return yyresult;
984 }
985 catch (...)
986 {
987 YYCDEBUG << "Exception caught: cleaning lookahead and stack"
988 << std::endl;
989 // Do not try to display the values of the reclaimed symbols,
990 // as their printer might throw an exception.
991 if (!yyempty)
992 yy_destroy_ (YY_NULLPTR, yyla);
993
994 while (1 < yystack_.size ())
995 {
996 yy_destroy_ (YY_NULLPTR, yystack_[0]);
997 yypop_ ();
998 }
999 throw;
1000 }
1001 }
1002
1003 void
1004 ]b4_parser_class_name[::error (const syntax_error& yyexc)
1005 {
1006 error (]b4_join(b4_locations_if([yyexc.location]),
1007 [[yyexc.what()]])[);
1008 }
1009
1010 // Generate an error message.
1011 std::string
1012 ]b4_parser_class_name[::yysyntax_error_ (]dnl
1013 b4_error_verbose_if([state_type yystate, symbol_number_type yytoken],
1014 [state_type, symbol_number_type])[) const
1015 {]b4_error_verbose_if([[
1016 // Number of reported tokens (one for the "unexpected", one per
1017 // "expected").
1018 size_t yycount = 0;
1019 // Its maximum.
1020 enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1021 // Arguments of yyformat.
1022 char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1023
1024 /* There are many possibilities here to consider:
1025 - If this state is a consistent state with a default action, then
1026 the only way this function was invoked is if the default action
1027 is an error action. In that case, don't check for expected
1028 tokens because there are none.
1029 - The only way there can be no lookahead present (in yytoken) is
1030 if this state is a consistent state with a default action.
1031 Thus, detecting the absence of a lookahead is sufficient to
1032 determine that there is no unexpected or expected token to
1033 report. In that case, just report a simple "syntax error".
1034 - Don't assume there isn't a lookahead just because this state is
1035 a consistent state with a default action. There might have
1036 been a previous inconsistent state, consistent state with a
1037 non-default action, or user semantic action that manipulated
1038 yyla. (However, yyla is currently not documented for users.)
1039 - Of course, the expected token list depends on states to have
1040 correct lookahead information, and it depends on the parser not
1041 to perform extra reductions after fetching a lookahead from the
1042 scanner and before detecting a syntax error. Thus, state
1043 merging (from LALR or IELR) and default reductions corrupt the
1044 expected token list. However, the list is correct for
1045 canonical LR with one exception: it will still contain any
1046 token that will not be accepted due to an error action in a
1047 later state.
1048 */
1049 if (yytoken != yyempty_)
1050 {
1051 yyarg[yycount++] = yytname_[yytoken];
1052 int yyn = yypact_[yystate];
1053 if (!yy_pact_value_is_default_ (yyn))
1054 {
1055 /* Start YYX at -YYN if negative to avoid negative indexes in
1056 YYCHECK. In other words, skip the first -YYN actions for
1057 this state because they are default actions. */
1058 int yyxbegin = yyn < 0 ? -yyn : 0;
1059 // Stay within bounds of both yycheck and yytname.
1060 int yychecklim = yylast_ - yyn + 1;
1061 int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_;
1062 for (int yyx = yyxbegin; yyx < yyxend; ++yyx)
1063 if (yycheck_[yyx + yyn] == yyx && yyx != yyterror_
1064 && !yy_table_value_is_error_ (yytable_[yyx + yyn]))
1065 {
1066 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1067 {
1068 yycount = 1;
1069 break;
1070 }
1071 else
1072 yyarg[yycount++] = yytname_[yyx];
1073 }
1074 }
1075 }
1076
1077 char const* yyformat = YY_NULLPTR;
1078 switch (yycount)
1079 {
1080 #define YYCASE_(N, S) \
1081 case N: \
1082 yyformat = S; \
1083 break
1084 YYCASE_(0, YY_("syntax error"));
1085 YYCASE_(1, YY_("syntax error, unexpected %s"));
1086 YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1087 YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1088 YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1089 YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1090 #undef YYCASE_
1091 }
1092
1093 std::string yyres;
1094 // Argument number.
1095 size_t yyi = 0;
1096 for (char const* yyp = yyformat; *yyp; ++yyp)
1097 if (yyp[0] == '%' && yyp[1] == 's' && yyi < yycount)
1098 {
1099 yyres += yytnamerr_ (yyarg[yyi++]);
1100 ++yyp;
1101 }
1102 else
1103 yyres += *yyp;
1104 return yyres;]], [[
1105 return YY_("syntax error");]])[
1106 }
1107
1108
1109 const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) b4_parser_class_name::yypact_ninf_ = b4_pact_ninf[;
1110
1111 const ]b4_int_type(b4_table_ninf, b4_table_ninf) b4_parser_class_name::yytable_ninf_ = b4_table_ninf[;
1112
1113 ]b4_parser_tables_define[
1114
1115 ]b4_token_table_if([], [[#if ]b4_api_PREFIX[DEBUG]])[
1116 // YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
1117 // First, the terminals, then, starting at \a yyntokens_, nonterminals.
1118 const char*
1119 const ]b4_parser_class_name[::yytname_[] =
1120 {
1121 ]b4_tname[
1122 };
1123
1124 ]b4_token_table_if([[#if ]b4_api_PREFIX[DEBUG]])[
1125 ]b4_integral_parser_table_define([rline], [b4_rline])[
1126
1127 // Print the state stack on the debug stream.
1128 void
1129 ]b4_parser_class_name[::yystack_print_ ()
1130 {
1131 *yycdebug_ << "Stack now";
1132 for (stack_type::const_iterator
1133 i = yystack_.begin (),
1134 i_end = yystack_.end ();
1135 i != i_end; ++i)
1136 *yycdebug_ << ' ' << i->state;
1137 *yycdebug_ << std::endl;
1138 }
1139
1140 // Report on the debug stream that the rule \a yyrule is going to be reduced.
1141 void
1142 ]b4_parser_class_name[::yy_reduce_print_ (int yyrule)
1143 {
1144 unsigned int yylno = yyrline_[yyrule];
1145 int yynrhs = yyr2_[yyrule];
1146 // Print the symbols being reduced, and their result.
1147 *yycdebug_ << "Reducing stack by rule " << yyrule - 1
1148 << " (line " << yylno << "):" << std::endl;
1149 // The symbols being reduced.
1150 for (int yyi = 0; yyi < yynrhs; yyi++)
1151 YY_SYMBOL_PRINT (" $" << yyi + 1 << " =",
1152 ]b4_rhs_data(yynrhs, yyi + 1)[);
1153 }
1154 #endif // ]b4_api_PREFIX[DEBUG
1155
1156 ]b4_token_ctor_if([], [b4_yytranslate_define])[
1157 ]b4_namespace_close[
1158 ]b4_epilogue[]dnl
1159 b4_output_end()
1160
1161
1162 m4_popdef([b4_copyright_years])dnl