]> git.saurik.com Git - bison.git/blob - data/lalr1.cc
Add i18n support to the GLR skeleton. Partially fix the C++
[bison.git] / data / lalr1.cc
1 m4_divert(-1)
2
3 # C++ skeleton for Bison
4
5 # Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301 USA
21
22 ## ---------------- ##
23 ## Default values. ##
24 ## ---------------- ##
25
26 # Default parser class name.
27 m4_define_default([b4_parser_class_name], [parser])
28
29
30
31 ## ----------------- ##
32 ## Semantic Values. ##
33 ## ----------------- ##
34
35
36 # b4_lhs_value([TYPE])
37 # --------------------
38 # Expansion of $<TYPE>$.
39 m4_define([b4_lhs_value],
40 [(yyval[]m4_ifval([$1], [.$1]))])
41
42
43 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
44 # --------------------------------------
45 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
46 # symbols on RHS.
47 m4_define([b4_rhs_value],
48 [(yysemantic_stack_@{m4_eval([$1 - $2])@}m4_ifval([$3], [.$3]))])
49
50 m4_define_default([b4_location_type], [location])
51 m4_define_default([b4_filename_type], [std::string])
52
53 # b4_lhs_location()
54 # -----------------
55 # Expansion of @$.
56 m4_define([b4_lhs_location],
57 [(yyloc)])
58
59
60 # b4_rhs_location(RULE-LENGTH, NUM)
61 # ---------------------------------
62 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
63 # on RHS.
64 m4_define([b4_rhs_location],
65 [(yylocation_stack_@{m4_eval([$1 - $2])@})])
66
67
68 # b4_parse_param_decl
69 # -------------------
70 # Extra formal arguments of the constructor.
71 # Change the parameter names from "foo" into "foo_yyarg", so that
72 # there is no collision bw the user chosen attribute name, and the
73 # argument name in the constructor.
74 m4_define([b4_parse_param_decl],
75 [m4_ifset([b4_parse_param],
76 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
77
78 m4_define([b4_parse_param_decl_1],
79 [$1_yyarg])
80
81
82
83 # b4_parse_param_cons
84 # -------------------
85 # Extra initialisations of the constructor.
86 m4_define([b4_parse_param_cons],
87 [m4_ifset([b4_parse_param],
88 [,
89 b4_cc_constructor_calls(b4_parse_param)])])
90 m4_define([b4_cc_constructor_calls],
91 [m4_map_sep([b4_cc_constructor_call], [,
92 ], [$@])])
93 m4_define([b4_cc_constructor_call],
94 [$2 ($2_yyarg)])
95
96 # b4_parse_param_vars
97 # -------------------
98 # Extra instance variables.
99 m4_define([b4_parse_param_vars],
100 [m4_ifset([b4_parse_param],
101 [
102 /* User arguments. */
103 b4_cc_var_decls(b4_parse_param)])])
104 m4_define([b4_cc_var_decls],
105 [m4_map_sep([b4_cc_var_decl], [
106 ], [$@])])
107 m4_define([b4_cc_var_decl],
108 [ $1;])
109
110
111
112 # We do want M4 expansion after # for CPP macros.
113 m4_changecom()
114 m4_divert(0)dnl
115 m4_if(b4_defines_flag, 0, [],
116 [@output @output_header_name@
117 b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
118 [2002, 2003, 2004, 2005])[
119 /* FIXME: This is wrong, we want computed header guards.
120 I don't know why the macros are missing now. :( */
121 #ifndef PARSER_HEADER_H
122 # define PARSER_HEADER_H
123
124 #include <string>
125 #include <iostream>
126
127 /* Using locations. */
128 #define YYLSP_NEEDED ]b4_locations_flag[
129
130 namespace yy
131 {
132 class position;
133 class location;
134 }
135
136 ]b4_token_defines(b4_tokens)[
137
138 /* Copy the first part of user declarations. */
139 ]b4_pre_prologue[
140
141 ]/* Line __line__ of lalr1.cc. */
142 b4_syncline([@oline@], [@ofile@])[
143
144 #include "stack.hh"
145 #include "location.hh"
146
147 /* Enabling traces. */
148 #ifndef YYDEBUG
149 # define YYDEBUG ]b4_debug[
150 #endif
151
152 /* Enabling verbose error message. */
153 #ifndef YYERROR_VERBOSE
154 # define YYERROR_VERBOSE ]b4_error_verbose[
155 #endif
156
157 #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
158 ]m4_ifdef([b4_stype],
159 [b4_syncline([b4_stype_line], [b4_filename])
160 union YYSTYPE b4_stype;
161 /* Line __line__ of lalr1.cc. */
162 b4_syncline([@oline@], [@ofile@])],
163 [typedef int YYSTYPE;])[
164 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
165 # define YYSTYPE_IS_DECLARED 1
166 # define YYSTYPE_IS_TRIVIAL 1
167 #endif
168
169 /* Copy the second part of user declarations. */
170 ]b4_post_prologue[
171
172 ]/* Line __line__ of lalr1.cc. */
173 b4_syncline([@oline@], [@ofile@])[
174 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
175 If N is 0, then set CURRENT to the empty location which ends
176 the previous symbol: RHS[0] (always defined). */
177
178 #ifndef YYLLOC_DEFAULT
179 # define YYLLOC_DEFAULT(Current, Rhs, N) \
180 do { \
181 if (N) \
182 { \
183 (Current).begin = (Rhs)[1].begin; \
184 (Current).end = (Rhs)[N].end; \
185 } \
186 else \
187 { \
188 (Current).begin = (Current).end = (Rhs)[0].end; \
189 } \
190 } while (0)
191 #endif
192
193 namespace yy
194 {
195 class ]b4_parser_class_name[;
196
197 template <typename P>
198 struct traits
199 {
200 };
201
202 template <>
203 struct traits<]b4_parser_class_name[>
204 {
205 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
206 typedef ]b4_int_type_for([b4_rhs])[ rhs_number_type;
207 typedef int state_type;
208 typedef YYSTYPE semantic_type;
209 typedef ]b4_location_type[ location_type;
210 };
211 }
212
213 namespace yy
214 {
215 /// A Bison parser.
216 class ]b4_parser_class_name[
217 {
218 /// Symbol semantic values.
219 typedef traits<]b4_parser_class_name[>::semantic_type semantic_type;
220 /// Symbol locations.
221 typedef traits<]b4_parser_class_name[>::location_type location_type;
222
223 public:
224 /// Build a parser object.
225 ]b4_parser_class_name[ (]b4_parse_param_decl[) :
226 yydebug_ (false),
227 yycdebug_ (&std::cerr)]b4_parse_param_cons[
228 {
229 }
230
231 virtual ~]b4_parser_class_name[ ()
232 {
233 }
234
235 /// Parse.
236 /// \returns 0 iff parsing succeeded.
237 virtual int parse ();
238
239 /// The current debugging stream.
240 std::ostream& debug_stream () const;
241 /// Set the current debugging stream.
242 void set_debug_stream (std::ostream &);
243
244 /// Type for debugging levels.
245 typedef int debug_level_type;
246 /// The current debugging level.
247 debug_level_type debug_level () const;
248 /// Set the current debugging level.
249 void set_debug_level (debug_level_type l);
250
251 private:
252
253 /// Call the scanner.
254 virtual void yylex_ ();
255
256 /// Report a syntax error.
257 /// \param loc where the syntax error is found.
258 /// \param msg a description of the syntax error.
259 virtual void error (const location_type& loc, const std::string& msg);
260
261 /// Generate an error message, and invoke error.
262 virtual void yyreport_syntax_error_ ();
263
264 #if YYDEBUG
265 /// \brief Report a symbol on the debug stream.
266 /// \param yytype The token type.
267 /// \param yyvaluep Its semantic value.
268 /// \param yylocationp Its location.
269 virtual void yysymprint_ (int yytype,
270 const semantic_type* yyvaluep,
271 const location_type* yylocationp);
272 #endif /* ! YYDEBUG */
273
274
275 /// State numbers.
276 typedef traits<]b4_parser_class_name[>::state_type state_type;
277 /// State stack type.
278 typedef stack<state_type> state_stack_type;
279 /// Semantic value stack type.
280 typedef stack<semantic_type> semantic_stack_type;
281 /// location stack type.
282 typedef stack<location_type> location_stack_type;
283
284 /// The state stack.
285 state_stack_type yystate_stack_;
286 /// The semantic value stack.
287 semantic_stack_type yysemantic_stack_;
288 /// The location stack.
289 location_stack_type yylocation_stack_;
290
291 /// Internal symbol numbers.
292 typedef traits<]b4_parser_class_name[>::token_number_type token_number_type;
293 /* Tables. */
294 /// For a state, the index in \a yytable_ of its portion.
295 static const ]b4_int_type_for([b4_pact])[ yypact_[];
296 static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_;
297
298 /// For a state, default rule to reduce.
299 /// Unless\a yytable_ specifies something else to do.
300 /// Zero means the default is an error.
301 static const ]b4_int_type_for([b4_defact])[ yydefact_[];
302
303 static const ]b4_int_type_for([b4_pgoto])[ yypgoto_[];
304 static const ]b4_int_type_for([b4_defgoto])[ yydefgoto_[];
305
306 /// What to do in a state.
307 /// \a yytable_[yypact_[s]]: what to do in state \a s.
308 /// - if positive, shift that token.
309 /// - if negative, reduce the rule which number is the opposite.
310 /// - if zero, do what YYDEFACT says.
311 static const ]b4_int_type_for([b4_table])[ yytable_[];
312 static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ yytable_ninf_;
313
314 static const ]b4_int_type_for([b4_check])[ yycheck_[];
315
316 /// For a state, its accessing symbol.
317 static const ]b4_int_type_for([b4_stos])[ yystos_[];
318
319 /// For a rule, its LHS.
320 static const ]b4_int_type_for([b4_r1])[ yyr1_[];
321 /// For a rule, its RHS length.
322 static const ]b4_int_type_for([b4_r2])[ yyr2_[];
323
324 #if YYDEBUG || YYERROR_VERBOSE
325 /// For a symbol, its name in clear.
326 static const char* const yyname_[];
327 #endif
328
329 #if YYDEBUG
330 /// A type to store symbol numbers and -1.
331 typedef traits<]b4_parser_class_name[>::rhs_number_type rhs_number_type;
332 /// A `-1'-separated list of the rules' RHS.
333 static const rhs_number_type yyrhs_[];
334 /// For each rule, the index of the first RHS symbol in \a yyrhs_.
335 static const ]b4_int_type_for([b4_prhs])[ yyprhs_[];
336 /// For each rule, its source line number.
337 static const ]b4_int_type_for([b4_rline])[ yyrline_[];
338 /// For each scanner token number, its symbol number.
339 static const ]b4_int_type_for([b4_toknum])[ yytoken_number_[];
340 /// Report on the debug stream that the rule \a r is going to be reduced.
341 virtual void yyreduce_print_ (int r);
342 /// Print the state stack on the debug stream.
343 virtual void yystack_print_ ();
344 #endif
345
346 /// Convert a scanner token number to a symbol number.
347 inline token_number_type yytranslate_ (int token);
348
349 /// \brief Reclaim the memory associated to a symbol.
350 /// \param yymsg Why this token is reclaimed.
351 /// \param yytype The symbol type.
352 /// \param yyvaluep Its semantic value.
353 /// \param yylocationp Its location.
354 inline void yydestruct_ (const char* yymsg,
355 int yytype,
356 semantic_type* yyvaluep,
357 location_type* yylocationp);
358
359 /// Pop \a n symbols the three stacks.
360 inline void yypop_ (unsigned int n = 1);
361
362 /* Constants. */
363 static const int yyeof_;
364 /* LAST_ -- Last index in TABLE_. */
365 static const int yylast_;
366 static const int yynnts_;
367 static const int yyempty_;
368 static const int yyfinal_;
369 static const int yyterror_;
370 static const int yyerrcode_;
371 static const int yyntokens_;
372 static const unsigned int yyuser_token_number_max_;
373 static const token_number_type yyundef_token_;
374
375 /* State. */
376 int yyn_;
377 int yylen_;
378 int yystate_;
379
380 /* Error handling. */
381 int yynerrs_;
382 int yyerrstatus_;
383
384 /* Debugging. */
385 int yydebug_;
386 std::ostream* yycdebug_;
387
388 /* Look-ahead and look-ahead in internal form. */
389 int yylooka_;
390 int yyilooka_;
391
392 /// Semantic value of the look-ahead.
393 semantic_type yylval;
394 /// Location of the look-ahead.
395 location_type yylloc;
396 /// The locations where the error started and ended.
397 location yyerror_range_[2];
398
399 /// $$.
400 semantic_type yyval;
401 /// @@$.
402 location_type yyloc;
403 ]b4_parse_param_vars[
404 };
405 }
406
407 #endif /* ! defined PARSER_HEADER_H */]
408 ])dnl
409 @output @output_parser_name@
410 b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
411 [2002, 2003, 2004, 2005])
412 m4_if(b4_prefix[], [yy], [],
413 [
414 // Take the name prefix into account.
415 #define yylex b4_prefix[]lex])
416 m4_if(b4_defines_flag, 0, [],
417 [
418 #include @output_header_name@])[
419
420 #ifndef YY_
421 # if YYENABLE_NLS
422 # if ENABLE_NLS
423 # include <libintl.h> /* FIXME: INFRINGES ON USER NAME SPACE */
424 # define YY_(msgid) dgettext ("bison-runtime", msgid)
425 # endif
426 # endif
427 # ifndef YY_
428 # define YY_(msgid) msgid
429 # endif
430 #endif
431
432 /* A pseudo ostream that takes yydebug_ into account. */
433 # define YYCDEBUG \
434 for (bool yydebugcond_ = yydebug_; yydebugcond_; yydebugcond_ = false) \
435 (*yycdebug_)
436
437 /* Enable debugging if requested. */
438 #if YYDEBUG
439
440 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
441 do { \
442 if (yydebug_) \
443 { \
444 *yycdebug_ << (Title) << ' '; \
445 yysymprint_ ((Type), (Value), (Location)); \
446 *yycdebug_ << std::endl; \
447 } \
448 } while (0)
449
450 # define YY_REDUCE_PRINT(Rule) \
451 do { \
452 if (yydebug_) \
453 yyreduce_print_ (Rule); \
454 } while (0)
455
456 # define YY_STACK_PRINT() \
457 do { \
458 if (yydebug_) \
459 yystack_print_ (); \
460 } while (0)
461
462 #else /* !YYDEBUG */
463
464 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
465 # define YY_REDUCE_PRINT(Rule)
466 # define YY_STACK_PRINT()
467
468 #endif /* !YYDEBUG */
469
470 #define YYACCEPT goto yyacceptlab
471 #define YYABORT goto yyabortlab
472 #define YYERROR goto yyerrorlab
473
474 #if YYDEBUG
475 /*--------------------------------.
476 | Print this symbol on YYOUTPUT. |
477 `--------------------------------*/
478
479 void
480 yy::]b4_parser_class_name[::yysymprint_ (int yytype,
481 const semantic_type* yyvaluep, const location_type* yylocationp)
482 {
483 /* Pacify ``unused variable'' warnings. */
484 (void) yyvaluep;
485 (void) yylocationp;
486 /* Backward compatibility, but should be removed eventually. */
487 std::ostream& cdebug_ = *yycdebug_;
488 (void) cdebug_;
489
490 *yycdebug_ << (yytype < yyntokens_ ? "token" : "nterm")
491 << ' ' << yyname_[yytype] << " ("
492 << *yylocationp << ": ";
493 switch (yytype)
494 {
495 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
496 [ default:
497 break;
498 }
499 *yycdebug_ << ')';
500 }
501 #endif /* ! YYDEBUG */
502
503 void
504 yy::]b4_parser_class_name[::yydestruct_ (const char* yymsg,
505 int yytype, semantic_type* yyvaluep, location_type* yylocationp)
506 {
507 /* Pacify ``unused variable'' warnings. */
508 (void) yymsg;
509 (void) yyvaluep;
510 (void) yylocationp;
511
512 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
513
514 switch (yytype)
515 {
516 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
517 default:
518 break;
519 }
520 }
521
522 void
523 yy::]b4_parser_class_name[::yypop_ (unsigned int n)
524 {
525 yystate_stack_.pop (n);
526 yysemantic_stack_.pop (n);
527 yylocation_stack_.pop (n);
528 }
529
530 std::ostream&
531 yy::]b4_parser_class_name[::debug_stream () const
532 {
533 return *yycdebug_;
534 }
535
536 void
537 yy::]b4_parser_class_name[::set_debug_stream (std::ostream& o)
538 {
539 yycdebug_ = &o;
540 }
541
542
543 yy::]b4_parser_class_name[::debug_level_type
544 yy::]b4_parser_class_name[::debug_level () const
545 {
546 return yydebug_;
547 }
548
549 void
550 yy::]b4_parser_class_name[::set_debug_level (debug_level_type l)
551 {
552 yydebug_ = l;
553 }
554
555
556 int
557 yy::]b4_parser_class_name[::parse ()
558 {
559 int yyresult_;
560
561 YYCDEBUG << "Starting parse" << std::endl;
562
563 yynerrs_ = 0;
564 yyerrstatus_ = 0;
565
566 /* Start. */
567 yystate_ = 0;
568 yylooka_ = yyempty_;
569
570 ]m4_ifdef([b4_initial_action], [
571 m4_pushdef([b4_at_dollar], [yylloc])dnl
572 m4_pushdef([b4_dollar_dollar], [yylval])dnl
573 /* User initialization code. */
574 b4_initial_action
575 m4_popdef([b4_dollar_dollar])dnl
576 m4_popdef([b4_at_dollar])dnl
577 /* Line __line__ of yacc.c. */
578 b4_syncline([@oline@], [@ofile@])])dnl
579
580 [ /* Initialize the stacks. The initial state will be pushed in
581 yynewstate, since the latter expects the semantical and the
582 location values to have been already stored, initialize these
583 stacks with a primary value. */
584 yystate_stack_ = state_stack_type (0);
585 yysemantic_stack_ = semantic_stack_type (0);
586 yylocation_stack_ = location_stack_type (0);
587 yysemantic_stack_.push (yylval);
588 yylocation_stack_.push (yylloc);
589
590 /* New state. */
591 yynewstate:
592 yystate_stack_.push (yystate_);
593 YYCDEBUG << "Entering state " << yystate_ << std::endl;
594 goto yybackup;
595
596 /* Backup. */
597 yybackup:
598
599 /* Try to take a decision without look-ahead. */
600 yyn_ = yypact_[yystate_];
601 if (yyn_ == yypact_ninf_)
602 goto yydefault;
603
604 /* Read a look-ahead token. */
605 if (yylooka_ == yyempty_)
606 yylex_ ();
607
608 /* Convert token to internal form. */
609 if (yylooka_ <= yyeof_)
610 {
611 yylooka_ = yyilooka_ = yyeof_;
612 YYCDEBUG << "Now at end of input." << std::endl;
613 }
614 else
615 {
616 yyilooka_ = yytranslate_ (yylooka_);
617 YY_SYMBOL_PRINT ("Next token is", yyilooka_, &yylval, &yylloc);
618 }
619
620 /* If the proper action on seeing token ILOOKA_ is to reduce or to
621 detect an error, take that action. */
622 yyn_ += yyilooka_;
623 if (yyn_ < 0 || yylast_ < yyn_ || yycheck_[yyn_] != yyilooka_)
624 goto yydefault;
625
626 /* Reduce or error. */
627 yyn_ = yytable_[yyn_];
628 if (yyn_ < 0)
629 {
630 if (yyn_ == yytable_ninf_)
631 goto yyerrlab;
632 else
633 {
634 yyn_ = -yyn_;
635 goto yyreduce;
636 }
637 }
638 else if (yyn_ == 0)
639 goto yyerrlab;
640
641 /* Accept? */
642 if (yyn_ == yyfinal_)
643 goto yyacceptlab;
644
645 /* Shift the look-ahead token. */
646 YY_SYMBOL_PRINT ("Shifting", yyilooka_, &yylval, &yylloc);
647
648 /* Discard the token being shifted unless it is eof. */
649 if (yylooka_ != yyeof_)
650 yylooka_ = yyempty_;
651
652 yysemantic_stack_.push (yylval);
653 yylocation_stack_.push (yylloc);
654
655 /* Count tokens shifted since error; after three, turn off error
656 status. */
657 if (yyerrstatus_)
658 --yyerrstatus_;
659
660 yystate_ = yyn_;
661 goto yynewstate;
662
663 /*-----------------------------------------------------------.
664 | yydefault -- do the default action for the current state. |
665 `-----------------------------------------------------------*/
666 yydefault:
667 yyn_ = yydefact_[yystate_];
668 if (yyn_ == 0)
669 goto yyerrlab;
670 goto yyreduce;
671
672 /*-----------------------------.
673 | yyreduce -- Do a reduction. |
674 `-----------------------------*/
675 yyreduce:
676 yylen_ = yyr2_[yyn_];
677 /* If LEN_ is nonzero, implement the default value of the action:
678 `$$ = $1'. Otherwise, use the top of the stack.
679
680 Otherwise, the following line sets YYVAL to garbage.
681 This behavior is undocumented and Bison
682 users should not rely upon it. */
683 if (yylen_)
684 yyval = yysemantic_stack_[yylen_ - 1];
685 else
686 yyval = yysemantic_stack_[0];
687
688 {
689 slice<location_type, location_stack_type> slice (yylocation_stack_, yylen_);
690 YYLLOC_DEFAULT (yyloc, slice, yylen_);
691 }
692 YY_REDUCE_PRINT (yyn_);
693 switch (yyn_)
694 {
695 ]b4_actions[
696 default: break;
697 }
698
699 ]/* Line __line__ of lalr1.cc. */
700 b4_syncline([@oline@], [@ofile@])[
701
702 yypop_ (yylen_);
703
704 YY_STACK_PRINT ();
705
706 yysemantic_stack_.push (yyval);
707 yylocation_stack_.push (yyloc);
708
709 /* Shift the result of the reduction. */
710 yyn_ = yyr1_[yyn_];
711 yystate_ = yypgoto_[yyn_ - yyntokens_] + yystate_stack_[0];
712 if (0 <= yystate_ && yystate_ <= yylast_
713 && yycheck_[yystate_] == yystate_stack_[0])
714 yystate_ = yytable_[yystate_];
715 else
716 yystate_ = yydefgoto_[yyn_ - yyntokens_];
717 goto yynewstate;
718
719 /*------------------------------------.
720 | yyerrlab -- here on detecting error |
721 `------------------------------------*/
722 yyerrlab:
723 /* If not already recovering from an error, report this error. */
724 yyreport_syntax_error_ ();
725
726 yyerror_range_[0] = yylloc;
727 if (yyerrstatus_ == 3)
728 {
729 /* If just tried and failed to reuse look-ahead token after an
730 error, discard it. */
731
732 /* Return failure if at end of input. */
733 if (yylooka_ <= yyeof_)
734 {
735 /* If at end of input, pop the error token,
736 then the rest of the stack, then return failure. */
737 if (yylooka_ == yyeof_)
738 for (;;)
739 {
740 yyerror_range_[0] = yylocation_stack_[0];
741 yypop_ ();
742 if (yystate_stack_.height () == 1)
743 YYABORT;
744 yydestruct_ ("Error: popping",
745 yystos_[yystate_stack_[0]],
746 &yysemantic_stack_[0],
747 &yylocation_stack_[0]);
748 }
749 }
750 else
751 {
752 yydestruct_ ("Error: discarding", yyilooka_, &yylval, &yylloc);
753 yylooka_ = yyempty_;
754 }
755 }
756
757 /* Else will try to reuse look-ahead token after shifting the error
758 token. */
759 goto yyerrlab1;
760
761
762 /*---------------------------------------------------.
763 | yyerrorlab -- error raised explicitly by YYERROR. |
764 `---------------------------------------------------*/
765 yyerrorlab:
766
767 /* Pacify compilers like GCC when the user code never invokes
768 YYERROR and the label yyerrorlab therefore never appears in user
769 code. */
770 if (false)
771 goto yyerrorlab;
772
773 yyerror_range_[0] = yylocation_stack_[yylen_ - 1];
774 yypop_ (yylen_);
775 yystate_ = yystate_stack_[0];
776 goto yyerrlab1;
777
778 /*-------------------------------------------------------------.
779 | yyerrlab1 -- common code for both syntax error and YYERROR. |
780 `-------------------------------------------------------------*/
781 yyerrlab1:
782 yyerrstatus_ = 3; /* Each real token shifted decrements this. */
783
784 for (;;)
785 {
786 yyn_ = yypact_[yystate_];
787 if (yyn_ != yypact_ninf_)
788 {
789 yyn_ += yyterror_;
790 if (0 <= yyn_ && yyn_ <= yylast_ && yycheck_[yyn_] == yyterror_)
791 {
792 yyn_ = yytable_[yyn_];
793 if (0 < yyn_)
794 break;
795 }
796 }
797
798 /* Pop the current state because it cannot handle the error token. */
799 if (yystate_stack_.height () == 1)
800 YYABORT;
801
802 yyerror_range_[0] = yylocation_stack_[0];
803 yydestruct_ ("Error: popping",
804 yystos_[yystate_],
805 &yysemantic_stack_[0], &yylocation_stack_[0]);
806 yypop_ ();
807 yystate_ = yystate_stack_[0];
808 YY_STACK_PRINT ();
809 }
810
811 if (yyn_ == yyfinal_)
812 goto yyacceptlab;
813
814 yyerror_range_[1] = yylloc;
815 // Using YYLLOC is tempting, but would change the location of
816 // the look-ahead. YYLOC is available though.
817 YYLLOC_DEFAULT (yyloc, yyerror_range_ - 1, 2);
818 yysemantic_stack_.push (yylval);
819 yylocation_stack_.push (yyloc);
820
821 /* Shift the error token. */
822 YY_SYMBOL_PRINT ("Shifting", yystos_[yyn_],
823 &yysemantic_stack_[0], &yylocation_stack_[0]);
824
825 yystate_ = yyn_;
826 goto yynewstate;
827
828 /* Accept. */
829 yyacceptlab:
830 yyresult_ = 0;
831 goto yyreturn;
832
833 /* Abort. */
834 yyabortlab:
835 yyresult_ = 1;
836 goto yyreturn;
837
838 yyreturn:
839 if (yylooka_ != yyeof_ && yylooka_ != yyempty_)
840 yydestruct_ ("Error: discarding lookahead", yyilooka_, &yylval, &yylloc);
841 return yyresult_;
842 }
843
844 void
845 yy::]b4_parser_class_name[::yylex_ ()
846 {
847 YYCDEBUG << "Reading a token: ";
848 yylooka_ = ]b4_c_function_call([yylex], [int],
849 [[YYSTYPE*], [&yylval]][]dnl
850 b4_location_if([, [[location*], [&yylloc]]])dnl
851 m4_ifdef([b4_lex_param], [, ]b4_lex_param))[;
852 }
853
854 // Generate an error message, and invoke error.
855 void
856 yy::]b4_parser_class_name[::yyreport_syntax_error_ ()
857 {
858 /* If not already recovering from an error, report this error. */
859 if (!yyerrstatus_)
860 {
861 ++yynerrs_;
862 std::string message;
863 #if YYERROR_VERBOSE
864 yyn_ = yypact_[yystate_];
865 if (yypact_ninf_ < yyn_ && yyn_ < yylast_)
866 {
867 /* Start YYX at -YYN if negative to avoid negative indexes in
868 YYCHECK. */
869 int yyxbegin = yyn_ < 0 ? -yyn_ : 0;
870
871 /* Stay within bounds of both yycheck and yytname. */
872 int yychecklim = yylast_ - yyn_;
873 int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_;
874 int count = 0;
875 for (int x = yyxbegin; x < yyxend; ++x)
876 if (yycheck_[x + yyn_] == x && x != yyterror_)
877 ++count;
878
879 // FIXME: This method of building the message is not compatible
880 // with internationalization. It should work like yacc.c does it.
881 // That is, first build a string that looks like this:
882 // "syntax error, unexpected %s or %s or %s"
883 // Then, invoke YY_ on this string.
884 // Finally, use the string as a format to output
885 // yyname_[yyilooka_], etc.
886 // Until this gets fixed, this message appears in English only.
887 message = "syntax error, unexpected ";
888 message += yyname_[yyilooka_];
889 if (count < 5)
890 {
891 count = 0;
892 for (int x = yyxbegin; x < yyxend; ++x)
893 if (yycheck_[x + yyn_] == x && x != yyterror_)
894 {
895 message += (!count++) ? ", expecting " : " or ";
896 message += yyname_[x];
897 }
898 }
899 }
900 else
901 #endif
902 message = YY_("syntax error");
903 error (yylloc, message);
904 }
905 }
906
907
908 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
909 STATE-NUM. */
910 const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) yy::b4_parser_class_name::yypact_ninf_ = b4_pact_ninf[;
911 const ]b4_int_type_for([b4_pact])[
912 yy::]b4_parser_class_name[::yypact_[] =
913 {
914 ]b4_pact[
915 };
916
917 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
918 doesn't specify something else to do. Zero means the default is an
919 error. */
920 const ]b4_int_type_for([b4_defact])[
921 yy::]b4_parser_class_name[::yydefact_[] =
922 {
923 ]b4_defact[
924 };
925
926 /* YYPGOTO[NTERM-NUM]. */
927 const ]b4_int_type_for([b4_pgoto])[
928 yy::]b4_parser_class_name[::yypgoto_[] =
929 {
930 ]b4_pgoto[
931 };
932
933 /* YYDEFGOTO[NTERM-NUM]. */
934 const ]b4_int_type_for([b4_defgoto])[
935 yy::]b4_parser_class_name[::yydefgoto_[] =
936 {
937 ]b4_defgoto[
938 };
939
940 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
941 positive, shift that token. If negative, reduce the rule which
942 number is the opposite. If zero, do what YYDEFACT says. */
943 const ]b4_int_type(b4_table_ninf, b4_table_ninf) yy::b4_parser_class_name::yytable_ninf_ = b4_table_ninf[;
944 const ]b4_int_type_for([b4_table])[
945 yy::]b4_parser_class_name[::yytable_[] =
946 {
947 ]b4_table[
948 };
949
950 /* YYCHECK. */
951 const ]b4_int_type_for([b4_check])[
952 yy::]b4_parser_class_name[::yycheck_[] =
953 {
954 ]b4_check[
955 };
956
957 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
958 symbol of state STATE-NUM. */
959 const ]b4_int_type_for([b4_stos])[
960 yy::]b4_parser_class_name[::yystos_[] =
961 {
962 ]b4_stos[
963 };
964
965 #if YYDEBUG
966 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding
967 to YYLEX-NUM. */
968 const ]b4_int_type_for([b4_toknum])[
969 yy::]b4_parser_class_name[::yytoken_number_[] =
970 {
971 ]b4_toknum[
972 };
973 #endif
974
975 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
976 const ]b4_int_type_for([b4_r1])[
977 yy::]b4_parser_class_name[::yyr1_[] =
978 {
979 ]b4_r1[
980 };
981
982 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
983 const ]b4_int_type_for([b4_r2])[
984 yy::]b4_parser_class_name[::yyr2_[] =
985 {
986 ]b4_r2[
987 };
988
989 #if YYDEBUG || YYERROR_VERBOSE
990 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
991 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
992 const char*
993 const yy::]b4_parser_class_name[::yyname_[] =
994 {
995 ]b4_tname[
996 };
997 #endif
998
999 #if YYDEBUG
1000 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
1001 const yy::]b4_parser_class_name[::rhs_number_type
1002 yy::]b4_parser_class_name[::yyrhs_[] =
1003 {
1004 ]b4_rhs[
1005 };
1006
1007 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
1008 YYRHS. */
1009 const ]b4_int_type_for([b4_prhs])[
1010 yy::]b4_parser_class_name[::yyprhs_[] =
1011 {
1012 ]b4_prhs[
1013 };
1014
1015 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
1016 const ]b4_int_type_for([b4_rline])[
1017 yy::]b4_parser_class_name[::yyrline_[] =
1018 {
1019 ]b4_rline[
1020 };
1021
1022 // Print the state stack on the debug stream.
1023 void
1024 yy::]b4_parser_class_name[::yystack_print_ ()
1025 {
1026 *yycdebug_ << "Stack now";
1027 for (state_stack_type::const_iterator i = yystate_stack_.begin ();
1028 i != yystate_stack_.end (); ++i)
1029 *yycdebug_ << ' ' << *i;
1030 *yycdebug_ << std::endl;
1031 }
1032
1033 // Report on the debug stream that the rule \a yyrule is going to be reduced.
1034 void
1035 yy::]b4_parser_class_name[::yyreduce_print_ (int yyrule)
1036 {
1037 unsigned int yylno = yyrline_[yyrule];
1038 /* Print the symbols being reduced, and their result. */
1039 *yycdebug_ << "Reducing stack by rule " << yyn_ - 1
1040 << " (line " << yylno << "), ";
1041 for (]b4_int_type_for([b4_prhs])[ i = yyprhs_[yyn_];
1042 0 <= yyrhs_[i]; ++i)
1043 *yycdebug_ << yyname_[yyrhs_[i]] << ' ';
1044 *yycdebug_ << "-> " << yyname_[yyr1_[yyn_]] << std::endl;
1045 }
1046 #endif // YYDEBUG
1047
1048 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
1049 yy::]b4_parser_class_name[::token_number_type
1050 yy::]b4_parser_class_name[::yytranslate_ (int token)
1051 {
1052 static
1053 const token_number_type
1054 translate_table[] =
1055 {
1056 ]b4_translate[
1057 };
1058 if ((unsigned int) token <= yyuser_token_number_max_)
1059 return translate_table[token];
1060 else
1061 return yyundef_token_;
1062 }
1063
1064 const int yy::]b4_parser_class_name[::yyeof_ = 0;
1065 const int yy::]b4_parser_class_name[::yylast_ = ]b4_last[;
1066 const int yy::]b4_parser_class_name[::yynnts_ = ]b4_nterms_number[;
1067 const int yy::]b4_parser_class_name[::yyempty_ = -2;
1068 const int yy::]b4_parser_class_name[::yyfinal_ = ]b4_final_state_number[;
1069 const int yy::]b4_parser_class_name[::yyterror_ = 1;
1070 const int yy::]b4_parser_class_name[::yyerrcode_ = 256;
1071 const int yy::]b4_parser_class_name[::yyntokens_ = ]b4_tokens_number[;
1072
1073 const unsigned int yy::]b4_parser_class_name[::yyuser_token_number_max_ = ]b4_user_token_number_max[;
1074 const yy::]b4_parser_class_name[::token_number_type yy::]b4_parser_class_name[::yyundef_token_ = ]b4_undef_token_number[;
1075
1076 ]b4_epilogue
1077 dnl
1078 @output stack.hh
1079 b4_copyright([stack handling for Bison C++ parsers], [2002, 2003, 2004, 2005])[
1080
1081 #ifndef BISON_STACK_HH
1082 # define BISON_STACK_HH
1083
1084 #include <deque>
1085
1086 namespace yy
1087 {
1088 template <class T, class S = std::deque<T> >
1089 class stack
1090 {
1091 public:
1092
1093 // Hide our reversed order.
1094 typedef typename S::reverse_iterator iterator;
1095 typedef typename S::const_reverse_iterator const_iterator;
1096
1097 stack () : seq_ ()
1098 {
1099 }
1100
1101 stack (unsigned int n) : seq_ (n)
1102 {
1103 }
1104
1105 inline
1106 T&
1107 operator [] (unsigned int i)
1108 {
1109 return seq_[i];
1110 }
1111
1112 inline
1113 const T&
1114 operator [] (unsigned int i) const
1115 {
1116 return seq_[i];
1117 }
1118
1119 inline
1120 void
1121 push (const T& t)
1122 {
1123 seq_.push_front (t);
1124 }
1125
1126 inline
1127 void
1128 pop (unsigned int n = 1)
1129 {
1130 for (; n; --n)
1131 seq_.pop_front ();
1132 }
1133
1134 inline
1135 unsigned int
1136 height () const
1137 {
1138 return seq_.size ();
1139 }
1140
1141 inline const_iterator begin () const { return seq_.rbegin (); }
1142 inline const_iterator end () const { return seq_.rend (); }
1143
1144 private:
1145
1146 S seq_;
1147 };
1148
1149 /// Present a slice of the top of a stack.
1150 template <class T, class S = stack<T> >
1151 class slice
1152 {
1153 public:
1154
1155 slice (const S& stack,
1156 unsigned int range) : stack_ (stack),
1157 range_ (range)
1158 {
1159 }
1160
1161 inline
1162 const T&
1163 operator [] (unsigned int i) const
1164 {
1165 return stack_[range_ - i];
1166 }
1167
1168 private:
1169
1170 const S& stack_;
1171 unsigned int range_;
1172 };
1173 }
1174
1175 #endif // not BISON_STACK_HH]
1176 dnl
1177 @output position.hh
1178 b4_copyright([Position class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
1179
1180 /**
1181 ** \file position.hh
1182 ** Define the position class.
1183 */
1184
1185 #ifndef BISON_POSITION_HH
1186 # define BISON_POSITION_HH
1187
1188 # include <iostream>
1189 # include <string>
1190
1191 namespace yy
1192 {
1193 /// Abstract a position.
1194 class position
1195 {
1196 public:
1197 /// Initial column number.
1198 static const unsigned int initial_column = 0;
1199 /// Initial line number.
1200 static const unsigned int initial_line = 1;
1201
1202 /** \name Ctor & dtor.
1203 ** \{ */
1204 public:
1205 /// Construct a position.
1206 position () :
1207 filename (0),
1208 line (initial_line),
1209 column (initial_column)
1210 {
1211 }
1212 /** \} */
1213
1214
1215 /** \name Line and Column related manipulators
1216 ** \{ */
1217 public:
1218 /// (line related) Advance to the COUNT next lines.
1219 inline void lines (int count = 1)
1220 {
1221 column = initial_column;
1222 line += count;
1223 }
1224
1225 /// (column related) Advance to the COUNT next columns.
1226 inline void columns (int count = 1)
1227 {
1228 int leftmost = initial_column;
1229 int current = column;
1230 if (leftmost <= current + count)
1231 column += count;
1232 else
1233 column = initial_column;
1234 }
1235 /** \} */
1236
1237 public:
1238 /// File name to which this position refers.
1239 ]b4_filename_type[* filename;
1240 /// Current line number.
1241 unsigned int line;
1242 /// Current column number.
1243 unsigned int column;
1244 };
1245
1246 /// Add and assign a position.
1247 inline const position&
1248 operator+= (position& res, const int width)
1249 {
1250 res.columns (width);
1251 return res;
1252 }
1253
1254 /// Add two position objects.
1255 inline const position
1256 operator+ (const position& begin, const int width)
1257 {
1258 position res = begin;
1259 return res += width;
1260 }
1261
1262 /// Add and assign a position.
1263 inline const position&
1264 operator-= (position& res, const int width)
1265 {
1266 return res += -width;
1267 }
1268
1269 /// Add two position objects.
1270 inline const position
1271 operator- (const position& begin, const int width)
1272 {
1273 return begin + -width;
1274 }
1275
1276 /** \brief Intercept output stream redirection.
1277 ** \param ostr the destination output stream
1278 ** \param pos a reference to the position to redirect
1279 */
1280 inline std::ostream&
1281 operator<< (std::ostream& ostr, const position& pos)
1282 {
1283 if (pos.filename)
1284 ostr << *pos.filename << ':';
1285 return ostr << pos.line << '.' << pos.column;
1286 }
1287
1288 }
1289 #endif // not BISON_POSITION_HH]
1290 @output location.hh
1291 b4_copyright([Location class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
1292
1293 /**
1294 ** \file location.hh
1295 ** Define the location class.
1296 */
1297
1298 #ifndef BISON_LOCATION_HH
1299 # define BISON_LOCATION_HH
1300
1301 # include <iostream>
1302 # include <string>
1303 # include "position.hh"
1304
1305 namespace yy
1306 {
1307
1308 /// Abstract a location.
1309 class location
1310 {
1311 /** \name Ctor & dtor.
1312 ** \{ */
1313 public:
1314 /// Construct a location.
1315 location () :
1316 begin (),
1317 end ()
1318 {
1319 }
1320 /** \} */
1321
1322
1323 /** \name Line and Column related manipulators
1324 ** \{ */
1325 public:
1326 /// Reset initial location to final location.
1327 inline void step ()
1328 {
1329 begin = end;
1330 }
1331
1332 /// Extend the current location to the COUNT next columns.
1333 inline void columns (unsigned int count = 1)
1334 {
1335 end += count;
1336 }
1337
1338 /// Extend the current location to the COUNT next lines.
1339 inline void lines (unsigned int count = 1)
1340 {
1341 end.lines (count);
1342 }
1343 /** \} */
1344
1345
1346 public:
1347 /// Beginning of the located region.
1348 position begin;
1349 /// End of the located region.
1350 position end;
1351 };
1352
1353 /// Join two location objects to create a location.
1354 inline const location operator+ (const location& begin, const location& end)
1355 {
1356 location res = begin;
1357 res.end = end.end;
1358 return res;
1359 }
1360
1361 /// Add two location objects.
1362 inline const location operator+ (const location& begin, unsigned int width)
1363 {
1364 location res = begin;
1365 res.columns (width);
1366 return res;
1367 }
1368
1369 /// Add and assign a location.
1370 inline location& operator+= (location& res, unsigned int width)
1371 {
1372 res.columns (width);
1373 return res;
1374 }
1375
1376 /** \brief Intercept output stream redirection.
1377 ** \param ostr the destination output stream
1378 ** \param loc a reference to the location to redirect
1379 **
1380 ** Avoid duplicate information.
1381 */
1382 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
1383 {
1384 position last = loc.end - 1;
1385 ostr << loc.begin;
1386 if (last.filename
1387 && (!loc.begin.filename
1388 || *loc.begin.filename != *last.filename))
1389 ostr << '-' << last;
1390 else if (loc.begin.line != last.line)
1391 ostr << '-' << last.line << '.' << last.column;
1392 else if (loc.begin.column != last.column)
1393 ostr << '-' << last.column;
1394 return ostr;
1395 }
1396
1397 }
1398
1399 #endif // not BISON_LOCATION_HH]