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