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 # b4_lhs_value([TYPE])
23 # --------------------
24 # Expansion of $<TYPE>$.
25 m4_define([b4_lhs_value
],
26 [yyval
[]m4_ifval([$
1], [.$
1])])
29 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
30 # --------------------------------------
31 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
33 m4_define([b4_rhs_value
],
34 [semantic_stack_@
<:@
m4_eval([$
1 - $
2])@
:>@
m4_ifval([$
3], [.$
3])])
40 m4_define([b4_lhs_location
],
44 # b4_rhs_location(RULE-LENGTH, NUM)
45 # ---------------------------------
46 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
48 m4_define([b4_rhs_location
],
49 [location_stack_@
<:@
m4_eval([$
1 - $
2])@
:>@
])
52 m4_define_default([b4_input_suffix
], [.y
])
54 m4_define_default([b4_output_parser_suffix
],
55 [m4_translit(b4_input_suffix
, [yY
], [cC
])])
57 m4_define_default([b4_output_parser_name
],
58 [b4_output_prefix
[]b4_output_infix
[]b4_output_parser_suffix
[]])
61 m4_define_default([b4_output_header_suffix
],
62 [m4_translit(b4_input_suffix
, [yY
], [hH
])])
64 m4_define_default([b4_output_header_name
],
65 [b4_output_prefix
[]b4_output_infix
[]b4_output_header_suffix
[]])
67 m4_define_default([b4_header_guard
],
68 [m4_bpatsubst(m4_toupper([BISON_
]b4_output_header_name
),
69 [[^ABCDEFGHIJKLMNOPQRSTUVWXYZ
]], [_
])])
71 m4_define([b4_inherit
],
83 m4_define([b4_constructor
],
91 #output "b4_output_header_name"
92 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
94 #ifndef b4_header_guard
95 # define b4_header_guard
98 #include "location.hh"
103 /* Using locations. */
104 #define YYLSP_NEEDED b4_locations_flag
106 /* Copy the first part of user declarations. */
109 /* Line __line__ of __file__. */
110 #line __oline__ "__ofile__"
113 b4_token_defines(b4_tokens
)
115 /* Enabling traces. */
117 # define YYDEBUG b4_debug
120 /* Enabling verbose error message. */
121 #ifndef YYERROR_VERBOSE
122 # define YYERROR_VERBOSE b4_error_verbose
127 [#line b4_stype_line "b4_filename"
128 typedef union b4_stype yystype
;
129 /* Line __line__ of __file__. */
130 #line __oline__ "__ofile__"],
131 [typedef int yystype
;])
132 # define YYSTYPE yystype
135 /* Copy the second part of user declarations. */
138 /* Line __line__ of __file__. */
139 #line __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;
146 m4_if(b4_locations_flag
, [0], [],
148 typedef struct yyltype
155 # define YYLTYPE yyltype
162 template < typename P
>
168 struct Traits
< b4_name
>
170 typedef b4_int_type_for([b4_translate
]) TokenNumberType
;
171 typedef b4_int_type_for([b4_rhs
]) RhsNumberType
;
172 typedef int StateType
;
173 typedef yystype SemanticType
;
174 typedef b4_ltype LocationType
;
180 class b4_name b4_inherit
184 typedef Traits
< b4_name
>::TokenNumberType TokenNumberType
;
185 typedef Traits
< b4_name
>::RhsNumberType RhsNumberType
;
186 typedef Traits
< b4_name
>::StateType StateType
;
187 typedef Traits
< b4_name
>::SemanticType SemanticType
;
188 typedef Traits
< b4_name
>::LocationType LocationType
;
190 typedef Stack
< StateType
> StateStack
;
191 typedef Stack
< SemanticType
> SemanticStack
;
192 typedef Stack
< LocationType
> LocationStack
;
196 LocationType initlocation
[]b4_param
) :
197 b4_constructor
[]debug_ (debug
),
199 initlocation_ (initlocation
)
201 b4_name (bool debug
[]b4_param
) :
202 b4_constructor
[]debug_ (debug
),
212 virtual int parse ();
216 virtual void lex_ ();
217 virtual void error_ ();
218 virtual void print_ ();
221 StateStack state_stack_
;
222 SemanticStack semantic_stack_
;
223 LocationStack location_stack_
;
226 static const b4_int_type_for([b4_pact
]) pact_
[[]];
227 static const b4_int_type_for([b4_pact
]) pact_ninf_
;
228 static const b4_int_type_for([b4_defact
]) defact_
[[]];
229 static const b4_int_type_for([b4_pgoto
]) pgoto_
[[]];
230 static const b4_int_type_for([b4_defgoto
]) defgoto_
[[]];
231 static const b4_int_type_for([b4_table
]) table_
[[]];
232 static const b4_int_type_for([b4_table
]) table_ninf_
;
233 static const b4_int_type_for([b4_check
]) check_
[[]];
234 static const b4_int_type_for([b4_r1
]) r1_
[[]];
235 static const b4_int_type_for([b4_r2
]) r2_
[[]];
237 #if YYDEBUG || YYERROR_VERBOSE
238 static const char* const name_
[[]];
241 /* More tables, for debugging. */
243 static const RhsNumberType rhs_
[[]];
244 static const b4_int_type_for([b4_prhs
]) prhs_
[[]];
245 static const b4_int_type_for([b4_rline
]) rline_
[[]];
246 static const b4_int_type_for([b4_stos
]) stos_
[[]];
247 static const b4_int_type_for([b4_toknum
]) token_number_
[[]];
250 /* Even more tables. */
251 static inline TokenNumberType
translate_ (int token
);
254 static const int eof_
;
255 static const int last_
;
256 static const int nnts_
;
257 static const int empty_
;
258 static const int final_
;
259 static const int terror_
;
260 static const int errcode_
;
261 static const int ntokens_
;
262 static const int initdepth_
;
263 static const unsigned user_token_number_max_
;
264 static const TokenNumberType undef_token_
;
273 std::ostream
&cdebug_
;
275 /* Lookahead and lookahead in internal form. */
282 /* Semantic value and location of lookahead token. */
284 LocationType location
;
290 /* Initial location. */
291 LocationType initlocation_
;
295 #endif /* not b4_header_guard */
297 #output "b4_output_prefix[]b4_output_infix[].cc"
298 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
301 #include "b4_output_header_name"
303 /* Enable debugging if requested. */
305 # define YYCDEBUG if (debug_) cdebug_
307 # define YYCDEBUG if (0) cdebug_
308 #endif /* !YYDEBUG */
311 yy::b4_name::parse ()
316 /* Initialize stack. */
317 state_stack_
= StateStack (0);
318 semantic_stack_
= SemanticStack (1);
319 location_stack_
= LocationStack (1);
325 location
= initlocation_
;
327 YYCDEBUG
<< "Starting parse" << std::endl
;
331 state_stack_
.push (state_
);
332 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
338 /* Try to take a decision without lookahead. */
339 n_
= pact_
[[state_]];
340 if (n_
== pact_ninf_
)
343 /* Read a lookahead token. */
344 if (looka_
== empty_
)
346 YYCDEBUG
<< "Reading a token: ";
350 /* Convert token to internal form. */
355 YYCDEBUG
<< "Now at end of input." << std::endl
;
359 ilooka_
= translate_ (looka_
);
363 YYCDEBUG
<< "Next token is " << looka_
364 << " (" << name_
[[ilooka_]];
366 YYCDEBUG
<< ')' << std::endl
;
372 if (n_
< 0 || n_
> last_
|| check_
[[n_]] != ilooka_
)
375 /* Reduce or error. */
379 if (n_
== table_ninf_
)
394 /* Shift the lookahead token. */
395 YYCDEBUG
<< "Shifting token " << looka_
396 << " (" << name_
[[ilooka_]] << "), ";
398 /* Discard the token being shifted unless it is eof. */
402 semantic_stack_
.push (value
);
403 location_stack_
.push (location
);
405 /* Count tokens shifted since error; after three, turn off error
413 /* Default action. */
415 n_
= defact_
[[state_]];
425 yyval
= semantic_stack_
[[len_
- 1]];
426 yyloc
= location_stack_
[[len_
- 1]];
430 yyval
= semantic_stack_
[[0]];
431 yyloc
= location_stack_
[[0]];
437 YYCDEBUG
<< "Reducing via rule " << n_
- 1
438 << " (line " << rline_
[[n_]] << "), ";
439 for (b4_int_type_for([b4_prhs
]) i
= prhs_
[[n_]];
441 YYCDEBUG
<< name_
[[rhs_
[i
]]] << ' ';
442 YYCDEBUG
<< "-> " << name_
[[r1_
[n_
]]] << std::endl
;
448 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
449 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
457 /* Line __line__ of __file__. */
458 #line __oline__ "__ofile__"
460 state_stack_
.pop (len_
);
461 semantic_stack_
.pop (len_
);
462 location_stack_
.pop (len_
);
467 YYCDEBUG
<< "state stack now";
468 for (StateStack::ConstIterator i
= state_stack_
.begin ();
469 i
!= state_stack_
.end (); ++i
)
470 YYCDEBUG
<< ' ' << *i
;
471 YYCDEBUG
<< std::endl
;
475 semantic_stack_
.push (yyval
);
476 location_stack_
.push (yyloc
);
478 /* Shift the result of the reduction. */
480 state_
= pgoto_
[[n_
- ntokens_
]] + state_stack_
[[0]];
481 if (state_
>= 0 && state_
<= last_
&& check_
[[state_]] == state_stack_
[[0]])
482 state_
= table_
[[state_]];
484 state_
= defgoto_
[[n_
- ntokens_
]];
487 /* Report and recover from errors. This is very incomplete. */
489 /* If not already recovering from an error, report this error. */
495 n_
= pact_
[[state_]];
496 if (pact_ninf_
< n_
&& n_
< last_
)
498 message
= "parse error, unexpected ";
499 message
+= name_
[[ilooka_]];
502 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
503 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
508 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
509 if (check_
[[x
+ n_
]] == x
&& x
!= terror_
)
511 message
+= (!count
++) ? ", expecting " : " or ";
512 message
+= name_
[[x]];
519 message
= "parse error";
524 /* Error raised explicitly by an action. */
528 /* If just tried and failed to reuse lookahead token after an
529 error, discard it. */
531 /* Return failure if at end of input. */
534 YYCDEBUG
<< "Discarding token " << looka_
535 << " (" << name_
[[ilooka_]] << ")." << std::endl
;
539 /* Else will try to reuse lookahead token after shifting the error
546 n_
= pact_
[[state_]];
547 if (n_
!= pact_ninf_
)
550 if (0 <= n_
&& n_
<= last_
&& check_
[[n_]] == terror_
)
558 /* Pop the current state because it cannot handle the error token. */
559 if (!state_stack_
.height ())
565 if (stos_
[[state_]] < ntokens_
)
567 YYCDEBUG
<< "Error: popping token "
568 << token_number_
[[stos_
[state_
]]]
569 << " (" << name_
[[stos_
[state_
]]];
571 YYPRINT (stderr
, token_number_
[[stos_
[state_
]]],
572 semantic_stack_
.top ());
574 YYCDEBUG
<< ')' << std::endl
;
578 YYCDEBUG
<< "Error: popping nonterminal ("
579 << name_
[[stos_
[state_
]]] << ')' << std::endl
;
584 state_
= (state_stack_
.pop (), state_stack_
[[0]]);
585 semantic_stack_
.pop ();
586 location_stack_
.pop ();;
591 YYCDEBUG
<< "Error: state stack now";
592 for (StateStack::ConstIterator i
= state_stack_
.begin ();
593 i
!= state_stack_
.end (); ++i
)
594 YYCDEBUG
<< ' ' << *i
;
595 YYCDEBUG
<< std::endl
;
603 YYCDEBUG
<< "Shifting error token, ";
605 semantic_stack_
.push (value
);
606 location_stack_
.push (location
);
624 looka_
= yylex (&value
, &location
);
626 looka_
= yylex (&value
);
630 /* YYPACT[[STATE-NUM]] -- Index in YYTABLE of the portion describing
632 const b4_int_type_for([b4_pact
]) yy::b4_name::pact_ninf_
= b4_pact_ninf
;
633 const b4_int_type_for([b4_pact
])
634 yy::b4_name::pact_
[[]] =
639 /* YYDEFACT[[S]] -- default rule to reduce with in state S when YYTABLE
640 doesn't specify something else to do. Zero means the default is an
642 const b4_int_type_for([b4_defact
])
643 yy::b4_name::defact_
[[]] =
648 /* YYPGOTO[[NTERM-NUM]]. */
649 const b4_int_type_for([b4_pgoto
])
650 yy::b4_name::pgoto_
[[]] =
655 /* YYDEFGOTO[[NTERM-NUM]]. */
656 const b4_int_type_for([b4_defgoto
])
657 yy::b4_name::defgoto_
[[]] =
662 /* YYTABLE[[YYPACT[STATE-NUM]]]. What to do in state STATE-NUM. If
663 positive, shift that token. If negative, reduce the rule which
664 number is the opposite. If zero, do what YYDEFACT says. */
665 const b4_int_type_for([b4_table
]) yy::b4_name::table_ninf_
= b4_table_ninf
;
666 const b4_int_type_for([b4_table
])
667 yy::b4_name::table_
[[]] =
673 const b4_int_type_for([b4_check
])
674 yy::b4_name::check_
[[]] =
680 /* STOS_[[STATE-NUM]] -- The (internal number of the) accessing
681 symbol of state STATE-NUM. */
682 const b4_int_type_for([b4_stos
])
683 yy::b4_name::stos_
[[]] =
688 /* TOKEN_NUMBER_[[YYLEX-NUM]] -- Internal token number corresponding
690 const b4_int_type_for([b4_toknum
])
691 yy::b4_name::token_number_
[[]] =
697 /* YYR1[[YYN]] -- Symbol number of symbol that rule YYN derives. */
698 const b4_int_type_for([b4_r1
])
699 yy::b4_name::r1_
[[]] =
704 /* YYR2[[YYN]] -- Number of symbols composing right hand side of rule YYN. */
705 const b4_int_type_for([b4_r2
])
706 yy::b4_name::r2_
[[]] =
711 #if YYDEBUG || YYERROR_VERBOSE
712 /* YYTNAME[[SYMBOL-NUM]] -- String name of the symbol SYMBOL-NUM.
713 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
715 const yy::b4_name::name_
[[]] =
722 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
723 const yy::b4_name::RhsNumberType
724 yy::b4_name::rhs_
[[]] =
729 /* YYPRHS[[YYN]] -- Index of the first RHS symbol of rule number YYN in
731 const b4_int_type_for([b4_prhs
])
732 yy::b4_name::prhs_
[[]] =
737 /* YYRLINE[[YYN]] -- source line where rule number YYN was defined. */
738 const b4_int_type_for([b4_rline
])
739 yy::b4_name::rline_
[[]] =
745 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
746 yy::b4_name::TokenNumberType
747 yy::b4_name::translate_ (int token
)
750 const TokenNumberType
755 if ((unsigned) token
<= user_token_number_max_
)
756 return translate_
[[token]];
761 const int yy::b4_name::eof_
= 0;
762 const int yy::b4_name::last_
= b4_last
;
763 const int yy::b4_name::nnts_
= b4_nterms_number
;
764 const int yy::b4_name::empty_
= -2;
765 const int yy::b4_name::final_
= b4_final_state_number
;
766 const int yy::b4_name::terror_
= 1;
767 const int yy::b4_name::errcode_
= 256;
768 const int yy::b4_name::ntokens_
= b4_tokens_number
;
769 const int yy::b4_name::initdepth_
= b4_initdepth
;
771 const unsigned yy::b4_name::user_token_number_max_
= b4_user_token_number_max
;
772 const yy::b4_name::TokenNumberType
yy::b4_name::undef_token_
= b4_undef_token_number
;
779 #ifndef BISON_STACK_HH
780 # define BISON_STACK_HH
786 template < class T
, class S
= std::deque
< T
> >
791 typedef typename
S::iterator Iterator
;
792 typedef typename
S::const_iterator ConstIterator
;
798 Stack (unsigned n
) : seq_ (n
)
804 operator [[]] (unsigned index
)
806 return seq_
[[index]];
811 operator [[]] (unsigned index
) const
813 return seq_
[[index]];
838 inline ConstIterator
begin () const { return seq_
.begin (); }
839 inline ConstIterator
end () const { return seq_
.end (); }
846 template < class T
, class S
= Stack
< T
> >
851 Slice (const S
& stack
,
852 unsigned range
) : stack_ (stack
),
859 operator [[]] (unsigned index
) const
861 return stack_
[[range_
- index
]];
871 #endif // not BISON_STACK_HH
873 #output "location.hh"
876 #ifndef BISON_LOCATION_HH
877 # define BISON_LOCATION_HH
894 #endif // not BISON_LOCATION_HH