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