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