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