]>
Commit | Line | Data |
---|---|---|
a75c057f | 1 | m4_divert(-1) |
60491a94 | 2 | # C++ skeleton for Bison |
b61c6978 | 3 | # Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
60491a94 AD |
4 | |
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. | |
9 | ||
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. | |
14 | ||
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 | |
18 | # 02111-1307 USA | |
19 | ||
66d30cd4 AD |
20 | ## ---------------- ## |
21 | ## Default values. ## | |
22 | ## ---------------- ## | |
23 | ||
66d30cd4 AD |
24 | # Default Parser class name. |
25 | m4_define_default([b4_parser_class_name], [Parser]) | |
26 | ||
27 | ||
28 | ||
29 | ## ----------------- ## | |
30 | ## Semantic Values. ## | |
31 | ## ----------------- ## | |
32 | ||
33 | ||
82b6cb3f AD |
34 | # b4_lhs_value([TYPE]) |
35 | # -------------------- | |
36 | # Expansion of $<TYPE>$. | |
37 | m4_define([b4_lhs_value], | |
38 | [yyval[]m4_ifval([$1], [.$1])]) | |
39 | ||
40 | ||
41 | # b4_rhs_value(RULE-LENGTH, NUM, [TYPE]) | |
42 | # -------------------------------------- | |
43 | # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH | |
44 | # symbols on RHS. | |
45 | m4_define([b4_rhs_value], | |
6ad762a8 | 46 | [semantic_stack_@{m4_eval([$1 - $2])@}m4_ifval([$3], [.$3])]) |
82b6cb3f | 47 | |
66d30cd4 | 48 | m4_define_default([b4_location_type], [Location]) |
82b6cb3f AD |
49 | |
50 | # b4_lhs_location() | |
51 | # ----------------- | |
52 | # Expansion of @$. | |
53 | m4_define([b4_lhs_location], | |
54 | [yyloc]) | |
55 | ||
56 | ||
57 | # b4_rhs_location(RULE-LENGTH, NUM) | |
58 | # --------------------------------- | |
59 | # Expansion of @NUM, where the current rule has RULE-LENGTH symbols | |
60 | # on RHS. | |
61 | m4_define([b4_rhs_location], | |
6ad762a8 | 62 | [location_stack_@{m4_eval([$1 - $2])@}]) |
82b6cb3f AD |
63 | |
64 | ||
af27eacb | 65 | m4_define([b4_inherit], |
0d8bed56 | 66 | [m4_ifdef([b4_root], |
af27eacb RA |
67 | [: public b4_root |
68 | ], | |
69 | [])]) | |
70 | ||
71 | m4_define([b4_param], | |
72 | [m4_ifdef([b4_root], | |
73 | [, | |
74 | const Param& param], | |
75 | [])]) | |
76 | ||
77 | m4_define([b4_constructor], | |
78 | [m4_ifdef([b4_root], | |
79 | [b4_root (param), | |
80 | ], | |
81 | [])]) | |
82 | ||
98254360 | 83 | |
b526ee61 AD |
84 | # We do want M4 expansion after # for CPP macros. |
85 | m4_changecom() | |
98254360 | 86 | m4_divert(0)dnl |
b61c6978 PE |
87 | m4_if(b4_defines_flag, 0, [], |
88 | [@output @output_header_name@ | |
fb8135fa | 89 | b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison], |
b61c6978 | 90 | [2002, 2003])[ |
c5e3e510 AD |
91 | /* FIXME: This is wrong, we want computed header guards. |
92 | I don't know why the macros are missing now. :( */ | |
93 | #ifndef PARSER_HEADER_H | |
94 | # define PARSER_HEADER_H | |
2b548aa6 RA |
95 | |
96 | #include "stack.hh" | |
2b548aa6 RA |
97 | #include "location.hh" |
98 | ||
8d69a1a3 | 99 | #include <string> |
717be197 | 100 | #include <iostream> |
50997c6e | 101 | |
4162fa07 | 102 | /* Using locations. */ |
c5e3e510 | 103 | #define YYLSP_NEEDED ]b4_locations_flag[ |
4162fa07 | 104 | |
c5e3e510 | 105 | ]b4_token_defines(b4_tokens)[ |
17acead5 | 106 | |
0dd1580a | 107 | /* Copy the first part of user declarations. */ |
c5e3e510 | 108 | ]b4_pre_prologue[ |
4162fa07 | 109 | |
996b1c7e | 110 | ]/* Line __line__ of lalr1.cc. */ |
c5e3e510 | 111 | b4_syncline([@oline@], [@ofile@])[ |
e96c9728 | 112 | |
69e2658b RA |
113 | /* Enabling traces. */ |
114 | #ifndef YYDEBUG | |
c5e3e510 | 115 | # define YYDEBUG ]b4_debug[ |
69e2658b RA |
116 | #endif |
117 | ||
118 | /* Enabling verbose error message. */ | |
119 | #ifndef YYERROR_VERBOSE | |
c5e3e510 | 120 | # define YYERROR_VERBOSE ]b4_error_verbose[ |
69e2658b RA |
121 | #endif |
122 | ||
98254360 | 123 | #ifndef YYSTYPE |
c5e3e510 | 124 | ]m4_ifdef([b4_stype], |
437c2d80 | 125 | [b4_syncline([b4_stype_line], [b4_filename]) |
e96c9728 | 126 | typedef union b4_stype yystype; |
996b1c7e | 127 | /* Line __line__ of lalr1.cc. */ |
6ad762a8 | 128 | b4_syncline([@oline@], [@ofile@])], |
c5e3e510 | 129 | [typedef int yystype;])[ |
98254360 RA |
130 | # define YYSTYPE yystype |
131 | #endif | |
132 | ||
0dd1580a | 133 | /* Copy the second part of user declarations. */ |
c5e3e510 | 134 | ]b4_post_prologue[ |
0dd1580a | 135 | |
996b1c7e | 136 | ]/* Line __line__ of lalr1.cc. */ |
c5e3e510 | 137 | b4_syncline([@oline@], [@ofile@])[ |
374f5a14 RA |
138 | #ifndef YYLLOC_DEFAULT |
139 | # define YYLLOC_DEFAULT(Current, Rhs, N) \ | |
7548fed2 | 140 | Current.end = Rhs[N].end; |
374f5a14 RA |
141 | #endif |
142 | ||
2b548aa6 RA |
143 | namespace yy |
144 | { | |
c5e3e510 | 145 | class ]b4_parser_class_name[; |
3f3eed27 | 146 | |
2b548aa6 RA |
147 | template < typename P > |
148 | struct Traits | |
149 | { | |
150 | }; | |
151 | ||
152 | template < > | |
c5e3e510 | 153 | struct Traits< ]b4_parser_class_name[ > |
2b548aa6 | 154 | { |
c5e3e510 AD |
155 | typedef ]b4_int_type_for([b4_translate])[ TokenNumberType; |
156 | typedef ]b4_int_type_for([b4_rhs])[ RhsNumberType; | |
50997c6e RA |
157 | typedef int StateType; |
158 | typedef yystype SemanticType; | |
c5e3e510 | 159 | typedef ]b4_location_type[ LocationType; |
2b548aa6 RA |
160 | }; |
161 | } | |
162 | ||
2b548aa6 RA |
163 | namespace yy |
164 | { | |
c5e3e510 | 165 | class ]b4_parser_class_name b4_inherit[ |
2b548aa6 RA |
166 | { |
167 | public: | |
3f3eed27 | 168 | |
c5e3e510 AD |
169 | typedef Traits< ]b4_parser_class_name[ >::TokenNumberType TokenNumberType; |
170 | typedef Traits< ]b4_parser_class_name[ >::RhsNumberType RhsNumberType; | |
171 | typedef Traits< ]b4_parser_class_name[ >::StateType StateType; | |
172 | typedef Traits< ]b4_parser_class_name[ >::SemanticType SemanticType; | |
173 | typedef Traits< ]b4_parser_class_name[ >::LocationType LocationType; | |
2b548aa6 RA |
174 | |
175 | typedef Stack< StateType > StateStack; | |
176 | typedef Stack< SemanticType > SemanticStack; | |
177 | typedef Stack< LocationType > LocationStack; | |
178 | ||
8d69a1a3 | 179 | #if YYLSP_NEEDED |
c5e3e510 AD |
180 | ]b4_parser_class_name[ (bool debug, |
181 | LocationType initlocation][]b4_param[) : | |
182 | ]b4_constructor[][debug_ (debug), | |
717be197 | 183 | cdebug_ (std::cerr), |
af27eacb | 184 | initlocation_ (initlocation) |
8d69a1a3 | 185 | #else |
c5e3e510 AD |
186 | ]b4_parser_class_name[ (bool debug][]b4_param[) : |
187 | ]b4_constructor[][debug_ (debug), | |
717be197 | 188 | cdebug_ (std::cerr) |
8d69a1a3 | 189 | #endif |
50997c6e | 190 | { |
50997c6e RA |
191 | } |
192 | ||
c5e3e510 | 193 | virtual ~]b4_parser_class_name[ () |
2b548aa6 | 194 | { |
2b548aa6 RA |
195 | } |
196 | ||
197 | virtual int parse (); | |
198 | ||
199 | private: | |
3f3eed27 | 200 | |
69e2658b | 201 | virtual void lex_ (); |
4bb2bc3f | 202 | virtual void error_ (); |
4aacc3a7 | 203 | virtual void print_ (); |
4bb2bc3f | 204 | |
2b548aa6 | 205 | /* Stacks. */ |
69e2658b RA |
206 | StateStack state_stack_; |
207 | SemanticStack semantic_stack_; | |
208 | LocationStack location_stack_; | |
2b548aa6 RA |
209 | |
210 | /* Tables. */ | |
c5e3e510 AD |
211 | static const ]b4_int_type_for([b4_pact])[ pact_[]; |
212 | static const ]b4_int_type_for([b4_pact])[ pact_ninf_; | |
213 | static const ]b4_int_type_for([b4_defact])[ defact_[]; | |
214 | static const ]b4_int_type_for([b4_pgoto])[ pgoto_[]; | |
215 | static const ]b4_int_type_for([b4_defgoto])[ defgoto_[]; | |
216 | static const ]b4_int_type_for([b4_table])[ table_[]; | |
217 | static const ]b4_int_type_for([b4_table])[ table_ninf_; | |
218 | static const ]b4_int_type_for([b4_check])[ check_[]; | |
219 | static const ]b4_int_type_for([b4_r1])[ r1_[]; | |
220 | static const ]b4_int_type_for([b4_r2])[ r2_[]; | |
69e2658b RA |
221 | |
222 | #if YYDEBUG || YYERROR_VERBOSE | |
c5e3e510 | 223 | static const char* const name_[]; |
69e2658b | 224 | #endif |
2b548aa6 RA |
225 | |
226 | /* More tables, for debugging. */ | |
69e2658b | 227 | #if YYDEBUG |
c5e3e510 AD |
228 | static const RhsNumberType rhs_[]; |
229 | static const ]b4_int_type_for([b4_prhs])[ prhs_[]; | |
230 | static const ]b4_int_type_for([b4_rline])[ rline_[]; | |
231 | static const ]b4_int_type_for([b4_stos])[ stos_[]; | |
232 | static const ]b4_int_type_for([b4_toknum])[ token_number_[]; | |
69e2658b | 233 | #endif |
2b548aa6 RA |
234 | |
235 | /* Even more tables. */ | |
680e8701 | 236 | static inline TokenNumberType translate_ (int token); |
2b548aa6 | 237 | |
762a801e | 238 | /* Constants. */ |
50997c6e | 239 | static const int eof_; |
39912f52 | 240 | /* LAST_ -- Last index in TABLE_. */ |
69e2658b | 241 | static const int last_; |
69e2658b | 242 | static const int nnts_; |
50997c6e | 243 | static const int empty_; |
69e2658b | 244 | static const int final_; |
762a801e RA |
245 | static const int terror_; |
246 | static const int errcode_; | |
50997c6e | 247 | static const int ntokens_; |
007a50a4 AD |
248 | static const unsigned user_token_number_max_; |
249 | static const TokenNumberType undef_token_; | |
2b548aa6 RA |
250 | |
251 | /* State. */ | |
69e2658b RA |
252 | int n_; |
253 | int len_; | |
69e2658b | 254 | int state_; |
4bb2bc3f | 255 | |
717be197 AD |
256 | /* Debugging. */ |
257 | int debug_; | |
258 | std::ostream &cdebug_; | |
259 | ||
69e2658b RA |
260 | /* Lookahead and lookahead in internal form. */ |
261 | int looka_; | |
262 | int ilooka_; | |
4bb2bc3f RA |
263 | |
264 | /* Message. */ | |
265 | std::string message; | |
266 | ||
374f5a14 | 267 | /* Semantic value and location of lookahead token. */ |
2b548aa6 RA |
268 | SemanticType value; |
269 | LocationType location; | |
8d69a1a3 | 270 | |
6ad762a8 | 271 | /* @@$ and $$. */ |
374f5a14 RA |
272 | SemanticType yyval; |
273 | LocationType yyloc; | |
274 | ||
8d69a1a3 RA |
275 | /* Initial location. */ |
276 | LocationType initlocation_; | |
2b548aa6 RA |
277 | }; |
278 | } | |
279 | ||
c5e3e510 | 280 | #endif /* ! defined PARSER_HEADER_H */] |
b61c6978 | 281 | ])dnl |
6ad762a8 | 282 | @output @output_parser_name@ |
fb8135fa | 283 | b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison], |
b61c6978 | 284 | [2002, 2003]) |
98254360 | 285 | |
b61c6978 | 286 | m4_if(b4_defines_flag, 0, [], [#include @output_header_name@])[ |
50997c6e | 287 | |
2b548aa6 | 288 | /* Enable debugging if requested. */ |
50997c6e | 289 | #if YYDEBUG |
717be197 | 290 | # define YYCDEBUG if (debug_) cdebug_ |
2b548aa6 | 291 | #else /* !YYDEBUG */ |
717be197 | 292 | # define YYCDEBUG if (0) cdebug_ |
2b548aa6 RA |
293 | #endif /* !YYDEBUG */ |
294 | ||
60a777aa AD |
295 | #define YYACCEPT goto yyacceptlab |
296 | #define YYABORT goto yyabortlab | |
297 | #define YYERROR goto yyerrlab1 | |
298 | ||
299 | ||
2b548aa6 | 300 | int |
c5e3e510 | 301 | yy::]b4_parser_class_name[::parse () |
2b548aa6 | 302 | { |
4bb2bc3f RA |
303 | int nerrs = 0; |
304 | int errstatus = 0; | |
305 | ||
3ae831b4 AD |
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. */ | |
69e2658b RA |
310 | state_stack_ = StateStack (0); |
311 | semantic_stack_ = SemanticStack (1); | |
312 | location_stack_ = LocationStack (1); | |
2b548aa6 | 313 | |
2b548aa6 | 314 | /* Start. */ |
69e2658b RA |
315 | state_ = 0; |
316 | looka_ = empty_; | |
8d69a1a3 RA |
317 | #if YYLSP_NEEDED |
318 | location = initlocation_; | |
319 | #endif | |
717be197 | 320 | YYCDEBUG << "Starting parse" << std::endl; |
2b548aa6 RA |
321 | |
322 | /* New state. */ | |
323 | yynewstate: | |
69e2658b | 324 | state_stack_.push (state_); |
717be197 | 325 | YYCDEBUG << "Entering state " << state_ << std::endl; |
50997c6e | 326 | goto yybackup; |
2b548aa6 RA |
327 | |
328 | /* Backup. */ | |
329 | yybackup: | |
330 | ||
331 | /* Try to take a decision without lookahead. */ | |
c5e3e510 | 332 | n_ = pact_[state_]; |
12b0043a | 333 | if (n_ == pact_ninf_) |
2b548aa6 RA |
334 | goto yydefault; |
335 | ||
336 | /* Read a lookahead token. */ | |
69e2658b | 337 | if (looka_ == empty_) |
2b548aa6 | 338 | { |
717be197 | 339 | YYCDEBUG << "Reading a token: "; |
69e2658b | 340 | lex_ (); |
2b548aa6 RA |
341 | } |
342 | ||
343 | /* Convert token to internal form. */ | |
69e2658b | 344 | if (looka_ <= 0) |
2b548aa6 | 345 | { |
69e2658b RA |
346 | looka_ = eof_; |
347 | ilooka_ = 0; | |
717be197 | 348 | YYCDEBUG << "Now at end of input." << std::endl; |
2b548aa6 RA |
349 | } |
350 | else | |
351 | { | |
69e2658b | 352 | ilooka_ = translate_ (looka_); |
50997c6e RA |
353 | #if YYDEBUG |
354 | if (debug_) | |
355 | { | |
717be197 | 356 | YYCDEBUG << "Next token is " << looka_ |
c5e3e510 | 357 | << " (" << name_[ilooka_]; |
4aacc3a7 | 358 | print_ (); |
717be197 | 359 | YYCDEBUG << ')' << std::endl; |
50997c6e | 360 | } |
2b548aa6 RA |
361 | #endif |
362 | } | |
363 | ||
69e2658b | 364 | n_ += ilooka_; |
c5e3e510 | 365 | if (n_ < 0 || last_ < n_ || check_[n_] != ilooka_) |
2b548aa6 RA |
366 | goto yydefault; |
367 | ||
368 | /* Reduce or error. */ | |
c5e3e510 | 369 | n_ = table_[n_]; |
69e2658b | 370 | if (n_ < 0) |
2b548aa6 | 371 | { |
12b0043a | 372 | if (n_ == table_ninf_) |
2b548aa6 RA |
373 | goto yyerrlab; |
374 | else | |
375 | { | |
69e2658b | 376 | n_ = -n_; |
2b548aa6 RA |
377 | goto yyreduce; |
378 | } | |
379 | } | |
69e2658b | 380 | else if (n_ == 0) |
2b548aa6 | 381 | goto yyerrlab; |
a75c057f | 382 | |
2b548aa6 | 383 | /* Accept? */ |
69e2658b | 384 | if (n_ == final_) |
2b548aa6 RA |
385 | goto yyacceptlab; |
386 | ||
387 | /* Shift the lookahead token. */ | |
fc049e9c | 388 | #if YYDEBUG |
717be197 | 389 | YYCDEBUG << "Shifting token " << looka_ |
fc049e9c AD |
390 | << " (" << name_[ilooka_] << "), "; |
391 | #endif | |
2b548aa6 RA |
392 | |
393 | /* Discard the token being shifted unless it is eof. */ | |
69e2658b RA |
394 | if (looka_ != eof_) |
395 | looka_ = empty_; | |
2b548aa6 | 396 | |
69e2658b RA |
397 | semantic_stack_.push (value); |
398 | location_stack_.push (location); | |
8d69a1a3 RA |
399 | |
400 | /* Count tokens shifted since error; after three, turn off error | |
401 | status. */ | |
402 | if (errstatus) | |
403 | --errstatus; | |
404 | ||
69e2658b | 405 | state_ = n_; |
2b548aa6 RA |
406 | goto yynewstate; |
407 | ||
408 | /* Default action. */ | |
409 | yydefault: | |
c5e3e510 | 410 | n_ = defact_[state_]; |
69e2658b | 411 | if (n_ == 0) |
2b548aa6 | 412 | goto yyerrlab; |
50997c6e | 413 | goto yyreduce; |
2b548aa6 RA |
414 | |
415 | /* Reduce. */ | |
416 | yyreduce: | |
c5e3e510 | 417 | len_ = r2_[n_]; |
69e2658b | 418 | if (len_) |
50997c6e | 419 | { |
c5e3e510 AD |
420 | yyval = semantic_stack_[len_ - 1]; |
421 | yyloc = location_stack_[len_ - 1]; | |
50997c6e RA |
422 | } |
423 | else | |
69e2658b | 424 | { |
c5e3e510 AD |
425 | yyval = semantic_stack_[0]; |
426 | yyloc = location_stack_[0]; | |
50997c6e | 427 | } |
2b548aa6 | 428 | |
50997c6e RA |
429 | #if YYDEBUG |
430 | if (debug_) | |
2b548aa6 | 431 | { |
717be197 | 432 | YYCDEBUG << "Reducing via rule " << n_ - 1 |
c5e3e510 AD |
433 | << " (line " << rline_[n_] << "), "; |
434 | for (]b4_int_type_for([b4_prhs])[ i = prhs_[n_]; | |
435 | 0 <= rhs_[i]; ++i) | |
436 | YYCDEBUG << name_[rhs_[i]] << ' '; | |
437 | YYCDEBUG << "-> " << name_[r1_[n_]] << std::endl; | |
2b548aa6 RA |
438 | } |
439 | #endif | |
a75c057f | 440 | |
374f5a14 RA |
441 | if (len_) |
442 | { | |
443 | Slice< LocationType, LocationStack > slice (location_stack_, len_); | |
444 | YYLLOC_DEFAULT (yyloc, slice, len_); | |
445 | } | |
446 | ||
45119f04 RA |
447 | switch (n_) |
448 | { | |
c5e3e510 | 449 | ]b4_actions[ |
45119f04 | 450 | } |
a75c057f | 451 | |
996b1c7e | 452 | ]/* Line __line__ of lalr1.cc. */ |
c5e3e510 | 453 | b4_syncline([@oline@], [@ofile@])[ |
06446ccf | 454 | |
69e2658b RA |
455 | state_stack_.pop (len_); |
456 | semantic_stack_.pop (len_); | |
457 | location_stack_.pop (len_); | |
2b548aa6 | 458 | |
50997c6e RA |
459 | #if YYDEBUG |
460 | if (debug_) | |
2b548aa6 | 461 | { |
717be197 | 462 | YYCDEBUG << "state stack now"; |
a75c057f | 463 | for (StateStack::ConstIterator i = state_stack_.begin (); |
69e2658b | 464 | i != state_stack_.end (); ++i) |
717be197 AD |
465 | YYCDEBUG << ' ' << *i; |
466 | YYCDEBUG << std::endl; | |
2b548aa6 RA |
467 | } |
468 | #endif | |
a75c057f | 469 | |
374f5a14 RA |
470 | semantic_stack_.push (yyval); |
471 | location_stack_.push (yyloc); | |
2b548aa6 RA |
472 | |
473 | /* Shift the result of the reduction. */ | |
c5e3e510 AD |
474 | n_ = r1_[n_]; |
475 | state_ = pgoto_[n_ - ntokens_] + state_stack_[0]; | |
476 | if (0 <= state_ && state_ <= last_ && check_[state_] == state_stack_[0]) | |
477 | state_ = table_[state_]; | |
2b548aa6 | 478 | else |
c5e3e510 | 479 | state_ = defgoto_[n_ - ntokens_]; |
2b548aa6 RA |
480 | goto yynewstate; |
481 | ||
482 | /* Report and recover from errors. This is very incomplete. */ | |
483 | yyerrlab: | |
4bb2bc3f RA |
484 | /* If not already recovering from an error, report this error. */ |
485 | if (!errstatus) | |
486 | { | |
487 | ++nerrs; | |
a75c057f | 488 | |
69e2658b | 489 | #if YYERROR_VERBOSE |
c5e3e510 | 490 | n_ = pact_[state_]; |
12b0043a | 491 | if (pact_ninf_ < n_ && n_ < last_) |
4bb2bc3f | 492 | { |
6e649e65 | 493 | message = "syntax error, unexpected "; |
c5e3e510 | 494 | message += name_[ilooka_]; |
4bb2bc3f RA |
495 | { |
496 | int count = 0; | |
69e2658b | 497 | for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x) |
c5e3e510 | 498 | if (check_[x + n_] == x && x != terror_) |
4bb2bc3f RA |
499 | ++count; |
500 | if (count < 5) | |
501 | { | |
502 | count = 0; | |
69e2658b | 503 | for (int x = (n_ < 0 ? -n_ : 0); x < ntokens_ + nnts_; ++x) |
c5e3e510 | 504 | if (check_[x + n_] == x && x != terror_) |
4bb2bc3f | 505 | { |
a75c057f | 506 | message += (!count++) ? ", expecting " : " or "; |
c5e3e510 | 507 | message += name_[x]; |
4bb2bc3f RA |
508 | } |
509 | } | |
510 | } | |
511 | } | |
512 | else | |
69e2658b | 513 | #endif |
6e649e65 | 514 | message = "syntax error"; |
8d69a1a3 | 515 | error_ (); |
4bb2bc3f | 516 | } |
762a801e | 517 | goto yyerrlab1; |
a75c057f | 518 | |
60a777aa AD |
519 | |
520 | /*----------------------------------------------------. | |
521 | | yyerrlab1 -- error raised explicitly by an action. | | |
522 | `----------------------------------------------------*/ | |
762a801e RA |
523 | yyerrlab1: |
524 | if (errstatus == 3) | |
525 | { | |
526 | /* If just tried and failed to reuse lookahead token after an | |
527 | error, discard it. */ | |
528 | ||
529 | /* Return failure if at end of input. */ | |
69e2658b | 530 | if (looka_ == eof_) |
762a801e | 531 | goto yyabortlab; |
7548fed2 | 532 | #if YYDEBUG |
717be197 | 533 | YYCDEBUG << "Discarding token " << looka_ |
7548fed2 AD |
534 | << " (" << name_[ilooka_] << ")." << std::endl; |
535 | #endif | |
69e2658b | 536 | looka_ = empty_; |
762a801e RA |
537 | } |
538 | ||
539 | /* Else will try to reuse lookahead token after shifting the error | |
540 | token. */ | |
541 | ||
542 | errstatus = 3; | |
762a801e | 543 | |
769b430f AD |
544 | for (;;) |
545 | { | |
c5e3e510 | 546 | n_ = pact_[state_]; |
12b0043a | 547 | if (n_ != pact_ninf_) |
769b430f AD |
548 | { |
549 | n_ += terror_; | |
c5e3e510 | 550 | if (0 <= n_ && n_ <= last_ && check_[n_] == terror_) |
769b430f | 551 | { |
c5e3e510 | 552 | n_ = table_[n_]; |
769b430f AD |
553 | if (0 < n_) |
554 | break; | |
555 | } | |
556 | } | |
557 | ||
558 | /* Pop the current state because it cannot handle the error token. */ | |
3ae831b4 | 559 | if (state_stack_.height () == 1) |
769b430f | 560 | goto yyabortlab; |
762a801e RA |
561 | |
562 | #if YYDEBUG | |
769b430f AD |
563 | if (debug_) |
564 | { | |
c5e3e510 | 565 | if (stos_[state_] < ntokens_) |
769b430f | 566 | { |
717be197 | 567 | YYCDEBUG << "Error: popping token " |
c5e3e510 AD |
568 | << token_number_[stos_[state_]] |
569 | << " (" << name_[stos_[state_]]; | |
769b430f | 570 | # ifdef YYPRINT |
c5e3e510 | 571 | YYPRINT (stderr, token_number_[stos_[state_]], |
769b430f AD |
572 | semantic_stack_.top ()); |
573 | # endif | |
717be197 | 574 | YYCDEBUG << ')' << std::endl; |
769b430f AD |
575 | } |
576 | else | |
577 | { | |
717be197 | 578 | YYCDEBUG << "Error: popping nonterminal (" |
c5e3e510 | 579 | << name_[stos_[state_]] << ')' << std::endl; |
769b430f AD |
580 | } |
581 | } | |
762a801e RA |
582 | #endif |
583 | ||
c5e3e510 | 584 | state_ = (state_stack_.pop (), state_stack_[0]); |
769b430f AD |
585 | semantic_stack_.pop (); |
586 | location_stack_.pop ();; | |
762a801e | 587 | |
769b430f AD |
588 | #if YYDEBUG |
589 | if (debug_) | |
590 | { | |
717be197 | 591 | YYCDEBUG << "Error: state stack now"; |
769b430f AD |
592 | for (StateStack::ConstIterator i = state_stack_.begin (); |
593 | i != state_stack_.end (); ++i) | |
717be197 AD |
594 | YYCDEBUG << ' ' << *i; |
595 | YYCDEBUG << std::endl; | |
769b430f AD |
596 | } |
597 | #endif | |
762a801e | 598 | } |
762a801e | 599 | |
69e2658b | 600 | if (n_ == final_) |
762a801e RA |
601 | goto yyacceptlab; |
602 | ||
717be197 | 603 | YYCDEBUG << "Shifting error token, "; |
762a801e | 604 | |
69e2658b RA |
605 | semantic_stack_.push (value); |
606 | location_stack_.push (location); | |
762a801e | 607 | |
69e2658b | 608 | state_ = n_; |
762a801e RA |
609 | goto yynewstate; |
610 | ||
2b548aa6 RA |
611 | /* Accept. */ |
612 | yyacceptlab: | |
613 | return 0; | |
762a801e RA |
614 | |
615 | /* Abort. */ | |
616 | yyabortlab: | |
617 | return 1; | |
2b548aa6 RA |
618 | } |
619 | ||
69e2658b | 620 | void |
c5e3e510 | 621 | yy::]b4_parser_class_name[::lex_ () |
69e2658b RA |
622 | { |
623 | #if YYLSP_NEEDED | |
624 | looka_ = yylex (&value, &location); | |
625 | #else | |
626 | looka_ = yylex (&value); | |
627 | #endif | |
628 | } | |
629 | ||
c5e3e510 | 630 | /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing |
2b548aa6 | 631 | STATE-NUM. */ |
c5e3e510 AD |
632 | const ]b4_int_type_for([b4_pact]) yy::b4_parser_class_name::pact_ninf_ = b4_pact_ninf[; |
633 | const ]b4_int_type_for([b4_pact])[ | |
634 | yy::]b4_parser_class_name[::pact_[] = | |
2b548aa6 | 635 | { |
c5e3e510 | 636 | ]b4_pact[ |
2b548aa6 RA |
637 | }; |
638 | ||
c5e3e510 | 639 | /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE |
2b548aa6 RA |
640 | doesn't specify something else to do. Zero means the default is an |
641 | error. */ | |
c5e3e510 AD |
642 | const ]b4_int_type_for([b4_defact])[ |
643 | yy::]b4_parser_class_name[::defact_[] = | |
2b548aa6 | 644 | { |
c5e3e510 | 645 | ]b4_defact[ |
2b548aa6 RA |
646 | }; |
647 | ||
c5e3e510 AD |
648 | /* YYPGOTO[NTERM-NUM]. */ |
649 | const ]b4_int_type_for([b4_pgoto])[ | |
650 | yy::]b4_parser_class_name[::pgoto_[] = | |
2b548aa6 | 651 | { |
c5e3e510 | 652 | ]b4_pgoto[ |
2b548aa6 RA |
653 | }; |
654 | ||
c5e3e510 AD |
655 | /* YYDEFGOTO[NTERM-NUM]. */ |
656 | const ]b4_int_type_for([b4_defgoto])[ | |
657 | yy::]b4_parser_class_name[::defgoto_[] = | |
2b548aa6 | 658 | { |
c5e3e510 | 659 | ]b4_defgoto[ |
2b548aa6 RA |
660 | }; |
661 | ||
c5e3e510 | 662 | /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If |
2b548aa6 RA |
663 | positive, shift that token. If negative, reduce the rule which |
664 | number is the opposite. If zero, do what YYDEFACT says. */ | |
c5e3e510 AD |
665 | const ]b4_int_type_for([b4_table]) yy::b4_parser_class_name::table_ninf_ = b4_table_ninf[; |
666 | const ]b4_int_type_for([b4_table])[ | |
667 | yy::]b4_parser_class_name[::table_[] = | |
2b548aa6 | 668 | { |
c5e3e510 | 669 | ]b4_table[ |
2b548aa6 RA |
670 | }; |
671 | ||
672 | /* YYCHECK. */ | |
c5e3e510 AD |
673 | const ]b4_int_type_for([b4_check])[ |
674 | yy::]b4_parser_class_name[::check_[] = | |
2b548aa6 | 675 | { |
c5e3e510 | 676 | ]b4_check[ |
2b548aa6 RA |
677 | }; |
678 | ||
769b430f | 679 | #if YYDEBUG |
c5e3e510 | 680 | /* STOS_[STATE-NUM] -- The (internal number of the) accessing |
769b430f | 681 | symbol of state STATE-NUM. */ |
c5e3e510 AD |
682 | const ]b4_int_type_for([b4_stos])[ |
683 | yy::]b4_parser_class_name[::stos_[] = | |
769b430f | 684 | { |
c5e3e510 | 685 | ]b4_stos[ |
769b430f AD |
686 | }; |
687 | ||
c5e3e510 | 688 | /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding |
769b430f | 689 | to YYLEX-NUM. */ |
c5e3e510 AD |
690 | const ]b4_int_type_for([b4_toknum])[ |
691 | yy::]b4_parser_class_name[::token_number_[] = | |
769b430f | 692 | { |
c5e3e510 | 693 | ]b4_toknum[ |
769b430f AD |
694 | }; |
695 | #endif | |
696 | ||
c5e3e510 AD |
697 | /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ |
698 | const ]b4_int_type_for([b4_r1])[ | |
699 | yy::]b4_parser_class_name[::r1_[] = | |
2b548aa6 | 700 | { |
c5e3e510 | 701 | ]b4_r1[ |
2b548aa6 RA |
702 | }; |
703 | ||
c5e3e510 AD |
704 | /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */ |
705 | const ]b4_int_type_for([b4_r2])[ | |
706 | yy::]b4_parser_class_name[::r2_[] = | |
2b548aa6 | 707 | { |
c5e3e510 | 708 | ]b4_r2[ |
2b548aa6 RA |
709 | }; |
710 | ||
69e2658b | 711 | #if YYDEBUG || YYERROR_VERBOSE |
c5e3e510 | 712 | /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. |
2b548aa6 RA |
713 | First, the terminals, then, starting at YYNTOKENS, nonterminals. */ |
714 | const char* | |
c5e3e510 | 715 | const yy::]b4_parser_class_name[::name_[] = |
2b548aa6 | 716 | { |
c5e3e510 | 717 | ]b4_tname[ |
2b548aa6 | 718 | }; |
69e2658b | 719 | #endif |
2b548aa6 | 720 | |
69e2658b | 721 | #if YYDEBUG |
2b548aa6 | 722 | /* YYRHS -- A `-1'-separated list of the rules' RHS. */ |
c5e3e510 AD |
723 | const yy::]b4_parser_class_name[::RhsNumberType |
724 | yy::]b4_parser_class_name[::rhs_[] = | |
2b548aa6 | 725 | { |
c5e3e510 | 726 | ]b4_rhs[ |
2b548aa6 RA |
727 | }; |
728 | ||
c5e3e510 | 729 | /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in |
2b548aa6 | 730 | YYRHS. */ |
c5e3e510 AD |
731 | const ]b4_int_type_for([b4_prhs])[ |
732 | yy::]b4_parser_class_name[::prhs_[] = | |
2b548aa6 | 733 | { |
c5e3e510 | 734 | ]b4_prhs[ |
2b548aa6 RA |
735 | }; |
736 | ||
c5e3e510 AD |
737 | /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ |
738 | const ]b4_int_type_for([b4_rline])[ | |
739 | yy::]b4_parser_class_name[::rline_[] = | |
69e2658b | 740 | { |
c5e3e510 | 741 | ]b4_rline[ |
69e2658b RA |
742 | }; |
743 | #endif | |
744 | ||
2b548aa6 | 745 | /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ |
c5e3e510 AD |
746 | yy::]b4_parser_class_name[::TokenNumberType |
747 | yy::]b4_parser_class_name[::translate_ (int token) | |
2b548aa6 | 748 | { |
a75c057f | 749 | static |
680e8701 | 750 | const TokenNumberType |
c5e3e510 | 751 | translate_[] = |
2b548aa6 | 752 | { |
c5e3e510 | 753 | ]b4_translate[ |
2b548aa6 | 754 | }; |
007a50a4 | 755 | if ((unsigned) token <= user_token_number_max_) |
c5e3e510 | 756 | return translate_[token]; |
007a50a4 AD |
757 | else |
758 | return undef_token_; | |
2b548aa6 RA |
759 | } |
760 | ||
c5e3e510 AD |
761 | const int yy::]b4_parser_class_name[::eof_ = 0; |
762 | const int yy::]b4_parser_class_name[::last_ = ]b4_last[; | |
763 | const int yy::]b4_parser_class_name[::nnts_ = ]b4_nterms_number[; | |
764 | const int yy::]b4_parser_class_name[::empty_ = -2; | |
765 | const int yy::]b4_parser_class_name[::final_ = ]b4_final_state_number[; | |
766 | const int yy::]b4_parser_class_name[::terror_ = 1; | |
767 | const int yy::]b4_parser_class_name[::errcode_ = 256; | |
768 | const int yy::]b4_parser_class_name[::ntokens_ = ]b4_tokens_number[; | |
66d30cd4 | 769 | |
c5e3e510 AD |
770 | const unsigned yy::]b4_parser_class_name[::user_token_number_max_ = ]b4_user_token_number_max[; |
771 | const yy::]b4_parser_class_name[::TokenNumberType yy::]b4_parser_class_name[::undef_token_ = ]b4_undef_token_number[; | |
2b548aa6 | 772 | |
c5e3e510 | 773 | ]b4_epilogue |
21846f69 | 774 | dnl |
6ad762a8 | 775 | @output stack.hh |
7548fed2 | 776 | b4_copyright([Stack handling for Bison C++ parsers], [2002, 2003])[ |
98254360 | 777 | |
2b548aa6 RA |
778 | #ifndef BISON_STACK_HH |
779 | # define BISON_STACK_HH | |
780 | ||
45119f04 | 781 | #include <deque> |
2b548aa6 RA |
782 | |
783 | namespace yy | |
784 | { | |
45119f04 | 785 | template < class T, class S = std::deque< T > > |
2b548aa6 RA |
786 | class Stack |
787 | { | |
788 | public: | |
789 | ||
790 | typedef typename S::iterator Iterator; | |
791 | typedef typename S::const_iterator ConstIterator; | |
792 | ||
793 | Stack () : seq_ () | |
794 | { | |
795 | } | |
796 | ||
797 | Stack (unsigned n) : seq_ (n) | |
798 | { | |
799 | } | |
800 | ||
801 | inline | |
802 | T& | |
c5e3e510 | 803 | operator [] (unsigned index) |
2b548aa6 | 804 | { |
c5e3e510 | 805 | return seq_[index]; |
2b548aa6 RA |
806 | } |
807 | ||
808 | inline | |
809 | const T& | |
c5e3e510 | 810 | operator [] (unsigned index) const |
2b548aa6 | 811 | { |
c5e3e510 | 812 | return seq_[index]; |
2b548aa6 RA |
813 | } |
814 | ||
815 | inline | |
816 | void | |
817 | push (const T& t) | |
818 | { | |
45119f04 | 819 | seq_.push_front (t); |
2b548aa6 RA |
820 | } |
821 | ||
822 | inline | |
823 | void | |
824 | pop (unsigned n = 1) | |
825 | { | |
826 | for (; n; --n) | |
45119f04 | 827 | seq_.pop_front (); |
2b548aa6 RA |
828 | } |
829 | ||
762a801e RA |
830 | inline |
831 | unsigned | |
832 | height () const | |
833 | { | |
834 | return seq_.size (); | |
835 | } | |
836 | ||
2b548aa6 RA |
837 | inline ConstIterator begin () const { return seq_.begin (); } |
838 | inline ConstIterator end () const { return seq_.end (); } | |
839 | ||
840 | private: | |
841 | ||
842 | S seq_; | |
843 | }; | |
374f5a14 RA |
844 | |
845 | template < class T, class S = Stack< T > > | |
846 | class Slice | |
847 | { | |
848 | public: | |
849 | ||
850 | Slice (const S& stack, | |
851 | unsigned range) : stack_ (stack), | |
852 | range_ (range) | |
853 | { | |
854 | } | |
855 | ||
856 | inline | |
857 | const T& | |
c5e3e510 | 858 | operator [] (unsigned index) const |
374f5a14 | 859 | { |
c5e3e510 | 860 | return stack_[range_ - index]; |
374f5a14 RA |
861 | } |
862 | ||
863 | private: | |
864 | ||
865 | const S& stack_; | |
866 | unsigned range_; | |
867 | }; | |
2b548aa6 RA |
868 | } |
869 | ||
c5e3e510 | 870 | #endif // not BISON_STACK_HH] |
21846f69 | 871 | dnl |
6ad762a8 | 872 | @output location.hh |
7548fed2 | 873 | b4_copyright([Location class for Bison C++ parsers], [2002, 2003])[ |
98254360 | 874 | |
2b548aa6 RA |
875 | #ifndef BISON_LOCATION_HH |
876 | # define BISON_LOCATION_HH | |
877 | ||
7548fed2 AD |
878 | # include <iostream> |
879 | # include <string> | |
880 | ||
2b548aa6 RA |
881 | namespace yy |
882 | { | |
7548fed2 | 883 | class Position |
2b548aa6 | 884 | { |
7548fed2 AD |
885 | public: |
886 | Position () | |
887 | : filename (), line (1), column (0) | |
888 | {} | |
889 | ||
890 | std::string filename; | |
2b548aa6 RA |
891 | int line; |
892 | int column; | |
893 | }; | |
894 | ||
7548fed2 AD |
895 | inline std::ostream& |
896 | operator<< (std::ostream& ostr, const Position& pos) | |
897 | { | |
898 | if (pos.filename != "") | |
899 | ostr << pos.filename << ':'; | |
900 | ostr << pos.line << '.' << pos.column; | |
901 | return ostr; | |
902 | } | |
903 | ||
904 | inline Position | |
905 | operator- (const Position& pos, int col) | |
906 | { | |
907 | Position res (pos); | |
908 | res.column -= col; | |
909 | return res; | |
910 | } | |
911 | ||
912 | ||
913 | class Location | |
2b548aa6 | 914 | { |
7548fed2 AD |
915 | public: |
916 | Position begin; | |
917 | Position end; | |
2b548aa6 | 918 | }; |
7548fed2 AD |
919 | |
920 | /* Don't issue twice the line number when the location is on a single | |
921 | line. */ | |
922 | ||
923 | inline std::ostream& | |
924 | operator<< (std::ostream& ostr, const Location& pos) | |
925 | { | |
926 | ostr << pos.begin; | |
927 | if (pos.begin.filename != pos.end.filename) | |
928 | ostr << '-' << pos.end - 1; | |
929 | else if (pos.begin.line != pos.end.line) | |
930 | ostr << '-' << pos.end.line << '.' << pos.end.column - 1; | |
931 | else if (pos.begin.column != pos.end.column - 1) | |
932 | ostr << '-' << pos.end.column - 1; | |
933 | return ostr; | |
934 | } | |
935 | ||
2b548aa6 RA |
936 | } |
937 | ||
c5e3e510 | 938 | #endif // not BISON_LOCATION_HH] |