]> git.saurik.com Git - bison.git/blob - data/bison.c++
751ceb151193c4b7dd96d6ca0266bcd772cc639b
[bison.git] / data / bison.c++
1 #output "b4_output_prefix[]b4_output_infix[].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 (C) 2002 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 "parser.hh" */
32 #include "location.hh"
33
34 namespace yy
35 {
36 class b4_name;
37
38 template < typename P >
39 struct Traits
40 {
41 };
42
43 template < >
44 struct Traits< b4_name >
45 {
46 typedef int StateType;
47 typedef b4_stype SemanticType;
48 typedef Location LocationType;
49 };
50 }
51
52 b4_prologue
53
54 namespace yy
55 {
56 class b4_name
57 {
58 public:
59
60 typedef Traits< b4_name >::StateType StateType;
61 typedef Traits< b4_name >::SemanticType SemanticType;
62 typedef Traits< b4_name >::LocationType LocationType;
63
64 typedef Stack< StateType > StateStack;
65 typedef Stack< SemanticType > SemanticStack;
66 typedef Stack< LocationType > LocationStack;
67
68 b4_name () : debug (1)
69 {
70 looka = empty;
71 }
72
73 virtual int parse ();
74
75 private:
76
77 /* Call to lexical analyser. */
78 virtual
79 void
80 lex ()
81 {
82 looka = yylex (&value);
83 }
84
85 /* Stacks. */
86 StateStack state_stack;
87 SemanticStack semantic_stack;
88 LocationStack location_stack;
89
90 /* Tables. */
91 static const short pact_[[]];
92 static const short defact_[[]];
93 static const short pgoto_[[]];
94 static const short defgoto_[[]];
95 static const short table_[[]];
96 static const short check_[[]];
97 static const short line_[[]];
98 static const short r1_[[]];
99 static const short r2_[[]];
100 static const char* const name_[[]];
101
102 /* More tables, for debugging. */
103 /* FIXME: These tables should not be printed when not in debug
104 mode. %%ifdef? */
105 static const short rhs_[[]];
106 static const short prhs_[[]];
107 static const short rline_[[]];
108
109 /* Even more tables. */
110 static inline char translate (int token);
111
112 /* Eof and empty. */
113 static const int eof;
114 static const int empty;
115
116 /* State. */
117 int n;
118 int len;
119 int debug;
120 int state;
121
122 /* Lookahead. */
123 int looka;
124 int ilooka;
125
126 /* @$ and $$. */
127 SemanticType value;
128 LocationType location;
129 };
130 }
131
132 #output "b4_output_prefix[]b4_output_infix[].cc"
133 #include "b4_output_prefix[]b4_output_infix.hh"
134
135 /* Enable debugging if requested. */
136 #if b4_debug
137
138 # include <cstdio>
139 # define YYFPRINTF std::fprintf
140
141 # define YYDPRINTF(Args) \
142 do { \
143 if (debug) \
144 YYFPRINTF Args; \
145 } while (0)
146 /* Nonzero means print parse trace. Since this is uninitialized, it
147 does not stop multiple parsers from coexisting. */
148 int yydebug;
149 #else /* !YYDEBUG */
150 # define YYDPRINTF(Args)
151 #endif /* !YYDEBUG */
152
153 /* Tokens. */
154 b4_tokendef
155
156 int
157 yy::b4_name::parse ()
158 {
159 /* Initialize stack. */
160 state_stack = StateStack (0);
161 semantic_stack = SemanticStack (1);
162 location_stack = LocationStack (1);
163
164 /* Reserve initial space. The C parser needed that, but is it really
165 useful here? */
166 state_stack.reserve (b4_initdepth);
167 semantic_stack.reserve (b4_initdepth);
168 location_stack.reserve (b4_initdepth);
169
170 /* Start. */
171 state = 0;
172 YYDPRINTF ((stderr, "Starting parse\n"));
173
174 /* New state. */
175 yynewstate:
176 state_stack.push (state);
177 YYDPRINTF ((stderr, "Entering state %d\n", state));
178
179 /* Backup. */
180 yybackup:
181
182 /* Try to take a decision without lookahead. */
183 n = pact_[[state]];
184 if (n == b4_flag)
185 goto yydefault;
186
187 /* Read a lookahead token. */
188 if (looka == empty)
189 {
190 YYDPRINTF ((stderr, "Reading a token: "));
191 lex ();
192 }
193
194 /* Convert token to internal form. */
195 if (looka <= 0)
196 {
197 looka = eof;
198 ilooka = 0;
199 YYDPRINTF ((stderr, "Now at end of input.\n"));
200 }
201 else
202 {
203 ilooka = translate (looka);
204 #if b4_debug
205 YYFPRINTF (stderr, "Next token is %d (%s", looka, name_[[ilooka]]);
206 YYFPRINTF (stderr, ")\n");
207 #endif
208 }
209
210 n += ilooka;
211 if (n < 0 || n > b4_last || check_[[n]] != ilooka)
212 goto yydefault;
213
214 /* Reduce or error. */
215 n = table_[[n]];
216 if (n < 0)
217 {
218 if (n == b4_flag)
219 goto yyerrlab;
220 else
221 {
222 n = -n;
223 goto yyreduce;
224 }
225 }
226 else if (n == 0)
227 goto yyerrlab;
228
229 /* Accept? */
230 if (n == b4_final)
231 goto yyacceptlab;
232
233 /* Shift the lookahead token. */
234 YYDPRINTF ((stderr, "Shifting token %d (%s), ", looka, name_[[ilooka]]));
235
236 /* Discard the token being shifted unless it is eof. */
237 if (looka != eof)
238 looka = empty;
239
240 semantic_stack.push (value);
241 location_stack.push (location);
242 state = n;
243 goto yynewstate;
244
245 /* Default action. */
246 yydefault:
247 n = defact_[[state]];
248 if (n == 0)
249 goto yyerrlab;
250
251 /* Reduce. */
252 yyreduce:
253 len = r2_[[n]];
254 value = semantic_stack[[1 - len]];
255 location = location_stack[[1 - len]];
256
257 #if b4_debug
258 if (debug)
259 {
260 YYFPRINTF (stderr, "Reducing via rule %d (line %d), ", n - 1, rline_[[n]]);
261 for (unsigned i = prhs_[[n]];
262 rhs_[[i]] >= 0; ++i)
263 YYFPRINTF (stderr, "%s ", name_[[rhs_[i]]]);
264 YYFPRINTF (stderr, "-> %s\n", name_[[r1_[n]]]);
265 }
266 #endif
267
268 {
269 SemanticType& yyval (value);
270 SemanticStack& yyvsp (semantic_stack);
271
272 switch (n)
273 {
274 b4_actions
275 }
276 }
277
278 state_stack.pop (len);
279 semantic_stack.pop (len);
280 location_stack.pop (len);
281
282 #if b4_debug
283 if (debug)
284 {
285 YYFPRINTF (stderr, "state stack now");
286 for (StateStack::ConstIterator i = state_stack.begin ();
287 i != state_stack.end (); ++i)
288 YYFPRINTF (stderr, " %d", *i);
289 YYFPRINTF (stderr, "\n");
290 }
291 #endif
292
293 semantic_stack.push (value);
294 location_stack.push (location);
295
296 /* Shift the result of the reduction. */
297 n = r1_[[n]];
298 state = pgoto_[[n - b4_ntokens]] + state_stack[[0]];
299 if (state >= 0 && state <= b4_last && check_[[state]] == state_stack[[0]])
300 state = table_[[state]];
301 else
302 state = defgoto_[[n - b4_ntokens]];
303 goto yynewstate;
304
305 /* Report and recover from errors. This is very incomplete. */
306 yyerrlab:
307 // FIXME: Need something like yyerror?
308 std::cerr << "Parse error." << std::endl;
309 return 1;
310
311 /* Accept. */
312 yyacceptlab:
313 return 0;
314 }
315
316 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
317 STATE-NUM. */
318 const short
319 yy::b4_name::pact_[[]] =
320 {
321 b4_pact
322 };
323
324 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
325 doesn't specify something else to do. Zero means the default is an
326 error. */
327 const short
328 yy::b4_name::defact_[[]] =
329 {
330 b4_defact
331 };
332
333 /* YYPGOTO[[NTERM-NUM]]. */
334 const short
335 yy::b4_name::pgoto_[[]] =
336 {
337 b4_pgoto
338 };
339
340 /* YYDEFGOTO[[NTERM-NUM]]. */
341 const short
342 yy::b4_name::defgoto_[[]] =
343 {
344 b4_defgoto
345 };
346
347 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
348 positive, shift that token. If negative, reduce the rule which
349 number is the opposite. If zero, do what YYDEFACT says. */
350 const short
351 yy::b4_name::table_[[]] =
352 {
353 b4_table
354 };
355
356 /* YYCHECK. */
357 const short
358 yy::b4_name::check_[[]] =
359 {
360 b4_check
361 };
362
363 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
364 const short
365 yy::b4_name::line_[[]] =
366 {
367 b4_line
368 };
369
370 /*YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
371 const short
372 yy::b4_name::r1_[[]] =
373 {
374 b4_r1
375 };
376
377 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
378 const short
379 yy::b4_name::r2_[[]] =
380 {
381 b4_r2
382 };
383
384 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
385 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
386 const char*
387 const yy::b4_name::name_[[]] =
388 {
389 b4_tname
390 };
391
392 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
393 const short
394 yy::b4_name::rhs_[[]] =
395 {
396 b4_rhs
397 };
398
399 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
400 YYRHS. */
401 const short
402 yy::b4_name::prhs_[[]] =
403 {
404 b4_prhs
405 };
406
407 /* YYRLINE[[YYN]] -- Source line where rule number YYN was defined. */
408 const short
409 yy::b4_name::rline_[[]] =
410 {
411 b4_rline
412 };
413
414 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
415 char
416 yy::b4_name::translate (int token)
417 {
418 static
419 const char
420 translate_[[]] =
421 {
422 b4_translate
423 };
424 return ((unsigned)(token) <= b4_maxtok ? translate_[[token]] : b4_nsym);
425 }
426
427 const int yy::b4_name::eof = 0;
428 const int yy::b4_name::empty = -2;
429
430 b4_epilogue
431
432 #output "stack.hh"
433 #ifndef BISON_STACK_HH
434 # define BISON_STACK_HH
435
436 #include <vector>
437
438 namespace yy
439 {
440 template < class T, class S = std::vector< T > >
441 class Stack
442 {
443 public:
444
445 typedef typename S::iterator Iterator;
446 typedef typename S::const_iterator ConstIterator;
447
448 Stack () : seq_ ()
449 {
450 }
451
452 Stack (unsigned n) : seq_ (n)
453 {
454 }
455
456 inline
457 T&
458 operator [[]] (int index)
459 {
460 return seq_[[seq_.size () - 1 + index]];
461 }
462
463 inline
464 const T&
465 operator [[]] (int index) const
466 {
467 return seq_[[seq_.size () - 1 + index]];
468 }
469
470 inline
471 void
472 push (const T& t)
473 {
474 seq_.push_back (t);
475 }
476
477 inline
478 void
479 pop (unsigned n = 1)
480 {
481 for (; n; --n)
482 seq_.pop_back ();
483 }
484
485 inline
486 void
487 reserve (unsigned n)
488 {
489 seq_.reserve (n);
490 }
491
492 inline ConstIterator begin () const { return seq_.begin (); }
493 inline ConstIterator end () const { return seq_.end (); }
494
495 private:
496
497 S seq_;
498 };
499 }
500
501 #endif // not BISON_STACK_HH
502
503 #output "location.hh"
504 #ifndef BISON_LOCATION_HH
505 # define BISON_LOCATION_HH
506
507 namespace yy
508 {
509 struct Position
510 {
511 int line;
512 int column;
513 };
514
515 struct Location
516 {
517 Position first;
518 Position last;
519 };
520 }
521
522 #endif // not BISON_LOCATION_HH