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. */
389 YYCDEBUG
<< "Shifting token " << looka_
390 << " (" << name_
[ilooka_
] << "), ";
393 /* Discard the token being shifted unless it is eof. */
397 semantic_stack_
.push (value
);
398 location_stack_
.push (location
);
400 /* Count tokens shifted since error; after three, turn off error
408 /* Default action. */
410 n_
= defact_
[state_
];
420 yyval
= semantic_stack_
[len_
- 1];
421 yyloc
= location_stack_
[len_
- 1];
425 yyval
= semantic_stack_
[0];
426 yyloc
= location_stack_
[0];
432 YYCDEBUG
<< "Reducing via rule " << n_
- 1
433 << " (line " << rline_
[n_
] << "), ";
434 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
436 YYCDEBUG
<< name_
[rhs_
[i
]] << ' ';
437 YYCDEBUG
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
443 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
444 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
452 ]/* Line __line__ of lalr1.cc. */
453 b4_syncline([@oline@
], [@ofile@
])[
455 state_stack_
.pop (len_
);
456 semantic_stack_
.pop (len_
);
457 location_stack_
.pop (len_
);
462 YYCDEBUG
<< "state stack now";
463 for (StateStack::ConstIterator i
= state_stack_
.begin ();
464 i
!= state_stack_
.end (); ++i
)
465 YYCDEBUG
<< ' ' << *i
;
466 YYCDEBUG
<< std::endl
;
470 semantic_stack_
.push (yyval
);
471 location_stack_
.push (yyloc
);
473 /* Shift the result of the reduction. */
475 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
476 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
477 state_
= table_
[state_
];
479 state_
= defgoto_
[n_
- ntokens_
];
482 /* Report and recover from errors. This is very incomplete. */
484 /* If not already recovering from an error, report this error. */
491 if (pact_ninf_
< n_
&& n_
< last_
)
493 message
= "syntax error, unexpected ";
494 message
+= name_
[ilooka_
];
497 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
498 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
503 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
504 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
506 message
+= (!count
++) ? ", expecting " : " or ";
514 message
= "syntax error";
519 /* Error raised explicitly by an action. */
523 /* If just tried and failed to reuse lookahead token after an
524 error, discard it. */
526 /* Return failure if at end of input. */
529 YYCDEBUG
<< "Discarding token " << looka_
530 << " (" << name_
[ilooka_
] << ")." << std::endl
;
534 /* Else will try to reuse lookahead token after shifting the error
542 if (n_
!= pact_ninf_
)
545 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
553 /* Pop the current state because it cannot handle the error token. */
554 if (state_stack_
.height () == 1)
560 if (stos_
[state_
] < ntokens_
)
562 YYCDEBUG
<< "Error: popping token "
563 << token_number_
[stos_
[state_
]]
564 << " (" << name_
[stos_
[state_
]];
566 YYPRINT (stderr
, token_number_
[stos_
[state_
]],
567 semantic_stack_
.top ());
569 YYCDEBUG
<< ')' << std::endl
;
573 YYCDEBUG
<< "Error: popping nonterminal ("
574 << name_
[stos_
[state_
]] << ')' << std::endl
;
579 state_
= (state_stack_
.pop (), state_stack_
[0]);
580 semantic_stack_
.pop ();
581 location_stack_
.pop ();;
586 YYCDEBUG
<< "Error: state stack now";
587 for (StateStack::ConstIterator i
= state_stack_
.begin ();
588 i
!= state_stack_
.end (); ++i
)
589 YYCDEBUG
<< ' ' << *i
;
590 YYCDEBUG
<< std::endl
;
598 YYCDEBUG
<< "Shifting error token, ";
600 semantic_stack_
.push (value
);
601 location_stack_
.push (location
);
616 yy::]b4_parser_class_name
[::lex_ ()
619 looka_
= yylex (&value
, &location
);
621 looka_
= yylex (&value
);
625 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
627 const ]b4_int_type_for([b4_pact
]) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
628 const ]b4_int_type_for([b4_pact
])[
629 yy::]b4_parser_class_name
[::pact_
[] =
634 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
635 doesn't specify something else to do. Zero means the default is an
637 const ]b4_int_type_for([b4_defact
])[
638 yy::]b4_parser_class_name
[::defact_
[] =
643 /* YYPGOTO[NTERM-NUM]. */
644 const ]b4_int_type_for([b4_pgoto
])[
645 yy::]b4_parser_class_name
[::pgoto_
[] =
650 /* YYDEFGOTO[NTERM-NUM]. */
651 const ]b4_int_type_for([b4_defgoto
])[
652 yy::]b4_parser_class_name
[::defgoto_
[] =
657 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
658 positive, shift that token. If negative, reduce the rule which
659 number is the opposite. If zero, do what YYDEFACT says. */
660 const ]b4_int_type_for([b4_table
]) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
661 const ]b4_int_type_for([b4_table
])[
662 yy::]b4_parser_class_name
[::table_
[] =
668 const ]b4_int_type_for([b4_check
])[
669 yy::]b4_parser_class_name
[::check_
[] =
675 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
676 symbol of state STATE-NUM. */
677 const ]b4_int_type_for([b4_stos
])[
678 yy::]b4_parser_class_name
[::stos_
[] =
683 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
685 const ]b4_int_type_for([b4_toknum
])[
686 yy::]b4_parser_class_name
[::token_number_
[] =
692 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
693 const ]b4_int_type_for([b4_r1
])[
694 yy::]b4_parser_class_name
[::r1_
[] =
699 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
700 const ]b4_int_type_for([b4_r2
])[
701 yy::]b4_parser_class_name
[::r2_
[] =
706 #if YYDEBUG || YYERROR_VERBOSE
707 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
708 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
710 const yy::]b4_parser_class_name
[::name_
[] =
717 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
718 const yy::]b4_parser_class_name
[::RhsNumberType
719 yy::]b4_parser_class_name
[::rhs_
[] =
724 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
726 const ]b4_int_type_for([b4_prhs
])[
727 yy::]b4_parser_class_name
[::prhs_
[] =
732 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
733 const ]b4_int_type_for([b4_rline
])[
734 yy::]b4_parser_class_name
[::rline_
[] =
740 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
741 yy::]b4_parser_class_name
[::TokenNumberType
742 yy::]b4_parser_class_name
[::translate_ (int token
)
745 const TokenNumberType
750 if ((unsigned) token
<= user_token_number_max_
)
751 return translate_
[token
];
756 const int yy::]b4_parser_class_name
[::eof_
= 0;
757 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
758 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
759 const int yy::]b4_parser_class_name
[::empty_
= -2;
760 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
761 const int yy::]b4_parser_class_name
[::terror_
= 1;
762 const int yy::]b4_parser_class_name
[::errcode_
= 256;
763 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
764 const int yy::]b4_parser_class_name
[::initdepth_
= ]b4_stack_depth_init
[;
766 const unsigned yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
767 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
772 b4_copyright([2002, 2003])[
774 #ifndef BISON_STACK_HH
775 # define BISON_STACK_HH
781 template < class T
, class S
= std::deque
< T
> >
786 typedef typename
S::iterator Iterator
;
787 typedef typename
S::const_iterator ConstIterator
;
793 Stack (unsigned n
) : seq_ (n
)
799 operator [] (unsigned index
)
806 operator [] (unsigned index
) const
833 inline ConstIterator
begin () const { return seq_
.begin (); }
834 inline ConstIterator
end () const { return seq_
.end (); }
841 template < class T
, class S
= Stack
< T
> >
846 Slice (const S
& stack
,
847 unsigned range
) : stack_ (stack
),
854 operator [] (unsigned index
) const
856 return stack_
[range_
- index
];
866 #endif // not BISON_STACK_HH]
869 b4_copyright([2002, 2003])[
871 #ifndef BISON_LOCATION_HH
872 # define BISON_LOCATION_HH
889 #endif // not BISON_LOCATION_HH]