]> git.saurik.com Git - bison.git/blob - data/bison.c++
f2ccd3dde5d19d2fb73aae881c091d88f6c295de
[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 #ifndef YYLLOC_DEFAULT
141 # define YYLLOC_DEFAULT(Current, Rhs, N) \
142 Current.last_line = Rhs[[N]].last_line; \
143 Current.last_column = Rhs[[N]].last_column;
144 #endif
145
146 m4_if(b4_locations_flag, [0], [],
147 [#ifndef YYLTYPE
148 typedef struct yyltype
149 {
150 int first_line;
151 int first_column;
152 int last_line;
153 int last_column;
154 } yyltype;
155 # define YYLTYPE yyltype
156 #endif])
157
158 namespace yy
159 {
160 class b4_name;
161
162 template < typename P >
163 struct Traits
164 {
165 };
166
167 template < >
168 struct Traits< b4_name >
169 {
170 typedef typedef b4_uint_type(b4_token_number_max) TokenNumberType;
171 typedef typedef b4_sint_type(b4_item_number_max) RhsNumberType;
172 typedef int StateType;
173 typedef yystype SemanticType;
174 typedef b4_ltype LocationType;
175 };
176 }
177
178 namespace yy
179 {
180 class b4_name b4_inherit
181 {
182 public:
183
184 typedef Traits< b4_name >::TokenNumberType TokenNumberType;
185 typedef Traits< b4_name >::RhsNumberType TokenNumberType;
186 typedef Traits< b4_name >::StateType StateType;
187 typedef Traits< b4_name >::SemanticType SemanticType;
188 typedef Traits< b4_name >::LocationType LocationType;
189
190 typedef Stack< StateType > StateStack;
191 typedef Stack< SemanticType > SemanticStack;
192 typedef Stack< LocationType > LocationStack;
193
194 #if YYLSP_NEEDED
195 b4_name (bool debug,
196 LocationType initlocation[]b4_param) :
197 b4_constructor[]debug_ (debug),
198 initlocation_ (initlocation)
199 #else
200 b4_name (bool debug[]b4_param) :
201 b4_constructor[]debug_ (debug)
202 #endif
203 {
204 }
205
206 virtual ~b4_name ()
207 {
208 }
209
210 virtual int parse ();
211
212 private:
213
214 virtual void lex_ ();
215 virtual void error_ ();
216 virtual void print_ ();
217
218 /* Stacks. */
219 StateStack state_stack_;
220 SemanticStack semantic_stack_;
221 LocationStack location_stack_;
222
223 /* Tables. */
224 static const short pact_[[]];
225 static const short defact_[[]];
226 static const short pgoto_[[]];
227 static const short defgoto_[[]];
228 static const short table_[[]];
229 static const short check_[[]];
230 static const short r1_[[]];
231 static const short r2_[[]];
232
233 #if YYDEBUG || YYERROR_VERBOSE
234 static const char* const name_[[]];
235 #endif
236
237 /* More tables, for debugging. */
238 #if YYDEBUG
239 static const short rhs_[[]];
240 static const short prhs_[[]];
241 static const short rline_[[]];
242 #endif
243
244 /* Even more tables. */
245 static inline TokenNumberType translate_ (int token);
246
247 /* Constants. */
248 static const int eof_;
249 static const int last_;
250 static const int flag_;
251 static const int nnts_;
252 static const int nsym_;
253 static const int empty_;
254 static const int final_;
255 static const int terror_;
256 static const int errcode_;
257 static const int ntokens_;
258 static const int initdepth_;
259 static const unsigned maxtok_;
260
261 /* State. */
262 int n_;
263 int len_;
264 int debug_;
265 int state_;
266
267 /* Lookahead and lookahead in internal form. */
268 int looka_;
269 int ilooka_;
270
271 /* Message. */
272 std::string message;
273
274 /* Semantic value and location of lookahead token. */
275 SemanticType value;
276 LocationType location;
277
278 /* @$ and $$. */
279 SemanticType yyval;
280 LocationType yyloc;
281
282 /* Initial location. */
283 LocationType initlocation_;
284 };
285 }
286
287 #endif /* not b4_header_guard */
288
289 #output "b4_output_prefix[]b4_output_infix[].cc"
290 b4_copyright
291
292 #include "b4_output_header_name"
293
294 /* Enable debugging if requested. */
295 #if YYDEBUG
296 # include <cstdio>
297 # define YYFPRINTF std::fprintf
298 # define YYDPRINTF(Args) \
299 do { \
300 if (debug_) \
301 YYFPRINTF Args; \
302 } while (0)
303 #else /* !YYDEBUG */
304 # define YYDPRINTF(Args)
305 #endif /* !YYDEBUG */
306
307 int
308 yy::b4_name::parse ()
309 {
310 int nerrs = 0;
311 int errstatus = 0;
312
313 /* Initialize stack. */
314 state_stack_ = StateStack (0);
315 semantic_stack_ = SemanticStack (1);
316 location_stack_ = LocationStack (1);
317
318 /* Reserve initial space. The C parser needed that, but is it really
319 useful here? */
320 state_stack_.reserve (initdepth_);
321 semantic_stack_.reserve (initdepth_);
322 location_stack_.reserve (initdepth_);
323
324 /* Start. */
325 state_ = 0;
326 looka_ = empty_;
327 #if YYLSP_NEEDED
328 location = initlocation_;
329 #endif
330 YYDPRINTF ((stderr, "Starting parse\n"));
331
332 /* New state. */
333 yynewstate:
334 state_stack_.push (state_);
335 YYDPRINTF ((stderr, "Entering state %d\n", state_));
336 goto yybackup;
337
338 /* Backup. */
339 yybackup:
340
341 /* Try to take a decision without lookahead. */
342 n_ = pact_[[state_]];
343 if (n_ == flag_)
344 goto yydefault;
345
346 /* Read a lookahead token. */
347 if (looka_ == empty_)
348 {
349 YYDPRINTF ((stderr, "Reading a token: "));
350 lex_ ();
351 }
352
353 /* Convert token to internal form. */
354 if (looka_ <= 0)
355 {
356 looka_ = eof_;
357 ilooka_ = 0;
358 YYDPRINTF ((stderr, "Now at end of input.\n"));
359 }
360 else
361 {
362 ilooka_ = translate_ (looka_);
363 #if YYDEBUG
364 if (debug_)
365 {
366 YYFPRINTF (stderr, "Next token is %d (%s", looka_, name_[[ilooka_]]);
367 print_ ();
368 YYFPRINTF (stderr, ")\n");
369 }
370 #endif
371 }
372
373 n_ += ilooka_;
374 if (n_ < 0 || n_ > last_ || check_[[n_]] != ilooka_)
375 goto yydefault;
376
377 /* Reduce or error. */
378 n_ = table_[[n_]];
379 if (n_ < 0)
380 {
381 if (n_ == flag_)
382 goto yyerrlab;
383 else
384 {
385 n_ = -n_;
386 goto yyreduce;
387 }
388 }
389 else if (n_ == 0)
390 goto yyerrlab;
391
392 /* Accept? */
393 if (n_ == final_)
394 goto yyacceptlab;
395
396 /* Shift the lookahead token. */
397 YYDPRINTF ((stderr, "Shifting token %d (%s), ", looka_, name_[[ilooka_]]));
398
399 /* Discard the token being shifted unless it is eof. */
400 if (looka_ != eof_)
401 looka_ = empty_;
402
403 semantic_stack_.push (value);
404 location_stack_.push (location);
405
406 /* Count tokens shifted since error; after three, turn off error
407 status. */
408 if (errstatus)
409 --errstatus;
410
411 state_ = n_;
412 goto yynewstate;
413
414 /* Default action. */
415 yydefault:
416 n_ = defact_[[state_]];
417 if (n_ == 0)
418 goto yyerrlab;
419 goto yyreduce;
420
421 /* Reduce. */
422 yyreduce:
423 len_ = r2_[[n_]];
424 if (len_)
425 {
426 yyval = semantic_stack_[[1 - len_]];
427 yyloc = location_stack_[[1 - len_]];
428 }
429 else
430 {
431 yyval = semantic_stack_[[0]];
432 yyloc = location_stack_[[0]];
433 }
434
435 #if YYDEBUG
436 if (debug_)
437 {
438 YYFPRINTF (stderr, "Reducing via rule %d (line %d), ", n_ - 1, rline_[[n_]]);
439 for (unsigned i = prhs_[[n_]];
440 rhs_[[i]] >= 0; ++i)
441 YYFPRINTF (stderr, "%s ", name_[[rhs_[i]]]);
442 YYFPRINTF (stderr, "-> %s\n", name_[[r1_[n_]]]);
443 }
444 #endif
445
446 if (len_)
447 {
448 Slice< LocationType, LocationStack > slice (location_stack_, len_);
449 YYLLOC_DEFAULT (yyloc, slice, len_);
450 }
451
452 {
453 SemanticStack& yyvsp (semantic_stack_);
454 LocationStack& yylsp (location_stack_);
455
456 switch (n_)
457 {
458 b4_actions
459 }
460 }
461
462 state_stack_.pop (len_);
463 semantic_stack_.pop (len_);
464 location_stack_.pop (len_);
465
466 #if YYDEBUG
467 if (debug_)
468 {
469 YYFPRINTF (stderr, "state stack now");
470 for (StateStack::ConstIterator i = state_stack_.begin ();
471 i != state_stack_.end (); ++i)
472 YYFPRINTF (stderr, " %d", *i);
473 YYFPRINTF (stderr, "\n");
474 }
475 #endif
476
477 semantic_stack_.push (yyval);
478 location_stack_.push (yyloc);
479
480 /* Shift the result of the reduction. */
481 n_ = r1_[[n_]];
482 state_ = pgoto_[[n_ - ntokens_]] + state_stack_[[0]];
483 if (state_ >= 0 && state_ <= last_ && check_[[state_]] == state_stack_[[0]])
484 state_ = table_[[state_]];
485 else
486 state_ = defgoto_[[n_ - ntokens_]];
487 goto yynewstate;
488
489 /* Report and recover from errors. This is very incomplete. */
490 yyerrlab:
491 /* If not already recovering from an error, report this error. */
492 if (!errstatus)
493 {
494 ++nerrs;
495
496 #if YYERROR_VERBOSE
497 n_ = pact_[[state_]];
498 if (n_ > flag_ && n_ < last_)
499 {
500 message = "parse error, unexpected ";
501 message += name_[[ilooka_]];
502 {
503 int count = 0;
504 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
505 if (check_[[x + n_]] == x)
506 ++count;
507 if (count < 5)
508 {
509 count = 0;
510 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
511 if (check_[[x + n_]] == x)
512 {
513 message += (!count++) ? ", expecting " : " or ";
514 message += name_[[x]];
515 }
516 }
517 }
518 }
519 else
520 #endif
521 message = "parse error";
522 error_ ();
523 }
524 goto yyerrlab1;
525
526 /* Error raised explicitly by an action. */
527 yyerrlab1:
528 if (errstatus == 3)
529 {
530 /* If just tried and failed to reuse lookahead token after an
531 error, discard it. */
532
533 /* Return failure if at end of input. */
534 if (looka_ == eof_)
535 goto yyabortlab;
536 YYDPRINTF ((stderr, "Discarding token %d (%s).\n", looka_, name_[[ilooka_]]));
537 looka_ = empty_;
538 }
539
540 /* Else will try to reuse lookahead token after shifting the error
541 token. */
542
543 errstatus = 3;
544 goto yyerrhandle;
545
546 /* Pop the current state because it cannot handle the error token. */
547 yyerrdefault:
548 yyerrpop:
549 if (!state_stack_.height ())
550 goto yyabortlab;
551 state_ = (state_stack_.pop (), state_stack_[[0]]);
552 semantic_stack_.pop ();
553 location_stack_.pop ();;
554
555 #if YYDEBUG
556 if (debug_)
557 {
558 YYFPRINTF (stderr, "Error: state stack now");
559 for (StateStack::ConstIterator i = state_stack_.begin ();
560 i != state_stack_.end (); ++i)
561 YYFPRINTF (stderr, " %d", *i);
562 YYFPRINTF (stderr, "\n");
563 }
564 #endif
565
566 yyerrhandle:
567 n_ = pact_[[state_]];
568 if (n_ == flag_)
569 goto yyerrdefault;
570
571 n_ += terror_;
572 if (n_ < 0 || n_ > last_ || check_[[n_]] != terror_)
573 goto yyerrdefault;
574
575 n_ = table_[[n_]];
576 if (n_ < 0)
577 {
578 if (n_ == flag_)
579 goto yyerrpop;
580 n_ = -n_;
581 goto yyreduce;
582 }
583 else if (!n_)
584 goto yyerrpop;
585
586 if (n_ == final_)
587 goto yyacceptlab;
588
589 YYDPRINTF ((stderr, "Shifting error token, "));
590
591 semantic_stack_.push (value);
592 location_stack_.push (location);
593
594 state_ = n_;
595 goto yynewstate;
596
597 /* Accept. */
598 yyacceptlab:
599 return 0;
600
601 /* Abort. */
602 yyabortlab:
603 return 1;
604 }
605
606 void
607 yy::b4_name::lex_ ()
608 {
609 #if YYLSP_NEEDED
610 looka_ = yylex (&value, &location);
611 #else
612 looka_ = yylex (&value);
613 #endif
614 }
615
616 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
617 STATE-NUM. */
618 const short
619 yy::b4_name::pact_[[]] =
620 {
621 b4_pact
622 };
623
624 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
625 doesn't specify something else to do. Zero means the default is an
626 error. */
627 const short
628 yy::b4_name::defact_[[]] =
629 {
630 b4_defact
631 };
632
633 /* YYPGOTO[[NTERM-NUM]]. */
634 const short
635 yy::b4_name::pgoto_[[]] =
636 {
637 b4_pgoto
638 };
639
640 /* YYDEFGOTO[[NTERM-NUM]]. */
641 const short
642 yy::b4_name::defgoto_[[]] =
643 {
644 b4_defgoto
645 };
646
647 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
648 positive, shift that token. If negative, reduce the rule which
649 number is the opposite. If zero, do what YYDEFACT says. */
650 const short
651 yy::b4_name::table_[[]] =
652 {
653 b4_table
654 };
655
656 /* YYCHECK. */
657 const short
658 yy::b4_name::check_[[]] =
659 {
660 b4_check
661 };
662
663 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
664 const short
665 yy::b4_name::r1_[[]] =
666 {
667 b4_r1
668 };
669
670 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
671 const short
672 yy::b4_name::r2_[[]] =
673 {
674 b4_r2
675 };
676
677 #if YYDEBUG || YYERROR_VERBOSE
678 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
679 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
680 const char*
681 const yy::b4_name::name_[[]] =
682 {
683 b4_tname
684 };
685 #endif
686
687 #if YYDEBUG
688 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
689 const RhsNumberType
690 yy::b4_name::rhs_[[]] =
691 {
692 b4_rhs
693 };
694
695 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
696 YYRHS. */
697 const short
698 yy::b4_name::prhs_[[]] =
699 {
700 b4_prhs
701 };
702
703 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
704 const short
705 yy::b4_name::rline_[[]] =
706 {
707 b4_rline
708 };
709 #endif
710
711 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
712 TokenNumberType
713 yy::b4_name::translate_ (int token)
714 {
715 static
716 const TokenNumberType
717 translate_[[]] =
718 {
719 b4_translate
720 };
721 return (unsigned)(token) <= maxtok_ ? translate_[[token]] : nsym_;
722 }
723
724 const int yy::b4_name::eof_ = 0;
725 const int yy::b4_name::last_ = b4_last;
726 const int yy::b4_name::flag_ = b4_flag;
727 const int yy::b4_name::nnts_ = b4_nnts;
728 const int yy::b4_name::nsym_ = b4_nsym;
729 const int yy::b4_name::empty_ = -2;
730 const int yy::b4_name::final_ = b4_final;
731 const int yy::b4_name::terror_ = 1;
732 const int yy::b4_name::errcode_ = 256;
733 const int yy::b4_name::ntokens_ = b4_ntokens;
734 const int yy::b4_name::initdepth_ = b4_initdepth;
735
736 const unsigned yy::b4_name::maxtok_ = b4_maxtok;
737
738 b4_epilogue
739
740 #output "stack.hh"
741 b4_copyright
742
743 #ifndef BISON_STACK_HH
744 # define BISON_STACK_HH
745
746 #include <vector>
747
748 namespace yy
749 {
750 template < class T, class S = std::vector< T > >
751 class Stack
752 {
753 public:
754
755 typedef typename S::iterator Iterator;
756 typedef typename S::const_iterator ConstIterator;
757
758 Stack () : seq_ ()
759 {
760 }
761
762 Stack (unsigned n) : seq_ (n)
763 {
764 }
765
766 inline
767 T&
768 operator [[]] (int index)
769 {
770 return seq_[[height () - 1 + index]];
771 }
772
773 inline
774 const T&
775 operator [[]] (int index) const
776 {
777 return seq_[[height () - 1 + index]];
778 }
779
780 inline
781 void
782 push (const T& t)
783 {
784 seq_.push_back (t);
785 }
786
787 inline
788 void
789 pop (unsigned n = 1)
790 {
791 for (; n; --n)
792 seq_.pop_back ();
793 }
794
795 inline
796 void
797 reserve (unsigned n)
798 {
799 seq_.reserve (n);
800 }
801
802 inline
803 unsigned
804 height () const
805 {
806 return seq_.size ();
807 }
808
809 inline ConstIterator begin () const { return seq_.begin (); }
810 inline ConstIterator end () const { return seq_.end (); }
811
812 private:
813
814 S seq_;
815 };
816
817 template < class T, class S = Stack< T > >
818 class Slice
819 {
820 public:
821
822 Slice (const S& stack,
823 unsigned range) : stack_ (stack),
824 range_ (range)
825 {
826 }
827
828 inline
829 const T&
830 operator [[]] (unsigned index) const
831 {
832 return stack_[[index - range_]];
833 }
834
835 private:
836
837 const S& stack_;
838 unsigned range_;
839 };
840 }
841
842 #endif // not BISON_STACK_HH
843
844 #output "location.hh"
845 b4_copyright
846
847 #ifndef BISON_LOCATION_HH
848 # define BISON_LOCATION_HH
849
850 namespace yy
851 {
852 struct Position
853 {
854 int line;
855 int column;
856 };
857
858 struct Location
859 {
860 Position first;
861 Position last;
862 };
863 }
864
865 #endif // not BISON_LOCATION_HH