2 # C++ skeleton for Bison
3 # Copyright (C) 2002, 2003 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 m4_if(b4_defines_flag
, 0, [],
91 [@output @output_header_name@
92 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
94 /* FIXME: This is wrong, we want computed header guards.
95 I don't know why the macros are missing now. :( */
96 #ifndef PARSER_HEADER_H
97 # define PARSER_HEADER_H
100 #include "location.hh"
105 /* Using locations. */
106 #define YYLSP_NEEDED ]b4_locations_flag[
108 ]b4_token_defines(b4_tokens
)[
110 /* Copy the first part of user declarations. */
113 ]/* Line __line__ of lalr1.cc. */
114 b4_syncline([@oline@
], [@ofile@
])[
116 /* Enabling traces. */
118 # define YYDEBUG ]b4_debug[
121 /* Enabling verbose error message. */
122 #ifndef YYERROR_VERBOSE
123 # define YYERROR_VERBOSE ]b4_error_verbose[
127 ]m4_ifdef([b4_stype
],
128 [b4_syncline([b4_stype_line
], [b4_filename
])
129 typedef union b4_stype yystype
;
130 /* Line __line__ of lalr1.cc. */
131 b4_syncline([@oline@
], [@ofile@
])],
132 [typedef int yystype
;])[
133 # define YYSTYPE yystype
136 /* Copy the second part of user declarations. */
139 ]/* Line __line__ of lalr1.cc. */
140 b4_syncline([@oline@
], [@ofile@
])[
141 #ifndef YYLLOC_DEFAULT
142 # define YYLLOC_DEFAULT(Current, Rhs, N) \
143 Current.last_line = Rhs[N].last_line; \
144 Current.last_column = Rhs[N].last_column;
149 class ]b4_parser_class_name
[;
151 template < typename P
>
157 struct Traits
< ]b4_parser_class_name
[ >
159 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
160 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
161 typedef int StateType
;
162 typedef yystype SemanticType
;
163 typedef ]b4_location_type
[ LocationType
;
169 class ]b4_parser_class_name b4_inherit
[
173 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
174 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
175 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
176 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
177 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
179 typedef Stack
< StateType
> StateStack
;
180 typedef Stack
< SemanticType
> SemanticStack
;
181 typedef Stack
< LocationType
> LocationStack
;
184 ]b4_parser_class_name
[ (bool debug
,
185 LocationType initlocation
][]b4_param
[) :
186 ]b4_constructor
[][debug_ (debug
),
188 initlocation_ (initlocation
)
190 ]b4_parser_class_name
[ (bool debug
][]b4_param
[) :
191 ]b4_constructor
[][debug_ (debug
),
197 virtual ~]b4_parser_class_name
[ ()
201 virtual int parse ();
205 virtual void lex_ ();
206 virtual void error_ ();
207 virtual void print_ ();
210 StateStack state_stack_
;
211 SemanticStack semantic_stack_
;
212 LocationStack location_stack_
;
215 static const ]b4_int_type_for([b4_pact
])[ pact_
[];
216 static const ]b4_int_type_for([b4_pact
])[ pact_ninf_
;
217 static const ]b4_int_type_for([b4_defact
])[ defact_
[];
218 static const ]b4_int_type_for([b4_pgoto
])[ pgoto_
[];
219 static const ]b4_int_type_for([b4_defgoto
])[ defgoto_
[];
220 static const ]b4_int_type_for([b4_table
])[ table_
[];
221 static const ]b4_int_type_for([b4_table
])[ table_ninf_
;
222 static const ]b4_int_type_for([b4_check
])[ check_
[];
223 static const ]b4_int_type_for([b4_r1
])[ r1_
[];
224 static const ]b4_int_type_for([b4_r2
])[ r2_
[];
226 #if YYDEBUG || YYERROR_VERBOSE
227 static const char* const name_
[];
230 /* More tables, for debugging. */
232 static const RhsNumberType rhs_
[];
233 static const ]b4_int_type_for([b4_prhs
])[ prhs_
[];
234 static const ]b4_int_type_for([b4_rline
])[ rline_
[];
235 static const ]b4_int_type_for([b4_stos
])[ stos_
[];
236 static const ]b4_int_type_for([b4_toknum
])[ token_number_
[];
239 /* Even more tables. */
240 static inline TokenNumberType
translate_ (int token
);
243 static const int eof_
;
244 /* LAST_ -- Last index in TABLE_. */
245 static const int last_
;
246 static const int nnts_
;
247 static const int empty_
;
248 static const int final_
;
249 static const int terror_
;
250 static const int errcode_
;
251 static const int ntokens_
;
252 static const int initdepth_
;
253 static const unsigned user_token_number_max_
;
254 static const TokenNumberType undef_token_
;
263 std::ostream
&cdebug_
;
265 /* Lookahead and lookahead in internal form. */
272 /* Semantic value and location of lookahead token. */
274 LocationType location
;
280 /* Initial location. */
281 LocationType initlocation_
;
285 #endif /* ! defined PARSER_HEADER_H */]
287 @output @output_parser_name@
288 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
291 m4_if(b4_defines_flag
, 0, [], [#include @output_header_name@])[
293 /* Enable debugging if requested. */
295 # define YYCDEBUG if (debug_) cdebug_
297 # define YYCDEBUG if (0) cdebug_
298 #endif /* !YYDEBUG */
301 yy::]b4_parser_class_name
[::parse ()
306 /* Initialize the stacks. The initial state will be pushed in
307 yynewstate, since the latter expects the semantical and the
308 location values to have been already stored, initialize these
309 stacks with a primary value. */
310 state_stack_
= StateStack (0);
311 semantic_stack_
= SemanticStack (1);
312 location_stack_
= LocationStack (1);
318 location
= initlocation_
;
320 YYCDEBUG
<< "Starting parse" << std::endl
;
324 state_stack_
.push (state_
);
325 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
331 /* Try to take a decision without lookahead. */
333 if (n_
== pact_ninf_
)
336 /* Read a lookahead token. */
337 if (looka_
== empty_
)
339 YYCDEBUG
<< "Reading a token: ";
343 /* Convert token to internal form. */
348 YYCDEBUG
<< "Now at end of input." << std::endl
;
352 ilooka_
= translate_ (looka_
);
356 YYCDEBUG
<< "Next token is " << looka_
357 << " (" << name_
[ilooka_
];
359 YYCDEBUG
<< ')' << std::endl
;
365 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
368 /* Reduce or error. */
372 if (n_
== table_ninf_
)
387 /* Shift the lookahead token. */
388 YYCDEBUG
<< "Shifting token " << looka_
389 << " (" << name_
[ilooka_
] << "), ";
391 /* Discard the token being shifted unless it is eof. */
395 semantic_stack_
.push (value
);
396 location_stack_
.push (location
);
398 /* Count tokens shifted since error; after three, turn off error
406 /* Default action. */
408 n_
= defact_
[state_
];
418 yyval
= semantic_stack_
[len_
- 1];
419 yyloc
= location_stack_
[len_
- 1];
423 yyval
= semantic_stack_
[0];
424 yyloc
= location_stack_
[0];
430 YYCDEBUG
<< "Reducing via rule " << n_
- 1
431 << " (line " << rline_
[n_
] << "), ";
432 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
434 YYCDEBUG
<< name_
[rhs_
[i
]] << ' ';
435 YYCDEBUG
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
441 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
442 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
450 ]/* Line __line__ of lalr1.cc. */
451 b4_syncline([@oline@
], [@ofile@
])[
453 state_stack_
.pop (len_
);
454 semantic_stack_
.pop (len_
);
455 location_stack_
.pop (len_
);
460 YYCDEBUG
<< "state stack now";
461 for (StateStack::ConstIterator i
= state_stack_
.begin ();
462 i
!= state_stack_
.end (); ++i
)
463 YYCDEBUG
<< ' ' << *i
;
464 YYCDEBUG
<< std::endl
;
468 semantic_stack_
.push (yyval
);
469 location_stack_
.push (yyloc
);
471 /* Shift the result of the reduction. */
473 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
474 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
475 state_
= table_
[state_
];
477 state_
= defgoto_
[n_
- ntokens_
];
480 /* Report and recover from errors. This is very incomplete. */
482 /* If not already recovering from an error, report this error. */
489 if (pact_ninf_
< n_
&& n_
< last_
)
491 message
= "syntax error, unexpected ";
492 message
+= name_
[ilooka_
];
495 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
496 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
501 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
502 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
504 message
+= (!count
++) ? ", expecting " : " or ";
512 message
= "syntax error";
517 /* Error raised explicitly by an action. */
521 /* If just tried and failed to reuse lookahead token after an
522 error, discard it. */
524 /* Return failure if at end of input. */
527 YYCDEBUG
<< "Discarding token " << looka_
528 << " (" << name_
[ilooka_
] << ")." << std::endl
;
532 /* Else will try to reuse lookahead token after shifting the error
540 if (n_
!= pact_ninf_
)
543 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
551 /* Pop the current state because it cannot handle the error token. */
552 if (state_stack_
.height () == 1)
558 if (stos_
[state_
] < ntokens_
)
560 YYCDEBUG
<< "Error: popping token "
561 << token_number_
[stos_
[state_
]]
562 << " (" << name_
[stos_
[state_
]];
564 YYPRINT (stderr
, token_number_
[stos_
[state_
]],
565 semantic_stack_
.top ());
567 YYCDEBUG
<< ')' << std::endl
;
571 YYCDEBUG
<< "Error: popping nonterminal ("
572 << name_
[stos_
[state_
]] << ')' << std::endl
;
577 state_
= (state_stack_
.pop (), state_stack_
[0]);
578 semantic_stack_
.pop ();
579 location_stack_
.pop ();;
584 YYCDEBUG
<< "Error: state stack now";
585 for (StateStack::ConstIterator i
= state_stack_
.begin ();
586 i
!= state_stack_
.end (); ++i
)
587 YYCDEBUG
<< ' ' << *i
;
588 YYCDEBUG
<< std::endl
;
596 YYCDEBUG
<< "Shifting error token, ";
598 semantic_stack_
.push (value
);
599 location_stack_
.push (location
);
614 yy::]b4_parser_class_name
[::lex_ ()
617 looka_
= yylex (&value
, &location
);
619 looka_
= yylex (&value
);
623 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
625 const ]b4_int_type_for([b4_pact
]) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
626 const ]b4_int_type_for([b4_pact
])[
627 yy::]b4_parser_class_name
[::pact_
[] =
632 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
633 doesn't specify something else to do. Zero means the default is an
635 const ]b4_int_type_for([b4_defact
])[
636 yy::]b4_parser_class_name
[::defact_
[] =
641 /* YYPGOTO[NTERM-NUM]. */
642 const ]b4_int_type_for([b4_pgoto
])[
643 yy::]b4_parser_class_name
[::pgoto_
[] =
648 /* YYDEFGOTO[NTERM-NUM]. */
649 const ]b4_int_type_for([b4_defgoto
])[
650 yy::]b4_parser_class_name
[::defgoto_
[] =
655 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
656 positive, shift that token. If negative, reduce the rule which
657 number is the opposite. If zero, do what YYDEFACT says. */
658 const ]b4_int_type_for([b4_table
]) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
659 const ]b4_int_type_for([b4_table
])[
660 yy::]b4_parser_class_name
[::table_
[] =
666 const ]b4_int_type_for([b4_check
])[
667 yy::]b4_parser_class_name
[::check_
[] =
673 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
674 symbol of state STATE-NUM. */
675 const ]b4_int_type_for([b4_stos
])[
676 yy::]b4_parser_class_name
[::stos_
[] =
681 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
683 const ]b4_int_type_for([b4_toknum
])[
684 yy::]b4_parser_class_name
[::token_number_
[] =
690 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
691 const ]b4_int_type_for([b4_r1
])[
692 yy::]b4_parser_class_name
[::r1_
[] =
697 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
698 const ]b4_int_type_for([b4_r2
])[
699 yy::]b4_parser_class_name
[::r2_
[] =
704 #if YYDEBUG || YYERROR_VERBOSE
705 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
706 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
708 const yy::]b4_parser_class_name
[::name_
[] =
715 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
716 const yy::]b4_parser_class_name
[::RhsNumberType
717 yy::]b4_parser_class_name
[::rhs_
[] =
722 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
724 const ]b4_int_type_for([b4_prhs
])[
725 yy::]b4_parser_class_name
[::prhs_
[] =
730 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
731 const ]b4_int_type_for([b4_rline
])[
732 yy::]b4_parser_class_name
[::rline_
[] =
738 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
739 yy::]b4_parser_class_name
[::TokenNumberType
740 yy::]b4_parser_class_name
[::translate_ (int token
)
743 const TokenNumberType
748 if ((unsigned) token
<= user_token_number_max_
)
749 return translate_
[token
];
754 const int yy::]b4_parser_class_name
[::eof_
= 0;
755 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
756 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
757 const int yy::]b4_parser_class_name
[::empty_
= -2;
758 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
759 const int yy::]b4_parser_class_name
[::terror_
= 1;
760 const int yy::]b4_parser_class_name
[::errcode_
= 256;
761 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
762 const int yy::]b4_parser_class_name
[::initdepth_
= ]b4_stack_depth_init
[;
764 const unsigned yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
765 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
770 b4_copyright([2002, 2003])[
772 #ifndef BISON_STACK_HH
773 # define BISON_STACK_HH
779 template < class T
, class S
= std::deque
< T
> >
784 typedef typename
S::iterator Iterator
;
785 typedef typename
S::const_iterator ConstIterator
;
791 Stack (unsigned n
) : seq_ (n
)
797 operator [] (unsigned index
)
804 operator [] (unsigned index
) const
831 inline ConstIterator
begin () const { return seq_
.begin (); }
832 inline ConstIterator
end () const { return seq_
.end (); }
839 template < class T
, class S
= Stack
< T
> >
844 Slice (const S
& stack
,
845 unsigned range
) : stack_ (stack
),
852 operator [] (unsigned index
) const
854 return stack_
[range_
- index
];
864 #endif // not BISON_STACK_HH]
867 b4_copyright([2002, 2003])[
869 #ifndef BISON_LOCATION_HH
870 # define BISON_LOCATION_HH
887 #endif // not BISON_LOCATION_HH]