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