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