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 ## ---------------- ##
24 ## ---------------- ##
27 m4_define_default([b4_stack_depth_init
], [200])
29 # Default Parser class name.
30 m4_define_default([b4_parser_class_name
], [Parser
])
34 ## ----------------- ##
35 ## Semantic Values. ##
36 ## ----------------- ##
39 # b4_lhs_value([TYPE])
40 # --------------------
41 # Expansion of $<TYPE>$.
42 m4_define([b4_lhs_value
],
43 [yyval
[]m4_ifval([$
1], [.$
1])])
46 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
47 # --------------------------------------
48 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
50 m4_define([b4_rhs_value
],
51 [semantic_stack_@
<:@
m4_eval([$
1 - $
2])@
:>@
m4_ifval([$
3], [.$
3])])
53 m4_define_default([b4_location_type
], [Location
])
58 m4_define([b4_lhs_location
],
62 # b4_rhs_location(RULE-LENGTH, NUM)
63 # ---------------------------------
64 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
66 m4_define([b4_rhs_location
],
67 [location_stack_@
<:@
m4_eval([$
1 - $
2])@
:>@
])
70 m4_define_default([b4_input_suffix
], [.y
])
72 m4_define_default([b4_output_parser_suffix
],
73 [m4_translit(b4_input_suffix
, [yY
], [cC
])])
75 m4_define_default([b4_output_parser_name
],
76 [b4_output_prefix
[]b4_output_infix
[]b4_output_parser_suffix
[]])
79 m4_define_default([b4_output_header_suffix
],
80 [m4_translit(b4_input_suffix
, [yY
], [hH
])])
82 m4_define_default([b4_output_header_name
],
83 [b4_output_prefix
[]b4_output_infix
[]b4_output_header_suffix
[]])
85 m4_define_default([b4_header_guard
],
86 [m4_bpatsubst(m4_toupper([BISON_
]b4_output_header_name
),
87 [[^ABCDEFGHIJKLMNOPQRSTUVWXYZ
]], [_
])])
89 m4_define([b4_inherit
],
101 m4_define([b4_constructor
],
108 # We do want M4 expansion after # for CPP macros.
111 #output "b4_output_header_name"
112 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
114 #ifndef b4_header_guard
115 # define b4_header_guard
118 #include "location.hh"
123 /* Using locations. */
124 #define YYLSP_NEEDED b4_locations_flag
126 b4_token_defines(b4_tokens
)
128 /* Copy the first part of user declarations. */
131 /* Line __line__ of __file__. */
132 #line __oline__ "__ofile__"
134 /* Enabling traces. */
136 # define YYDEBUG b4_debug
139 /* Enabling verbose error message. */
140 #ifndef YYERROR_VERBOSE
141 # define YYERROR_VERBOSE b4_error_verbose
146 [#line b4_stype_line "b4_filename"
147 typedef union b4_stype yystype
;
148 /* Line __line__ of __file__. */
149 #line __oline__ "__ofile__"],
150 [typedef int yystype
;])
151 # define YYSTYPE yystype
154 /* Copy the second part of user declarations. */
157 /* Line __line__ of __file__. */
158 #line __oline__ "__ofile__"
159 #ifndef YYLLOC_DEFAULT
160 # define YYLLOC_DEFAULT(Current, Rhs, N) \
161 Current.last_line = Rhs[[N]].last_line; \
162 Current.last_column = Rhs[[N]].last_column;
167 class b4_parser_class_name
;
169 template < typename P
>
175 struct Traits
< b4_parser_class_name
>
177 typedef b4_int_type_for([b4_translate
]) TokenNumberType
;
178 typedef b4_int_type_for([b4_rhs
]) RhsNumberType
;
179 typedef int StateType
;
180 typedef yystype SemanticType
;
181 typedef b4_location_type LocationType
;
187 class b4_parser_class_name b4_inherit
191 typedef Traits
< b4_parser_class_name
>::TokenNumberType TokenNumberType
;
192 typedef Traits
< b4_parser_class_name
>::RhsNumberType RhsNumberType
;
193 typedef Traits
< b4_parser_class_name
>::StateType StateType
;
194 typedef Traits
< b4_parser_class_name
>::SemanticType SemanticType
;
195 typedef Traits
< b4_parser_class_name
>::LocationType LocationType
;
197 typedef Stack
< StateType
> StateStack
;
198 typedef Stack
< SemanticType
> SemanticStack
;
199 typedef Stack
< LocationType
> LocationStack
;
202 b4_parser_class_name (bool debug
,
203 LocationType initlocation
[]b4_param
) :
204 b4_constructor
[]debug_ (debug
),
206 initlocation_ (initlocation
)
208 b4_parser_class_name (bool debug
[]b4_param
) :
209 b4_constructor
[]debug_ (debug
),
215 virtual ~b4_parser_class_name ()
219 virtual int parse ();
223 virtual void lex_ ();
224 virtual void error_ ();
225 virtual void print_ ();
228 StateStack state_stack_
;
229 SemanticStack semantic_stack_
;
230 LocationStack location_stack_
;
233 static const b4_int_type_for([b4_pact
]) pact_
[[]];
234 static const b4_int_type_for([b4_pact
]) pact_ninf_
;
235 static const b4_int_type_for([b4_defact
]) defact_
[[]];
236 static const b4_int_type_for([b4_pgoto
]) pgoto_
[[]];
237 static const b4_int_type_for([b4_defgoto
]) defgoto_
[[]];
238 static const b4_int_type_for([b4_table
]) table_
[[]];
239 static const b4_int_type_for([b4_table
]) table_ninf_
;
240 static const b4_int_type_for([b4_check
]) check_
[[]];
241 static const b4_int_type_for([b4_r1
]) r1_
[[]];
242 static const b4_int_type_for([b4_r2
]) r2_
[[]];
244 #if YYDEBUG || YYERROR_VERBOSE
245 static const char* const name_
[[]];
248 /* More tables, for debugging. */
250 static const RhsNumberType rhs_
[[]];
251 static const b4_int_type_for([b4_prhs
]) prhs_
[[]];
252 static const b4_int_type_for([b4_rline
]) rline_
[[]];
253 static const b4_int_type_for([b4_stos
]) stos_
[[]];
254 static const b4_int_type_for([b4_toknum
]) token_number_
[[]];
257 /* Even more tables. */
258 static inline TokenNumberType
translate_ (int token
);
261 static const int eof_
;
262 /* LAST_ -- Last index in TABLE_. */
263 static const int last_
;
264 static const int nnts_
;
265 static const int empty_
;
266 static const int final_
;
267 static const int terror_
;
268 static const int errcode_
;
269 static const int ntokens_
;
270 static const int initdepth_
;
271 static const unsigned user_token_number_max_
;
272 static const TokenNumberType undef_token_
;
281 std::ostream
&cdebug_
;
283 /* Lookahead and lookahead in internal form. */
290 /* Semantic value and location of lookahead token. */
292 LocationType location
;
298 /* Initial location. */
299 LocationType initlocation_
;
303 #endif /* not b4_header_guard */
305 #output "b4_output_prefix[]b4_output_infix[].cc"
306 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
309 #include "b4_output_header_name"
311 /* Enable debugging if requested. */
313 # define YYCDEBUG if (debug_) cdebug_
315 # define YYCDEBUG if (0) cdebug_
316 #endif /* !YYDEBUG */
319 yy::b4_parser_class_name::parse ()
324 /* Initialize stack. */
325 state_stack_
= StateStack (0);
326 semantic_stack_
= SemanticStack (1);
327 location_stack_
= LocationStack (1);
333 location
= initlocation_
;
335 YYCDEBUG
<< "Starting parse" << std::endl
;
339 state_stack_
.push (state_
);
340 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
346 /* Try to take a decision without lookahead. */
347 n_
= pact_
[[state_]];
348 if (n_
== pact_ninf_
)
351 /* Read a lookahead token. */
352 if (looka_
== empty_
)
354 YYCDEBUG
<< "Reading a token: ";
358 /* Convert token to internal form. */
363 YYCDEBUG
<< "Now at end of input." << std::endl
;
367 ilooka_
= translate_ (looka_
);
371 YYCDEBUG
<< "Next token is " << looka_
372 << " (" << name_
[[ilooka_]];
374 YYCDEBUG
<< ')' << std::endl
;
380 if (n_
< 0 || last_
< n_
|| check_
[[n_]] != ilooka_
)
383 /* Reduce or error. */
387 if (n_
== table_ninf_
)
402 /* Shift the lookahead token. */
403 YYCDEBUG
<< "Shifting token " << looka_
404 << " (" << name_
[[ilooka_]] << "), ";
406 /* Discard the token being shifted unless it is eof. */
410 semantic_stack_
.push (value
);
411 location_stack_
.push (location
);
413 /* Count tokens shifted since error; after three, turn off error
421 /* Default action. */
423 n_
= defact_
[[state_]];
433 yyval
= semantic_stack_
[[len_
- 1]];
434 yyloc
= location_stack_
[[len_
- 1]];
438 yyval
= semantic_stack_
[[0]];
439 yyloc
= location_stack_
[[0]];
445 YYCDEBUG
<< "Reducing via rule " << n_
- 1
446 << " (line " << rline_
[[n_]] << "), ";
447 for (b4_int_type_for([b4_prhs
]) i
= prhs_
[[n_]];
449 YYCDEBUG
<< name_
[[rhs_
[i
]]] << ' ';
450 YYCDEBUG
<< "-> " << name_
[[r1_
[n_
]]] << std::endl
;
456 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
457 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
465 /* Line __line__ of __file__. */
466 #line __oline__ "__ofile__"
468 state_stack_
.pop (len_
);
469 semantic_stack_
.pop (len_
);
470 location_stack_
.pop (len_
);
475 YYCDEBUG
<< "state stack now";
476 for (StateStack::ConstIterator i
= state_stack_
.begin ();
477 i
!= state_stack_
.end (); ++i
)
478 YYCDEBUG
<< ' ' << *i
;
479 YYCDEBUG
<< std::endl
;
483 semantic_stack_
.push (yyval
);
484 location_stack_
.push (yyloc
);
486 /* Shift the result of the reduction. */
488 state_
= pgoto_
[[n_
- ntokens_
]] + state_stack_
[[0]];
489 if (0 <= state_
&& state_
<= last_
&& check_
[[state_]] == state_stack_
[[0]])
490 state_
= table_
[[state_]];
492 state_
= defgoto_
[[n_
- ntokens_
]];
495 /* Report and recover from errors. This is very incomplete. */
497 /* If not already recovering from an error, report this error. */
503 n_
= pact_
[[state_]];
504 if (pact_ninf_
< n_
&& n_
< last_
)
506 message
= "parse error, unexpected ";
507 message
+= name_
[[ilooka_]];
510 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
511 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
516 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
517 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
519 message
+= (!count
++) ? ", expecting " : " or ";
520 message
+= name_
[[x]];
527 message
= "parse error";
532 /* Error raised explicitly by an action. */
536 /* If just tried and failed to reuse lookahead token after an
537 error, discard it. */
539 /* Return failure if at end of input. */
542 YYCDEBUG
<< "Discarding token " << looka_
543 << " (" << name_
[[ilooka_]] << ")." << std::endl
;
547 /* Else will try to reuse lookahead token after shifting the error
554 n_
= pact_
[[state_]];
555 if (n_
!= pact_ninf_
)
558 if (0 <= n_
&& n_
<= last_
&& check_
[[n_]] == terror_
)
566 /* Pop the current state because it cannot handle the error token. */
567 if (!state_stack_
.height ())
573 if (stos_
[[state_]] < ntokens_
)
575 YYCDEBUG
<< "Error: popping token "
576 << token_number_
[[stos_
[state_
]]]
577 << " (" << name_
[[stos_
[state_
]]];
579 YYPRINT (stderr
, token_number_
[[stos_
[state_
]]],
580 semantic_stack_
.top ());
582 YYCDEBUG
<< ')' << std::endl
;
586 YYCDEBUG
<< "Error: popping nonterminal ("
587 << name_
[[stos_
[state_
]]] << ')' << std::endl
;
592 state_
= (state_stack_
.pop (), state_stack_
[[0]]);
593 semantic_stack_
.pop ();
594 location_stack_
.pop ();;
599 YYCDEBUG
<< "Error: state stack now";
600 for (StateStack::ConstIterator i
= state_stack_
.begin ();
601 i
!= state_stack_
.end (); ++i
)
602 YYCDEBUG
<< ' ' << *i
;
603 YYCDEBUG
<< std::endl
;
611 YYCDEBUG
<< "Shifting error token, ";
613 semantic_stack_
.push (value
);
614 location_stack_
.push (location
);
629 yy::b4_parser_class_name::lex_ ()
632 looka_
= yylex (&value
, &location
);
634 looka_
= yylex (&value
);
638 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
640 const b4_int_type_for([b4_pact
]) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
;
641 const b4_int_type_for([b4_pact
])
642 yy::b4_parser_class_name::pact_
[[]] =
647 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
648 doesn't specify something else to do. Zero means the default is an
650 const b4_int_type_for([b4_defact
])
651 yy::b4_parser_class_name::defact_
[[]] =
656 /* YYPGOTO[[NTERM-NUM]]. */
657 const b4_int_type_for([b4_pgoto
])
658 yy::b4_parser_class_name::pgoto_
[[]] =
663 /* YYDEFGOTO[[NTERM-NUM]]. */
664 const b4_int_type_for([b4_defgoto
])
665 yy::b4_parser_class_name::defgoto_
[[]] =
670 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
671 positive, shift that token. If negative, reduce the rule which
672 number is the opposite. If zero, do what YYDEFACT says. */
673 const b4_int_type_for([b4_table
]) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
;
674 const b4_int_type_for([b4_table
])
675 yy::b4_parser_class_name::table_
[[]] =
681 const b4_int_type_for([b4_check
])
682 yy::b4_parser_class_name::check_
[[]] =
688 /* STOS_[[STATE-NUM]] -- The (internal number of the) accessing
689 symbol of state STATE-NUM. */
690 const b4_int_type_for([b4_stos
])
691 yy::b4_parser_class_name::stos_
[[]] =
696 /* TOKEN_NUMBER_[[YYLEX-NUM]] -- Internal token number corresponding
698 const b4_int_type_for([b4_toknum
])
699 yy::b4_parser_class_name::token_number_
[[]] =
705 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
706 const b4_int_type_for([b4_r1
])
707 yy::b4_parser_class_name::r1_
[[]] =
712 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
713 const b4_int_type_for([b4_r2
])
714 yy::b4_parser_class_name::r2_
[[]] =
719 #if YYDEBUG || YYERROR_VERBOSE
720 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
721 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
723 const yy::b4_parser_class_name::name_
[[]] =
730 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
731 const yy::b4_parser_class_name::RhsNumberType
732 yy::b4_parser_class_name::rhs_
[[]] =
737 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
739 const b4_int_type_for([b4_prhs
])
740 yy::b4_parser_class_name::prhs_
[[]] =
745 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
746 const b4_int_type_for([b4_rline
])
747 yy::b4_parser_class_name::rline_
[[]] =
753 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
754 yy::b4_parser_class_name::TokenNumberType
755 yy::b4_parser_class_name::translate_ (int token
)
758 const TokenNumberType
763 if ((unsigned) token
<= user_token_number_max_
)
764 return translate_
[[token]];
769 const int yy::b4_parser_class_name::eof_
= 0;
770 const int yy::b4_parser_class_name::last_
= b4_last
;
771 const int yy::b4_parser_class_name::nnts_
= b4_nterms_number
;
772 const int yy::b4_parser_class_name::empty_
= -2;
773 const int yy::b4_parser_class_name::final_
= b4_final_state_number
;
774 const int yy::b4_parser_class_name::terror_
= 1;
775 const int yy::b4_parser_class_name::errcode_
= 256;
776 const int yy::b4_parser_class_name::ntokens_
= b4_tokens_number
;
777 const int yy::b4_parser_class_name::initdepth_
= b4_stack_depth_init
;
779 const unsigned yy::b4_parser_class_name::user_token_number_max_
= b4_user_token_number_max
;
780 const yy::b4_parser_class_name::TokenNumberType
yy::b4_parser_class_name::undef_token_
= b4_undef_token_number
;
787 #ifndef BISON_STACK_HH
788 # define BISON_STACK_HH
794 template < class T
, class S
= std::deque
< T
> >
799 typedef typename
S::iterator Iterator
;
800 typedef typename
S::const_iterator ConstIterator
;
806 Stack (unsigned n
) : seq_ (n
)
812 operator [[]] (unsigned index
)
814 return seq_
[[index]];
819 operator [[]] (unsigned index
) const
821 return seq_
[[index]];
846 inline ConstIterator
begin () const { return seq_
.begin (); }
847 inline ConstIterator
end () const { return seq_
.end (); }
854 template < class T
, class S
= Stack
< T
> >
859 Slice (const S
& stack
,
860 unsigned range
) : stack_ (stack
),
867 operator [[]] (unsigned index
) const
869 return stack_
[[range_
- index
]];
879 #endif // not BISON_STACK_HH
881 #output "location.hh"
884 #ifndef BISON_LOCATION_HH
885 # define BISON_LOCATION_HH
902 #endif // not BISON_LOCATION_HH