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