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