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