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