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