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