]> git.saurik.com Git - bison.git/blob - data/bison.c++
a07d27c6e233e768ab7ab77c04a9a7750a395a74
[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 std::cout << "Reduced location is :" << yyloc << std::endl;
398
399 {
400 SemanticStack& yyvsp (semantic_stack_);
401 LocationStack& yylsp (location_stack_);
402
403 switch (n_)
404 {
405 b4_actions
406 }
407 }
408
409 state_stack_.pop (len_);
410 semantic_stack_.pop (len_);
411 location_stack_.pop (len_);
412
413 #if YYDEBUG
414 if (debug_)
415 {
416 YYFPRINTF (stderr, "state stack now");
417 for (StateStack::ConstIterator i = state_stack_.begin ();
418 i != state_stack_.end (); ++i)
419 YYFPRINTF (stderr, " %d", *i);
420 YYFPRINTF (stderr, "\n");
421 }
422 #endif
423
424 semantic_stack_.push (yyval);
425 location_stack_.push (yyloc);
426
427 /* Shift the result of the reduction. */
428 n_ = r1_[[n_]];
429 state_ = pgoto_[[n_ - ntokens_]] + state_stack_[[0]];
430 if (state_ >= 0 && state_ <= last_ && check_[[state_]] == state_stack_[[0]])
431 state_ = table_[[state_]];
432 else
433 state_ = defgoto_[[n_ - ntokens_]];
434 goto yynewstate;
435
436 /* Report and recover from errors. This is very incomplete. */
437 yyerrlab:
438 /* If not already recovering from an error, report this error. */
439 if (!errstatus)
440 {
441 ++nerrs;
442
443 #if YYERROR_VERBOSE
444 n_ = pact_[[state_]];
445 if (n_ > flag_ && n_ < last_)
446 {
447 message = "parse error, unexpected ";
448 message += name_[[ilooka_]];
449 {
450 int count = 0;
451 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
452 if (check_[[x + n_]] == x)
453 ++count;
454 if (count < 5)
455 {
456 count = 0;
457 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
458 if (check_[[x + n_]] == x)
459 {
460 message += (!count++) ? ", expecting " : " or ";
461 message += name_[[x]];
462 }
463 }
464 }
465 }
466 else
467 #endif
468 message = "parse error";
469 error_ ();
470 }
471 goto yyerrlab1;
472
473 /* Error raised explicitly by an action. */
474 yyerrlab1:
475 if (errstatus == 3)
476 {
477 /* If just tried and failed to reuse lookahead token after an
478 error, discard it. */
479
480 /* Return failure if at end of input. */
481 if (looka_ == eof_)
482 goto yyabortlab;
483 YYDPRINTF ((stderr, "Discarding token %d (%s).\n", looka_, name_[[ilooka_]]));
484 looka_ = empty_;
485 }
486
487 /* Else will try to reuse lookahead token after shifting the error
488 token. */
489
490 errstatus = 3;
491 goto yyerrhandle;
492
493 /* Pop the current state because it cannot handle the error token. */
494 yyerrdefault:
495 yyerrpop:
496 if (!state_stack_.height ())
497 goto yyabortlab;
498 state_ = (state_stack_.pop (), state_stack_[[0]]);
499 semantic_stack_.pop ();
500 location_stack_.pop ();;
501
502 #if YYDEBUG
503 if (debug_)
504 {
505 YYFPRINTF (stderr, "Error: state stack now");
506 for (StateStack::ConstIterator i = state_stack_.begin ();
507 i != state_stack_.end (); ++i)
508 YYFPRINTF (stderr, " %d", *i);
509 YYFPRINTF (stderr, "\n");
510 }
511 #endif
512
513 yyerrhandle:
514 n_ = pact_[[state_]];
515 if (n_ == flag_)
516 goto yyerrdefault;
517
518 n_ += terror_;
519 if (n_ < 0 || n_ > last_ || check_[[n_]] != terror_)
520 goto yyerrdefault;
521
522 n_ = table_[[n_]];
523 if (n_ < 0)
524 {
525 if (n_ == flag_)
526 goto yyerrpop;
527 n_ = -n_;
528 goto yyreduce;
529 }
530 else if (!n_)
531 goto yyerrpop;
532
533 if (n_ == final_)
534 goto yyacceptlab;
535
536 YYDPRINTF ((stderr, "Shifting error token, "));
537
538 semantic_stack_.push (value);
539 location_stack_.push (location);
540
541 state_ = n_;
542 goto yynewstate;
543
544 /* Accept. */
545 yyacceptlab:
546 return 0;
547
548 /* Abort. */
549 yyabortlab:
550 return 1;
551 }
552
553 void
554 yy::b4_name::lex_ ()
555 {
556 #if YYLSP_NEEDED
557 looka_ = yylex (&value, &location);
558 #else
559 looka_ = yylex (&value);
560 #endif
561 }
562
563 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
564 STATE-NUM. */
565 const short
566 yy::b4_name::pact_[[]] =
567 {
568 b4_pact
569 };
570
571 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
572 doesn't specify something else to do. Zero means the default is an
573 error. */
574 const short
575 yy::b4_name::defact_[[]] =
576 {
577 b4_defact
578 };
579
580 /* YYPGOTO[[NTERM-NUM]]. */
581 const short
582 yy::b4_name::pgoto_[[]] =
583 {
584 b4_pgoto
585 };
586
587 /* YYDEFGOTO[[NTERM-NUM]]. */
588 const short
589 yy::b4_name::defgoto_[[]] =
590 {
591 b4_defgoto
592 };
593
594 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
595 positive, shift that token. If negative, reduce the rule which
596 number is the opposite. If zero, do what YYDEFACT says. */
597 const short
598 yy::b4_name::table_[[]] =
599 {
600 b4_table
601 };
602
603 /* YYCHECK. */
604 const short
605 yy::b4_name::check_[[]] =
606 {
607 b4_check
608 };
609
610 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
611 const short
612 yy::b4_name::r1_[[]] =
613 {
614 b4_r1
615 };
616
617 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
618 const short
619 yy::b4_name::r2_[[]] =
620 {
621 b4_r2
622 };
623
624 #if YYDEBUG || YYERROR_VERBOSE
625 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
626 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
627 const char*
628 const yy::b4_name::name_[[]] =
629 {
630 b4_tname
631 };
632 #endif
633
634 #if YYDEBUG
635 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
636 const short
637 yy::b4_name::rhs_[[]] =
638 {
639 b4_rhs
640 };
641
642 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
643 YYRHS. */
644 const short
645 yy::b4_name::prhs_[[]] =
646 {
647 b4_prhs
648 };
649
650 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
651 const short
652 yy::b4_name::rline_[[]] =
653 {
654 b4_rline
655 };
656 #endif
657
658 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
659 char
660 yy::b4_name::translate_ (int token)
661 {
662 static
663 const char
664 translate_[[]] =
665 {
666 b4_translate
667 };
668 return ((unsigned)(token) <= maxtok_ ? translate_[[token]] : nsym_);
669 }
670
671 const int yy::b4_name::eof_ = 0;
672 const int yy::b4_name::last_ = b4_last;
673 const int yy::b4_name::flag_ = b4_flag;
674 const int yy::b4_name::nnts_ = b4_nnts;
675 const int yy::b4_name::nsym_ = b4_nsym;
676 const int yy::b4_name::empty_ = -2;
677 const int yy::b4_name::final_ = b4_final;
678 const int yy::b4_name::terror_ = 1;
679 const int yy::b4_name::errcode_ = 256;
680 const int yy::b4_name::ntokens_ = b4_ntokens;
681 const int yy::b4_name::initdepth_ = b4_initdepth;
682
683 const unsigned yy::b4_name::maxtok_ = b4_maxtok;
684
685 b4_epilogue
686
687 #output "stack.hh"
688 b4_copyright
689
690 #ifndef BISON_STACK_HH
691 # define BISON_STACK_HH
692
693 #include <vector>
694
695 namespace yy
696 {
697 template < class T, class S = std::vector< T > >
698 class Stack
699 {
700 public:
701
702 typedef typename S::iterator Iterator;
703 typedef typename S::const_iterator ConstIterator;
704
705 Stack () : seq_ ()
706 {
707 }
708
709 Stack (unsigned n) : seq_ (n)
710 {
711 }
712
713 inline
714 T&
715 operator [[]] (int index)
716 {
717 return seq_[[height () - 1 + index]];
718 }
719
720 inline
721 const T&
722 operator [[]] (int index) const
723 {
724 return seq_[[height () - 1 + index]];
725 }
726
727 inline
728 void
729 push (const T& t)
730 {
731 seq_.push_back (t);
732 }
733
734 inline
735 void
736 pop (unsigned n = 1)
737 {
738 for (; n; --n)
739 seq_.pop_back ();
740 }
741
742 inline
743 void
744 reserve (unsigned n)
745 {
746 seq_.reserve (n);
747 }
748
749 inline
750 unsigned
751 height () const
752 {
753 return seq_.size ();
754 }
755
756 inline ConstIterator begin () const { return seq_.begin (); }
757 inline ConstIterator end () const { return seq_.end (); }
758
759 private:
760
761 S seq_;
762 };
763
764 template < class T, class S = Stack< T > >
765 class Slice
766 {
767 public:
768
769 Slice (const S& stack,
770 unsigned range) : stack_ (stack),
771 range_ (range)
772 {
773 }
774
775 inline
776 const T&
777 operator [[]] (unsigned index) const
778 {
779 return stack_[[index - range_]];
780 }
781
782 private:
783
784 const S& stack_;
785 unsigned range_;
786 };
787 }
788
789 #endif // not BISON_STACK_HH
790
791 #output "location.hh"
792 b4_copyright
793
794 #ifndef BISON_LOCATION_HH
795 # define BISON_LOCATION_HH
796
797 namespace yy
798 {
799 struct Position
800 {
801 int line;
802 int column;
803 };
804
805 struct Location
806 {
807 Position first;
808 Position last;
809 };
810 }
811
812 #endif // not BISON_LOCATION_HH