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