]> git.saurik.com Git - bison.git/blob - data/bison.c++
* data/bison.c++: Make the user able to add members to the generated
[bison.git] / data / bison.c++
1 m4_divert(-1)
2
3 # m4_define_default(MACRO, VALUE)
4 # -------------------------------
5 # Define MACRO to VALUE, unless already defined.
6 m4_define([m4_define_default],
7 [m4_ifdef([$1], [], [m4_define($@)])])
8
9 m4_define_default([b4_input_suffix], [.y])
10
11 m4_define_default([b4_output_parser_suffix],
12 [m4_translit(b4_input_suffix, [yY], [cC])])
13
14 m4_define_default([b4_output_parser_name],
15 [b4_output_prefix[]b4_output_infix[]b4_output_parser_suffix[]])
16
17
18 m4_define_default([b4_output_header_suffix],
19 [m4_translit(b4_input_suffix, [yY], [hH])])
20
21 m4_define_default([b4_output_header_name],
22 [b4_output_prefix[]b4_output_infix[]b4_output_header_suffix[]])
23
24 m4_define_default([b4_header_guard],
25 [m4_bpatsubst(m4_toupper([BISON_]b4_output_header_name),
26 [[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]], [_])])
27
28 m4_define([b4_inherit],
29 [m4_ifdef([b4_root],
30 [: public b4_root
31 ],
32 [])])
33
34 m4_define([b4_param],
35 [m4_ifdef([b4_root],
36 [,
37 const Param& param],
38 [])])
39
40 m4_define([b4_constructor],
41 [m4_ifdef([b4_root],
42 [b4_root (param),
43 ],
44 [])])
45
46 m4_define([b4_copyright],
47 [/* -*- C++ -*- */
48 /* A Bison parser, made from b4_filename,
49 by GNU bison b4_version. */
50
51 /* Skeleton output parser for bison,
52 Copyright 2002 Free Software Foundation, Inc.
53
54 This program is free software; you can redistribute it and/or modify
55 it under the terms of the GNU General Public License as published by
56 the Free Software Foundation; either version 2, or (at your option)
57 any later version.
58
59 This program is distributed in the hope that it will be useful,
60 but WITHOUT ANY WARRANTY; without even the implied warranty of
61 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62 GNU General Public License for more details.
63
64 You should have received a copy of the GNU General Public License
65 along with this program; if not, write to the Free Software
66 Foundation, Inc., 59 Temple Place - Suite 330,
67 Boston, MA 02111-1307, USA. */
68
69 /* As a special exception, when this file is copied by Bison into a
70 Bison output file, you may use that output file without restriction.
71 This special exception was added by the Free Software Foundation
72 in version 1.24 of Bison. */])
73
74 m4_divert(0)dnl
75 #output "b4_output_header_name"
76 b4_copyright
77 #ifndef b4_header_guard
78 # define b4_header_guard
79
80 #include "stack.hh"
81 #include "location.hh"
82
83 #include <string>
84
85 /* Using locations. */
86 #define YYLSP_NEEDED b4_locations_flag
87
88 b4_prologue
89
90 /* Tokens. */
91 b4_tokendef
92
93 /* Enabling traces. */
94 #ifndef YYDEBUG
95 # define YYDEBUG b4_debug
96 #endif
97
98 /* Enabling verbose error message. */
99 #ifndef YYERROR_VERBOSE
100 # define YYERROR_VERBOSE b4_error_verbose
101 #endif
102
103 #ifndef YYSTYPE
104 typedef b4_stype
105 yystype;
106 # define YYSTYPE yystype
107 #endif
108
109 #ifndef YYLLOC_DEFAULT
110 # define YYLLOC_DEFAULT(Current, Rhs, N) \
111 Current.last_line = Rhs[[N]].last_line; \
112 Current.last_column = Rhs[[N]].last_column;
113 #endif
114
115 m4_if(b4_locations_flag, [0], [],
116 [#ifndef YYLTYPE
117 typedef struct yyltype
118 {
119 int first_line;
120 int first_column;
121 int last_line;
122 int last_column;
123 } yyltype;
124 # define YYLTYPE yyltype
125 #endif])
126
127 namespace yy
128 {
129 class b4_name;
130
131 template < typename P >
132 struct Traits
133 {
134 };
135
136 template < >
137 struct Traits< b4_name >
138 {
139 typedef int StateType;
140 typedef yystype SemanticType;
141 typedef b4_ltype LocationType;
142 };
143 }
144
145 namespace yy
146 {
147 class b4_name b4_inherit
148 {
149 public:
150
151 typedef Traits< b4_name >::StateType StateType;
152 typedef Traits< b4_name >::SemanticType SemanticType;
153 typedef Traits< b4_name >::LocationType LocationType;
154
155 typedef Stack< StateType > StateStack;
156 typedef Stack< SemanticType > SemanticStack;
157 typedef Stack< LocationType > LocationStack;
158
159 #if YYLSP_NEEDED
160 b4_name (bool debug,
161 LocationType initlocation[]b4_param) :
162 b4_constructor[]debug_ (debug),
163 initlocation_ (initlocation)
164 #else
165 b4_name (bool debug[]b4_param) :
166 b4_constructor[]debug_ (debug)
167 #endif
168 {
169 }
170
171 virtual ~b4_name ()
172 {
173 }
174
175 virtual int parse ();
176
177 private:
178
179 virtual void lex_ ();
180 virtual void error_ ();
181 virtual void print_ ();
182
183 /* Stacks. */
184 StateStack state_stack_;
185 SemanticStack semantic_stack_;
186 LocationStack location_stack_;
187
188 /* Tables. */
189 static const short pact_[[]];
190 static const short defact_[[]];
191 static const short pgoto_[[]];
192 static const short defgoto_[[]];
193 static const short table_[[]];
194 static const short check_[[]];
195 static const short r1_[[]];
196 static const short r2_[[]];
197
198 #if YYDEBUG || YYERROR_VERBOSE
199 static const char* const name_[[]];
200 #endif
201
202 /* More tables, for debugging. */
203 #if YYDEBUG
204 static const short rhs_[[]];
205 static const short prhs_[[]];
206 static const short rline_[[]];
207 #endif
208
209 /* Even more tables. */
210 static inline char translate_ (int token);
211
212 /* Constants. */
213 static const int eof_;
214 static const int last_;
215 static const int flag_;
216 static const int nnts_;
217 static const int nsym_;
218 static const int empty_;
219 static const int final_;
220 static const int terror_;
221 static const int errcode_;
222 static const int ntokens_;
223 static const int initdepth_;
224 static const unsigned maxtok_;
225
226 /* State. */
227 int n_;
228 int len_;
229 int debug_;
230 int state_;
231
232 /* Lookahead and lookahead in internal form. */
233 int looka_;
234 int ilooka_;
235
236 /* Message. */
237 std::string message;
238
239 /* Semantic value and location of lookahead token. */
240 SemanticType value;
241 LocationType location;
242
243 /* @$ and $$. */
244 SemanticType yyval;
245 LocationType yyloc;
246
247 /* Initial location. */
248 LocationType initlocation_;
249 };
250 }
251
252 #endif /* not b4_header_guard */
253
254 #output "b4_output_prefix[]b4_output_infix[].cc"
255 b4_copyright
256
257 #include "b4_output_header_name"
258
259 /* Enable debugging if requested. */
260 #if YYDEBUG
261 # include <cstdio>
262 # define YYFPRINTF std::fprintf
263 # define YYDPRINTF(Args) \
264 do { \
265 if (debug_) \
266 YYFPRINTF Args; \
267 } while (0)
268 #else /* !YYDEBUG */
269 # define YYDPRINTF(Args)
270 #endif /* !YYDEBUG */
271
272 int
273 yy::b4_name::parse ()
274 {
275 int nerrs = 0;
276 int errstatus = 0;
277
278 /* Initialize stack. */
279 state_stack_ = StateStack (0);
280 semantic_stack_ = SemanticStack (1);
281 location_stack_ = LocationStack (1);
282
283 /* Reserve initial space. The C parser needed that, but is it really
284 useful here? */
285 state_stack_.reserve (initdepth_);
286 semantic_stack_.reserve (initdepth_);
287 location_stack_.reserve (initdepth_);
288
289 /* Start. */
290 state_ = 0;
291 looka_ = empty_;
292 #if YYLSP_NEEDED
293 location = initlocation_;
294 #endif
295 YYDPRINTF ((stderr, "Starting parse\n"));
296
297 /* New state. */
298 yynewstate:
299 state_stack_.push (state_);
300 YYDPRINTF ((stderr, "Entering state %d\n", state_));
301 goto yybackup;
302
303 /* Backup. */
304 yybackup:
305
306 /* Try to take a decision without lookahead. */
307 n_ = pact_[[state_]];
308 if (n_ == flag_)
309 goto yydefault;
310
311 /* Read a lookahead token. */
312 if (looka_ == empty_)
313 {
314 YYDPRINTF ((stderr, "Reading a token: "));
315 lex_ ();
316 }
317
318 /* Convert token to internal form. */
319 if (looka_ <= 0)
320 {
321 looka_ = eof_;
322 ilooka_ = 0;
323 YYDPRINTF ((stderr, "Now at end of input.\n"));
324 }
325 else
326 {
327 ilooka_ = translate_ (looka_);
328 #if YYDEBUG
329 if (debug_)
330 {
331 YYFPRINTF (stderr, "Next token is %d (%s", looka_, name_[[ilooka_]]);
332 print_ ();
333 YYFPRINTF (stderr, ")\n");
334 }
335 #endif
336 }
337
338 n_ += ilooka_;
339 if (n_ < 0 || n_ > last_ || check_[[n_]] != ilooka_)
340 goto yydefault;
341
342 /* Reduce or error. */
343 n_ = table_[[n_]];
344 if (n_ < 0)
345 {
346 if (n_ == flag_)
347 goto yyerrlab;
348 else
349 {
350 n_ = -n_;
351 goto yyreduce;
352 }
353 }
354 else if (n_ == 0)
355 goto yyerrlab;
356
357 /* Accept? */
358 if (n_ == final_)
359 goto yyacceptlab;
360
361 /* Shift the lookahead token. */
362 YYDPRINTF ((stderr, "Shifting token %d (%s), ", looka_, name_[[ilooka_]]));
363
364 /* Discard the token being shifted unless it is eof. */
365 if (looka_ != eof_)
366 looka_ = empty_;
367
368 semantic_stack_.push (value);
369 location_stack_.push (location);
370
371 /* Count tokens shifted since error; after three, turn off error
372 status. */
373 if (errstatus)
374 --errstatus;
375
376 state_ = n_;
377 goto yynewstate;
378
379 /* Default action. */
380 yydefault:
381 n_ = defact_[[state_]];
382 if (n_ == 0)
383 goto yyerrlab;
384 goto yyreduce;
385
386 /* Reduce. */
387 yyreduce:
388 len_ = r2_[[n_]];
389 if (len_)
390 {
391 yyval = semantic_stack_[[1 - len_]];
392 yyloc = location_stack_[[1 - len_]];
393 }
394 else
395 {
396 yyval = semantic_stack_[[0]];
397 yyloc = location_stack_[[0]];
398 }
399
400 #if YYDEBUG
401 if (debug_)
402 {
403 YYFPRINTF (stderr, "Reducing via rule %d (line %d), ", n_ - 1, rline_[[n_]]);
404 for (unsigned i = prhs_[[n_]];
405 rhs_[[i]] >= 0; ++i)
406 YYFPRINTF (stderr, "%s ", name_[[rhs_[i]]]);
407 YYFPRINTF (stderr, "-> %s\n", name_[[r1_[n_]]]);
408 }
409 #endif
410
411 if (len_)
412 {
413 Slice< LocationType, LocationStack > slice (location_stack_, len_);
414 YYLLOC_DEFAULT (yyloc, slice, len_);
415 }
416
417 {
418 SemanticStack& yyvsp (semantic_stack_);
419 LocationStack& yylsp (location_stack_);
420
421 switch (n_)
422 {
423 b4_actions
424 }
425 }
426
427 state_stack_.pop (len_);
428 semantic_stack_.pop (len_);
429 location_stack_.pop (len_);
430
431 #if YYDEBUG
432 if (debug_)
433 {
434 YYFPRINTF (stderr, "state stack now");
435 for (StateStack::ConstIterator i = state_stack_.begin ();
436 i != state_stack_.end (); ++i)
437 YYFPRINTF (stderr, " %d", *i);
438 YYFPRINTF (stderr, "\n");
439 }
440 #endif
441
442 semantic_stack_.push (yyval);
443 location_stack_.push (yyloc);
444
445 /* Shift the result of the reduction. */
446 n_ = r1_[[n_]];
447 state_ = pgoto_[[n_ - ntokens_]] + state_stack_[[0]];
448 if (state_ >= 0 && state_ <= last_ && check_[[state_]] == state_stack_[[0]])
449 state_ = table_[[state_]];
450 else
451 state_ = defgoto_[[n_ - ntokens_]];
452 goto yynewstate;
453
454 /* Report and recover from errors. This is very incomplete. */
455 yyerrlab:
456 /* If not already recovering from an error, report this error. */
457 if (!errstatus)
458 {
459 ++nerrs;
460
461 #if YYERROR_VERBOSE
462 n_ = pact_[[state_]];
463 if (n_ > flag_ && n_ < last_)
464 {
465 message = "parse error, unexpected ";
466 message += name_[[ilooka_]];
467 {
468 int count = 0;
469 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
470 if (check_[[x + n_]] == x)
471 ++count;
472 if (count < 5)
473 {
474 count = 0;
475 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
476 if (check_[[x + n_]] == x)
477 {
478 message += (!count++) ? ", expecting " : " or ";
479 message += name_[[x]];
480 }
481 }
482 }
483 }
484 else
485 #endif
486 message = "parse error";
487 error_ ();
488 }
489 goto yyerrlab1;
490
491 /* Error raised explicitly by an action. */
492 yyerrlab1:
493 if (errstatus == 3)
494 {
495 /* If just tried and failed to reuse lookahead token after an
496 error, discard it. */
497
498 /* Return failure if at end of input. */
499 if (looka_ == eof_)
500 goto yyabortlab;
501 YYDPRINTF ((stderr, "Discarding token %d (%s).\n", looka_, name_[[ilooka_]]));
502 looka_ = empty_;
503 }
504
505 /* Else will try to reuse lookahead token after shifting the error
506 token. */
507
508 errstatus = 3;
509 goto yyerrhandle;
510
511 /* Pop the current state because it cannot handle the error token. */
512 yyerrdefault:
513 yyerrpop:
514 if (!state_stack_.height ())
515 goto yyabortlab;
516 state_ = (state_stack_.pop (), state_stack_[[0]]);
517 semantic_stack_.pop ();
518 location_stack_.pop ();;
519
520 #if YYDEBUG
521 if (debug_)
522 {
523 YYFPRINTF (stderr, "Error: state stack now");
524 for (StateStack::ConstIterator i = state_stack_.begin ();
525 i != state_stack_.end (); ++i)
526 YYFPRINTF (stderr, " %d", *i);
527 YYFPRINTF (stderr, "\n");
528 }
529 #endif
530
531 yyerrhandle:
532 n_ = pact_[[state_]];
533 if (n_ == flag_)
534 goto yyerrdefault;
535
536 n_ += terror_;
537 if (n_ < 0 || n_ > last_ || check_[[n_]] != terror_)
538 goto yyerrdefault;
539
540 n_ = table_[[n_]];
541 if (n_ < 0)
542 {
543 if (n_ == flag_)
544 goto yyerrpop;
545 n_ = -n_;
546 goto yyreduce;
547 }
548 else if (!n_)
549 goto yyerrpop;
550
551 if (n_ == final_)
552 goto yyacceptlab;
553
554 YYDPRINTF ((stderr, "Shifting error token, "));
555
556 semantic_stack_.push (value);
557 location_stack_.push (location);
558
559 state_ = n_;
560 goto yynewstate;
561
562 /* Accept. */
563 yyacceptlab:
564 return 0;
565
566 /* Abort. */
567 yyabortlab:
568 return 1;
569 }
570
571 void
572 yy::b4_name::lex_ ()
573 {
574 #if YYLSP_NEEDED
575 looka_ = yylex (&value, &location);
576 #else
577 looka_ = yylex (&value);
578 #endif
579 }
580
581 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
582 STATE-NUM. */
583 const short
584 yy::b4_name::pact_[[]] =
585 {
586 b4_pact
587 };
588
589 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
590 doesn't specify something else to do. Zero means the default is an
591 error. */
592 const short
593 yy::b4_name::defact_[[]] =
594 {
595 b4_defact
596 };
597
598 /* YYPGOTO[[NTERM-NUM]]. */
599 const short
600 yy::b4_name::pgoto_[[]] =
601 {
602 b4_pgoto
603 };
604
605 /* YYDEFGOTO[[NTERM-NUM]]. */
606 const short
607 yy::b4_name::defgoto_[[]] =
608 {
609 b4_defgoto
610 };
611
612 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
613 positive, shift that token. If negative, reduce the rule which
614 number is the opposite. If zero, do what YYDEFACT says. */
615 const short
616 yy::b4_name::table_[[]] =
617 {
618 b4_table
619 };
620
621 /* YYCHECK. */
622 const short
623 yy::b4_name::check_[[]] =
624 {
625 b4_check
626 };
627
628 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
629 const short
630 yy::b4_name::r1_[[]] =
631 {
632 b4_r1
633 };
634
635 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
636 const short
637 yy::b4_name::r2_[[]] =
638 {
639 b4_r2
640 };
641
642 #if YYDEBUG || YYERROR_VERBOSE
643 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
644 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
645 const char*
646 const yy::b4_name::name_[[]] =
647 {
648 b4_tname
649 };
650 #endif
651
652 #if YYDEBUG
653 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
654 const short
655 yy::b4_name::rhs_[[]] =
656 {
657 b4_rhs
658 };
659
660 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
661 YYRHS. */
662 const short
663 yy::b4_name::prhs_[[]] =
664 {
665 b4_prhs
666 };
667
668 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
669 const short
670 yy::b4_name::rline_[[]] =
671 {
672 b4_rline
673 };
674 #endif
675
676 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
677 char
678 yy::b4_name::translate_ (int token)
679 {
680 static
681 const char
682 translate_[[]] =
683 {
684 b4_translate
685 };
686 return ((unsigned)(token) <= maxtok_ ? translate_[[token]] : nsym_);
687 }
688
689 const int yy::b4_name::eof_ = 0;
690 const int yy::b4_name::last_ = b4_last;
691 const int yy::b4_name::flag_ = b4_flag;
692 const int yy::b4_name::nnts_ = b4_nnts;
693 const int yy::b4_name::nsym_ = b4_nsym;
694 const int yy::b4_name::empty_ = -2;
695 const int yy::b4_name::final_ = b4_final;
696 const int yy::b4_name::terror_ = 1;
697 const int yy::b4_name::errcode_ = 256;
698 const int yy::b4_name::ntokens_ = b4_ntokens;
699 const int yy::b4_name::initdepth_ = b4_initdepth;
700
701 const unsigned yy::b4_name::maxtok_ = b4_maxtok;
702
703 b4_epilogue
704
705 #output "stack.hh"
706 b4_copyright
707
708 #ifndef BISON_STACK_HH
709 # define BISON_STACK_HH
710
711 #include <vector>
712
713 namespace yy
714 {
715 template < class T, class S = std::vector< T > >
716 class Stack
717 {
718 public:
719
720 typedef typename S::iterator Iterator;
721 typedef typename S::const_iterator ConstIterator;
722
723 Stack () : seq_ ()
724 {
725 }
726
727 Stack (unsigned n) : seq_ (n)
728 {
729 }
730
731 inline
732 T&
733 operator [[]] (int index)
734 {
735 return seq_[[height () - 1 + index]];
736 }
737
738 inline
739 const T&
740 operator [[]] (int index) const
741 {
742 return seq_[[height () - 1 + index]];
743 }
744
745 inline
746 void
747 push (const T& t)
748 {
749 seq_.push_back (t);
750 }
751
752 inline
753 void
754 pop (unsigned n = 1)
755 {
756 for (; n; --n)
757 seq_.pop_back ();
758 }
759
760 inline
761 void
762 reserve (unsigned n)
763 {
764 seq_.reserve (n);
765 }
766
767 inline
768 unsigned
769 height () const
770 {
771 return seq_.size ();
772 }
773
774 inline ConstIterator begin () const { return seq_.begin (); }
775 inline ConstIterator end () const { return seq_.end (); }
776
777 private:
778
779 S seq_;
780 };
781
782 template < class T, class S = Stack< T > >
783 class Slice
784 {
785 public:
786
787 Slice (const S& stack,
788 unsigned range) : stack_ (stack),
789 range_ (range)
790 {
791 }
792
793 inline
794 const T&
795 operator [[]] (unsigned index) const
796 {
797 return stack_[[index - range_]];
798 }
799
800 private:
801
802 const S& stack_;
803 unsigned range_;
804 };
805 }
806
807 #endif // not BISON_STACK_HH
808
809 #output "location.hh"
810 b4_copyright
811
812 #ifndef BISON_LOCATION_HH
813 # define BISON_LOCATION_HH
814
815 namespace yy
816 {
817 struct Position
818 {
819 int line;
820 int column;
821 };
822
823 struct Location
824 {
825 Position first;
826 Position last;
827 };
828 }
829
830 #endif // not BISON_LOCATION_HH