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