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