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