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