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([b4_inherit
],
82 m4_define([b4_constructor
],
89 # We do want M4 expansion after # for CPP macros.
92 @output @output_header_name@
93 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
95 /* FIXME: This is wrong, we want computed header guards.
96 I don't know why the macros are missing now. :( */
97 #ifndef PARSER_HEADER_H
98 # define PARSER_HEADER_H
101 #include "location.hh"
106 /* Using locations. */
107 #define YYLSP_NEEDED ]b4_locations_flag[
109 ]b4_token_defines(b4_tokens
)[
111 /* Copy the first part of user declarations. */
114 ]/* Line __line__ of lalr1.cc. */
115 b4_syncline([@oline@
], [@ofile@
])[
117 /* Enabling traces. */
119 # define YYDEBUG ]b4_debug[
122 /* Enabling verbose error message. */
123 #ifndef YYERROR_VERBOSE
124 # define YYERROR_VERBOSE ]b4_error_verbose[
128 ]m4_ifdef([b4_stype
],
129 [b4_syncline([b4_stype_line
], [b4_filename
])
130 typedef union b4_stype yystype
;
131 /* Line __line__ of lalr1.cc. */
132 b4_syncline([@oline@
], [@ofile@
])],
133 [typedef int yystype
;])[
134 # define YYSTYPE yystype
137 /* Copy the second part of user declarations. */
140 ]/* Line __line__ of lalr1.cc. */
141 b4_syncline([@oline@
], [@ofile@
])[
142 #ifndef YYLLOC_DEFAULT
143 # define YYLLOC_DEFAULT(Current, Rhs, N) \
144 Current.last_line = Rhs[N].last_line; \
145 Current.last_column = Rhs[N].last_column;
150 class ]b4_parser_class_name
[;
152 template < typename P
>
158 struct Traits
< ]b4_parser_class_name
[ >
160 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
161 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
162 typedef int StateType
;
163 typedef yystype SemanticType
;
164 typedef ]b4_location_type
[ LocationType
;
170 class ]b4_parser_class_name b4_inherit
[
174 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
175 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
176 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
177 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
178 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
180 typedef Stack
< StateType
> StateStack
;
181 typedef Stack
< SemanticType
> SemanticStack
;
182 typedef Stack
< LocationType
> LocationStack
;
185 ]b4_parser_class_name
[ (bool debug
,
186 LocationType initlocation
][]b4_param
[) :
187 ]b4_constructor
[][debug_ (debug
),
189 initlocation_ (initlocation
)
191 ]b4_parser_class_name
[ (bool debug
][]b4_param
[) :
192 ]b4_constructor
[][debug_ (debug
),
198 virtual ~]b4_parser_class_name
[ ()
202 virtual int parse ();
206 virtual void lex_ ();
207 virtual void error_ ();
208 virtual void print_ ();
211 StateStack state_stack_
;
212 SemanticStack semantic_stack_
;
213 LocationStack location_stack_
;
216 static const ]b4_int_type_for([b4_pact
])[ pact_
[];
217 static const ]b4_int_type_for([b4_pact
])[ pact_ninf_
;
218 static const ]b4_int_type_for([b4_defact
])[ defact_
[];
219 static const ]b4_int_type_for([b4_pgoto
])[ pgoto_
[];
220 static const ]b4_int_type_for([b4_defgoto
])[ defgoto_
[];
221 static const ]b4_int_type_for([b4_table
])[ table_
[];
222 static const ]b4_int_type_for([b4_table
])[ table_ninf_
;
223 static const ]b4_int_type_for([b4_check
])[ check_
[];
224 static const ]b4_int_type_for([b4_r1
])[ r1_
[];
225 static const ]b4_int_type_for([b4_r2
])[ r2_
[];
227 #if YYDEBUG || YYERROR_VERBOSE
228 static const char* const name_
[];
231 /* More tables, for debugging. */
233 static const RhsNumberType rhs_
[];
234 static const ]b4_int_type_for([b4_prhs
])[ prhs_
[];
235 static const ]b4_int_type_for([b4_rline
])[ rline_
[];
236 static const ]b4_int_type_for([b4_stos
])[ stos_
[];
237 static const ]b4_int_type_for([b4_toknum
])[ token_number_
[];
240 /* Even more tables. */
241 static inline TokenNumberType
translate_ (int token
);
244 static const int eof_
;
245 /* LAST_ -- Last index in TABLE_. */
246 static const int last_
;
247 static const int nnts_
;
248 static const int empty_
;
249 static const int final_
;
250 static const int terror_
;
251 static const int errcode_
;
252 static const int ntokens_
;
253 static const int initdepth_
;
254 static const unsigned user_token_number_max_
;
255 static const TokenNumberType undef_token_
;
264 std::ostream
&cdebug_
;
266 /* Lookahead and lookahead in internal form. */
273 /* Semantic value and location of lookahead token. */
275 LocationType location
;
281 /* Initial location. */
282 LocationType initlocation_
;
286 #endif /* ! defined PARSER_HEADER_H */]
288 @output @output_parser_name@
289 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
292 #include @output_header_name@
294 /* Enable debugging if requested. */
296 # define YYCDEBUG if (debug_) cdebug_
298 # define YYCDEBUG if (0) cdebug_
299 #endif /* !YYDEBUG */
302 yy::]b4_parser_class_name
[::parse ()
307 /* Initialize stack. */
308 state_stack_
= StateStack (0);
309 semantic_stack_
= SemanticStack (1);
310 location_stack_
= LocationStack (1);
316 location
= initlocation_
;
318 YYCDEBUG
<< "Starting parse" << std::endl
;
322 state_stack_
.push (state_
);
323 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
329 /* Try to take a decision without lookahead. */
331 if (n_
== pact_ninf_
)
334 /* Read a lookahead token. */
335 if (looka_
== empty_
)
337 YYCDEBUG
<< "Reading a token: ";
341 /* Convert token to internal form. */
346 YYCDEBUG
<< "Now at end of input." << std::endl
;
350 ilooka_
= translate_ (looka_
);
354 YYCDEBUG
<< "Next token is " << looka_
355 << " (" << name_
[ilooka_
];
357 YYCDEBUG
<< ')' << std::endl
;
363 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
366 /* Reduce or error. */
370 if (n_
== table_ninf_
)
385 /* Shift the lookahead token. */
386 YYCDEBUG
<< "Shifting token " << looka_
387 << " (" << name_
[ilooka_
] << "), ";
389 /* Discard the token being shifted unless it is eof. */
393 semantic_stack_
.push (value
);
394 location_stack_
.push (location
);
396 /* Count tokens shifted since error; after three, turn off error
404 /* Default action. */
406 n_
= defact_
[state_
];
416 yyval
= semantic_stack_
[len_
- 1];
417 yyloc
= location_stack_
[len_
- 1];
421 yyval
= semantic_stack_
[0];
422 yyloc
= location_stack_
[0];
428 YYCDEBUG
<< "Reducing via rule " << n_
- 1
429 << " (line " << rline_
[n_
] << "), ";
430 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
432 YYCDEBUG
<< name_
[rhs_
[i
]] << ' ';
433 YYCDEBUG
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
439 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
440 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
448 ]/* Line __line__ of lalr1.cc. */
449 b4_syncline([@oline@
], [@ofile@
])[
451 state_stack_
.pop (len_
);
452 semantic_stack_
.pop (len_
);
453 location_stack_
.pop (len_
);
458 YYCDEBUG
<< "state stack now";
459 for (StateStack::ConstIterator i
= state_stack_
.begin ();
460 i
!= state_stack_
.end (); ++i
)
461 YYCDEBUG
<< ' ' << *i
;
462 YYCDEBUG
<< std::endl
;
466 semantic_stack_
.push (yyval
);
467 location_stack_
.push (yyloc
);
469 /* Shift the result of the reduction. */
471 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
472 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
473 state_
= table_
[state_
];
475 state_
= defgoto_
[n_
- ntokens_
];
478 /* Report and recover from errors. This is very incomplete. */
480 /* If not already recovering from an error, report this error. */
487 if (pact_ninf_
< n_
&& n_
< last_
)
489 message
= "syntax error, unexpected ";
490 message
+= name_
[ilooka_
];
493 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
494 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
499 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
500 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
502 message
+= (!count
++) ? ", expecting " : " or ";
510 message
= "syntax error";
515 /* Error raised explicitly by an action. */
519 /* If just tried and failed to reuse lookahead token after an
520 error, discard it. */
522 /* Return failure if at end of input. */
525 YYCDEBUG
<< "Discarding token " << looka_
526 << " (" << name_
[ilooka_
] << ")." << std::endl
;
530 /* Else will try to reuse lookahead token after shifting the error
538 if (n_
!= pact_ninf_
)
541 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
549 /* Pop the current state because it cannot handle the error token. */
550 if (!state_stack_
.height ())
556 if (stos_
[state_
] < ntokens_
)
558 YYCDEBUG
<< "Error: popping token "
559 << token_number_
[stos_
[state_
]]
560 << " (" << name_
[stos_
[state_
]];
562 YYPRINT (stderr
, token_number_
[stos_
[state_
]],
563 semantic_stack_
.top ());
565 YYCDEBUG
<< ')' << std::endl
;
569 YYCDEBUG
<< "Error: popping nonterminal ("
570 << name_
[stos_
[state_
]] << ')' << std::endl
;
575 state_
= (state_stack_
.pop (), state_stack_
[0]);
576 semantic_stack_
.pop ();
577 location_stack_
.pop ();;
582 YYCDEBUG
<< "Error: state stack now";
583 for (StateStack::ConstIterator i
= state_stack_
.begin ();
584 i
!= state_stack_
.end (); ++i
)
585 YYCDEBUG
<< ' ' << *i
;
586 YYCDEBUG
<< std::endl
;
594 YYCDEBUG
<< "Shifting error token, ";
596 semantic_stack_
.push (value
);
597 location_stack_
.push (location
);
612 yy::]b4_parser_class_name
[::lex_ ()
615 looka_
= yylex (&value
, &location
);
617 looka_
= yylex (&value
);
621 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
623 const ]b4_int_type_for([b4_pact
]) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
624 const ]b4_int_type_for([b4_pact
])[
625 yy::]b4_parser_class_name
[::pact_
[] =
630 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
631 doesn't specify something else to do. Zero means the default is an
633 const ]b4_int_type_for([b4_defact
])[
634 yy::]b4_parser_class_name
[::defact_
[] =
639 /* YYPGOTO[NTERM-NUM]. */
640 const ]b4_int_type_for([b4_pgoto
])[
641 yy::]b4_parser_class_name
[::pgoto_
[] =
646 /* YYDEFGOTO[NTERM-NUM]. */
647 const ]b4_int_type_for([b4_defgoto
])[
648 yy::]b4_parser_class_name
[::defgoto_
[] =
653 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
654 positive, shift that token. If negative, reduce the rule which
655 number is the opposite. If zero, do what YYDEFACT says. */
656 const ]b4_int_type_for([b4_table
]) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
657 const ]b4_int_type_for([b4_table
])[
658 yy::]b4_parser_class_name
[::table_
[] =
664 const ]b4_int_type_for([b4_check
])[
665 yy::]b4_parser_class_name
[::check_
[] =
671 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
672 symbol of state STATE-NUM. */
673 const ]b4_int_type_for([b4_stos
])[
674 yy::]b4_parser_class_name
[::stos_
[] =
679 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
681 const ]b4_int_type_for([b4_toknum
])[
682 yy::]b4_parser_class_name
[::token_number_
[] =
688 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
689 const ]b4_int_type_for([b4_r1
])[
690 yy::]b4_parser_class_name
[::r1_
[] =
695 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
696 const ]b4_int_type_for([b4_r2
])[
697 yy::]b4_parser_class_name
[::r2_
[] =
702 #if YYDEBUG || YYERROR_VERBOSE
703 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
704 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
706 const yy::]b4_parser_class_name
[::name_
[] =
713 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
714 const yy::]b4_parser_class_name
[::RhsNumberType
715 yy::]b4_parser_class_name
[::rhs_
[] =
720 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
722 const ]b4_int_type_for([b4_prhs
])[
723 yy::]b4_parser_class_name
[::prhs_
[] =
728 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
729 const ]b4_int_type_for([b4_rline
])[
730 yy::]b4_parser_class_name
[::rline_
[] =
736 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
737 yy::]b4_parser_class_name
[::TokenNumberType
738 yy::]b4_parser_class_name
[::translate_ (int token
)
741 const TokenNumberType
746 if ((unsigned) token
<= user_token_number_max_
)
747 return translate_
[token
];
752 const int yy::]b4_parser_class_name
[::eof_
= 0;
753 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
754 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
755 const int yy::]b4_parser_class_name
[::empty_
= -2;
756 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
757 const int yy::]b4_parser_class_name
[::terror_
= 1;
758 const int yy::]b4_parser_class_name
[::errcode_
= 256;
759 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
760 const int yy::]b4_parser_class_name
[::initdepth_
= ]b4_stack_depth_init
[;
762 const unsigned yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
763 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
768 b4_copyright([2002])[
770 #ifndef BISON_STACK_HH
771 # define BISON_STACK_HH
777 template < class T
, class S
= std::deque
< T
> >
782 typedef typename
S::iterator Iterator
;
783 typedef typename
S::const_iterator ConstIterator
;
789 Stack (unsigned n
) : seq_ (n
)
795 operator [] (unsigned index
)
802 operator [] (unsigned index
) const
829 inline ConstIterator
begin () const { return seq_
.begin (); }
830 inline ConstIterator
end () const { return seq_
.end (); }
837 template < class T
, class S
= Stack
< T
> >
842 Slice (const S
& stack
,
843 unsigned range
) : stack_ (stack
),
850 operator [] (unsigned index
) const
852 return stack_
[range_
- index
];
862 #endif // not BISON_STACK_HH]
865 b4_copyright([2002])[
867 #ifndef BISON_LOCATION_HH
868 # define BISON_LOCATION_HH
885 #endif // not BISON_LOCATION_HH]