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