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
],
109 #output "b4_output_header_name"
110 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
112 #ifndef b4_header_guard
113 # define b4_header_guard
116 #include "location.hh"
121 /* Using locations. */
122 #define YYLSP_NEEDED b4_locations_flag
124 b4_token_defines(b4_tokens
)
126 /* Copy the first part of user declarations. */
129 /* Line __line__ of __file__. */
130 #line __oline__ "__ofile__"
132 /* Enabling traces. */
134 # define YYDEBUG b4_debug
137 /* Enabling verbose error message. */
138 #ifndef YYERROR_VERBOSE
139 # define YYERROR_VERBOSE b4_error_verbose
144 [#line b4_stype_line "b4_filename"
145 typedef union b4_stype yystype
;
146 /* Line __line__ of __file__. */
147 #line __oline__ "__ofile__"],
148 [typedef int yystype
;])
149 # define YYSTYPE yystype
152 /* Copy the second part of user declarations. */
155 /* Line __line__ of __file__. */
156 #line __oline__ "__ofile__"
157 #ifndef YYLLOC_DEFAULT
158 # define YYLLOC_DEFAULT(Current, Rhs, N) \
159 Current.last_line = Rhs[[N]].last_line; \
160 Current.last_column = Rhs[[N]].last_column;
165 class b4_parser_class_name
;
167 template < typename P
>
173 struct Traits
< b4_parser_class_name
>
175 typedef b4_int_type_for([b4_translate
]) TokenNumberType
;
176 typedef b4_int_type_for([b4_rhs
]) RhsNumberType
;
177 typedef int StateType
;
178 typedef yystype SemanticType
;
179 typedef b4_location_type LocationType
;
185 class b4_parser_class_name b4_inherit
189 typedef Traits
< b4_parser_class_name
>::TokenNumberType TokenNumberType
;
190 typedef Traits
< b4_parser_class_name
>::RhsNumberType RhsNumberType
;
191 typedef Traits
< b4_parser_class_name
>::StateType StateType
;
192 typedef Traits
< b4_parser_class_name
>::SemanticType SemanticType
;
193 typedef Traits
< b4_parser_class_name
>::LocationType LocationType
;
195 typedef Stack
< StateType
> StateStack
;
196 typedef Stack
< SemanticType
> SemanticStack
;
197 typedef Stack
< LocationType
> LocationStack
;
200 b4_parser_class_name (bool debug
,
201 LocationType initlocation
[]b4_param
) :
202 b4_constructor
[]debug_ (debug
),
204 initlocation_ (initlocation
)
206 b4_parser_class_name (bool debug
[]b4_param
) :
207 b4_constructor
[]debug_ (debug
),
213 virtual ~b4_parser_class_name ()
217 virtual int parse ();
221 virtual void lex_ ();
222 virtual void error_ ();
223 virtual void print_ ();
226 StateStack state_stack_
;
227 SemanticStack semantic_stack_
;
228 LocationStack location_stack_
;
231 static const b4_int_type_for([b4_pact
]) pact_
[[]];
232 static const b4_int_type_for([b4_pact
]) pact_ninf_
;
233 static const b4_int_type_for([b4_defact
]) defact_
[[]];
234 static const b4_int_type_for([b4_pgoto
]) pgoto_
[[]];
235 static const b4_int_type_for([b4_defgoto
]) defgoto_
[[]];
236 static const b4_int_type_for([b4_table
]) table_
[[]];
237 static const b4_int_type_for([b4_table
]) table_ninf_
;
238 static const b4_int_type_for([b4_check
]) check_
[[]];
239 static const b4_int_type_for([b4_r1
]) r1_
[[]];
240 static const b4_int_type_for([b4_r2
]) r2_
[[]];
242 #if YYDEBUG || YYERROR_VERBOSE
243 static const char* const name_
[[]];
246 /* More tables, for debugging. */
248 static const RhsNumberType rhs_
[[]];
249 static const b4_int_type_for([b4_prhs
]) prhs_
[[]];
250 static const b4_int_type_for([b4_rline
]) rline_
[[]];
251 static const b4_int_type_for([b4_stos
]) stos_
[[]];
252 static const b4_int_type_for([b4_toknum
]) token_number_
[[]];
255 /* Even more tables. */
256 static inline TokenNumberType
translate_ (int token
);
259 static const int eof_
;
260 static const int last_
;
261 static const int nnts_
;
262 static const int empty_
;
263 static const int final_
;
264 static const int terror_
;
265 static const int errcode_
;
266 static const int ntokens_
;
267 static const int initdepth_
;
268 static const unsigned user_token_number_max_
;
269 static const TokenNumberType undef_token_
;
278 std::ostream
&cdebug_
;
280 /* Lookahead and lookahead in internal form. */
287 /* Semantic value and location of lookahead token. */
289 LocationType location
;
295 /* Initial location. */
296 LocationType initlocation_
;
300 #endif /* not b4_header_guard */
302 #output "b4_output_prefix[]b4_output_infix[].cc"
303 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
306 #include "b4_output_header_name"
308 /* Enable debugging if requested. */
310 # define YYCDEBUG if (debug_) cdebug_
312 # define YYCDEBUG if (0) cdebug_
313 #endif /* !YYDEBUG */
316 yy::b4_parser_class_name::parse ()
321 /* Initialize stack. */
322 state_stack_
= StateStack (0);
323 semantic_stack_
= SemanticStack (1);
324 location_stack_
= LocationStack (1);
330 location
= initlocation_
;
332 YYCDEBUG
<< "Starting parse" << std::endl
;
336 state_stack_
.push (state_
);
337 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
343 /* Try to take a decision without lookahead. */
344 n_
= pact_
[[state_]];
345 if (n_
== pact_ninf_
)
348 /* Read a lookahead token. */
349 if (looka_
== empty_
)
351 YYCDEBUG
<< "Reading a token: ";
355 /* Convert token to internal form. */
360 YYCDEBUG
<< "Now at end of input." << std::endl
;
364 ilooka_
= translate_ (looka_
);
368 YYCDEBUG
<< "Next token is " << looka_
369 << " (" << name_
[[ilooka_]];
371 YYCDEBUG
<< ')' << std::endl
;
377 if (n_
< 0 || n_
> last_
|| check_
[[n_]] != ilooka_
)
380 /* Reduce or error. */
384 if (n_
== table_ninf_
)
399 /* Shift the lookahead token. */
400 YYCDEBUG
<< "Shifting token " << looka_
401 << " (" << name_
[[ilooka_]] << "), ";
403 /* Discard the token being shifted unless it is eof. */
407 semantic_stack_
.push (value
);
408 location_stack_
.push (location
);
410 /* Count tokens shifted since error; after three, turn off error
418 /* Default action. */
420 n_
= defact_
[[state_]];
430 yyval
= semantic_stack_
[[len_
- 1]];
431 yyloc
= location_stack_
[[len_
- 1]];
435 yyval
= semantic_stack_
[[0]];
436 yyloc
= location_stack_
[[0]];
442 YYCDEBUG
<< "Reducing via rule " << n_
- 1
443 << " (line " << rline_
[[n_]] << "), ";
444 for (b4_int_type_for([b4_prhs
]) i
= prhs_
[[n_]];
446 YYCDEBUG
<< name_
[[rhs_
[i
]]] << ' ';
447 YYCDEBUG
<< "-> " << name_
[[r1_
[n_
]]] << std::endl
;
453 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
454 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
462 /* Line __line__ of __file__. */
463 #line __oline__ "__ofile__"
465 state_stack_
.pop (len_
);
466 semantic_stack_
.pop (len_
);
467 location_stack_
.pop (len_
);
472 YYCDEBUG
<< "state stack now";
473 for (StateStack::ConstIterator i
= state_stack_
.begin ();
474 i
!= state_stack_
.end (); ++i
)
475 YYCDEBUG
<< ' ' << *i
;
476 YYCDEBUG
<< std::endl
;
480 semantic_stack_
.push (yyval
);
481 location_stack_
.push (yyloc
);
483 /* Shift the result of the reduction. */
485 state_
= pgoto_
[[n_
- ntokens_
]] + state_stack_
[[0]];
486 if (state_
>= 0 && state_
<= last_
&& check_
[[state_]] == state_stack_
[[0]])
487 state_
= table_
[[state_]];
489 state_
= defgoto_
[[n_
- ntokens_
]];
492 /* Report and recover from errors. This is very incomplete. */
494 /* If not already recovering from an error, report this error. */
500 n_
= pact_
[[state_]];
501 if (pact_ninf_
< n_
&& n_
< last_
)
503 message
= "parse error, unexpected ";
504 message
+= name_
[[ilooka_]];
507 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
508 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
513 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
514 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
516 message
+= (!count
++) ? ", expecting " : " or ";
517 message
+= name_
[[x]];
524 message
= "parse error";
529 /* Error raised explicitly by an action. */
533 /* If just tried and failed to reuse lookahead token after an
534 error, discard it. */
536 /* Return failure if at end of input. */
539 YYCDEBUG
<< "Discarding token " << looka_
540 << " (" << name_
[[ilooka_]] << ")." << std::endl
;
544 /* Else will try to reuse lookahead token after shifting the error
551 n_
= pact_
[[state_]];
552 if (n_
!= pact_ninf_
)
555 if (0 <= n_
&& n_
<= last_
&& check_
[[n_]] == terror_
)
563 /* Pop the current state because it cannot handle the error token. */
564 if (!state_stack_
.height ())
570 if (stos_
[[state_]] < ntokens_
)
572 YYCDEBUG
<< "Error: popping token "
573 << token_number_
[[stos_
[state_
]]]
574 << " (" << name_
[[stos_
[state_
]]];
576 YYPRINT (stderr
, token_number_
[[stos_
[state_
]]],
577 semantic_stack_
.top ());
579 YYCDEBUG
<< ')' << std::endl
;
583 YYCDEBUG
<< "Error: popping nonterminal ("
584 << name_
[[stos_
[state_
]]] << ')' << std::endl
;
589 state_
= (state_stack_
.pop (), state_stack_
[[0]]);
590 semantic_stack_
.pop ();
591 location_stack_
.pop ();;
596 YYCDEBUG
<< "Error: state stack now";
597 for (StateStack::ConstIterator i
= state_stack_
.begin ();
598 i
!= state_stack_
.end (); ++i
)
599 YYCDEBUG
<< ' ' << *i
;
600 YYCDEBUG
<< std::endl
;
608 YYCDEBUG
<< "Shifting error token, ";
610 semantic_stack_
.push (value
);
611 location_stack_
.push (location
);
626 yy::b4_parser_class_name::lex_ ()
629 looka_
= yylex (&value
, &location
);
631 looka_
= yylex (&value
);
635 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
637 const b4_int_type_for([b4_pact
]) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
;
638 const b4_int_type_for([b4_pact
])
639 yy::b4_parser_class_name::pact_
[[]] =
644 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
645 doesn't specify something else to do. Zero means the default is an
647 const b4_int_type_for([b4_defact
])
648 yy::b4_parser_class_name::defact_
[[]] =
653 /* YYPGOTO[[NTERM-NUM]]. */
654 const b4_int_type_for([b4_pgoto
])
655 yy::b4_parser_class_name::pgoto_
[[]] =
660 /* YYDEFGOTO[[NTERM-NUM]]. */
661 const b4_int_type_for([b4_defgoto
])
662 yy::b4_parser_class_name::defgoto_
[[]] =
667 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
668 positive, shift that token. If negative, reduce the rule which
669 number is the opposite. If zero, do what YYDEFACT says. */
670 const b4_int_type_for([b4_table
]) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
;
671 const b4_int_type_for([b4_table
])
672 yy::b4_parser_class_name::table_
[[]] =
678 const b4_int_type_for([b4_check
])
679 yy::b4_parser_class_name::check_
[[]] =
685 /* STOS_[[STATE-NUM]] -- The (internal number of the) accessing
686 symbol of state STATE-NUM. */
687 const b4_int_type_for([b4_stos
])
688 yy::b4_parser_class_name::stos_
[[]] =
693 /* TOKEN_NUMBER_[[YYLEX-NUM]] -- Internal token number corresponding
695 const b4_int_type_for([b4_toknum
])
696 yy::b4_parser_class_name::token_number_
[[]] =
702 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
703 const b4_int_type_for([b4_r1
])
704 yy::b4_parser_class_name::r1_
[[]] =
709 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
710 const b4_int_type_for([b4_r2
])
711 yy::b4_parser_class_name::r2_
[[]] =
716 #if YYDEBUG || YYERROR_VERBOSE
717 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
718 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
720 const yy::b4_parser_class_name::name_
[[]] =
727 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
728 const yy::b4_parser_class_name::RhsNumberType
729 yy::b4_parser_class_name::rhs_
[[]] =
734 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
736 const b4_int_type_for([b4_prhs
])
737 yy::b4_parser_class_name::prhs_
[[]] =
742 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
743 const b4_int_type_for([b4_rline
])
744 yy::b4_parser_class_name::rline_
[[]] =
750 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
751 yy::b4_parser_class_name::TokenNumberType
752 yy::b4_parser_class_name::translate_ (int token
)
755 const TokenNumberType
760 if ((unsigned) token
<= user_token_number_max_
)
761 return translate_
[[token]];
766 const int yy::b4_parser_class_name::eof_
= 0;
767 const int yy::b4_parser_class_name::last_
= b4_last
;
768 const int yy::b4_parser_class_name::nnts_
= b4_nterms_number
;
769 const int yy::b4_parser_class_name::empty_
= -2;
770 const int yy::b4_parser_class_name::final_
= b4_final_state_number
;
771 const int yy::b4_parser_class_name::terror_
= 1;
772 const int yy::b4_parser_class_name::errcode_
= 256;
773 const int yy::b4_parser_class_name::ntokens_
= b4_tokens_number
;
774 const int yy::b4_parser_class_name::initdepth_
= b4_stack_depth_init
;
776 const unsigned yy::b4_parser_class_name::user_token_number_max_
= b4_user_token_number_max
;
777 const yy::b4_parser_class_name::TokenNumberType
yy::b4_name::undef_token_
= b4_undef_token_number
;
784 #ifndef BISON_STACK_HH
785 # define BISON_STACK_HH
791 template < class T
, class S
= std::deque
< T
> >
796 typedef typename
S::iterator Iterator
;
797 typedef typename
S::const_iterator ConstIterator
;
803 Stack (unsigned n
) : seq_ (n
)
809 operator [[]] (unsigned index
)
811 return seq_
[[index]];
816 operator [[]] (unsigned index
) const
818 return seq_
[[index]];
843 inline ConstIterator
begin () const { return seq_
.begin (); }
844 inline ConstIterator
end () const { return seq_
.end (); }
851 template < class T
, class S
= Stack
< T
> >
856 Slice (const S
& stack
,
857 unsigned range
) : stack_ (stack
),
864 operator [[]] (unsigned index
) const
866 return stack_
[[range_
- index
]];
876 #endif // not BISON_STACK_HH
878 #output "location.hh"
881 #ifndef BISON_LOCATION_HH
882 # define BISON_LOCATION_HH
899 #endif // not BISON_LOCATION_HH