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 /* LAST_ -- Last index in TABLE_. */
261 static const int last_
;
262 static const int nnts_
;
263 static const int empty_
;
264 static const int final_
;
265 static const int terror_
;
266 static const int errcode_
;
267 static const int ntokens_
;
268 static const int initdepth_
;
269 static const unsigned user_token_number_max_
;
270 static const TokenNumberType undef_token_
;
279 std::ostream
&cdebug_
;
281 /* Lookahead and lookahead in internal form. */
288 /* Semantic value and location of lookahead token. */
290 LocationType location
;
296 /* Initial location. */
297 LocationType initlocation_
;
301 #endif /* not b4_header_guard */
303 #output "b4_output_prefix[]b4_output_infix[].cc"
304 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
307 #include "b4_output_header_name"
309 /* Enable debugging if requested. */
311 # define YYCDEBUG if (debug_) cdebug_
313 # define YYCDEBUG if (0) cdebug_
314 #endif /* !YYDEBUG */
317 yy::b4_parser_class_name::parse ()
322 /* Initialize stack. */
323 state_stack_
= StateStack (0);
324 semantic_stack_
= SemanticStack (1);
325 location_stack_
= LocationStack (1);
331 location
= initlocation_
;
333 YYCDEBUG
<< "Starting parse" << std::endl
;
337 state_stack_
.push (state_
);
338 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
344 /* Try to take a decision without lookahead. */
345 n_
= pact_
[[state_]];
346 if (n_
== pact_ninf_
)
349 /* Read a lookahead token. */
350 if (looka_
== empty_
)
352 YYCDEBUG
<< "Reading a token: ";
356 /* Convert token to internal form. */
361 YYCDEBUG
<< "Now at end of input." << std::endl
;
365 ilooka_
= translate_ (looka_
);
369 YYCDEBUG
<< "Next token is " << looka_
370 << " (" << name_
[[ilooka_]];
372 YYCDEBUG
<< ')' << std::endl
;
378 if (n_
< 0 || last_
< n_
|| check_
[[n_]] != ilooka_
)
381 /* Reduce or error. */
385 if (n_
== table_ninf_
)
400 /* Shift the lookahead token. */
401 YYCDEBUG
<< "Shifting token " << looka_
402 << " (" << name_
[[ilooka_]] << "), ";
404 /* Discard the token being shifted unless it is eof. */
408 semantic_stack_
.push (value
);
409 location_stack_
.push (location
);
411 /* Count tokens shifted since error; after three, turn off error
419 /* Default action. */
421 n_
= defact_
[[state_]];
431 yyval
= semantic_stack_
[[len_
- 1]];
432 yyloc
= location_stack_
[[len_
- 1]];
436 yyval
= semantic_stack_
[[0]];
437 yyloc
= location_stack_
[[0]];
443 YYCDEBUG
<< "Reducing via rule " << n_
- 1
444 << " (line " << rline_
[[n_]] << "), ";
445 for (b4_int_type_for([b4_prhs
]) i
= prhs_
[[n_]];
447 YYCDEBUG
<< name_
[[rhs_
[i
]]] << ' ';
448 YYCDEBUG
<< "-> " << name_
[[r1_
[n_
]]] << std::endl
;
454 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
455 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
463 /* Line __line__ of __file__. */
464 #line __oline__ "__ofile__"
466 state_stack_
.pop (len_
);
467 semantic_stack_
.pop (len_
);
468 location_stack_
.pop (len_
);
473 YYCDEBUG
<< "state stack now";
474 for (StateStack::ConstIterator i
= state_stack_
.begin ();
475 i
!= state_stack_
.end (); ++i
)
476 YYCDEBUG
<< ' ' << *i
;
477 YYCDEBUG
<< std::endl
;
481 semantic_stack_
.push (yyval
);
482 location_stack_
.push (yyloc
);
484 /* Shift the result of the reduction. */
486 state_
= pgoto_
[[n_
- ntokens_
]] + state_stack_
[[0]];
487 if (0 <= state_
&& state_
<= last_
&& check_
[[state_]] == state_stack_
[[0]])
488 state_
= table_
[[state_]];
490 state_
= defgoto_
[[n_
- ntokens_
]];
493 /* Report and recover from errors. This is very incomplete. */
495 /* If not already recovering from an error, report this error. */
501 n_
= pact_
[[state_]];
502 if (pact_ninf_
< n_
&& n_
< last_
)
504 message
= "parse error, unexpected ";
505 message
+= name_
[[ilooka_]];
508 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
509 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
514 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
515 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
517 message
+= (!count
++) ? ", expecting " : " or ";
518 message
+= name_
[[x]];
525 message
= "parse error";
530 /* Error raised explicitly by an action. */
534 /* If just tried and failed to reuse lookahead token after an
535 error, discard it. */
537 /* Return failure if at end of input. */
540 YYCDEBUG
<< "Discarding token " << looka_
541 << " (" << name_
[[ilooka_]] << ")." << std::endl
;
545 /* Else will try to reuse lookahead token after shifting the error
552 n_
= pact_
[[state_]];
553 if (n_
!= pact_ninf_
)
556 if (0 <= n_
&& n_
<= last_
&& check_
[[n_]] == terror_
)
564 /* Pop the current state because it cannot handle the error token. */
565 if (!state_stack_
.height ())
571 if (stos_
[[state_]] < ntokens_
)
573 YYCDEBUG
<< "Error: popping token "
574 << token_number_
[[stos_
[state_
]]]
575 << " (" << name_
[[stos_
[state_
]]];
577 YYPRINT (stderr
, token_number_
[[stos_
[state_
]]],
578 semantic_stack_
.top ());
580 YYCDEBUG
<< ')' << std::endl
;
584 YYCDEBUG
<< "Error: popping nonterminal ("
585 << name_
[[stos_
[state_
]]] << ')' << std::endl
;
590 state_
= (state_stack_
.pop (), state_stack_
[[0]]);
591 semantic_stack_
.pop ();
592 location_stack_
.pop ();;
597 YYCDEBUG
<< "Error: state stack now";
598 for (StateStack::ConstIterator i
= state_stack_
.begin ();
599 i
!= state_stack_
.end (); ++i
)
600 YYCDEBUG
<< ' ' << *i
;
601 YYCDEBUG
<< std::endl
;
609 YYCDEBUG
<< "Shifting error token, ";
611 semantic_stack_
.push (value
);
612 location_stack_
.push (location
);
627 yy::b4_parser_class_name::lex_ ()
630 looka_
= yylex (&value
, &location
);
632 looka_
= yylex (&value
);
636 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
638 const b4_int_type_for([b4_pact
]) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
;
639 const b4_int_type_for([b4_pact
])
640 yy::b4_parser_class_name::pact_
[[]] =
645 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
646 doesn't specify something else to do. Zero means the default is an
648 const b4_int_type_for([b4_defact
])
649 yy::b4_parser_class_name::defact_
[[]] =
654 /* YYPGOTO[[NTERM-NUM]]. */
655 const b4_int_type_for([b4_pgoto
])
656 yy::b4_parser_class_name::pgoto_
[[]] =
661 /* YYDEFGOTO[[NTERM-NUM]]. */
662 const b4_int_type_for([b4_defgoto
])
663 yy::b4_parser_class_name::defgoto_
[[]] =
668 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
669 positive, shift that token. If negative, reduce the rule which
670 number is the opposite. If zero, do what YYDEFACT says. */
671 const b4_int_type_for([b4_table
]) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
;
672 const b4_int_type_for([b4_table
])
673 yy::b4_parser_class_name::table_
[[]] =
679 const b4_int_type_for([b4_check
])
680 yy::b4_parser_class_name::check_
[[]] =
686 /* STOS_[[STATE-NUM]] -- The (internal number of the) accessing
687 symbol of state STATE-NUM. */
688 const b4_int_type_for([b4_stos
])
689 yy::b4_parser_class_name::stos_
[[]] =
694 /* TOKEN_NUMBER_[[YYLEX-NUM]] -- Internal token number corresponding
696 const b4_int_type_for([b4_toknum
])
697 yy::b4_parser_class_name::token_number_
[[]] =
703 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
704 const b4_int_type_for([b4_r1
])
705 yy::b4_parser_class_name::r1_
[[]] =
710 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
711 const b4_int_type_for([b4_r2
])
712 yy::b4_parser_class_name::r2_
[[]] =
717 #if YYDEBUG || YYERROR_VERBOSE
718 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
719 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
721 const yy::b4_parser_class_name::name_
[[]] =
728 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
729 const yy::b4_parser_class_name::RhsNumberType
730 yy::b4_parser_class_name::rhs_
[[]] =
735 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
737 const b4_int_type_for([b4_prhs
])
738 yy::b4_parser_class_name::prhs_
[[]] =
743 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
744 const b4_int_type_for([b4_rline
])
745 yy::b4_parser_class_name::rline_
[[]] =
751 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
752 yy::b4_parser_class_name::TokenNumberType
753 yy::b4_parser_class_name::translate_ (int token
)
756 const TokenNumberType
761 if ((unsigned) token
<= user_token_number_max_
)
762 return translate_
[[token]];
767 const int yy::b4_parser_class_name::eof_
= 0;
768 const int yy::b4_parser_class_name::last_
= b4_last
;
769 const int yy::b4_parser_class_name::nnts_
= b4_nterms_number
;
770 const int yy::b4_parser_class_name::empty_
= -2;
771 const int yy::b4_parser_class_name::final_
= b4_final_state_number
;
772 const int yy::b4_parser_class_name::terror_
= 1;
773 const int yy::b4_parser_class_name::errcode_
= 256;
774 const int yy::b4_parser_class_name::ntokens_
= b4_tokens_number
;
775 const int yy::b4_parser_class_name::initdepth_
= b4_stack_depth_init
;
777 const unsigned yy::b4_parser_class_name::user_token_number_max_
= b4_user_token_number_max
;
778 const yy::b4_parser_class_name::TokenNumberType
yy::b4_parser_class_name::undef_token_
= b4_undef_token_number
;
785 #ifndef BISON_STACK_HH
786 # define BISON_STACK_HH
792 template < class T
, class S
= std::deque
< T
> >
797 typedef typename
S::iterator Iterator
;
798 typedef typename
S::const_iterator ConstIterator
;
804 Stack (unsigned n
) : seq_ (n
)
810 operator [[]] (unsigned index
)
812 return seq_
[[index]];
817 operator [[]] (unsigned index
) const
819 return seq_
[[index]];
844 inline ConstIterator
begin () const { return seq_
.begin (); }
845 inline ConstIterator
end () const { return seq_
.end (); }
852 template < class T
, class S
= Stack
< T
> >
857 Slice (const S
& stack
,
858 unsigned range
) : stack_ (stack
),
865 operator [[]] (unsigned index
) const
867 return stack_
[[range_
- index
]];
877 #endif // not BISON_STACK_HH
879 #output "location.hh"
882 #ifndef BISON_LOCATION_HH
883 # define BISON_LOCATION_HH
900 #endif // not BISON_LOCATION_HH