]> git.saurik.com Git - bison.git/blob - data/bison.c++
* data/bison.c++ (yy::b4_name::error_): New method, replaces yyerror in
[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 /* Eof and empty. */
120 static const int eof_;
121 static const int empty_;
122 static const int ntokens_;
123
124 /* State. */
125 int n;
126 int len;
127 int debug_;
128 int state;
129
130 /* Lookahead. */
131 int looka;
132 int ilooka;
133
134 /* Message. */
135 std::string message;
136
137 /* @$ and $$. */
138 SemanticType value;
139 LocationType location;
140 };
141 }
142
143 #output "b4_output_prefix[]b4_output_infix[].cc"
144 #include "b4_output_prefix[]b4_output_infix-class.hh"
145
146 /* Enabling traces. */
147 #ifndef YYDEBUG
148 # define YYDEBUG b4_debug
149 #endif
150
151 /* Enable debugging if requested. */
152 #if YYDEBUG
153
154 # include <cstdio>
155 # define YYFPRINTF std::fprintf
156
157 # define YYDPRINTF(Args) \
158 do { \
159 if (debug_) \
160 YYFPRINTF Args; \
161 } while (0)
162 /* Nonzero means print parse trace. [The following comment makes no
163 sense to me. Could someone clarify it? --akim] Since this is
164 uninitialized, it does not stop multiple parsers from coexisting.
165 */
166 int yydebug;
167 #else /* !YYDEBUG */
168 # define YYDPRINTF(Args)
169 #endif /* !YYDEBUG */
170
171 /* Tokens. */
172 b4_tokendef
173
174 int
175 yy::b4_name::parse ()
176 {
177 int nerrs = 0;
178 int errstatus = 0;
179
180 /* Initialize stack. */
181 state_stack = StateStack (0);
182 semantic_stack = SemanticStack (1);
183 location_stack = LocationStack (1);
184
185 /* Reserve initial space. The C parser needed that, but is it really
186 useful here? */
187 state_stack.reserve (b4_initdepth);
188 semantic_stack.reserve (b4_initdepth);
189 location_stack.reserve (b4_initdepth);
190
191 /* Start. */
192 state = 0;
193 YYDPRINTF ((stderr, "Starting parse\n"));
194
195 /* New state. */
196 yynewstate:
197 state_stack.push (state);
198 YYDPRINTF ((stderr, "Entering state %d\n", state));
199 goto yybackup;
200
201 /* Backup. */
202 yybackup:
203
204 /* Try to take a decision without lookahead. */
205 n = pact_[[state]];
206 if (n == b4_flag)
207 goto yydefault;
208
209 /* Read a lookahead token. */
210 if (looka == empty_)
211 {
212 YYDPRINTF ((stderr, "Reading a token: "));
213 lex ();
214 }
215
216 /* Convert token to internal form. */
217 if (looka <= 0)
218 {
219 looka = eof_;
220 ilooka = 0;
221 YYDPRINTF ((stderr, "Now at end of input.\n"));
222 }
223 else
224 {
225 ilooka = translate (looka);
226 #if YYDEBUG
227 if (debug_)
228 {
229 YYFPRINTF (stderr, "Next token is %d (%s", looka, name_[[ilooka]]);
230 YYFPRINTF (stderr, ")\n");
231 }
232 #endif
233 }
234
235 n += ilooka;
236 if (n < 0 || n > b4_last || check_[[n]] != ilooka)
237 goto yydefault;
238
239 /* Reduce or error. */
240 n = table_[[n]];
241 if (n < 0)
242 {
243 if (n == b4_flag)
244 goto yyerrlab;
245 else
246 {
247 n = -n;
248 goto yyreduce;
249 }
250 }
251 else if (n == 0)
252 goto yyerrlab;
253
254 /* Accept? */
255 if (n == b4_final)
256 goto yyacceptlab;
257
258 /* Shift the lookahead token. */
259 YYDPRINTF ((stderr, "Shifting token %d (%s), ", looka, name_[[ilooka]]));
260
261 /* Discard the token being shifted unless it is eof. */
262 if (looka != eof_)
263 looka = empty_;
264
265 semantic_stack.push (value);
266 location_stack.push (location);
267 state = n;
268 goto yynewstate;
269
270 /* Default action. */
271 yydefault:
272 n = defact_[[state]];
273 if (n == 0)
274 goto yyerrlab;
275 goto yyreduce;
276
277 /* Reduce. */
278 yyreduce:
279 len = r2_[[n]];
280 if (len)
281 {
282 value = semantic_stack[[1 - len]];
283 location = location_stack[[1 - len]];
284 }
285 else
286 {
287 // FIXME: This is truly ugly.
288 value = semantic_stack[[0]];
289 location = location_stack[[0]];
290 }
291
292 #if YYDEBUG
293 if (debug_)
294 {
295 YYFPRINTF (stderr, "Reducing via rule %d (line %d), ", n - 1, rline_[[n]]);
296 for (unsigned i = prhs_[[n]];
297 rhs_[[i]] >= 0; ++i)
298 YYFPRINTF (stderr, "%s ", name_[[rhs_[i]]]);
299 YYFPRINTF (stderr, "-> %s\n", name_[[r1_[n]]]);
300 }
301 #endif
302
303 {
304 SemanticType& yyval (value);
305 LocationType& yyloc (location);
306 SemanticStack& yyvsp (semantic_stack);
307 LocationStack& yylsp (location_stack);
308
309 switch (n)
310 {
311 b4_actions
312 }
313 }
314
315 state_stack.pop (len);
316 semantic_stack.pop (len);
317 location_stack.pop (len);
318
319 #if YYDEBUG
320 if (debug_)
321 {
322 YYFPRINTF (stderr, "state stack now");
323 for (StateStack::ConstIterator i = state_stack.begin ();
324 i != state_stack.end (); ++i)
325 YYFPRINTF (stderr, " %d", *i);
326 YYFPRINTF (stderr, "\n");
327 }
328 #endif
329
330 semantic_stack.push (value);
331 location_stack.push (location);
332
333 /* Shift the result of the reduction. */
334 n = r1_[[n]];
335 state = pgoto_[[n - ntokens_]] + state_stack[[0]];
336 if (state >= 0 && state <= b4_last && check_[[state]] == state_stack[[0]])
337 state = table_[[state]];
338 else
339 state = defgoto_[[n - ntokens_]];
340 goto yynewstate;
341
342 /* Report and recover from errors. This is very incomplete. */
343 yyerrlab:
344 /* If not already recovering from an error, report this error. */
345 if (!errstatus)
346 {
347 ++nerrs;
348
349 // FIXME: Should be #if YYERROR_VERBOSE from here...
350 n = pact_[[state]];
351 if (n > b4_flag && n < b4_last)
352 {
353 message = "parse error, unexpected ";
354 message += name_[[ilooka]];
355 {
356 int count = 0;
357 for (int x = (n < 0 ? -n : 0); x < b4_ntokens + b4_nnts; ++x)
358 if (check_[[x + n]] == x)
359 ++count;
360 if (count < 5)
361 {
362 count = 0;
363 for (int x = (n < 0 ? -n : 0); x < b4_ntokens + b4_nnts; ++x)
364 if (check_[[x + n]] == x)
365 {
366 message += (!count++) ? ", expecting " : " or ";
367 message += name_[[x]];
368 }
369 }
370 }
371 }
372 else
373 // FIXME: to there...
374 message = "parse error";
375 }
376 error_ ();
377 return 1;
378
379 /* Accept. */
380 yyacceptlab:
381 return 0;
382 }
383
384 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
385 STATE-NUM. */
386 const short
387 yy::b4_name::pact_[[]] =
388 {
389 b4_pact
390 };
391
392 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
393 doesn't specify something else to do. Zero means the default is an
394 error. */
395 const short
396 yy::b4_name::defact_[[]] =
397 {
398 b4_defact
399 };
400
401 /* YYPGOTO[[NTERM-NUM]]. */
402 const short
403 yy::b4_name::pgoto_[[]] =
404 {
405 b4_pgoto
406 };
407
408 /* YYDEFGOTO[[NTERM-NUM]]. */
409 const short
410 yy::b4_name::defgoto_[[]] =
411 {
412 b4_defgoto
413 };
414
415 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
416 positive, shift that token. If negative, reduce the rule which
417 number is the opposite. If zero, do what YYDEFACT says. */
418 const short
419 yy::b4_name::table_[[]] =
420 {
421 b4_table
422 };
423
424 /* YYCHECK. */
425 const short
426 yy::b4_name::check_[[]] =
427 {
428 b4_check
429 };
430
431 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
432 const short
433 yy::b4_name::rline_[[]] =
434 {
435 b4_rline
436 };
437
438 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
439 const short
440 yy::b4_name::r1_[[]] =
441 {
442 b4_r1
443 };
444
445 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
446 const short
447 yy::b4_name::r2_[[]] =
448 {
449 b4_r2
450 };
451
452 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
453 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
454 const char*
455 const yy::b4_name::name_[[]] =
456 {
457 b4_tname
458 };
459
460 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
461 const short
462 yy::b4_name::rhs_[[]] =
463 {
464 b4_rhs
465 };
466
467 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
468 YYRHS. */
469 const short
470 yy::b4_name::prhs_[[]] =
471 {
472 b4_prhs
473 };
474
475 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
476 char
477 yy::b4_name::translate (int token)
478 {
479 static
480 const char
481 translate_[[]] =
482 {
483 b4_translate
484 };
485 return ((unsigned)(token) <= b4_maxtok ? translate_[[token]] : b4_nsym);
486 }
487
488 const int yy::b4_name::eof_ = 0;
489 const int yy::b4_name::empty_ = -2;
490 const int yy::b4_name::ntokens_ = b4_ntokens;
491
492 b4_epilogue
493
494 #output "stack.hh"
495 #ifndef BISON_STACK_HH
496 # define BISON_STACK_HH
497
498 #include <vector>
499
500 namespace yy
501 {
502 template < class T, class S = std::vector< T > >
503 class Stack
504 {
505 public:
506
507 typedef typename S::iterator Iterator;
508 typedef typename S::const_iterator ConstIterator;
509
510 Stack () : seq_ ()
511 {
512 }
513
514 Stack (unsigned n) : seq_ (n)
515 {
516 }
517
518 inline
519 T&
520 operator [[]] (int index)
521 {
522 return seq_[[seq_.size () - 1 + index]];
523 }
524
525 inline
526 const T&
527 operator [[]] (int index) const
528 {
529 return seq_[[seq_.size () - 1 + index]];
530 }
531
532 inline
533 void
534 push (const T& t)
535 {
536 seq_.push_back (t);
537 }
538
539 inline
540 void
541 pop (unsigned n = 1)
542 {
543 for (; n; --n)
544 seq_.pop_back ();
545 }
546
547 inline
548 void
549 reserve (unsigned n)
550 {
551 seq_.reserve (n);
552 }
553
554 inline ConstIterator begin () const { return seq_.begin (); }
555 inline ConstIterator end () const { return seq_.end (); }
556
557 private:
558
559 S seq_;
560 };
561 }
562
563 #endif // not BISON_STACK_HH
564
565 #output "location.hh"
566 #ifndef BISON_LOCATION_HH
567 # define BISON_LOCATION_HH
568
569 namespace yy
570 {
571 struct Position
572 {
573 int line;
574 int column;
575 };
576
577 struct Location
578 {
579 Position first;
580 Position last;
581 };
582 }
583
584 #endif // not BISON_LOCATION_HH
585
586 m4_if(b4_defines_flag, 0, [],
587 [#output "b4_output_header_name"
588 #ifndef b4_header_guard
589 # define b4_header_guard
590
591 b4_tokendef
592
593 #ifndef YYSTYPE
594 typedef b4_stype
595 yystype;
596 # define YYSTYPE yystype
597 #endif
598
599 m4_if(b4_pure, [0],
600 [extern YYSTYPE b4_prefix[]lval;])
601
602 m4_if(b4_locations_flag, [0], [],
603 [#ifndef YYLTYPE
604 typedef struct yyltype
605 {
606 int first_line;
607 int first_column;
608 int last_line;
609 int last_column;
610 } yyltype;
611 # define YYLTYPE yyltype
612 #endif
613
614 m4_if(b4_pure, [0],
615 [extern YYLTYPE b4_prefix[]lloc;])
616 ])
617 #endif /* not b4_header_guard */
618 ])