2 # C++ skeleton for Bison
3 # Copyright (C) 2002 Free Software Foundation, Inc.
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.
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.
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
22 # b4_lhs_value([TYPE])
23 # --------------------
24 # Expansion of $<TYPE>$.
25 m4_define([b4_lhs_value
],
26 [yyval
[]m4_ifval([$
1], [.$
1])])
29 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
30 # --------------------------------------
31 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
33 m4_define([b4_rhs_value
],
34 [semantic_stack_@
<:@
m4_eval([$
1 - $
2])@
:>@
m4_ifval([$
3], [.$
3])])
40 m4_define([b4_lhs_location
],
44 # b4_rhs_location(RULE-LENGTH, NUM)
45 # ---------------------------------
46 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
48 m4_define([b4_rhs_location
],
49 [location_stack_@
<:@
m4_eval([$
1 - $
2])@
:>@
])
52 m4_define_default([b4_input_suffix
], [.y
])
54 m4_define_default([b4_output_parser_suffix
],
55 [m4_translit(b4_input_suffix
, [yY
], [cC
])])
57 m4_define_default([b4_output_parser_name
],
58 [b4_output_prefix
[]b4_output_infix
[]b4_output_parser_suffix
[]])
61 m4_define_default([b4_output_header_suffix
],
62 [m4_translit(b4_input_suffix
, [yY
], [hH
])])
64 m4_define_default([b4_output_header_name
],
65 [b4_output_prefix
[]b4_output_infix
[]b4_output_header_suffix
[]])
67 m4_define_default([b4_header_guard
],
68 [m4_bpatsubst(m4_toupper([BISON_
]b4_output_header_name
),
69 [[^ABCDEFGHIJKLMNOPQRSTUVWXYZ
]], [_
])])
71 m4_define([b4_inherit
],
83 m4_define([b4_constructor
],
91 #output "b4_output_header_name"
92 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
94 #ifndef b4_header_guard
95 # define b4_header_guard
98 #include "location.hh"
103 /* Using locations. */
104 #define YYLSP_NEEDED b4_locations_flag
106 /* Copy the first part of user declarations. */
109 /* Line __line__ of __file__. */
110 #line __oline__ "__ofile__"
113 b4_token_defines(b4_tokens
)
115 /* Enabling traces. */
117 # define YYDEBUG b4_debug
120 /* Enabling verbose error message. */
121 #ifndef YYERROR_VERBOSE
122 # define YYERROR_VERBOSE b4_error_verbose
127 [#line b4_stype_line "b4_filename"
128 typedef union b4_stype yystype
;
129 /* Line __line__ of __file__. */
130 #line __oline__ "__ofile__"],
131 [typedef int yystype
;])
132 # define YYSTYPE yystype
135 /* Copy the second part of user declarations. */
138 /* Line __line__ of __file__. */
139 #line __oline__ "__ofile__"
140 #ifndef YYLLOC_DEFAULT
141 # define YYLLOC_DEFAULT(Current, Rhs, N) \
142 Current.last_line = Rhs[[N]].last_line; \
143 Current.last_column = Rhs[[N]].last_column;
146 m4_if(b4_locations_flag
, [0], [],
148 typedef struct yyltype
155 # define YYLTYPE yyltype
162 template < typename P
>
168 struct Traits
< b4_name
>
170 typedef b4_uint_type(b4_translate_max
) TokenNumberType
;
171 typedef b4_sint_type(b4_rhs_max
) RhsNumberType
;
172 typedef int StateType
;
173 typedef yystype SemanticType
;
174 typedef b4_ltype LocationType
;
180 class b4_name b4_inherit
184 typedef Traits
< b4_name
>::TokenNumberType TokenNumberType
;
185 typedef Traits
< b4_name
>::RhsNumberType RhsNumberType
;
186 typedef Traits
< b4_name
>::StateType StateType
;
187 typedef Traits
< b4_name
>::SemanticType SemanticType
;
188 typedef Traits
< b4_name
>::LocationType LocationType
;
190 typedef Stack
< StateType
> StateStack
;
191 typedef Stack
< SemanticType
> SemanticStack
;
192 typedef Stack
< LocationType
> LocationStack
;
196 LocationType initlocation
[]b4_param
) :
197 b4_constructor
[]debug_ (debug
),
199 initlocation_ (initlocation
)
201 b4_name (bool debug
[]b4_param
) :
202 b4_constructor
[]debug_ (debug
),
212 virtual int parse ();
216 virtual void lex_ ();
217 virtual void error_ ();
218 virtual void print_ ();
221 StateStack state_stack_
;
222 SemanticStack semantic_stack_
;
223 LocationStack location_stack_
;
226 static const short pact_
[[]];
227 static const short defact_
[[]];
228 static const short pgoto_
[[]];
229 static const short defgoto_
[[]];
230 static const short table_
[[]];
231 static const short check_
[[]];
232 static const b4_uint_type(b4_r1_max
) r1_
[[]];
233 static const b4_uint_type(b4_r2_max
) r2_
[[]];
235 #if YYDEBUG || YYERROR_VERBOSE
236 static const char* const name_
[[]];
239 /* More tables, for debugging. */
241 static const RhsNumberType rhs_
[[]];
242 static const b4_uint_type(b4_prhs_max
) prhs_
[[]];
243 static const b4_uint_type(b4_rline_max
) rline_
[[]];
244 static const b4_uint_type(b4_stos_max
) stos_
[[]];
245 static const short token_number_
[[]];
248 /* Even more tables. */
249 static inline TokenNumberType
translate_ (int token
);
252 static const int eof_
;
253 static const int last_
;
254 static const int flag_
;
255 static const int nnts_
;
256 static const int nsym_
;
257 static const int empty_
;
258 static const int final_
;
259 static const int terror_
;
260 static const int errcode_
;
261 static const int ntokens_
;
262 static const int initdepth_
;
263 static const unsigned user_token_number_max_
;
264 static const TokenNumberType undef_token_
;
273 std::ostream
&cdebug_
;
275 /* Lookahead and lookahead in internal form. */
282 /* Semantic value and location of lookahead token. */
284 LocationType location
;
290 /* Initial location. */
291 LocationType initlocation_
;
295 #endif /* not b4_header_guard */
297 #output "b4_output_prefix[]b4_output_infix[].cc"
298 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
301 #include "b4_output_header_name"
303 /* Enable debugging if requested. */
305 # define YYCDEBUG if (debug_) cdebug_
307 # define YYCDEBUG if (0) cdebug_
308 #endif /* !YYDEBUG */
311 yy::b4_name::parse ()
316 /* Initialize stack. */
317 state_stack_
= StateStack (0);
318 semantic_stack_
= SemanticStack (1);
319 location_stack_
= LocationStack (1);
325 location
= initlocation_
;
327 YYCDEBUG
<< "Starting parse" << std::endl
;
331 state_stack_
.push (state_
);
332 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
338 /* Try to take a decision without lookahead. */
339 n_
= pact_
[[state_]];
343 /* Read a lookahead token. */
344 if (looka_
== empty_
)
346 YYCDEBUG
<< "Reading a token: ";
350 /* Convert token to internal form. */
355 YYCDEBUG
<< "Now at end of input." << std::endl
;
359 ilooka_
= translate_ (looka_
);
363 YYCDEBUG
<< "Next token is " << looka_
364 << " (" << name_
[[ilooka_]];
366 YYCDEBUG
<< ')' << std::endl
;
372 if (n_
< 0 || n_
> last_
|| check_
[[n_]] != ilooka_
)
375 /* Reduce or error. */
394 /* Shift the lookahead token. */
395 YYCDEBUG
<< "Shifting token " << looka_
396 << " (" << name_
[[ilooka_]] << "), ";
398 /* Discard the token being shifted unless it is eof. */
402 semantic_stack_
.push (value
);
403 location_stack_
.push (location
);
405 /* Count tokens shifted since error; after three, turn off error
413 /* Default action. */
415 n_
= defact_
[[state_]];
425 yyval
= semantic_stack_
[[len_
- 1]];
426 yyloc
= location_stack_
[[len_
- 1]];
430 yyval
= semantic_stack_
[[0]];
431 yyloc
= location_stack_
[[0]];
437 YYCDEBUG
<< "Reducing via rule " << n_
- 1
438 << " (line " << rline_
[[n_]] << "), ";
439 for (b4_uint_type(b4_prhs_max
) i
= prhs_
[[n_]];
441 YYCDEBUG
<< name_
[[rhs_
[i
]]] << ' ';
442 YYCDEBUG
<< "-> " << name_
[[r1_
[n_
]]] << std::endl
;
448 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
449 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
457 /* Line __line__ of __file__. */
458 #line __oline__ "__ofile__"
460 state_stack_
.pop (len_
);
461 semantic_stack_
.pop (len_
);
462 location_stack_
.pop (len_
);
467 YYCDEBUG
<< "state stack now";
468 for (StateStack::ConstIterator i
= state_stack_
.begin ();
469 i
!= state_stack_
.end (); ++i
)
470 YYCDEBUG
<< ' ' << *i
;
471 YYCDEBUG
<< std::endl
;
475 semantic_stack_
.push (yyval
);
476 location_stack_
.push (yyloc
);
478 /* Shift the result of the reduction. */
480 state_
= pgoto_
[[n_
- ntokens_
]] + state_stack_
[[0]];
481 if (state_
>= 0 && state_
<= last_
&& check_
[[state_]] == state_stack_
[[0]])
482 state_
= table_
[[state_]];
484 state_
= defgoto_
[[n_
- ntokens_
]];
487 /* Report and recover from errors. This is very incomplete. */
489 /* If not already recovering from an error, report this error. */
495 n_
= pact_
[[state_]];
496 if (n_
> flag_
&& n_
< last_
)
498 message
= "parse error, unexpected ";
499 message
+= name_
[[ilooka_]];
502 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
503 if (check_
[[x
+ n_
]] == x
)
508 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
509 if (check_
[[x
+ n_
]] == x
)
511 message
+= (!count
++) ? ", expecting " : " or ";
512 message
+= name_
[[x]];
519 message
= "parse error";
524 /* Error raised explicitly by an action. */
528 /* If just tried and failed to reuse lookahead token after an
529 error, discard it. */
531 /* Return failure if at end of input. */
534 YYCDEBUG
<< "Discarding token " << looka_
535 << " (" << name_
[[ilooka_]] << ")." << std::endl
;
539 /* Else will try to reuse lookahead token after shifting the error
546 n_
= pact_
[[state_]];
550 if (0 <= n_
&& n_
<= last_
&& check_
[[n_]] == terror_
)
558 /* Pop the current state because it cannot handle the error token. */
559 if (!state_stack_
.height ())
565 if (stos_
[[state_]] < ntokens_
)
567 YYCDEBUG
<< "Error: popping token "
568 << token_number_
[[stos_
[state_
]]]
569 << " (" << name_
[[stos_
[state_
]]];
571 YYPRINT (stderr
, token_number_
[[stos_
[state_
]]],
572 semantic_stack_
.top ());
574 YYCDEBUG
<< ')' << std::endl
;
578 YYCDEBUG
<< "Error: popping nonterminal ("
579 << name_
[[stos_
[state_
]]] << ')' << std::endl
;
584 state_
= (state_stack_
.pop (), state_stack_
[[0]]);
585 semantic_stack_
.pop ();
586 location_stack_
.pop ();;
591 YYCDEBUG
<< "Error: state stack now";
592 for (StateStack::ConstIterator i
= state_stack_
.begin ();
593 i
!= state_stack_
.end (); ++i
)
594 YYCDEBUG
<< ' ' << *i
;
595 YYCDEBUG
<< std::endl
;
603 YYCDEBUG
<< "Shifting error token, ";
605 semantic_stack_
.push (value
);
606 location_stack_
.push (location
);
624 looka_
= yylex (&value
, &location
);
626 looka_
= yylex (&value
);
630 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
633 yy::b4_name::pact_
[[]] =
638 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
639 doesn't specify something else to do. Zero means the default is an
642 yy::b4_name::defact_
[[]] =
647 /* YYPGOTO[[NTERM-NUM]]. */
649 yy::b4_name::pgoto_
[[]] =
654 /* YYDEFGOTO[[NTERM-NUM]]. */
656 yy::b4_name::defgoto_
[[]] =
661 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
662 positive, shift that token. If negative, reduce the rule which
663 number is the opposite. If zero, do what YYDEFACT says. */
665 yy::b4_name::table_
[[]] =
672 yy::b4_name::check_
[[]] =
678 /* STOS_[[STATE-NUM]] -- The (internal number of the) accessing
679 symbol of state STATE-NUM. */
680 const b4_uint_type(b4_stos_max
)
681 yy::b4_name::stos_
[[]] =
686 /* TOKEN_NUMBER_[[YYLEX-NUM]] -- Internal token number corresponding
689 yy::b4_name::token_number_
[[]] =
695 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
696 const b4_uint_type(b4_r1_max
)
697 yy::b4_name::r1_
[[]] =
702 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
703 const b4_uint_type(b4_r2_max
)
704 yy::b4_name::r2_
[[]] =
709 #if YYDEBUG || YYERROR_VERBOSE
710 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
711 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
713 const yy::b4_name::name_
[[]] =
720 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
721 const yy::b4_name::RhsNumberType
722 yy::b4_name::rhs_
[[]] =
727 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
729 const b4_uint_type(b4_prhs_max
)
730 yy::b4_name::prhs_
[[]] =
735 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
736 const b4_uint_type(b4_rline_max
)
737 yy::b4_name::rline_
[[]] =
743 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
744 yy::b4_name::TokenNumberType
745 yy::b4_name::translate_ (int token
)
748 const TokenNumberType
753 if ((unsigned) token
<= user_token_number_max_
)
754 return translate_
[[token]];
759 const int yy::b4_name::eof_
= 0;
760 const int yy::b4_name::last_
= b4_last
;
761 const int yy::b4_name::flag_
= b4_flag
;
762 const int yy::b4_name::nnts_
= b4_nnts
;
763 const int yy::b4_name::nsym_
= b4_nsym
;
764 const int yy::b4_name::empty_
= -2;
765 const int yy::b4_name::final_
= b4_final
;
766 const int yy::b4_name::terror_
= 1;
767 const int yy::b4_name::errcode_
= 256;
768 const int yy::b4_name::ntokens_
= b4_ntokens
;
769 const int yy::b4_name::initdepth_
= b4_initdepth
;
771 const unsigned yy::b4_name::user_token_number_max_
= b4_user_token_number_max
;
772 const yy::b4_name::TokenNumberType
yy::b4_name::undef_token_
= b4_undef_token_number
;
779 #ifndef BISON_STACK_HH
780 # define BISON_STACK_HH
786 template < class T
, class S
= std::deque
< T
> >
791 typedef typename
S::iterator Iterator
;
792 typedef typename
S::const_iterator ConstIterator
;
798 Stack (unsigned n
) : seq_ (n
)
804 operator [[]] (unsigned index
)
806 return seq_
[[index]];
811 operator [[]] (unsigned index
) const
813 return seq_
[[index]];
838 inline ConstIterator
begin () const { return seq_
.begin (); }
839 inline ConstIterator
end () const { return seq_
.end (); }
846 template < class T
, class S
= Stack
< T
> >
851 Slice (const S
& stack
,
852 unsigned range
) : stack_ (stack
),
859 operator [[]] (unsigned index
) const
861 return stack_
[[range_
- index
]];
871 #endif // not BISON_STACK_HH
873 #output "location.hh"
876 #ifndef BISON_LOCATION_HH
877 # define BISON_LOCATION_HH
894 #endif // not BISON_LOCATION_HH