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