]> git.saurik.com Git - bison.git/blame - data/lalr1.cc
* tests/actions.at (_AT_CHECK_PRINTER_AND_DESTRUCTOR): Adjust
[bison.git] / data / lalr1.cc
CommitLineData
a75c057f 1m4_divert(-1)
60491a94 2# C++ skeleton for Bison
779e7ceb 3# Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
60491a94
AD
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18# 02111-1307 USA
19
66d30cd4
AD
20## ---------------- ##
21## Default values. ##
22## ---------------- ##
23
66d30cd4
AD
24# Default Parser class name.
25m4_define_default([b4_parser_class_name], [Parser])
26
27
28
29## ----------------- ##
30## Semantic Values. ##
31## ----------------- ##
32
33
82b6cb3f
AD
34# b4_lhs_value([TYPE])
35# --------------------
36# Expansion of $<TYPE>$.
37m4_define([b4_lhs_value],
38[yyval[]m4_ifval([$1], [.$1])])
39
40
41# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
42# --------------------------------------
43# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
44# symbols on RHS.
45m4_define([b4_rhs_value],
6ad762a8 46[semantic_stack_@{m4_eval([$1 - $2])@}m4_ifval([$3], [.$3])])
82b6cb3f 47
66d30cd4 48m4_define_default([b4_location_type], [Location])
82b6cb3f
AD
49
50# b4_lhs_location()
51# -----------------
52# Expansion of @$.
53m4_define([b4_lhs_location],
54[yyloc])
55
56
57# b4_rhs_location(RULE-LENGTH, NUM)
58# ---------------------------------
59# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
60# on RHS.
61m4_define([b4_rhs_location],
6ad762a8 62[location_stack_@{m4_eval([$1 - $2])@}])
82b6cb3f
AD
63
64
af27eacb 65m4_define([b4_inherit],
0d8bed56 66 [m4_ifdef([b4_root],
af27eacb
RA
67 [: public b4_root
68],
69 [])])
70
71m4_define([b4_param],
72 [m4_ifdef([b4_root],
73 [,
74 const Param& param],
75 [])])
76
77m4_define([b4_constructor],
78 [m4_ifdef([b4_root],
79 [b4_root (param),
80 ],
81 [])])
82
98254360 83
caf37a36
ADL
84# b4_parse_param_decl
85# -------------------
86# Constructor's extra arguments.
87m4_define([b4_parse_param_decl],
88 [m4_ifset([b4_parse_param], [, b4_c_ansi_formals(b4_parse_param)])])
89
90# b4_parse_param_cons
91# -------------------
92# constructor's extra initialisations.
93m4_define([b4_parse_param_cons],
94 [m4_ifset([b4_parse_param],
95 [,
96 b4_cc_constructor_calls(b4_parse_param)])])
97m4_define([b4_cc_constructor_calls],
98 [m4_map_sep([b4_cc_constructor_call], [,
99 ], [$@])])
100m4_define([b4_cc_constructor_call],
101 [$2($2)])
102
103# b4_parse_param_vars
104# -------------------
105# Extra instance variables.
106m4_define([b4_parse_param_vars],
107 [m4_ifset([b4_parse_param],
108 [
109 /* User arguments. */
110b4_cc_var_decls(b4_parse_param)])])
111m4_define([b4_cc_var_decls],
112 [m4_map_sep([b4_cc_var_decl], [
113], [$@])])
114m4_define([b4_cc_var_decl],
115 [ $1;])
116
a5eb1ed2
AD
117
118# b4_yysymprint_generate(FUNCTION-DECLARATOR)
119# -------------------------------------------
120# Generate the "symprint_" member function.
121m4_define([b4_yysymprint_generate],
122[[/*--------------------------------.
123| Print this symbol on YYOUTPUT. |
124`--------------------------------*/
125
126]void
127yy::b4_parser_class_name::symprint_ (int yytype, const SemanticType *yyvaluep, const LocationType *yylocationp)[
128{
129 /* Pacify ``unused variable'' warnings. */
130 (void) yyvaluep;
131 (void) yylocationp;
132
133 cdebug_ << (yytype < ntokens_ ? "token" : "nterm")
134 << ' ' << name_[yytype] << " (";
135 switch (yytype)
136 {
137]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
138[ default:
139 break;
140 }
141 cdebug_ << ')';
142}
143]])
144
145
c2729758
ADL
146# b4_cxx_destruct_def(IGNORED-ARGUMENTS)
147# --------------------------------------
148# Declare the destruct_ method.
149m4_define([b4_cxx_destruct_def],
150[void
151yy::b4_parser_class_name::destruct_ (int yytype, SemanticType *yyvaluep, LocationType *yylocationp)[]dnl
152])
153
a5eb1ed2 154
b526ee61
AD
155# We do want M4 expansion after # for CPP macros.
156m4_changecom()
98254360 157m4_divert(0)dnl
b61c6978
PE
158m4_if(b4_defines_flag, 0, [],
159[@output @output_header_name@
fb8135fa 160b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
779e7ceb 161 [2002, 2003, 2004])[
c5e3e510
AD
162/* FIXME: This is wrong, we want computed header guards.
163 I don't know why the macros are missing now. :( */
164#ifndef PARSER_HEADER_H
165# define PARSER_HEADER_H
2b548aa6
RA
166
167#include "stack.hh"
2b548aa6
RA
168#include "location.hh"
169
8d69a1a3 170#include <string>
717be197 171#include <iostream>
50997c6e 172
4162fa07 173/* Using locations. */
c5e3e510 174#define YYLSP_NEEDED ]b4_locations_flag[
4162fa07 175
c5e3e510 176]b4_token_defines(b4_tokens)[
17acead5 177
0dd1580a 178/* Copy the first part of user declarations. */
c5e3e510 179]b4_pre_prologue[
4162fa07 180
996b1c7e 181]/* Line __line__ of lalr1.cc. */
c5e3e510 182b4_syncline([@oline@], [@ofile@])[
e96c9728 183
69e2658b
RA
184/* Enabling traces. */
185#ifndef YYDEBUG
c5e3e510 186# define YYDEBUG ]b4_debug[
69e2658b
RA
187#endif
188
189/* Enabling verbose error message. */
190#ifndef YYERROR_VERBOSE
c5e3e510 191# define YYERROR_VERBOSE ]b4_error_verbose[
69e2658b
RA
192#endif
193
c0659069
PE
194#ifdef YYSTYPE
195typedef YYSTYPE yystype;
196#else
c5e3e510 197]m4_ifdef([b4_stype],
437c2d80 198[b4_syncline([b4_stype_line], [b4_filename])
e96c9728 199typedef union b4_stype yystype;
996b1c7e 200/* Line __line__ of lalr1.cc. */
6ad762a8 201b4_syncline([@oline@], [@ofile@])],
c5e3e510 202[typedef int yystype;])[
98254360
RA
203# define YYSTYPE yystype
204#endif
205
0dd1580a 206/* Copy the second part of user declarations. */
c5e3e510 207]b4_post_prologue[
0dd1580a 208
996b1c7e 209]/* Line __line__ of lalr1.cc. */
c5e3e510 210b4_syncline([@oline@], [@ofile@])[
374f5a14
RA
211#ifndef YYLLOC_DEFAULT
212# define YYLLOC_DEFAULT(Current, Rhs, N) \
0ae99356 213 ((Current).end = Rhs[N].end)
374f5a14
RA
214#endif
215
2b548aa6
RA
216namespace yy
217{
c5e3e510 218 class ]b4_parser_class_name[;
3f3eed27 219
2b548aa6
RA
220 template < typename P >
221 struct Traits
222 {
223 };
224
225 template < >
c5e3e510 226 struct Traits< ]b4_parser_class_name[ >
2b548aa6 227 {
c5e3e510
AD
228 typedef ]b4_int_type_for([b4_translate])[ TokenNumberType;
229 typedef ]b4_int_type_for([b4_rhs])[ RhsNumberType;
50997c6e
RA
230 typedef int StateType;
231 typedef yystype SemanticType;
c5e3e510 232 typedef ]b4_location_type[ LocationType;
2b548aa6
RA
233 };
234}
235
2b548aa6
RA
236namespace yy
237{
c5e3e510 238 class ]b4_parser_class_name b4_inherit[
2b548aa6
RA
239 {
240 public:
3f3eed27 241
c5e3e510
AD
242 typedef Traits< ]b4_parser_class_name[ >::TokenNumberType TokenNumberType;
243 typedef Traits< ]b4_parser_class_name[ >::RhsNumberType RhsNumberType;
244 typedef Traits< ]b4_parser_class_name[ >::StateType StateType;
245 typedef Traits< ]b4_parser_class_name[ >::SemanticType SemanticType;
246 typedef Traits< ]b4_parser_class_name[ >::LocationType LocationType;
2b548aa6
RA
247
248 typedef Stack< StateType > StateStack;
249 typedef Stack< SemanticType > SemanticStack;
250 typedef Stack< LocationType > LocationStack;
251
8d69a1a3 252#if YYLSP_NEEDED
c5e3e510 253 ]b4_parser_class_name[ (bool debug,
caf37a36 254 LocationType initlocation][]b4_param[]b4_parse_param_decl[) :
c5e3e510 255 ]b4_constructor[][debug_ (debug),
717be197 256 cdebug_ (std::cerr),
caf37a36 257 initlocation_ (initlocation)]b4_parse_param_cons[
8d69a1a3 258#else
caf37a36 259 ]b4_parser_class_name[ (bool debug][]b4_param[]b4_parse_param_decl[) :
c5e3e510 260 ]b4_constructor[][debug_ (debug),
caf37a36 261 cdebug_ (std::cerr)]b4_parse_param_cons[
8d69a1a3 262#endif
50997c6e 263 {
50997c6e
RA
264 }
265
c5e3e510 266 virtual ~]b4_parser_class_name[ ()
2b548aa6 267 {
2b548aa6
RA
268 }
269
270 virtual int parse ();
271
272 private:
3f3eed27 273
69e2658b 274 virtual void lex_ ();
4bb2bc3f 275 virtual void error_ ();
4aacc3a7 276 virtual void print_ ();
a08460b0 277 virtual void report_syntax_error_ ();
a5eb1ed2
AD
278#if YYDEBUG
279 virtual void symprint_ (int yytype,
280 const SemanticType *yyvaluep,
281 const LocationType *yylocationp);
282#endif /* ! YYDEBUG */
283
4bb2bc3f 284
2b548aa6 285 /* Stacks. */
69e2658b
RA
286 StateStack state_stack_;
287 SemanticStack semantic_stack_;
288 LocationStack location_stack_;
2b548aa6
RA
289
290 /* Tables. */
c5e3e510 291 static const ]b4_int_type_for([b4_pact])[ pact_[];
0c3c1f98 292 static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ pact_ninf_;
c5e3e510
AD
293 static const ]b4_int_type_for([b4_defact])[ defact_[];
294 static const ]b4_int_type_for([b4_pgoto])[ pgoto_[];
295 static const ]b4_int_type_for([b4_defgoto])[ defgoto_[];
296 static const ]b4_int_type_for([b4_table])[ table_[];
0c3c1f98 297 static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ table_ninf_;
c5e3e510 298 static const ]b4_int_type_for([b4_check])[ check_[];
c2729758 299 static const ]b4_int_type_for([b4_stos])[ stos_[];
c5e3e510
AD
300 static const ]b4_int_type_for([b4_r1])[ r1_[];
301 static const ]b4_int_type_for([b4_r2])[ r2_[];
69e2658b
RA
302
303#if YYDEBUG || YYERROR_VERBOSE
c5e3e510 304 static const char* const name_[];
69e2658b 305#endif
2b548aa6
RA
306
307 /* More tables, for debugging. */
69e2658b 308#if YYDEBUG
c5e3e510
AD
309 static const RhsNumberType rhs_[];
310 static const ]b4_int_type_for([b4_prhs])[ prhs_[];
311 static const ]b4_int_type_for([b4_rline])[ rline_[];
c5e3e510 312 static const ]b4_int_type_for([b4_toknum])[ token_number_[];
5348bfbe 313 virtual void reduce_print_ (int yyrule);
25f66e1a 314 virtual void stack_print_ ();
69e2658b 315#endif
2b548aa6
RA
316
317 /* Even more tables. */
680e8701 318 static inline TokenNumberType translate_ (int token);
c2729758
ADL
319 static inline void destruct_ (int yytype, SemanticType *yyvaluep,
320 LocationType *yylocationp);
2b548aa6 321
762a801e 322 /* Constants. */
50997c6e 323 static const int eof_;
39912f52 324 /* LAST_ -- Last index in TABLE_. */
69e2658b 325 static const int last_;
69e2658b 326 static const int nnts_;
50997c6e 327 static const int empty_;
69e2658b 328 static const int final_;
762a801e
RA
329 static const int terror_;
330 static const int errcode_;
50997c6e 331 static const int ntokens_;
779e7ceb 332 static const unsigned int user_token_number_max_;
007a50a4 333 static const TokenNumberType undef_token_;
2b548aa6
RA
334
335 /* State. */
69e2658b
RA
336 int n_;
337 int len_;
69e2658b 338 int state_;
4bb2bc3f 339
a08460b0
AD
340 /* Error handling. */
341 int nerrs_;
342 int errstatus_;
343
717be197
AD
344 /* Debugging. */
345 int debug_;
346 std::ostream &cdebug_;
347
8dd162d3 348 /* Look-ahead and look-ahead in internal form. */
69e2658b
RA
349 int looka_;
350 int ilooka_;
4bb2bc3f
RA
351
352 /* Message. */
353 std::string message;
354
8dd162d3 355 /* Semantic value and location of look-ahead token. */
2b548aa6
RA
356 SemanticType value;
357 LocationType location;
c2729758
ADL
358 /* Beginning of the last erroneous token popped off. */
359 Position error_start_;
8d69a1a3 360
6ad762a8 361 /* @@$ and $$. */
374f5a14
RA
362 SemanticType yyval;
363 LocationType yyloc;
364
8d69a1a3
RA
365 /* Initial location. */
366 LocationType initlocation_;
caf37a36 367]b4_parse_param_vars[
2b548aa6
RA
368 };
369}
370
c5e3e510 371#endif /* ! defined PARSER_HEADER_H */]
b61c6978 372])dnl
6ad762a8 373@output @output_parser_name@
fb8135fa 374b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
779e7ceb 375 [2002, 2003, 2004])
98254360 376
b61c6978 377m4_if(b4_defines_flag, 0, [], [#include @output_header_name@])[
50997c6e 378
2b548aa6 379/* Enable debugging if requested. */
50997c6e 380#if YYDEBUG
717be197 381# define YYCDEBUG if (debug_) cdebug_
5348bfbe
AD
382# define YY_REDUCE_PRINT(Rule) \
383do { \
384 if (debug_) \
385 reduce_print_ (Rule); \
386} while (0)
25f66e1a
AD
387# define YY_STACK_PRINT() \
388do { \
389 if (debug_) \
390 stack_print_ (); \
391} while (0)
2b548aa6 392#else /* !YYDEBUG */
717be197 393# define YYCDEBUG if (0) cdebug_
5348bfbe 394# define YY_REDUCE_PRINT(Rule)
25f66e1a 395# define YY_STACK_PRINT()
2b548aa6
RA
396#endif /* !YYDEBUG */
397
60a777aa
AD
398#define YYACCEPT goto yyacceptlab
399#define YYABORT goto yyabortlab
47301314 400#define YYERROR goto yyerrorlab
60a777aa 401
a5eb1ed2
AD
402#if YYDEBUG
403]b4_yysymprint_generate([b4_cxx_symprint_def])[
404#endif /* ! YYDEBUG */
c2729758 405]b4_yydestruct_generate([b4_cxx_destruct_def])[
60a777aa 406
2b548aa6 407int
c5e3e510 408yy::]b4_parser_class_name[::parse ()
2b548aa6 409{
a08460b0
AD
410 nerrs_ = 0;
411 errstatus_ = 0;
4bb2bc3f 412
3ae831b4
AD
413 /* Initialize the stacks. The initial state will be pushed in
414 yynewstate, since the latter expects the semantical and the
415 location values to have been already stored, initialize these
416 stacks with a primary value. */
69e2658b
RA
417 state_stack_ = StateStack (0);
418 semantic_stack_ = SemanticStack (1);
419 location_stack_ = LocationStack (1);
2b548aa6 420
2b548aa6 421 /* Start. */
69e2658b
RA
422 state_ = 0;
423 looka_ = empty_;
8d69a1a3
RA
424#if YYLSP_NEEDED
425 location = initlocation_;
426#endif
717be197 427 YYCDEBUG << "Starting parse" << std::endl;
2b548aa6
RA
428
429 /* New state. */
430 yynewstate:
69e2658b 431 state_stack_.push (state_);
717be197 432 YYCDEBUG << "Entering state " << state_ << std::endl;
50997c6e 433 goto yybackup;
2b548aa6
RA
434
435 /* Backup. */
436 yybackup:
437
8dd162d3 438 /* Try to take a decision without look-ahead. */
c5e3e510 439 n_ = pact_[state_];
12b0043a 440 if (n_ == pact_ninf_)
2b548aa6
RA
441 goto yydefault;
442
8dd162d3 443 /* Read a look-ahead token. */
69e2658b 444 if (looka_ == empty_)
2b548aa6 445 {
717be197 446 YYCDEBUG << "Reading a token: ";
69e2658b 447 lex_ ();
2b548aa6
RA
448 }
449
450 /* Convert token to internal form. */
69e2658b 451 if (looka_ <= 0)
2b548aa6 452 {
69e2658b
RA
453 looka_ = eof_;
454 ilooka_ = 0;
717be197 455 YYCDEBUG << "Now at end of input." << std::endl;
2b548aa6
RA
456 }
457 else
458 {
69e2658b 459 ilooka_ = translate_ (looka_);
50997c6e
RA
460#if YYDEBUG
461 if (debug_)
462 {
a5eb1ed2
AD
463 cdebug_ << "Next token is ";
464 symprint_ (ilooka_, &value, &location);
465 cdebug_ << std::endl;
50997c6e 466 }
2b548aa6
RA
467#endif
468 }
469
a5eb1ed2 470 /* If the proper action on seeing token ILOOKA_ is to reduce or to
47301314 471 detect an error, take that action. */
69e2658b 472 n_ += ilooka_;
c5e3e510 473 if (n_ < 0 || last_ < n_ || check_[n_] != ilooka_)
2b548aa6
RA
474 goto yydefault;
475
476 /* Reduce or error. */
c5e3e510 477 n_ = table_[n_];
69e2658b 478 if (n_ < 0)
2b548aa6 479 {
12b0043a 480 if (n_ == table_ninf_)
2b548aa6
RA
481 goto yyerrlab;
482 else
483 {
69e2658b 484 n_ = -n_;
2b548aa6
RA
485 goto yyreduce;
486 }
487 }
69e2658b 488 else if (n_ == 0)
2b548aa6 489 goto yyerrlab;
a75c057f 490
2b548aa6 491 /* Accept? */
69e2658b 492 if (n_ == final_)
2b548aa6
RA
493 goto yyacceptlab;
494
8dd162d3 495 /* Shift the look-ahead token. */
fc049e9c 496#if YYDEBUG
717be197 497 YYCDEBUG << "Shifting token " << looka_
fc049e9c
AD
498 << " (" << name_[ilooka_] << "), ";
499#endif
2b548aa6
RA
500
501 /* Discard the token being shifted unless it is eof. */
69e2658b
RA
502 if (looka_ != eof_)
503 looka_ = empty_;
2b548aa6 504
69e2658b
RA
505 semantic_stack_.push (value);
506 location_stack_.push (location);
8d69a1a3
RA
507
508 /* Count tokens shifted since error; after three, turn off error
509 status. */
a08460b0
AD
510 if (errstatus_)
511 --errstatus_;
8d69a1a3 512
69e2658b 513 state_ = n_;
2b548aa6
RA
514 goto yynewstate;
515
47301314
AD
516/*-----------------------------------------------------------.
517| yydefault -- do the default action for the current state. |
518`-----------------------------------------------------------*/
2b548aa6 519 yydefault:
c5e3e510 520 n_ = defact_[state_];
69e2658b 521 if (n_ == 0)
2b548aa6 522 goto yyerrlab;
50997c6e 523 goto yyreduce;
2b548aa6 524
47301314
AD
525/*-----------------------------.
526| yyreduce -- Do a reduction. |
527`-----------------------------*/
2b548aa6 528 yyreduce:
c5e3e510 529 len_ = r2_[n_];
47301314
AD
530 /* If LEN_ is nonzero, implement the default value of the action:
531 `$$ = $1'. Otherwise, use the top of the stack.
532
533 Otherwise, the following line sets YYVAL to garbage.
534 This behavior is undocumented and Bison
535 users should not rely upon it. */
69e2658b 536 if (len_)
50997c6e 537 {
c5e3e510
AD
538 yyval = semantic_stack_[len_ - 1];
539 yyloc = location_stack_[len_ - 1];
50997c6e
RA
540 }
541 else
69e2658b 542 {
c5e3e510
AD
543 yyval = semantic_stack_[0];
544 yyloc = location_stack_[0];
50997c6e 545 }
2b548aa6 546
374f5a14
RA
547 if (len_)
548 {
549 Slice< LocationType, LocationStack > slice (location_stack_, len_);
550 YYLLOC_DEFAULT (yyloc, slice, len_);
551 }
5348bfbe 552 YY_REDUCE_PRINT (n_);
45119f04
RA
553 switch (n_)
554 {
c5e3e510 555 ]b4_actions[
45119f04 556 }
a75c057f 557
996b1c7e 558]/* Line __line__ of lalr1.cc. */
c5e3e510 559b4_syncline([@oline@], [@ofile@])[
06446ccf 560
69e2658b
RA
561 state_stack_.pop (len_);
562 semantic_stack_.pop (len_);
563 location_stack_.pop (len_);
2b548aa6 564
25f66e1a 565 YY_STACK_PRINT ();
a75c057f 566
374f5a14
RA
567 semantic_stack_.push (yyval);
568 location_stack_.push (yyloc);
2b548aa6
RA
569
570 /* Shift the result of the reduction. */
c5e3e510
AD
571 n_ = r1_[n_];
572 state_ = pgoto_[n_ - ntokens_] + state_stack_[0];
573 if (0 <= state_ && state_ <= last_ && check_[state_] == state_stack_[0])
574 state_ = table_[state_];
2b548aa6 575 else
c5e3e510 576 state_ = defgoto_[n_ - ntokens_];
2b548aa6
RA
577 goto yynewstate;
578
47301314
AD
579/*------------------------------------.
580| yyerrlab -- here on detecting error |
581`------------------------------------*/
2b548aa6 582 yyerrlab:
4bb2bc3f 583 /* If not already recovering from an error, report this error. */
a08460b0 584 report_syntax_error_ ();
a75c057f 585
c2729758 586 error_start_ = location.begin;
a08460b0 587 if (errstatus_ == 3)
762a801e 588 {
8dd162d3 589 /* If just tried and failed to reuse look-ahead token after an
762a801e
RA
590 error, discard it. */
591
592 /* Return failure if at end of input. */
47301314
AD
593 if (looka_ <= eof_)
594 {
595 /* If at end of input, pop the error token,
596 then the rest of the stack, then return failure. */
597 if (looka_ == eof_)
598 for (;;)
599 {
c2729758 600 error_start_ = location_stack_[0].begin;
47301314
AD
601 state_stack_.pop ();
602 semantic_stack_.pop ();
603 location_stack_.pop ();
604 if (state_stack_.height () == 1)
605 YYABORT;
a5eb1ed2
AD
606#if YYDEBUG
607 if (debug_)
608 {
609 cdebug_ << "Error: popping ";
610 symprint_ (stos_[state_stack_[0]],
611 &semantic_stack_[0],
612 &location_stack_[0]);
613 cdebug_ << std::endl;
614 }
615#endif // YYDEBUG
616 destruct_ (stos_[state_stack_[0]],
c2729758
ADL
617 &semantic_stack_[0],
618 &location_stack_[0]);
47301314
AD
619 }
620 }
621 else
622 {
7548fed2 623#if YYDEBUG
a5eb1ed2
AD
624 if (debug_)
625 {
626 cdebug_ << "Error: discarding ";
627 symprint_ (ilooka_, &value, &location);
628 cdebug_ << std::endl;
629 }
7548fed2 630#endif
c2729758
ADL
631 destruct_ (ilooka_, &value, &location);
632 looka_ = empty_;
47301314 633 }
762a801e
RA
634 }
635
8dd162d3 636 /* Else will try to reuse look-ahead token after shifting the error
762a801e 637 token. */
47301314
AD
638 goto yyerrlab1;
639
640
641/*---------------------------------------------------.
642| yyerrorlab -- error raised explicitly by YYERROR. |
643`---------------------------------------------------*/
644yyerrorlab:
645
62b6aef9
AD
646#ifdef __GNUC__
647 /* Pacify GCC when the user code never invokes YYERROR and the label
648 yyerrorlab therefore never appears in user code. */
649 if (0)
650 goto yyerrorlab;
651#endif
652
47301314
AD
653 state_stack_.pop (len_);
654 semantic_stack_.pop (len_);
c2729758 655 error_start_ = location_stack_[len_ - 1].begin;
47301314
AD
656 location_stack_.pop (len_);
657 state_ = state_stack_[0];
658 goto yyerrlab1;
762a801e 659
47301314
AD
660/*-------------------------------------------------------------.
661| yyerrlab1 -- common code for both syntax error and YYERROR. |
662`-------------------------------------------------------------*/
663yyerrlab1:
a08460b0 664 errstatus_ = 3; /* Each real token shifted decrements this. */
762a801e 665
769b430f
AD
666 for (;;)
667 {
c5e3e510 668 n_ = pact_[state_];
12b0043a 669 if (n_ != pact_ninf_)
769b430f
AD
670 {
671 n_ += terror_;
c5e3e510 672 if (0 <= n_ && n_ <= last_ && check_[n_] == terror_)
769b430f 673 {
c5e3e510 674 n_ = table_[n_];
769b430f
AD
675 if (0 < n_)
676 break;
677 }
678 }
679
680 /* Pop the current state because it cannot handle the error token. */
3ae831b4 681 if (state_stack_.height () == 1)
47301314 682 YYABORT;
762a801e
RA
683
684#if YYDEBUG
769b430f
AD
685 if (debug_)
686 {
a5eb1ed2
AD
687 cdebug_ << "Error: popping ";
688 symprint_ (stos_[state_], &semantic_stack_[0], &location_stack_[0]);
689 cdebug_ << std::endl;
769b430f 690 }
762a801e 691#endif
c2729758
ADL
692 destruct_ (stos_[state_], &semantic_stack_[0], &location_stack_[0]);
693 error_start_ = location_stack_[0].begin;
762a801e 694
47301314 695 state_stack_.pop ();
769b430f 696 semantic_stack_.pop ();
47301314
AD
697 location_stack_.pop ();
698 state_ = state_stack_[0];
25f66e1a 699 YY_STACK_PRINT ();
762a801e 700 }
762a801e 701
69e2658b 702 if (n_ == final_)
762a801e
RA
703 goto yyacceptlab;
704
717be197 705 YYCDEBUG << "Shifting error token, ";
762a801e 706
c2729758
ADL
707 {
708 Location errloc;
709 errloc.begin = error_start_;
710 errloc.end = location.end;
711 semantic_stack_.push (value);
712 location_stack_.push (errloc);
713 }
762a801e 714
69e2658b 715 state_ = n_;
762a801e
RA
716 goto yynewstate;
717
2b548aa6
RA
718 /* Accept. */
719 yyacceptlab:
720 return 0;
762a801e
RA
721
722 /* Abort. */
723 yyabortlab:
724 return 1;
2b548aa6
RA
725}
726
69e2658b 727void
c5e3e510 728yy::]b4_parser_class_name[::lex_ ()
69e2658b
RA
729{
730#if YYLSP_NEEDED
731 looka_ = yylex (&value, &location);
732#else
733 looka_ = yylex (&value);
734#endif
735}
736
a08460b0
AD
737/** Generate an error message, and invoke yyerror. */
738void
739yy::]b4_parser_class_name[::report_syntax_error_ ()
740{
741 /* If not already recovering from an error, report this error. */
742 if (!errstatus_)
743 {
744 ++nerrs_;
745
746#if YYERROR_VERBOSE
747 n_ = pact_[state_];
748 if (pact_ninf_ < n_ && n_ < last_)
749 {
750 message = "syntax error, unexpected ";
751 message += name_[ilooka_];
752 {
753 int count = 0;
754 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
755 if (check_[x + n_] == x && x != terror_)
756 ++count;
757 if (count < 5)
758 {
759 count = 0;
760 for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x)
761 if (check_[x + n_] == x && x != terror_)
762 {
763 message += (!count++) ? ", expecting " : " or ";
764 message += name_[x];
765 }
766 }
767 }
768 }
769 else
770#endif
771 message = "syntax error";
772 error_ ();
773 }
774}
775
5348bfbe 776
c5e3e510 777/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
2b548aa6 778 STATE-NUM. */
0c3c1f98 779const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) yy::b4_parser_class_name::pact_ninf_ = b4_pact_ninf[;
c5e3e510
AD
780const ]b4_int_type_for([b4_pact])[
781yy::]b4_parser_class_name[::pact_[] =
2b548aa6 782{
c5e3e510 783 ]b4_pact[
2b548aa6
RA
784};
785
c5e3e510 786/* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
2b548aa6
RA
787 doesn't specify something else to do. Zero means the default is an
788 error. */
c5e3e510
AD
789const ]b4_int_type_for([b4_defact])[
790yy::]b4_parser_class_name[::defact_[] =
2b548aa6 791{
c5e3e510 792 ]b4_defact[
2b548aa6
RA
793};
794
c5e3e510
AD
795/* YYPGOTO[NTERM-NUM]. */
796const ]b4_int_type_for([b4_pgoto])[
797yy::]b4_parser_class_name[::pgoto_[] =
2b548aa6 798{
c5e3e510 799 ]b4_pgoto[
2b548aa6
RA
800};
801
c5e3e510
AD
802/* YYDEFGOTO[NTERM-NUM]. */
803const ]b4_int_type_for([b4_defgoto])[
804yy::]b4_parser_class_name[::defgoto_[] =
2b548aa6 805{
c5e3e510 806 ]b4_defgoto[
2b548aa6
RA
807};
808
c5e3e510 809/* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
2b548aa6
RA
810 positive, shift that token. If negative, reduce the rule which
811 number is the opposite. If zero, do what YYDEFACT says. */
0c3c1f98 812const ]b4_int_type(b4_table_ninf, b4_table_ninf) yy::b4_parser_class_name::table_ninf_ = b4_table_ninf[;
c5e3e510
AD
813const ]b4_int_type_for([b4_table])[
814yy::]b4_parser_class_name[::table_[] =
2b548aa6 815{
c5e3e510 816 ]b4_table[
2b548aa6
RA
817};
818
819/* YYCHECK. */
c5e3e510
AD
820const ]b4_int_type_for([b4_check])[
821yy::]b4_parser_class_name[::check_[] =
2b548aa6 822{
c5e3e510 823 ]b4_check[
2b548aa6
RA
824};
825
c5e3e510 826/* STOS_[STATE-NUM] -- The (internal number of the) accessing
769b430f 827 symbol of state STATE-NUM. */
c5e3e510
AD
828const ]b4_int_type_for([b4_stos])[
829yy::]b4_parser_class_name[::stos_[] =
769b430f 830{
c5e3e510 831 ]b4_stos[
769b430f
AD
832};
833
45d5982f 834#if YYDEBUG
c5e3e510 835/* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
769b430f 836 to YYLEX-NUM. */
c5e3e510
AD
837const ]b4_int_type_for([b4_toknum])[
838yy::]b4_parser_class_name[::token_number_[] =
769b430f 839{
c5e3e510 840 ]b4_toknum[
769b430f
AD
841};
842#endif
843
c5e3e510
AD
844/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
845const ]b4_int_type_for([b4_r1])[
846yy::]b4_parser_class_name[::r1_[] =
2b548aa6 847{
c5e3e510 848 ]b4_r1[
2b548aa6
RA
849};
850
c5e3e510
AD
851/* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
852const ]b4_int_type_for([b4_r2])[
853yy::]b4_parser_class_name[::r2_[] =
2b548aa6 854{
c5e3e510 855 ]b4_r2[
2b548aa6
RA
856};
857
69e2658b 858#if YYDEBUG || YYERROR_VERBOSE
c5e3e510 859/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
a5eb1ed2 860 First, the terminals, then, starting at NTOKENS_, nonterminals. */
2b548aa6 861const char*
c5e3e510 862const yy::]b4_parser_class_name[::name_[] =
2b548aa6 863{
c5e3e510 864 ]b4_tname[
2b548aa6 865};
69e2658b 866#endif
2b548aa6 867
69e2658b 868#if YYDEBUG
2b548aa6 869/* YYRHS -- A `-1'-separated list of the rules' RHS. */
c5e3e510
AD
870const yy::]b4_parser_class_name[::RhsNumberType
871yy::]b4_parser_class_name[::rhs_[] =
2b548aa6 872{
c5e3e510 873 ]b4_rhs[
2b548aa6
RA
874};
875
c5e3e510 876/* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
2b548aa6 877 YYRHS. */
c5e3e510
AD
878const ]b4_int_type_for([b4_prhs])[
879yy::]b4_parser_class_name[::prhs_[] =
2b548aa6 880{
c5e3e510 881 ]b4_prhs[
2b548aa6
RA
882};
883
c5e3e510
AD
884/* YYRLINE[YYN] -- source line where rule number YYN was defined. */
885const ]b4_int_type_for([b4_rline])[
886yy::]b4_parser_class_name[::rline_[] =
69e2658b 887{
c5e3e510 888 ]b4_rline[
69e2658b 889};
5348bfbe 890
25f66e1a
AD
891/** Print the state stack from its BOTTOM up to its TOP (included). */
892
893void
894yy::]b4_parser_class_name[::stack_print_ ()
895{
896 cdebug_ << "state stack now";
897 for (StateStack::ConstIterator i = state_stack_.begin ();
898 i != state_stack_.end (); ++i)
899 cdebug_ << ' ' << *i;
900 cdebug_ << std::endl;
901}
902
5348bfbe
AD
903/** Report that the YYRULE is going to be reduced. */
904
905void
906yy::]b4_parser_class_name[::reduce_print_ (int yyrule)
907{
5348bfbe
AD
908 unsigned int yylno = rline_[yyrule];
909 /* Print the symbols being reduced, and their result. */
910 cdebug_ << "Reducing via rule " << n_ - 1 << " (line " << yylno << "), ";
911 for (]b4_int_type_for([b4_prhs])[ i = prhs_[n_];
912 0 <= rhs_[i]; ++i)
913 cdebug_ << name_[rhs_[i]] << ' ';
914 cdebug_ << "-> " << name_[r1_[n_]] << std::endl;
915}
916#endif // YYDEBUG
69e2658b 917
2b548aa6 918/* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
c5e3e510
AD
919yy::]b4_parser_class_name[::TokenNumberType
920yy::]b4_parser_class_name[::translate_ (int token)
2b548aa6 921{
a75c057f 922 static
680e8701 923 const TokenNumberType
1d4055aa 924 translate_table[] =
2b548aa6 925 {
c5e3e510 926 ]b4_translate[
2b548aa6 927 };
779e7ceb 928 if ((unsigned int) token <= user_token_number_max_)
1d4055aa 929 return translate_table[token];
007a50a4
AD
930 else
931 return undef_token_;
2b548aa6
RA
932}
933
c5e3e510
AD
934const int yy::]b4_parser_class_name[::eof_ = 0;
935const int yy::]b4_parser_class_name[::last_ = ]b4_last[;
936const int yy::]b4_parser_class_name[::nnts_ = ]b4_nterms_number[;
937const int yy::]b4_parser_class_name[::empty_ = -2;
938const int yy::]b4_parser_class_name[::final_ = ]b4_final_state_number[;
939const int yy::]b4_parser_class_name[::terror_ = 1;
940const int yy::]b4_parser_class_name[::errcode_ = 256;
941const int yy::]b4_parser_class_name[::ntokens_ = ]b4_tokens_number[;
66d30cd4 942
779e7ceb 943const unsigned int yy::]b4_parser_class_name[::user_token_number_max_ = ]b4_user_token_number_max[;
c5e3e510 944const yy::]b4_parser_class_name[::TokenNumberType yy::]b4_parser_class_name[::undef_token_ = ]b4_undef_token_number[;
2b548aa6 945
c5e3e510 946]b4_epilogue
21846f69 947dnl
6ad762a8 948@output stack.hh
779e7ceb 949b4_copyright([Stack handling for Bison C++ parsers], [2002, 2003, 2004])[
98254360 950
2b548aa6
RA
951#ifndef BISON_STACK_HH
952# define BISON_STACK_HH
953
45119f04 954#include <deque>
2b548aa6
RA
955
956namespace yy
957{
45119f04 958 template < class T, class S = std::deque< T > >
2b548aa6
RA
959 class Stack
960 {
961 public:
962
963 typedef typename S::iterator Iterator;
964 typedef typename S::const_iterator ConstIterator;
965
966 Stack () : seq_ ()
967 {
968 }
969
779e7ceb 970 Stack (unsigned int n) : seq_ (n)
2b548aa6
RA
971 {
972 }
973
974 inline
975 T&
779e7ceb 976 operator [] (unsigned int i)
2b548aa6 977 {
1d4055aa 978 return seq_[i];
2b548aa6
RA
979 }
980
981 inline
982 const T&
779e7ceb 983 operator [] (unsigned int i) const
2b548aa6 984 {
1d4055aa 985 return seq_[i];
2b548aa6
RA
986 }
987
988 inline
989 void
990 push (const T& t)
991 {
45119f04 992 seq_.push_front (t);
2b548aa6
RA
993 }
994
995 inline
996 void
779e7ceb 997 pop (unsigned int n = 1)
2b548aa6
RA
998 {
999 for (; n; --n)
45119f04 1000 seq_.pop_front ();
2b548aa6
RA
1001 }
1002
762a801e 1003 inline
779e7ceb 1004 unsigned int
762a801e
RA
1005 height () const
1006 {
1007 return seq_.size ();
1008 }
1009
2b548aa6
RA
1010 inline ConstIterator begin () const { return seq_.begin (); }
1011 inline ConstIterator end () const { return seq_.end (); }
1012
1013 private:
1014
1015 S seq_;
1016 };
374f5a14
RA
1017
1018 template < class T, class S = Stack< T > >
1019 class Slice
1020 {
1021 public:
1022
1023 Slice (const S& stack,
779e7ceb
PE
1024 unsigned int range) : stack_ (stack),
1025 range_ (range)
374f5a14
RA
1026 {
1027 }
1028
1029 inline
1030 const T&
779e7ceb 1031 operator [] (unsigned int i) const
374f5a14 1032 {
1d4055aa 1033 return stack_[range_ - i];
374f5a14
RA
1034 }
1035
1036 private:
1037
1038 const S& stack_;
779e7ceb 1039 unsigned int range_;
374f5a14 1040 };
2b548aa6
RA
1041}
1042
c5e3e510 1043#endif // not BISON_STACK_HH]
21846f69 1044dnl
5d003116 1045@output position.hh
779e7ceb 1046b4_copyright([Position class for Bison C++ parsers], [2002, 2003, 2004])[
98254360 1047
5d003116
AD
1048/**
1049 ** \file position.hh
1050 ** Define the Location class.
1051 */
1052
1053#ifndef BISON_POSITION_HH
1054# define BISON_POSITION_HH
2b548aa6 1055
7548fed2
AD
1056# include <iostream>
1057# include <string>
1058
2b548aa6
RA
1059namespace yy
1060{
5d003116 1061 /** \brief Abstract a Position. */
7548fed2 1062 class Position
2b548aa6 1063 {
7548fed2 1064 public:
5d003116
AD
1065 /** \brief Initial column number. */
1066 static const unsigned int initial_column = 0;
1067 /** \brief Initial line number. */
1068 static const unsigned int initial_line = 1;
1069
1070 /** \name Ctor & dtor.
1071 ** \{ */
1072 public:
1073 /** \brief Construct a Position. */
1074 Position () :
1075 filename (),
1076 line (initial_line),
1077 column (initial_column)
1078 {
1079 }
1080 /** \} */
1081
7548fed2 1082
5d003116
AD
1083 /** \name Line and Column related manipulators
1084 ** \{ */
1085 public:
2ffbee0e
PE
1086 /** \brief (line related) Advance to the COUNT next lines. */
1087 inline void lines (int count = 1)
5d003116
AD
1088 {
1089 column = initial_column;
2ffbee0e 1090 line += count;
5d003116
AD
1091 }
1092
2ffbee0e
PE
1093 /** \brief (column related) Advance to the COUNT next columns. */
1094 inline void columns (int count = 1)
5d003116 1095 {
f939fc12
AD
1096 int leftmost = initial_column;
1097 int current = column;
2ffbee0e
PE
1098 if (leftmost <= current + count)
1099 column += count;
2cdc240e
AD
1100 else
1101 column = initial_column;
5d003116
AD
1102 }
1103 /** \} */
1104
1105 public:
1106 /** \brief File name to which this position refers. */
7548fed2 1107 std::string filename;
5d003116
AD
1108 /** \brief Current line number. */
1109 unsigned int line;
1110 /** \brief Current column number. */
1111 unsigned int column;
2b548aa6
RA
1112 };
1113
5d003116
AD
1114 /** \brief Add and assign a Position. */
1115 inline const Position&
1116 operator+= (Position& res, const int width)
1117 {
1118 res.columns (width);
1119 return res;
1120 }
1121
1122 /** \brief Add two Position objects. */
1123 inline const Position
1124 operator+ (const Position& begin, const int width)
1125 {
1126 Position res = begin;
1127 return res += width;
1128 }
1129
1130 /** \brief Add and assign a Position. */
1131 inline const Position&
1132 operator-= (Position& res, const int width)
1133 {
1134 return res += -width;
1135 }
1136
1137 /** \brief Add two Position objects. */
1138 inline const Position
1139 operator- (const Position& begin, const int width)
1140 {
1141 return begin + -width;
1142 }
1143
1144 /** \brief Intercept output stream redirection.
1145 ** \param ostr the destination output stream
1146 ** \param pos a reference to the Position to redirect
1147 */
7548fed2
AD
1148 inline std::ostream&
1149 operator<< (std::ostream& ostr, const Position& pos)
1150 {
b81a6e01 1151 if (!pos.filename.empty ())
7548fed2 1152 ostr << pos.filename << ':';
5d003116 1153 return ostr << pos.line << '.' << pos.column;
7548fed2
AD
1154 }
1155
5d003116
AD
1156}
1157#endif // not BISON_POSITION_HH]
1158@output location.hh
779e7ceb 1159b4_copyright([Location class for Bison C++ parsers], [2002, 2003, 2004])[
7548fed2 1160
5d003116
AD
1161/**
1162 ** \file location.hh
1163 ** Define the Location class.
1164 */
7548fed2 1165
5d003116
AD
1166#ifndef BISON_LOCATION_HH
1167# define BISON_LOCATION_HH
1168
1169# include <iostream>
1170# include <string>
1171# include "position.hh"
1172
1173namespace yy
1174{
1175
1176 /** \brief Abstract a Location. */
7548fed2 1177 class Location
2b548aa6 1178 {
5d003116
AD
1179 /** \name Ctor & dtor.
1180 ** \{ */
7548fed2 1181 public:
5d003116
AD
1182 /** \brief Construct a Location. */
1183 Location (void) :
1184 begin (),
1185 end ()
1186 {
1187 }
1188 /** \} */
1189
1190
1191 /** \name Line and Column related manipulators
1192 ** \{ */
1193 public:
1194 /** \brief Reset initial location to final location. */
1195 inline void step (void)
1196 {
1197 begin = end;
1198 }
1199
2ffbee0e
PE
1200 /** \brief Extend the current location to the COUNT next columns. */
1201 inline void columns (unsigned int count = 1)
5d003116 1202 {
2ffbee0e 1203 end += count;
5d003116
AD
1204 }
1205
2ffbee0e
PE
1206 /** \brief Extend the current location to the COUNT next lines. */
1207 inline void lines (unsigned int count = 1)
5d003116 1208 {
2ffbee0e 1209 end.lines (count);
5d003116
AD
1210 }
1211 /** \} */
1212
1213
1214 public:
1215 /** \brief Beginning of the located region. */
7548fed2 1216 Position begin;
5d003116 1217 /** \brief End of the located region. */
7548fed2 1218 Position end;
2b548aa6 1219 };
7548fed2 1220
5d003116
AD
1221 /** \brief Join two Location objects to create a Location. */
1222 inline const Location operator+ (const Location& begin, const Location& end)
1223 {
1224 Location res = begin;
1225 res.end = end.end;
1226 return res;
1227 }
1228
1229 /** \brief Add two Location objects */
779e7ceb 1230 inline const Location operator+ (const Location& begin, unsigned int width)
5d003116
AD
1231 {
1232 Location res = begin;
1233 res.columns (width);
1234 return res;
1235 }
7548fed2 1236
5d003116 1237 /** \brief Add and assign a Location */
779e7ceb 1238 inline Location &operator+= (Location& res, unsigned int width)
5d003116
AD
1239 {
1240 res.columns (width);
1241 return res;
1242 }
1243
1244 /** \brief Intercept output stream redirection.
1245 ** \param ostr the destination output stream
2cdc240e 1246 ** \param loc a reference to the Location to redirect
5d003116 1247 **
2cdc240e 1248 ** Avoid duplicate information.
5d003116 1249 */
2cdc240e 1250 inline std::ostream& operator<< (std::ostream& ostr, const Location& loc)
7548fed2 1251 {
2cdc240e
AD
1252 Position last = loc.end - 1;
1253 ostr << loc.begin;
1254 if (loc.begin.filename != last.filename)
1255 ostr << '-' << last;
1256 else if (loc.begin.line != last.line)
1257 ostr << '-' << last.line << '.' << last.column;
1258 else if (loc.begin.column != last.column)
1259 ostr << '-' << last.column;
7548fed2
AD
1260 return ostr;
1261 }
1262
2b548aa6
RA
1263}
1264
c5e3e510 1265#endif // not BISON_LOCATION_HH]