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