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