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