]> git.saurik.com Git - bison.git/blame - data/bison.c++
* data/bison.c++: Adjust to the M4 back end.
[bison.git] / data / bison.c++
CommitLineData
3f3eed27 1#output "b4_output_prefix[]b4_output_infix[].hh"
2b548aa6
RA
2/* -*- C++ -*- */
3
4/* A Bison parser, made from %%filename,
5 by GNU bison %%version. */
6
7/* Skeleton output parser for bison,
3f3eed27 8 Copyright (C) 2002 Free Software Foundation, Inc.
2b548aa6
RA
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{
3f3eed27
AD
36 class b4_name;
37
2b548aa6
RA
38 template < typename P >
39 struct Traits
40 {
41 };
42
43 template < >
3f3eed27 44 struct Traits< b4_name >
2b548aa6
RA
45 {
46 typedef int StateType;
3f3eed27 47 typedef b4_stype SemanticType;
2b548aa6
RA
48 typedef Location LocationType;
49 };
50}
51
3f3eed27 52b4_prologue
2b548aa6
RA
53
54namespace yy
55{
3f3eed27 56 class b4_name
2b548aa6
RA
57 {
58 public:
3f3eed27
AD
59
60 typedef Traits< b4_name >::StateType StateType;
61 typedef Traits< b4_name >::SemanticType SemanticType;
62 typedef Traits< b4_name >::LocationType LocationType;
2b548aa6
RA
63
64 typedef Stack< StateType > StateStack;
65 typedef Stack< SemanticType > SemanticStack;
66 typedef Stack< LocationType > LocationStack;
67
3f3eed27 68 b4_name () : debug (1)
2b548aa6
RA
69 {
70 looka = empty;
71 }
72
73 virtual int parse ();
74
75 private:
3f3eed27 76
2b548aa6 77 /* Call to lexical analyser. */
3f3eed27
AD
78 virtual
79 void
80 lex ()
81 {
2b548aa6
RA
82 looka = yylex (&value);
83 }
84
85 /* Stacks. */
86 StateStack state_stack;
87 SemanticStack semantic_stack;
88 LocationStack location_stack;
89
90 /* Tables. */
3f3eed27
AD
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_[[]];
2b548aa6
RA
101
102 /* More tables, for debugging. */
3f3eed27 103 /* FIXME: These tables should not be printed when not in debug
2b548aa6 104 mode. %%ifdef? */
3f3eed27
AD
105 static const short rhs_[[]];
106 static const short prhs_[[]];
107 static const short rline_[[]];
2b548aa6
RA
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;
3f3eed27 121
2b548aa6
RA
122 /* Lookahead. */
123 int looka;
124 int ilooka;
3f3eed27 125
2b548aa6
RA
126 /* @$ and $$. */
127 SemanticType value;
128 LocationType location;
129 };
130}
131
3f3eed27
AD
132#output "b4_output_prefix[]b4_output_infix[].cc"
133#include "b4_output_prefix[]b4_output_infix.hh"
2b548aa6
RA
134
135/* Enable debugging if requested. */
3f3eed27 136#if b4_debug
2b548aa6
RA
137
138# include <cstdio>
139# define YYFPRINTF std::fprintf
140
141# define YYDPRINTF(Args) \
142do { \
143 if (debug) \
144 YYFPRINTF Args; \
145} while (0)
3f3eed27
AD
146/* Nonzero means print parse trace. Since this is uninitialized, it
147 does not stop multiple parsers from coexisting. */
2b548aa6
RA
148int yydebug;
149#else /* !YYDEBUG */
150# define YYDPRINTF(Args)
151#endif /* !YYDEBUG */
152
153/* Tokens. */
3f3eed27 154b4_tokendef
2b548aa6
RA
155
156int
3f3eed27 157yy::b4_name::parse ()
2b548aa6
RA
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? */
3f3eed27
AD
166 state_stack.reserve (b4_initdepth);
167 semantic_stack.reserve (b4_initdepth);
168 location_stack.reserve (b4_initdepth);
2b548aa6
RA
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. */
3f3eed27
AD
183 n = pact_[[state]];
184 if (n == b4_flag)
2b548aa6
RA
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);
3f3eed27
AD
204#if b4_debug
205 YYFPRINTF (stderr, "Next token is %d (%s", looka, name_[[ilooka]]);
2b548aa6
RA
206 YYFPRINTF (stderr, ")\n");
207#endif
208 }
209
210 n += ilooka;
3f3eed27 211 if (n < 0 || n > b4_last || check_[[n]] != ilooka)
2b548aa6
RA
212 goto yydefault;
213
214 /* Reduce or error. */
3f3eed27 215 n = table_[[n]];
2b548aa6
RA
216 if (n < 0)
217 {
3f3eed27 218 if (n == b4_flag)
2b548aa6
RA
219 goto yyerrlab;
220 else
221 {
222 n = -n;
223 goto yyreduce;
224 }
225 }
226 else if (n == 0)
227 goto yyerrlab;
3f3eed27 228
2b548aa6 229 /* Accept? */
3f3eed27 230 if (n == b4_final)
2b548aa6
RA
231 goto yyacceptlab;
232
233 /* Shift the lookahead token. */
3f3eed27 234 YYDPRINTF ((stderr, "Shifting token %d (%s), ", looka, name_[[ilooka]]));
2b548aa6
RA
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:
3f3eed27 247 n = defact_[[state]];
2b548aa6
RA
248 if (n == 0)
249 goto yyerrlab;
250
251 /* Reduce. */
252 yyreduce:
3f3eed27
AD
253 len = r2_[[n]];
254 value = semantic_stack[[1 - len]];
255 location = location_stack[[1 - len]];
2b548aa6 256
3f3eed27 257#if b4_debug
2b548aa6
RA
258 if (debug)
259 {
3f3eed27
AD
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]]]);
2b548aa6
RA
265 }
266#endif
3f3eed27 267
2b548aa6
RA
268 {
269 SemanticType& yyval (value);
270 SemanticStack& yyvsp (semantic_stack);
271
272 switch (n)
273 {
3f3eed27 274 b4_actions
2b548aa6
RA
275 }
276 }
3f3eed27 277
2b548aa6
RA
278 state_stack.pop (len);
279 semantic_stack.pop (len);
280 location_stack.pop (len);
281
3f3eed27 282#if b4_debug
2b548aa6
RA
283 if (debug)
284 {
285 YYFPRINTF (stderr, "state stack now");
3f3eed27 286 for (StateStack::ConstIterator i = state_stack.begin ();
2b548aa6
RA
287 i != state_stack.end (); ++i)
288 YYFPRINTF (stderr, " %d", *i);
289 YYFPRINTF (stderr, "\n");
290 }
291#endif
3f3eed27 292
2b548aa6
RA
293 semantic_stack.push (value);
294 location_stack.push (location);
295
296 /* Shift the result of the reduction. */
3f3eed27
AD
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]];
2b548aa6 301 else
3f3eed27 302 state = defgoto_[[n - b4_ntokens]];
2b548aa6
RA
303 goto yynewstate;
304
305 /* Report and recover from errors. This is very incomplete. */
306 yyerrlab:
3f3eed27
AD
307 // FIXME: Need something like yyerror?
308 std::cerr << "Parse error." << std::endl;
2b548aa6 309 return 1;
3f3eed27 310
2b548aa6
RA
311 /* Accept. */
312 yyacceptlab:
313 return 0;
314}
315
3f3eed27 316/* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
2b548aa6 317 STATE-NUM. */
3f3eed27
AD
318const short
319yy::b4_name::pact_[[]] =
2b548aa6 320{
3f3eed27 321 b4_pact
2b548aa6
RA
322};
323
3f3eed27 324/* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
2b548aa6
RA
325 doesn't specify something else to do. Zero means the default is an
326 error. */
3f3eed27
AD
327const short
328yy::b4_name::defact_[[]] =
2b548aa6 329{
3f3eed27 330 b4_defact
2b548aa6
RA
331};
332
3f3eed27
AD
333/* YYPGOTO[[NTERM-NUM]]. */
334const short
335yy::b4_name::pgoto_[[]] =
2b548aa6 336{
3f3eed27 337 b4_pgoto
2b548aa6
RA
338};
339
3f3eed27
AD
340/* YYDEFGOTO[[NTERM-NUM]]. */
341const short
342yy::b4_name::defgoto_[[]] =
2b548aa6 343{
3f3eed27 344 b4_defgoto
2b548aa6
RA
345};
346
3f3eed27 347/* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
2b548aa6
RA
348 positive, shift that token. If negative, reduce the rule which
349 number is the opposite. If zero, do what YYDEFACT says. */
3f3eed27
AD
350const short
351yy::b4_name::table_[[]] =
2b548aa6 352{
3f3eed27 353 b4_table
2b548aa6
RA
354};
355
356/* YYCHECK. */
3f3eed27
AD
357const short
358yy::b4_name::check_[[]] =
2b548aa6 359{
3f3eed27 360 b4_check
2b548aa6
RA
361};
362
3f3eed27 363/* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
2b548aa6 364const short
3f3eed27 365yy::b4_name::line_[[]] =
2b548aa6 366{
3f3eed27 367 b4_line
2b548aa6
RA
368};
369
3f3eed27
AD
370/*YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
371const short
372yy::b4_name::r1_[[]] =
2b548aa6 373{
3f3eed27 374 b4_r1
2b548aa6
RA
375};
376
3f3eed27
AD
377/* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
378const short
379yy::b4_name::r2_[[]] =
2b548aa6 380{
3f3eed27 381 b4_r2
2b548aa6
RA
382};
383
3f3eed27 384/* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
2b548aa6
RA
385 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
386const char*
3f3eed27 387const yy::b4_name::name_[[]] =
2b548aa6 388{
3f3eed27 389 b4_tname
2b548aa6
RA
390};
391
392/* YYRHS -- A `-1'-separated list of the rules' RHS. */
3f3eed27
AD
393const short
394yy::b4_name::rhs_[[]] =
2b548aa6 395{
3f3eed27 396 b4_rhs
2b548aa6
RA
397};
398
3f3eed27 399/* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
2b548aa6 400 YYRHS. */
3f3eed27
AD
401const short
402yy::b4_name::prhs_[[]] =
2b548aa6 403{
3f3eed27 404 b4_prhs
2b548aa6
RA
405};
406
3f3eed27
AD
407/* YYRLINE[[YYN]] -- Source line where rule number YYN was defined. */
408const short
409yy::b4_name::rline_[[]] =
2b548aa6 410{
3f3eed27 411 b4_rline
2b548aa6
RA
412};
413
414/* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
415char
3f3eed27 416yy::b4_name::translate (int token)
2b548aa6 417{
3f3eed27
AD
418 static
419 const char
420 translate_[[]] =
2b548aa6 421 {
3f3eed27 422 b4_translate
2b548aa6 423 };
3f3eed27 424 return ((unsigned)(token) <= b4_maxtok ? translate_[[token]] : b4_nsym);
2b548aa6
RA
425}
426
3f3eed27
AD
427const int yy::b4_name::eof = 0;
428const int yy::b4_name::empty = -2;
2b548aa6 429
3f3eed27 430b4_epilogue
2b548aa6 431
3f3eed27 432#output "stack.hh"
2b548aa6
RA
433#ifndef BISON_STACK_HH
434# define BISON_STACK_HH
435
436#include <vector>
437
438namespace 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&
3f3eed27 458 operator [[]] (int index)
2b548aa6 459 {
3f3eed27 460 return seq_[[seq_.size () - 1 + index]];
2b548aa6
RA
461 }
462
463 inline
464 const T&
3f3eed27 465 operator [[]] (int index) const
2b548aa6 466 {
3f3eed27 467 return seq_[[seq_.size () - 1 + index]];
2b548aa6
RA
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 }
3f3eed27 484
2b548aa6
RA
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
3f3eed27 503#output "location.hh"
2b548aa6
RA
504#ifndef BISON_LOCATION_HH
505# define BISON_LOCATION_HH
506
507namespace 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