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