]> git.saurik.com Git - bison.git/blob - data/lalr1.cc
c0403fbc1488141af2eb854240c32b2fc82d5cd2
[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 # Stack parameters.
25 m4_define_default([b4_stack_depth_init], [200])
26
27 # Default Parser class name.
28 m4_define_default([b4_parser_class_name], [Parser])
29
30
31
32 ## ----------------- ##
33 ## Semantic Values. ##
34 ## ----------------- ##
35
36
37 # b4_lhs_value([TYPE])
38 # --------------------
39 # Expansion of $<TYPE>$.
40 m4_define([b4_lhs_value],
41 [yyval[]m4_ifval([$1], [.$1])])
42
43
44 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
45 # --------------------------------------
46 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
47 # symbols on RHS.
48 m4_define([b4_rhs_value],
49 [semantic_stack_@{m4_eval([$1 - $2])@}m4_ifval([$3], [.$3])])
50
51 m4_define_default([b4_location_type], [Location])
52
53 # b4_lhs_location()
54 # -----------------
55 # Expansion of @$.
56 m4_define([b4_lhs_location],
57 [yyloc])
58
59
60 # b4_rhs_location(RULE-LENGTH, NUM)
61 # ---------------------------------
62 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
63 # on RHS.
64 m4_define([b4_rhs_location],
65 [location_stack_@{m4_eval([$1 - $2])@}])
66
67
68 m4_define([b4_inherit],
69 [m4_ifdef([b4_root],
70 [: public b4_root
71 ],
72 [])])
73
74 m4_define([b4_param],
75 [m4_ifdef([b4_root],
76 [,
77 const Param& param],
78 [])])
79
80 m4_define([b4_constructor],
81 [m4_ifdef([b4_root],
82 [b4_root (param),
83 ],
84 [])])
85
86
87 # We do want M4 expansion after # for CPP macros.
88 m4_changecom()
89 m4_divert(0)dnl
90 m4_if(b4_defines_flag, 0, [],
91 [@output @output_header_name@
92 b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
93 [2002, 2003])[
94 /* FIXME: This is wrong, we want computed header guards.
95 I don't know why the macros are missing now. :( */
96 #ifndef PARSER_HEADER_H
97 # define PARSER_HEADER_H
98
99 #include "stack.hh"
100 #include "location.hh"
101
102 #include <string>
103 #include <iostream>
104
105 /* Using locations. */
106 #define YYLSP_NEEDED ]b4_locations_flag[
107
108 ]b4_token_defines(b4_tokens)[
109
110 /* Copy the first part of user declarations. */
111 ]b4_pre_prologue[
112
113 ]/* Line __line__ of lalr1.cc. */
114 b4_syncline([@oline@], [@ofile@])[
115
116 /* Enabling traces. */
117 #ifndef YYDEBUG
118 # define YYDEBUG ]b4_debug[
119 #endif
120
121 /* Enabling verbose error message. */
122 #ifndef YYERROR_VERBOSE
123 # define YYERROR_VERBOSE ]b4_error_verbose[
124 #endif
125
126 #ifndef YYSTYPE
127 ]m4_ifdef([b4_stype],
128 [b4_syncline([b4_stype_line], [b4_filename])
129 typedef union b4_stype yystype;
130 /* Line __line__ of lalr1.cc. */
131 b4_syncline([@oline@], [@ofile@])],
132 [typedef int yystype;])[
133 # define YYSTYPE yystype
134 #endif
135
136 /* Copy the second part of user declarations. */
137 ]b4_post_prologue[
138
139 ]/* Line __line__ of lalr1.cc. */
140 b4_syncline([@oline@], [@ofile@])[
141 #ifndef YYLLOC_DEFAULT
142 # define YYLLOC_DEFAULT(Current, Rhs, N) \
143 Current.last_line = Rhs[N].last_line; \
144 Current.last_column = Rhs[N].last_column;
145 #endif
146
147 namespace yy
148 {
149 class ]b4_parser_class_name[;
150
151 template < typename P >
152 struct Traits
153 {
154 };
155
156 template < >
157 struct Traits< ]b4_parser_class_name[ >
158 {
159 typedef ]b4_int_type_for([b4_translate])[ TokenNumberType;
160 typedef ]b4_int_type_for([b4_rhs])[ RhsNumberType;
161 typedef int StateType;
162 typedef yystype SemanticType;
163 typedef ]b4_location_type[ LocationType;
164 };
165 }
166
167 namespace yy
168 {
169 class ]b4_parser_class_name b4_inherit[
170 {
171 public:
172
173 typedef Traits< ]b4_parser_class_name[ >::TokenNumberType TokenNumberType;
174 typedef Traits< ]b4_parser_class_name[ >::RhsNumberType RhsNumberType;
175 typedef Traits< ]b4_parser_class_name[ >::StateType StateType;
176 typedef Traits< ]b4_parser_class_name[ >::SemanticType SemanticType;
177 typedef Traits< ]b4_parser_class_name[ >::LocationType LocationType;
178
179 typedef Stack< StateType > StateStack;
180 typedef Stack< SemanticType > SemanticStack;
181 typedef Stack< LocationType > LocationStack;
182
183 #if YYLSP_NEEDED
184 ]b4_parser_class_name[ (bool debug,
185 LocationType initlocation][]b4_param[) :
186 ]b4_constructor[][debug_ (debug),
187 cdebug_ (std::cerr),
188 initlocation_ (initlocation)
189 #else
190 ]b4_parser_class_name[ (bool debug][]b4_param[) :
191 ]b4_constructor[][debug_ (debug),
192 cdebug_ (std::cerr)
193 #endif
194 {
195 }
196
197 virtual ~]b4_parser_class_name[ ()
198 {
199 }
200
201 virtual int parse ();
202
203 private:
204
205 virtual void lex_ ();
206 virtual void error_ ();
207 virtual void print_ ();
208
209 /* Stacks. */
210 StateStack state_stack_;
211 SemanticStack semantic_stack_;
212 LocationStack location_stack_;
213
214 /* Tables. */
215 static const ]b4_int_type_for([b4_pact])[ pact_[];
216 static const ]b4_int_type_for([b4_pact])[ pact_ninf_;
217 static const ]b4_int_type_for([b4_defact])[ defact_[];
218 static const ]b4_int_type_for([b4_pgoto])[ pgoto_[];
219 static const ]b4_int_type_for([b4_defgoto])[ defgoto_[];
220 static const ]b4_int_type_for([b4_table])[ table_[];
221 static const ]b4_int_type_for([b4_table])[ table_ninf_;
222 static const ]b4_int_type_for([b4_check])[ check_[];
223 static const ]b4_int_type_for([b4_r1])[ r1_[];
224 static const ]b4_int_type_for([b4_r2])[ r2_[];
225
226 #if YYDEBUG || YYERROR_VERBOSE
227 static const char* const name_[];
228 #endif
229
230 /* More tables, for debugging. */
231 #if YYDEBUG
232 static const RhsNumberType rhs_[];
233 static const ]b4_int_type_for([b4_prhs])[ prhs_[];
234 static const ]b4_int_type_for([b4_rline])[ rline_[];
235 static const ]b4_int_type_for([b4_stos])[ stos_[];
236 static const ]b4_int_type_for([b4_toknum])[ token_number_[];
237 #endif
238
239 /* Even more tables. */
240 static inline TokenNumberType translate_ (int token);
241
242 /* Constants. */
243 static const int eof_;
244 /* LAST_ -- Last index in TABLE_. */
245 static const int last_;
246 static const int nnts_;
247 static const int empty_;
248 static const int final_;
249 static const int terror_;
250 static const int errcode_;
251 static const int ntokens_;
252 static const int initdepth_;
253 static const unsigned user_token_number_max_;
254 static const TokenNumberType undef_token_;
255
256 /* State. */
257 int n_;
258 int len_;
259 int state_;
260
261 /* Debugging. */
262 int debug_;
263 std::ostream &cdebug_;
264
265 /* Lookahead and lookahead in internal form. */
266 int looka_;
267 int ilooka_;
268
269 /* Message. */
270 std::string message;
271
272 /* Semantic value and location of lookahead token. */
273 SemanticType value;
274 LocationType location;
275
276 /* @@$ and $$. */
277 SemanticType yyval;
278 LocationType yyloc;
279
280 /* Initial location. */
281 LocationType initlocation_;
282 };
283 }
284
285 #endif /* ! defined PARSER_HEADER_H */]
286 ])dnl
287 @output @output_parser_name@
288 b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
289 [2002, 2003])
290
291 m4_if(b4_defines_flag, 0, [], [#include @output_header_name@])[
292
293 /* Enable debugging if requested. */
294 #if YYDEBUG
295 # define YYCDEBUG if (debug_) cdebug_
296 #else /* !YYDEBUG */
297 # define YYCDEBUG if (0) cdebug_
298 #endif /* !YYDEBUG */
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 YYCDEBUG << "Reducing via rule " << n_ - 1
433 << " (line " << rline_[n_] << "), ";
434 for (]b4_int_type_for([b4_prhs])[ i = prhs_[n_];
435 0 <= rhs_[i]; ++i)
436 YYCDEBUG << name_[rhs_[i]] << ' ';
437 YYCDEBUG << "-> " << name_[r1_[n_]] << std::endl;
438 }
439 #endif
440
441 if (len_)
442 {
443 Slice< LocationType, LocationStack > slice (location_stack_, len_);
444 YYLLOC_DEFAULT (yyloc, slice, len_);
445 }
446
447 switch (n_)
448 {
449 ]b4_actions[
450 }
451
452 ]/* Line __line__ of lalr1.cc. */
453 b4_syncline([@oline@], [@ofile@])[
454
455 state_stack_.pop (len_);
456 semantic_stack_.pop (len_);
457 location_stack_.pop (len_);
458
459 #if YYDEBUG
460 if (debug_)
461 {
462 YYCDEBUG << "state stack now";
463 for (StateStack::ConstIterator i = state_stack_.begin ();
464 i != state_stack_.end (); ++i)
465 YYCDEBUG << ' ' << *i;
466 YYCDEBUG << std::endl;
467 }
468 #endif
469
470 semantic_stack_.push (yyval);
471 location_stack_.push (yyloc);
472
473 /* Shift the result of the reduction. */
474 n_ = r1_[n_];
475 state_ = pgoto_[n_ - ntokens_] + state_stack_[0];
476 if (0 <= state_ && state_ <= last_ && check_[state_] == state_stack_[0])
477 state_ = table_[state_];
478 else
479 state_ = defgoto_[n_ - ntokens_];
480 goto yynewstate;
481
482 /* Report and recover from errors. This is very incomplete. */
483 yyerrlab:
484 /* If not already recovering from an error, report this error. */
485 if (!errstatus)
486 {
487 ++nerrs;
488
489 #if YYERROR_VERBOSE
490 n_ = pact_[state_];
491 if (pact_ninf_ < n_ && n_ < last_)
492 {
493 message = "syntax error, unexpected ";
494 message += name_[ilooka_];
495 {
496 int count = 0;
497 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
498 if (check_[x + n_] == x && x != terror_)
499 ++count;
500 if (count < 5)
501 {
502 count = 0;
503 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
504 if (check_[x + n_] == x && x != terror_)
505 {
506 message += (!count++) ? ", expecting " : " or ";
507 message += name_[x];
508 }
509 }
510 }
511 }
512 else
513 #endif
514 message = "syntax error";
515 error_ ();
516 }
517 goto yyerrlab1;
518
519 /* Error raised explicitly by an action. */
520 yyerrlab1:
521 if (errstatus == 3)
522 {
523 /* If just tried and failed to reuse lookahead token after an
524 error, discard it. */
525
526 /* Return failure if at end of input. */
527 if (looka_ == eof_)
528 goto yyabortlab;
529 YYCDEBUG << "Discarding token " << looka_
530 << " (" << name_[ilooka_] << ")." << std::endl;
531 looka_ = empty_;
532 }
533
534 /* Else will try to reuse lookahead token after shifting the error
535 token. */
536
537 errstatus = 3;
538
539 for (;;)
540 {
541 n_ = pact_[state_];
542 if (n_ != pact_ninf_)
543 {
544 n_ += terror_;
545 if (0 <= n_ && n_ <= last_ && check_[n_] == terror_)
546 {
547 n_ = table_[n_];
548 if (0 < n_)
549 break;
550 }
551 }
552
553 /* Pop the current state because it cannot handle the error token. */
554 if (state_stack_.height () == 1)
555 goto yyabortlab;
556
557 #if YYDEBUG
558 if (debug_)
559 {
560 if (stos_[state_] < ntokens_)
561 {
562 YYCDEBUG << "Error: popping token "
563 << token_number_[stos_[state_]]
564 << " (" << name_[stos_[state_]];
565 # ifdef YYPRINT
566 YYPRINT (stderr, token_number_[stos_[state_]],
567 semantic_stack_.top ());
568 # endif
569 YYCDEBUG << ')' << std::endl;
570 }
571 else
572 {
573 YYCDEBUG << "Error: popping nonterminal ("
574 << name_[stos_[state_]] << ')' << std::endl;
575 }
576 }
577 #endif
578
579 state_ = (state_stack_.pop (), state_stack_[0]);
580 semantic_stack_.pop ();
581 location_stack_.pop ();;
582
583 #if YYDEBUG
584 if (debug_)
585 {
586 YYCDEBUG << "Error: state stack now";
587 for (StateStack::ConstIterator i = state_stack_.begin ();
588 i != state_stack_.end (); ++i)
589 YYCDEBUG << ' ' << *i;
590 YYCDEBUG << std::endl;
591 }
592 #endif
593 }
594
595 if (n_ == final_)
596 goto yyacceptlab;
597
598 YYCDEBUG << "Shifting error token, ";
599
600 semantic_stack_.push (value);
601 location_stack_.push (location);
602
603 state_ = n_;
604 goto yynewstate;
605
606 /* Accept. */
607 yyacceptlab:
608 return 0;
609
610 /* Abort. */
611 yyabortlab:
612 return 1;
613 }
614
615 void
616 yy::]b4_parser_class_name[::lex_ ()
617 {
618 #if YYLSP_NEEDED
619 looka_ = yylex (&value, &location);
620 #else
621 looka_ = yylex (&value);
622 #endif
623 }
624
625 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
626 STATE-NUM. */
627 const ]b4_int_type_for([b4_pact]) yy::b4_parser_class_name::pact_ninf_ = b4_pact_ninf[;
628 const ]b4_int_type_for([b4_pact])[
629 yy::]b4_parser_class_name[::pact_[] =
630 {
631 ]b4_pact[
632 };
633
634 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
635 doesn't specify something else to do. Zero means the default is an
636 error. */
637 const ]b4_int_type_for([b4_defact])[
638 yy::]b4_parser_class_name[::defact_[] =
639 {
640 ]b4_defact[
641 };
642
643 /* YYPGOTO[NTERM-NUM]. */
644 const ]b4_int_type_for([b4_pgoto])[
645 yy::]b4_parser_class_name[::pgoto_[] =
646 {
647 ]b4_pgoto[
648 };
649
650 /* YYDEFGOTO[NTERM-NUM]. */
651 const ]b4_int_type_for([b4_defgoto])[
652 yy::]b4_parser_class_name[::defgoto_[] =
653 {
654 ]b4_defgoto[
655 };
656
657 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
658 positive, shift that token. If negative, reduce the rule which
659 number is the opposite. If zero, do what YYDEFACT says. */
660 const ]b4_int_type_for([b4_table]) yy::b4_parser_class_name::table_ninf_ = b4_table_ninf[;
661 const ]b4_int_type_for([b4_table])[
662 yy::]b4_parser_class_name[::table_[] =
663 {
664 ]b4_table[
665 };
666
667 /* YYCHECK. */
668 const ]b4_int_type_for([b4_check])[
669 yy::]b4_parser_class_name[::check_[] =
670 {
671 ]b4_check[
672 };
673
674 #if YYDEBUG
675 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
676 symbol of state STATE-NUM. */
677 const ]b4_int_type_for([b4_stos])[
678 yy::]b4_parser_class_name[::stos_[] =
679 {
680 ]b4_stos[
681 };
682
683 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
684 to YYLEX-NUM. */
685 const ]b4_int_type_for([b4_toknum])[
686 yy::]b4_parser_class_name[::token_number_[] =
687 {
688 ]b4_toknum[
689 };
690 #endif
691
692 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
693 const ]b4_int_type_for([b4_r1])[
694 yy::]b4_parser_class_name[::r1_[] =
695 {
696 ]b4_r1[
697 };
698
699 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
700 const ]b4_int_type_for([b4_r2])[
701 yy::]b4_parser_class_name[::r2_[] =
702 {
703 ]b4_r2[
704 };
705
706 #if YYDEBUG || YYERROR_VERBOSE
707 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
708 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
709 const char*
710 const yy::]b4_parser_class_name[::name_[] =
711 {
712 ]b4_tname[
713 };
714 #endif
715
716 #if YYDEBUG
717 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
718 const yy::]b4_parser_class_name[::RhsNumberType
719 yy::]b4_parser_class_name[::rhs_[] =
720 {
721 ]b4_rhs[
722 };
723
724 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
725 YYRHS. */
726 const ]b4_int_type_for([b4_prhs])[
727 yy::]b4_parser_class_name[::prhs_[] =
728 {
729 ]b4_prhs[
730 };
731
732 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
733 const ]b4_int_type_for([b4_rline])[
734 yy::]b4_parser_class_name[::rline_[] =
735 {
736 ]b4_rline[
737 };
738 #endif
739
740 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
741 yy::]b4_parser_class_name[::TokenNumberType
742 yy::]b4_parser_class_name[::translate_ (int token)
743 {
744 static
745 const TokenNumberType
746 translate_[] =
747 {
748 ]b4_translate[
749 };
750 if ((unsigned) token <= user_token_number_max_)
751 return translate_[token];
752 else
753 return undef_token_;
754 }
755
756 const int yy::]b4_parser_class_name[::eof_ = 0;
757 const int yy::]b4_parser_class_name[::last_ = ]b4_last[;
758 const int yy::]b4_parser_class_name[::nnts_ = ]b4_nterms_number[;
759 const int yy::]b4_parser_class_name[::empty_ = -2;
760 const int yy::]b4_parser_class_name[::final_ = ]b4_final_state_number[;
761 const int yy::]b4_parser_class_name[::terror_ = 1;
762 const int yy::]b4_parser_class_name[::errcode_ = 256;
763 const int yy::]b4_parser_class_name[::ntokens_ = ]b4_tokens_number[;
764 const int yy::]b4_parser_class_name[::initdepth_ = ]b4_stack_depth_init[;
765
766 const unsigned yy::]b4_parser_class_name[::user_token_number_max_ = ]b4_user_token_number_max[;
767 const yy::]b4_parser_class_name[::TokenNumberType yy::]b4_parser_class_name[::undef_token_ = ]b4_undef_token_number[;
768
769 ]b4_epilogue
770 dnl
771 @output stack.hh
772 b4_copyright([2002, 2003])[
773
774 #ifndef BISON_STACK_HH
775 # define BISON_STACK_HH
776
777 #include <deque>
778
779 namespace yy
780 {
781 template < class T, class S = std::deque< T > >
782 class Stack
783 {
784 public:
785
786 typedef typename S::iterator Iterator;
787 typedef typename S::const_iterator ConstIterator;
788
789 Stack () : seq_ ()
790 {
791 }
792
793 Stack (unsigned n) : seq_ (n)
794 {
795 }
796
797 inline
798 T&
799 operator [] (unsigned index)
800 {
801 return seq_[index];
802 }
803
804 inline
805 const T&
806 operator [] (unsigned index) const
807 {
808 return seq_[index];
809 }
810
811 inline
812 void
813 push (const T& t)
814 {
815 seq_.push_front (t);
816 }
817
818 inline
819 void
820 pop (unsigned n = 1)
821 {
822 for (; n; --n)
823 seq_.pop_front ();
824 }
825
826 inline
827 unsigned
828 height () const
829 {
830 return seq_.size ();
831 }
832
833 inline ConstIterator begin () const { return seq_.begin (); }
834 inline ConstIterator end () const { return seq_.end (); }
835
836 private:
837
838 S seq_;
839 };
840
841 template < class T, class S = Stack< T > >
842 class Slice
843 {
844 public:
845
846 Slice (const S& stack,
847 unsigned range) : stack_ (stack),
848 range_ (range)
849 {
850 }
851
852 inline
853 const T&
854 operator [] (unsigned index) const
855 {
856 return stack_[range_ - index];
857 }
858
859 private:
860
861 const S& stack_;
862 unsigned range_;
863 };
864 }
865
866 #endif // not BISON_STACK_HH]
867 dnl
868 @output location.hh
869 b4_copyright([2002, 2003])[
870
871 #ifndef BISON_LOCATION_HH
872 # define BISON_LOCATION_HH
873
874 namespace yy
875 {
876 struct Position
877 {
878 int line;
879 int column;
880 };
881
882 struct Location
883 {
884 Position first;
885 Position last;
886 };
887 }
888
889 #endif // not BISON_LOCATION_HH]