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
20 ## ---------------- ##
22 ## ---------------- ##
25 m4_define_default([b4_stack_depth_init
], [200])
27 # Default Parser class name.
28 m4_define_default([b4_parser_class_name
], [Parser
])
32 ## ----------------- ##
33 ## Semantic Values. ##
34 ## ----------------- ##
37 # b4_lhs_value([TYPE])
38 # --------------------
39 # Expansion of $<TYPE>$.
40 m4_define([b4_lhs_value
],
41 [yyval
[]m4_ifval([$
1], [.$
1])])
44 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
45 # --------------------------------------
46 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
48 m4_define([b4_rhs_value
],
49 [semantic_stack_@
{m4_eval([$
1 - $
2])@
}m4_ifval([$
3], [.$
3])])
51 m4_define_default([b4_location_type
], [Location
])
56 m4_define([b4_lhs_location
],
60 # b4_rhs_location(RULE-LENGTH, NUM)
61 # ---------------------------------
62 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
64 m4_define([b4_rhs_location
],
65 [location_stack_@
{m4_eval([$
1 - $
2])@
}])
68 m4_define([b4_inherit
],
80 m4_define([b4_constructor
],
87 # We do want M4 expansion after # for CPP macros.
90 @output @output_header_name@
91 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
93 /* FIXME: This is wrong, we want computed header guards.
94 I don't know why the macros are missing now. :( */
95 #ifndef PARSER_HEADER_H
96 # define PARSER_HEADER_H
99 #include "location.hh"
104 /* Using locations. */
105 #define YYLSP_NEEDED ]b4_locations_flag[
107 ]b4_token_defines(b4_tokens
)[
109 /* Copy the first part of user declarations. */
112 ]/* Line __line__ of lalr1.cc. */
113 b4_syncline([@oline@
], [@ofile@
])[
115 /* Enabling traces. */
117 # define YYDEBUG ]b4_debug[
120 /* Enabling verbose error message. */
121 #ifndef YYERROR_VERBOSE
122 # define YYERROR_VERBOSE ]b4_error_verbose[
126 ]m4_ifdef([b4_stype
],
127 [b4_syncline([b4_stype_line
], [b4_filename
])
128 typedef union b4_stype yystype
;
129 /* Line __line__ of lalr1.cc. */
130 b4_syncline([@oline@
], [@ofile@
])],
131 [typedef int yystype
;])[
132 # define YYSTYPE yystype
135 /* Copy the second part of user declarations. */
138 ]/* Line __line__ of lalr1.cc. */
139 b4_syncline([@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;
148 class ]b4_parser_class_name
[;
150 template < typename P
>
156 struct Traits
< ]b4_parser_class_name
[ >
158 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
159 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
160 typedef int StateType
;
161 typedef yystype SemanticType
;
162 typedef ]b4_location_type
[ LocationType
;
168 class ]b4_parser_class_name b4_inherit
[
172 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
173 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
174 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
175 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
176 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
178 typedef Stack
< StateType
> StateStack
;
179 typedef Stack
< SemanticType
> SemanticStack
;
180 typedef Stack
< LocationType
> LocationStack
;
183 ]b4_parser_class_name
[ (bool debug
,
184 LocationType initlocation
][]b4_param
[) :
185 ]b4_constructor
[][debug_ (debug
),
187 initlocation_ (initlocation
)
189 ]b4_parser_class_name
[ (bool debug
][]b4_param
[) :
190 ]b4_constructor
[][debug_ (debug
),
196 virtual ~]b4_parser_class_name
[ ()
200 virtual int parse ();
204 virtual void lex_ ();
205 virtual void error_ ();
206 virtual void print_ ();
209 StateStack state_stack_
;
210 SemanticStack semantic_stack_
;
211 LocationStack location_stack_
;
214 static const ]b4_int_type_for([b4_pact
])[ pact_
[];
215 static const ]b4_int_type_for([b4_pact
])[ pact_ninf_
;
216 static const ]b4_int_type_for([b4_defact
])[ defact_
[];
217 static const ]b4_int_type_for([b4_pgoto
])[ pgoto_
[];
218 static const ]b4_int_type_for([b4_defgoto
])[ defgoto_
[];
219 static const ]b4_int_type_for([b4_table
])[ table_
[];
220 static const ]b4_int_type_for([b4_table
])[ table_ninf_
;
221 static const ]b4_int_type_for([b4_check
])[ check_
[];
222 static const ]b4_int_type_for([b4_r1
])[ r1_
[];
223 static const ]b4_int_type_for([b4_r2
])[ r2_
[];
225 #if YYDEBUG || YYERROR_VERBOSE
226 static const char* const name_
[];
229 /* More tables, for debugging. */
231 static const RhsNumberType rhs_
[];
232 static const ]b4_int_type_for([b4_prhs
])[ prhs_
[];
233 static const ]b4_int_type_for([b4_rline
])[ rline_
[];
234 static const ]b4_int_type_for([b4_stos
])[ stos_
[];
235 static const ]b4_int_type_for([b4_toknum
])[ token_number_
[];
238 /* Even more tables. */
239 static inline TokenNumberType
translate_ (int token
);
242 static const int eof_
;
243 /* LAST_ -- Last index in TABLE_. */
244 static const int last_
;
245 static const int nnts_
;
246 static const int empty_
;
247 static const int final_
;
248 static const int terror_
;
249 static const int errcode_
;
250 static const int ntokens_
;
251 static const int initdepth_
;
252 static const unsigned user_token_number_max_
;
253 static const TokenNumberType undef_token_
;
262 std::ostream
&cdebug_
;
264 /* Lookahead and lookahead in internal form. */
271 /* Semantic value and location of lookahead token. */
273 LocationType location
;
279 /* Initial location. */
280 LocationType initlocation_
;
284 #endif /* ! defined PARSER_HEADER_H */]
286 @output @output_parser_name@
287 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
290 #include @output_header_name@
292 /* Enable debugging if requested. */
294 # define YYCDEBUG if (debug_) cdebug_
296 # define YYCDEBUG if (0) cdebug_
297 #endif /* !YYDEBUG */
300 yy::]b4_parser_class_name
[::parse ()
305 /* Initialize stack. */
306 state_stack_
= StateStack (0);
307 semantic_stack_
= SemanticStack (1);
308 location_stack_
= LocationStack (1);
314 location
= initlocation_
;
316 YYCDEBUG
<< "Starting parse" << std::endl
;
320 state_stack_
.push (state_
);
321 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
327 /* Try to take a decision without lookahead. */
329 if (n_
== pact_ninf_
)
332 /* Read a lookahead token. */
333 if (looka_
== empty_
)
335 YYCDEBUG
<< "Reading a token: ";
339 /* Convert token to internal form. */
344 YYCDEBUG
<< "Now at end of input." << std::endl
;
348 ilooka_
= translate_ (looka_
);
352 YYCDEBUG
<< "Next token is " << looka_
353 << " (" << name_
[ilooka_
];
355 YYCDEBUG
<< ')' << std::endl
;
361 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
364 /* Reduce or error. */
368 if (n_
== table_ninf_
)
383 /* Shift the lookahead token. */
384 YYCDEBUG
<< "Shifting token " << looka_
385 << " (" << name_
[ilooka_
] << "), ";
387 /* Discard the token being shifted unless it is eof. */
391 semantic_stack_
.push (value
);
392 location_stack_
.push (location
);
394 /* Count tokens shifted since error; after three, turn off error
402 /* Default action. */
404 n_
= defact_
[state_
];
414 yyval
= semantic_stack_
[len_
- 1];
415 yyloc
= location_stack_
[len_
- 1];
419 yyval
= semantic_stack_
[0];
420 yyloc
= location_stack_
[0];
426 YYCDEBUG
<< "Reducing via rule " << n_
- 1
427 << " (line " << rline_
[n_
] << "), ";
428 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
430 YYCDEBUG
<< name_
[rhs_
[i
]] << ' ';
431 YYCDEBUG
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
437 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
438 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
446 ]/* Line __line__ of lalr1.cc. */
447 b4_syncline([@oline@
], [@ofile@
])[
449 state_stack_
.pop (len_
);
450 semantic_stack_
.pop (len_
);
451 location_stack_
.pop (len_
);
456 YYCDEBUG
<< "state stack now";
457 for (StateStack::ConstIterator i
= state_stack_
.begin ();
458 i
!= state_stack_
.end (); ++i
)
459 YYCDEBUG
<< ' ' << *i
;
460 YYCDEBUG
<< std::endl
;
464 semantic_stack_
.push (yyval
);
465 location_stack_
.push (yyloc
);
467 /* Shift the result of the reduction. */
469 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
470 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
471 state_
= table_
[state_
];
473 state_
= defgoto_
[n_
- ntokens_
];
476 /* Report and recover from errors. This is very incomplete. */
478 /* If not already recovering from an error, report this error. */
485 if (pact_ninf_
< n_
&& n_
< last_
)
487 message
= "syntax error, unexpected ";
488 message
+= name_
[ilooka_
];
491 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
492 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
497 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
498 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
500 message
+= (!count
++) ? ", expecting " : " or ";
508 message
= "syntax error";
513 /* Error raised explicitly by an action. */
517 /* If just tried and failed to reuse lookahead token after an
518 error, discard it. */
520 /* Return failure if at end of input. */
523 YYCDEBUG
<< "Discarding token " << looka_
524 << " (" << name_
[ilooka_
] << ")." << std::endl
;
528 /* Else will try to reuse lookahead token after shifting the error
536 if (n_
!= pact_ninf_
)
539 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
547 /* Pop the current state because it cannot handle the error token. */
548 if (!state_stack_
.height ())
554 if (stos_
[state_
] < ntokens_
)
556 YYCDEBUG
<< "Error: popping token "
557 << token_number_
[stos_
[state_
]]
558 << " (" << name_
[stos_
[state_
]];
560 YYPRINT (stderr
, token_number_
[stos_
[state_
]],
561 semantic_stack_
.top ());
563 YYCDEBUG
<< ')' << std::endl
;
567 YYCDEBUG
<< "Error: popping nonterminal ("
568 << name_
[stos_
[state_
]] << ')' << std::endl
;
573 state_
= (state_stack_
.pop (), state_stack_
[0]);
574 semantic_stack_
.pop ();
575 location_stack_
.pop ();;
580 YYCDEBUG
<< "Error: state stack now";
581 for (StateStack::ConstIterator i
= state_stack_
.begin ();
582 i
!= state_stack_
.end (); ++i
)
583 YYCDEBUG
<< ' ' << *i
;
584 YYCDEBUG
<< std::endl
;
592 YYCDEBUG
<< "Shifting error token, ";
594 semantic_stack_
.push (value
);
595 location_stack_
.push (location
);
610 yy::]b4_parser_class_name
[::lex_ ()
613 looka_
= yylex (&value
, &location
);
615 looka_
= yylex (&value
);
619 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
621 const ]b4_int_type_for([b4_pact
]) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
622 const ]b4_int_type_for([b4_pact
])[
623 yy::]b4_parser_class_name
[::pact_
[] =
628 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
629 doesn't specify something else to do. Zero means the default is an
631 const ]b4_int_type_for([b4_defact
])[
632 yy::]b4_parser_class_name
[::defact_
[] =
637 /* YYPGOTO[NTERM-NUM]. */
638 const ]b4_int_type_for([b4_pgoto
])[
639 yy::]b4_parser_class_name
[::pgoto_
[] =
644 /* YYDEFGOTO[NTERM-NUM]. */
645 const ]b4_int_type_for([b4_defgoto
])[
646 yy::]b4_parser_class_name
[::defgoto_
[] =
651 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
652 positive, shift that token. If negative, reduce the rule which
653 number is the opposite. If zero, do what YYDEFACT says. */
654 const ]b4_int_type_for([b4_table
]) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
655 const ]b4_int_type_for([b4_table
])[
656 yy::]b4_parser_class_name
[::table_
[] =
662 const ]b4_int_type_for([b4_check
])[
663 yy::]b4_parser_class_name
[::check_
[] =
669 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
670 symbol of state STATE-NUM. */
671 const ]b4_int_type_for([b4_stos
])[
672 yy::]b4_parser_class_name
[::stos_
[] =
677 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
679 const ]b4_int_type_for([b4_toknum
])[
680 yy::]b4_parser_class_name
[::token_number_
[] =
686 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
687 const ]b4_int_type_for([b4_r1
])[
688 yy::]b4_parser_class_name
[::r1_
[] =
693 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
694 const ]b4_int_type_for([b4_r2
])[
695 yy::]b4_parser_class_name
[::r2_
[] =
700 #if YYDEBUG || YYERROR_VERBOSE
701 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
702 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
704 const yy::]b4_parser_class_name
[::name_
[] =
711 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
712 const yy::]b4_parser_class_name
[::RhsNumberType
713 yy::]b4_parser_class_name
[::rhs_
[] =
718 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
720 const ]b4_int_type_for([b4_prhs
])[
721 yy::]b4_parser_class_name
[::prhs_
[] =
726 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
727 const ]b4_int_type_for([b4_rline
])[
728 yy::]b4_parser_class_name
[::rline_
[] =
734 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
735 yy::]b4_parser_class_name
[::TokenNumberType
736 yy::]b4_parser_class_name
[::translate_ (int token
)
739 const TokenNumberType
744 if ((unsigned) token
<= user_token_number_max_
)
745 return translate_
[token
];
750 const int yy::]b4_parser_class_name
[::eof_
= 0;
751 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
752 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
753 const int yy::]b4_parser_class_name
[::empty_
= -2;
754 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
755 const int yy::]b4_parser_class_name
[::terror_
= 1;
756 const int yy::]b4_parser_class_name
[::errcode_
= 256;
757 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
758 const int yy::]b4_parser_class_name
[::initdepth_
= ]b4_stack_depth_init
[;
760 const unsigned yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
761 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
766 b4_copyright([2002])[
768 #ifndef BISON_STACK_HH
769 # define BISON_STACK_HH
775 template < class T
, class S
= std::deque
< T
> >
780 typedef typename
S::iterator Iterator
;
781 typedef typename
S::const_iterator ConstIterator
;
787 Stack (unsigned n
) : seq_ (n
)
793 operator [] (unsigned index
)
800 operator [] (unsigned index
) const
827 inline ConstIterator
begin () const { return seq_
.begin (); }
828 inline ConstIterator
end () const { return seq_
.end (); }
835 template < class T
, class S
= Stack
< T
> >
840 Slice (const S
& stack
,
841 unsigned range
) : stack_ (stack
),
848 operator [] (unsigned index
) const
850 return stack_
[range_
- index
];
860 #endif // not BISON_STACK_HH]
863 b4_copyright([2002])[
865 #ifndef BISON_LOCATION_HH
866 # define BISON_LOCATION_HH
883 #endif // not BISON_LOCATION_HH]