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