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