]> git.saurik.com Git - bison.git/blob - data/glr.c
2d30eb9e1ee361dba277296620420a84c1ca634a
[bison.git] / data / glr.c
1 m4_divert(-1) -*- C -*-
2 m4_include([c.m4])
3
4 # GLR skeleton for Bison
5 # Copyright (C) 2002 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # 02111-1307 USA
21
22 # b4_lhs_value([TYPE])
23 # --------------------
24 # Expansion of $<TYPE>$.
25 m4_define([b4_lhs_value],
26 [(*yyvalp)[]m4_ifval([$1], [.$1])])
27
28
29 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
30 # --------------------------------------
31 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
32 # symbols on RHS.
33 m4_define([b4_rhs_value],
34 [yyvsp@<:@m4_eval([$2 - $1])@:>@.yystate.yysemantics.yysval[]m4_ifval([$3], [.$3])])
35
36
37
38 ## ----------- ##
39 ## Locations. ##
40 ## ----------- ##
41
42 # b4_location_if(IF-TRUE, IF-FALSE)
43 # ---------------------------------
44 # Expand IF-TRUE, if locations are used, IF-FALSE otherwise.
45 m4_define([b4_location_if],
46 [m4_if(b4_locations_flag, [1],
47 [$1],
48 [$2])])
49
50
51 # b4_lhs_location()
52 # -----------------
53 # Expansion of @$.
54 m4_define([b4_lhs_location],
55 [(*yylocp)])
56
57
58 # b4_rhs_location(RULE-LENGTH, NUM)
59 # ---------------------------------
60 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
61 # on RHS.
62 m4_define([b4_rhs_location],
63 [yyvsp@<:@m4_eval([$2 - $1])@:>@.yystate.yyloc])
64
65
66
67 ## -------------- ##
68 ## %pure-parser. ##
69 ## -------------- ##
70
71 # b4_pure_if(IF-TRUE, IF-FALSE)
72 # -----------------------------
73 # Expand IF-TRUE, if %pure-parser, IF-FALSE otherwise.
74 m4_define([b4_pure_if],
75 [m4_if(b4_pure, [1],
76 [$1],
77 [$2])])
78
79
80 ## ------------------- ##
81 ## Output file names. ##
82 ## ------------------- ##
83
84 m4_define_default([b4_input_suffix], [.y])
85
86 m4_define_default([b4_output_parser_suffix],
87 [m4_translit(b4_input_suffix, [yY], [cC])])
88
89 m4_define_default([b4_output_parser_name],
90 [b4_output_prefix[]b4_output_infix[]b4_output_parser_suffix[]])
91
92
93 m4_define_default([b4_output_header_suffix],
94 [m4_translit(b4_input_suffix, [yY], [hH])])
95
96 m4_define_default([b4_output_header_name],
97 [b4_output_prefix[]b4_output_infix[]b4_output_header_suffix[]])
98
99 m4_define_default([b4_header_guard],
100 [m4_bpatsubst(m4_toupper([BISON_]b4_output_header_name),
101 [[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]], [_])])
102
103
104 m4_divert(0)dnl
105 #output "b4_output_parser_name"
106 b4_copyright([Skeleton parser for GLR parsing with Bison], [2002])
107 [
108 /* This is the parser code for GLR (Generalized LR) parser. */
109
110 /* FIXME: minimize these */
111 #include <stdlib.h>
112 #include <setjmp.h>
113 #include <stdio.h>
114 #include <stdarg.h>
115 #include <assert.h>
116
117 /* Identify Bison output. */
118 #define YYBISON 1
119
120 /* Pure parsers. */
121 #define YYPURE ]b4_pure[
122
123 /* Using locations. */
124 #define YYLSP_NEEDED ]b4_locations_flag[
125
126 ]m4_if(b4_prefix[], [yy], [],
127 [/* If NAME_PREFIX is specified substitute the variables and functions
128 names. */
129 #define yyparse b4_prefix[]parse
130 #define yylex b4_prefix[]lex
131 #define yyerror b4_prefix[]error
132 #define yylval b4_prefix[]lval
133 #define yychar b4_prefix[]char
134 #define yydebug b4_prefix[]debug
135 #define yynerrs b4_prefix[]nerrs
136 b4_location_if([#define yylloc b4_prefix[]lloc])])
137
138 /* Copy the first part of user declarations. */
139 b4_pre_prologue
140
141 b4_token_defines(b4_tokens)[
142
143 /* Enabling traces. */
144 #ifndef YYDEBUG
145 # define YYDEBUG ]b4_debug[
146 #endif
147
148 /* Enabling verbose error messages. */
149 #ifdef YYERROR_VERBOSE
150 # undef YYERROR_VERBOSE
151 # define YYERROR_VERBOSE 1
152 #else
153 # define YYERROR_VERBOSE ]b4_error_verbose[
154 #endif
155
156 #ifndef YYSTYPE
157 ]m4_ifdef([b4_stype],
158 [#line b4_stype_line "b4_filename"
159 typedef union b4_stype yystype;
160 /* Line __line__ of __file__. */
161 #line __oline__ "__ofile__"],
162 [typedef int yystype;])[
163 # define YYSTYPE yystype
164 # define YYSTYPE_IS_TRIVIAL 1
165 #endif
166
167 #ifndef YYLTYPE
168 typedef struct yyltype
169 {
170 ]b4_location_if([
171 int yyfirst_line;
172 int yyfirst_column;
173 int yylast_line;
174 int yylast_column;])[
175 } yyltype;
176 # define YYLTYPE ]b4_ltype[
177 # define YYLTYPE_IS_TRIVIAL 1
178 #endif
179
180 /* Default (constant) values used for initialization for null
181 right-hand sides. Unlike the standard bison.simple template,
182 here we set the default values of the $$ and $@ to zeroed-out
183 values. Since the default value of these quantities is undefined,
184 this behavior is technically correct. */
185 static YYSTYPE yyval_default;
186 static YYLTYPE yyloc_default;
187
188 /* Copy the second part of user declarations. */
189 ]b4_post_prologue[
190
191 ]/* Line __line__ of __file__. */
192 #line __oline__ "__ofile__"
193 [
194 #if ! defined (__cplusplus)
195 typedef char bool;
196 # define yytrue 1
197 # define yyfalse 0
198 #endif
199
200 /*-----------------.
201 | GCC extensions. |
202 `-----------------*/
203
204 #ifndef __attribute__
205 /* This feature is available in gcc versions 2.5 and later. */
206 # if !defined (__GNUC__) || __GNUC__ < 2 || \
207 (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
208 # define __attribute__(Spec) /* empty */
209 # endif
210 #endif
211
212 #ifndef ATTRIBUTE_UNUSED
213 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
214 #endif
215
216 #if ! defined (__GNUC__)
217 # define inline
218 #endif
219
220 /* YYFINAL -- State number of the termination state. */
221 #define YYFINAL ]b4_final_state_number[
222 #define YYFLAG ]b4_flag[
223 #define YYLAST ]b4_last[
224
225 /* YYNTOKENS -- Number of terminals. */
226 #define YYNTOKENS ]b4_tokens_number[
227 /* YYNNTS -- Number of nonterminals. */
228 #define YYNNTS ]b4_nterms_number[
229 /* YYNRULES -- Number of rules. */
230 #define YYNRULES ]b4_rules_number[
231 /* YYNRULES -- Number of states. */
232 #define YYNSTATES ]b4_states_number[
233 /* YYMAXRHS -- Maximum number of symbols on right-hand side of rule. */
234 #define YYMAXRHS ]b4_r2_max[
235
236 /* YYTRANSLATE(X) -- Bison symbol number corresponding to X. */
237 #define YYUNDEFTOK ]b4_undef_token_number[
238 #define YYMAXUTOK ]b4_user_token_number_max[
239
240 #define YYTRANSLATE(YYX) \
241 ((unsigned)(YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
242
243 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */
244 static const ]b4_uint_type(b4_translate_max)[ yytranslate[] =
245 {
246 ]b4_translate[
247 };
248
249 #if YYDEBUG
250 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
251 YYRHS. */
252 static const ]b4_uint_type(b4_prhs_max)[ yyprhs[] =
253 {
254 ]b4_prhs[
255 };
256
257 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
258 static const ]b4_sint_type(b4_rhs_max)[ yyrhs[] =
259 {
260 ]b4_rhs[
261 };
262
263 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
264 static const ]b4_uint_type(b4_rline_max)[ yyrline[] =
265 {
266 ]b4_rline[
267 };
268 #endif
269
270 #if (YYDEBUG) || YYERROR_VERBOSE
271 /* YYTNME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
272 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
273 static const char *const yytname[] =
274 {
275 ]b4_tname[
276 };
277
278 #define yytname_size ((int) (sizeof (yytname) / sizeof (yytname[0])))
279 #endif
280
281 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
282 static const ]b4_uint_type(b4_r1_max)[ yyr1[] =
283 {
284 ]b4_r1[
285 };
286
287 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
288 static const ]b4_uint_type(b4_r2_max)[ yyr2[] =
289 {
290 ]b4_r2[
291 };
292
293 /* YYDPREC[RULE-NUM] -- Dynamic precedence of rule #RULE-NUM (0 if none). */
294 static const short yydprec[] =
295 {
296 ]b4_dprec[
297 };
298
299 /* YYMERGER[RULE-NUM] -- Index of merging function for rule #RULE-NUM. */
300 static const short yymerger[] =
301 {
302 ]b4_merger[
303 };
304
305 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
306 doesn't specify something else to do. Zero means the default is an
307 error. */
308 static const short yydefact[] =
309 {
310 ]b4_defact[
311 };
312
313 /* YYPGOTO[NTERM-NUM]. */
314 static const short yydefgoto[] =
315 {
316 ]b4_defgoto[
317 };
318
319 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
320 STATE-NUM. */
321 static const short yypact[] =
322 {
323 ]b4_pact[
324 };
325
326 /* YYPGOTO[NTERM-NUM]. */
327 static const short yypgoto[] =
328 {
329 ]b4_pgoto[
330 };
331
332 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
333 positive, shift that token. If negative, reduce the rule which
334 number is the opposite. If zero, do what YYDEFACT says. */
335 static const short yytable[] =
336 {
337 ]b4_table[
338 };
339
340 /* YYCONFLP[YYPACT[STATE-NUM]] -- pointer into yyconfl of start of list
341 of conflicting reductions corresponding to action entry for state
342 STATE-NUM in yytable. 0 means no conflicts. The list in yyconfl
343 is terminated by a rule number of 0. */
344 static const short yyconflp[] =
345 {
346 ]b4_conflict_list_heads[
347 };
348
349 /* YYCONFL[I] -- lists of conflicting rule numbers, each terminated
350 by 0, pointed into by YYCONFLP. */
351 static const short yyconfl[] =
352 {
353 ]b4_conflicting_rules[
354 };
355
356 static const short yycheck[] =
357 {
358 ]b4_check[
359 };
360
361 \f
362 /* The user can define YYPARSE_PARAM as the name of an argument to be passed
363 into yyparse. The argument should have type void *.
364 It should actually point to an object.
365 Grammar actions can access the variable by casting it
366 to the proper pointer type. */
367
368 #ifdef YYPARSE_PARAM
369 # define YYPARSE_PARAM_ARG void *YYPARSE_PARAM
370 #else /* !YYPARSE_PARAM */
371 # define YYPARSE_PARAM_ARG void
372 #endif /* !YYPARSE_PARAM */
373
374 /* Prevent warning if -Wstrict-prototypes. */
375 #ifdef __GNUC__
376 # ifdef YYPARSE_PARAM
377 int yyparse (void *);
378 # else
379 int yyparse (void);
380 # endif
381 #endif
382
383 /* Error token number */
384 #define YYTERROR 1
385
386 /* YYLLOC_DEFAULT -- Compute the default location (before the actions
387 are run). */
388
389 #define YYRHSLOC(yyRhs,YYK) (yyRhs[YYK].yystate.yyloc)
390
391 #ifndef YYLLOC_DEFAULT
392 # define YYLLOC_DEFAULT(yyCurrent, yyRhs, YYN) \
393 yyCurrent.yyfirst_line = YYRHSLOC(yyRhs,1).yyfirst_line; \
394 yyCurrent.yyfirst_column = YYRHSLOC(yyRhs,1).yyfirst_column; \
395 yyCurrent.yylast_line = YYRHSLOC(yyRhs,YYN).yylast_line; \
396 yyCurrent.yylast_column = YYRHSLOC(yyRhs,YYN).yylast_column;
397 #endif
398
399 /* YYLEX -- calling `yylex' with the right arguments. */
400
401 ]b4_pure_if(
402 [
403 #ifdef YYLEX_PARAM
404 # define YYLEX yylex (yylvalp, b4_location_if([yyllocp, ])YYLEX_PARAM)
405 #else
406 # define YYLEX yylex (yylvalp[]b4_location_if([, yyllocp]))
407 #endif],
408 [#define YYLEX yylex ()])
409
410 b4_pure_if(
411 [
412 #undef yynerrs
413 #define yynerrs (yystack->yyerrcnt)
414 #undef yychar
415 #define yychar (yystack->yyrawchar)],
416 [YYSTYPE yylval;
417
418 YYLTYPE yylloc;
419
420 int yynerrs;
421 int yychar;])[
422
423 static const int YYEOF = 0;
424 static const int YYEMPTY = -2;
425
426 typedef enum { yyok, yyaccept, yyabort, yyerr } YYRESULTTAG;
427
428 #define YYCHK(YYE) \
429 do { YYRESULTTAG yyflag = YYE; if (yyflag != yyok) return yyflag; } \
430 while (0)
431
432 #if YYDEBUG
433
434 #if ! defined (YYFPRINTF)
435 # define YYFPRINTF fprintf
436 #endif
437
438 # define YYDPRINTF(Args) \
439 do { \
440 if (yydebug) \
441 YYFPRINTF Args; \
442 } while (0)
443 /* Nonzero means print parse trace. It is left uninitialized so that
444 multiple parsers can coexist. */
445 int yydebug;
446 #else /* !YYDEBUG */
447 /* Avoid empty `if' bodies. */
448 # define YYDPRINTF(Args) {}
449 #endif /* !YYDEBUG */
450
451 /* YYINITDEPTH -- initial size of the parser's stacks. */
452 #ifndef YYINITDEPTH
453 # define YYINITDEPTH ]b4_initdepth[
454 #endif
455
456 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
457 if the built-in stack extension method is used).
458
459 Do not make this value too large; the results are undefined if
460 SIZE_MAX < YYMAXDEPTH * sizeof (GLRStackItem)
461 evaluated with infinite-precision integer arithmetic. */
462
463 #if YYMAXDEPTH == 0
464 # undef YYMAXDEPTH
465 #endif
466
467 #ifndef YYMAXDEPTH
468 # define YYMAXDEPTH ]b4_maxdepth[
469 #endif
470
471 /* Minimum number of free items on the stack allowed after an
472 allocation. This is to allow allocation and initialization
473 to be completed by functions that call expandGLRStack before the
474 stack is expanded, thus insuring that all necessary pointers get
475 properly redirected to new data. */
476 #define YYHEADROOM 2
477
478 #if ! defined (YYSTACKEXPANDABLE) \
479 && (! defined (__cplusplus) || (YYLTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))
480 #define YYSTACKEXPANDABLE 1
481 #else
482 #define YYSTACKEXPANDABLE 0
483 #endif
484
485 /** State numbers, as in LALR(1) machine */
486 typedef int yyStateNum;
487
488 /** Rule numbers, as in LALR(1) machine */
489 typedef int yyRuleNum;
490
491 /** Grammar symbol */
492 typedef short yySymbol;
493
494 /** Item references, as in LALR(1) machine */
495 typedef short yyItemNum;
496
497 typedef struct yyGLRState yyGLRState;
498 typedef struct yySemanticOption yySemanticOption;
499 typedef union yyGLRStackItem yyGLRStackItem;
500 typedef struct yyGLRStack yyGLRStack;
501 typedef struct yyGLRStateSet yyGLRStateSet;
502
503 struct yyGLRState {
504 bool yyisState;
505 bool yyresolved;
506 yyStateNum yylrState;
507 yyGLRState* yypred;
508 size_t yyposn;
509 union {
510 yySemanticOption* yyfirstVal;
511 YYSTYPE yysval;
512 } yysemantics;
513 YYLTYPE yyloc;
514 };
515
516 struct yyGLRStateSet {
517 yyGLRState** yystates;
518 size_t yysize, yycapacity;
519 };
520
521 struct yySemanticOption {
522 bool yyisState;
523 yyRuleNum yyrule;
524 yyGLRState* yystate;
525 yySemanticOption* yynext;
526 };
527
528 union yyGLRStackItem {
529 yyGLRState yystate;
530 yySemanticOption yyoption;
531 };
532
533 struct yyGLRStack {
534 int yyerrflag;
535 int yyerrState;
536 ]b4_pure_if(
537 [
538 int yyerrcnt;
539 int yyrawchar;
540 ])[
541 yySymbol* yytokenp;
542 jmp_buf yyexception_buffer;
543 yyGLRStackItem* yyitems;
544 yyGLRStackItem* yynextFree;
545 int yyspaceLeft;
546 yyGLRState* yysplitPoint;
547 yyGLRState* yylastDeleted;
548 yyGLRStateSet yytops;
549 };
550
551 static void yyinitGLRStack (yyGLRStack* yystack, size_t yysize);
552 static void yyexpandGLRStack (yyGLRStack* yystack);
553 static void yyfreeGLRStack (yyGLRStack* yystack);
554
555 static void
556 yyFail (yyGLRStack* yystack, const char* yyformat, ...)
557 {
558 if (yyformat != NULL)
559 {
560 char yymsg[256];
561 va_list yyap;
562 va_start (yyap, yyformat);
563 yystack->yyerrflag = 1;
564 vsprintf (yymsg, yyformat, yyap);
565 yyerror (yymsg);
566 }
567 longjmp (yystack->yyexception_buffer, 1);
568 }
569
570 #if YYDEBUG || YYERROR_VERBOSE
571 /** A printable representation of TOKEN. Valid until next call to
572 * tokenName. */
573 static inline const char*
574 yytokenName (yySymbol yytoken)
575 {
576 return yytname[yytoken];
577 }
578 #endif
579
580 /** Perform user action for rule number YYN, with RHS length YYRHSLEN,
581 * and top stack item YYVSP. YYLVALP points to place to put semantic
582 * value ($$), and yylocp points to place for location information
583 * (@$). Returns yyok for normal return, yyaccept for YYACCEPT,
584 * yyerr for YYERROR, yyabort for YYABORT. */
585 static YYRESULTTAG
586 yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp,
587 YYSTYPE* yyvalp, YYLTYPE* yylocp, yyGLRStack* yystack)
588 {
589 /* Avoid `unused' warnings in there are no $n. */
590 (void) yystack;
591
592 if (yyrhslen == 0)
593 {
594 *yyvalp = yyval_default;
595 *yylocp = yyloc_default;
596 }
597 else
598 {
599 *yyvalp = yyvsp[1-yyrhslen].yystate.yysemantics.yysval;
600 *yylocp = yyvsp[1-yyrhslen].yystate.yyloc;
601 }
602 # undef yyval
603 # define yyval (*yyvalp)
604 # undef yyerrok
605 # define yyerrok (yystack->yyerrState = 0)
606 # undef YYACCEPT
607 # define YYACCEPT return yyaccept
608 # undef YYABORT
609 # define YYABORT return yyabort
610 # undef YYERROR
611 # define YYERROR return yyerr
612 # undef YYRECOVERING
613 # define YYRECOVERING (yystack->yyerrState != 0)
614 # undef yyclearin
615 # define yyclearin (yychar = *(yystack->yytokenp) = YYEMPTY)
616 # undef YYBACKUP
617 # define YYBACKUP(Token, Value) \
618 do { \
619 yyerror ("syntax error: cannot back up"); \
620 YYERROR; \
621 } while (0)
622
623 ]
624 switch (yyn)
625 {
626 b4_actions
627 }
628
629 return yyok;
630 # undef yyval
631 # undef yyerrok
632 # undef YYABORT
633 # undef YYACCEPT
634 # undef YYERROR
635 # undef YYBACKUP
636 # undef yyclearin
637 # undef YYRECOVERING
638 /* Line __line__ of __file__. */
639 #line __oline__ "__ofile__"
640 }
641 \f
642
643 static YYSTYPE
644 yyuserMerge (int yyn, YYSTYPE* yy0, YYSTYPE* yy1)
645 {
646 YYSTYPE yyval = *yy0;
647 /* `Use' the arguments. */
648 (void) yy0;
649 (void) yy1;
650
651 switch (yyn)
652 {
653 b4_mergers
654 }
655 return yyval;
656 }
657 [
658 /* Bison grammar-table manipulation */
659
660 /** Number of symbols composing the right hand side of rule #RULE. */
661 static inline int
662 yyrhsLength (yyRuleNum yyrule)
663 {
664 return yyr2[yyrule];
665 }
666
667 /** Left-hand-side symbol for rule #RULE. */
668 static inline yySymbol
669 yylhsNonterm (yyRuleNum yyrule)
670 {
671 return yyr1[yyrule];
672 }
673
674 /** True iff LR state STATE has only a default reduction (regardless
675 * of token). */
676 static inline bool
677 yyisDefaultedState (yyStateNum yystate)
678 {
679 return yypact[yystate] == YYFLAG;
680 }
681
682 /** The default reduction for STATE, assuming it has one. */
683 static inline yyRuleNum
684 yydefaultAction (yyStateNum yystate)
685 {
686 return yydefact[yystate];
687 }
688
689 /** Set *ACTION to the action to take in STATE on seeing TOKEN.
690 * Result R means
691 * R < 0: Reduce on rule -R.
692 * R = 0: Error.
693 * R > 0: Shift to state R.
694 * Set *CONFLICTS to a pointer into yyconfl to 0-terminated list of
695 * conflicting reductions.
696 */
697 static inline void
698 yygetLRActions (yyStateNum yystate, int yytoken,
699 int* yyaction, const short** yyconflicts)
700 {
701 int yyindex = yypact[yystate] + yytoken;
702 if (yyindex < 0 || yyindex > YYLAST || yycheck[yyindex] != yytoken)
703 {
704 *yyaction = -yydefact[yystate];
705 *yyconflicts = yyconfl;
706 }
707 else
708 {
709 *yyaction = yytable[yyindex];
710 *yyconflicts = yyconfl + yyconflp[yyindex];
711 }
712 }
713
714 static inline yyStateNum
715 yyLRgotoState (yyStateNum yystate, yySymbol yylhs)
716 {
717 int yyr;
718 yyr = yypgoto[yylhs - YYNTOKENS] + yystate;
719 if (yyr >= 0 && yyr <= YYLAST && yycheck[yyr] == yystate)
720 return yytable[yyr];
721 else
722 return yydefgoto[yylhs - YYNTOKENS];
723 }
724
725 static inline bool
726 yyisShiftAction (int yyaction)
727 {
728 return yyaction > 0;
729 }
730
731 static inline bool
732 yyisErrorAction (int yyaction)
733 {
734 return yyaction == 0 || yyaction == YYFLAG;
735 }
736
737 /* GLRStates */
738
739 /** True iff the semantic value of the edge leading to STATE is
740 * resolved. */
741 static inline bool
742 yyhasResolvedValue (yyGLRState* yystate)
743 {
744 return yystate->yyresolved;
745 }
746
747 static void
748 yyaddDeferredAction (yyGLRStack* yystack, yyGLRState* yystate,
749 yyGLRState* yyrhs, yyRuleNum yyrule)
750 {
751 yySemanticOption* yynewItem;
752 yynewItem = &yystack->yynextFree->yyoption;
753 yystack->yyspaceLeft -= 1;
754 yystack->yynextFree += 1;
755 yynewItem->yyisState = yyfalse;
756 yynewItem->yystate = yyrhs;
757 yynewItem->yyrule = yyrule;
758 yynewItem->yynext = yystate->yysemantics.yyfirstVal;
759 yystate->yysemantics.yyfirstVal = yynewItem;
760 if (yystack->yyspaceLeft < YYHEADROOM)
761 yyexpandGLRStack (yystack);
762 }
763
764 /* GLRStacks */
765
766 /** Initialize SET to a singleton set containing an empty stack. */
767 static void
768 yyinitStateSet (yyGLRStateSet* yyset)
769 {
770 yyset->yysize = 1;
771 yyset->yycapacity = 16;
772 yyset->yystates = (yyGLRState**) malloc (16 * sizeof (yyset->yystates[0]));
773 yyset->yystates[0] = NULL;
774 }
775
776 static void yyfreeStateSet (yyGLRStateSet* yyset)
777 {
778 free (yyset->yystates);
779 }
780
781 /** Initialize STACK to a single empty stack, with total maximum
782 * capacity for all stacks of SIZE. */
783 static void
784 yyinitGLRStack (yyGLRStack* yystack, size_t yysize)
785 {
786 yystack->yyerrflag = 0;
787 yystack->yyerrState = 0;
788 yynerrs = 0;
789 yystack->yyspaceLeft = yysize;
790 yystack->yynextFree = yystack->yyitems =
791 (yyGLRStackItem*) malloc (yysize * sizeof (yystack->yynextFree[0]));
792 yystack->yysplitPoint = NULL;
793 yystack->yylastDeleted = NULL;
794 yyinitStateSet (&yystack->yytops);
795 }
796
797 #define YYRELOC(YYFROMITEMS,YYTOITEMS,YYX,YYTYPE) \
798 &((YYTOITEMS) - ((YYFROMITEMS) - (yyGLRStackItem*) (YYX)))->YYTYPE
799
800 /** If STACK is expandable, extend it. WARNING: Pointers into the
801 stack from outside should be considered invalid after this call.
802 We always expand when there are 1 or fewer items left AFTER an
803 allocation, so that we can avoid having external pointers exist
804 across an allocation. */
805 static void
806 yyexpandGLRStack (yyGLRStack* yystack)
807 {
808 #if YYSTACKEXPANDABLE
809 yyGLRStack yynewStack;
810 yyGLRStackItem* yyp0, *yyp1;
811 size_t yysize, yynewSize;
812 size_t yyn;
813 yysize = yystack->yynextFree - yystack->yyitems;
814 if (yysize >= YYMAXDEPTH)
815 yyFail (yystack, "parsing stack overflow (%d items)", yysize);
816 yynewSize = 2*yysize;
817 if (yynewSize > YYMAXDEPTH)
818 yynewSize = YYMAXDEPTH;
819 yyinitGLRStack (&yynewStack, yynewSize);
820 for (yyp0 = yystack->yyitems, yyp1 = yynewStack.yyitems, yyn = yysize;
821 yyn > 0;
822 yyn -= 1, yyp0 += 1, yyp1 += 1)
823 {
824 *yyp1 = *yyp0;
825 if (*(bool*) yyp0)
826 {
827 yyGLRState* yys0 = &yyp0->yystate;
828 yyGLRState* yys1 = &yyp1->yystate;
829 if (yys0->yypred != NULL)
830 yys1->yypred =
831 YYRELOC (yyp0, yyp1, yys0->yypred, yystate);
832 if (! yys0->yyresolved && yys0->yysemantics.yyfirstVal != NULL)
833 yys1->yysemantics.yyfirstVal =
834 YYRELOC(yyp0, yyp1, yys0->yysemantics.yyfirstVal, yyoption);
835 }
836 else
837 {
838 yySemanticOption* yyv0 = &yyp0->yyoption;
839 yySemanticOption* yyv1 = &yyp1->yyoption;
840 if (yyv0->yystate != NULL)
841 yyv1->yystate = YYRELOC (yyp0, yyp1, yyv0->yystate, yystate);
842 if (yyv0->yynext != NULL)
843 yyv1->yynext = YYRELOC (yyp0, yyp1, yyv0->yynext, yyoption);
844 }
845 }
846 if (yystack->yysplitPoint != NULL)
847 yystack->yysplitPoint = YYRELOC (yystack->yyitems, yynewStack.yyitems,
848 yystack->yysplitPoint, yystate);
849
850 for (yyn = 0; yyn < yystack->yytops.yysize; yyn += 1)
851 if (yystack->yytops.yystates[yyn] != NULL)
852 yystack->yytops.yystates[yyn] =
853 YYRELOC (yystack->yyitems, yynewStack.yyitems,
854 yystack->yytops.yystates[yyn], yystate);
855 free (yystack->yyitems);
856 yystack->yyitems = yynewStack.yyitems;
857 yystack->yynextFree = yynewStack.yynextFree + yysize;
858 yystack->yyspaceLeft = yynewStack.yyspaceLeft - yysize;
859
860 #else
861
862 yyFail (yystack, "parsing stack overflow (%d items)", yysize);
863
864 #endif
865 }
866
867 static void
868 yyfreeGLRStack (yyGLRStack* yystack)
869 {
870 free (yystack->yyitems);
871 yyfreeStateSet (&yystack->yytops);
872 }
873
874 /** Assuming that S is a GLRState somewhere on STACK, update the
875 * splitpoint of STACK, if needed, so that it is at least as deep as
876 * S. */
877 static inline void
878 yyupdateSplit (yyGLRStack* yystack, yyGLRState* yys)
879 {
880 if (yystack->yysplitPoint != NULL && yystack->yysplitPoint > yys)
881 yystack->yysplitPoint = yys;
882 }
883
884 /** Invalidate stack #K in STACK. */
885 static inline void
886 yymarkStackDeleted (yyGLRStack* yystack, int yyk)
887 {
888 if (yystack->yytops.yystates[yyk] != NULL)
889 yystack->yylastDeleted = yystack->yytops.yystates[yyk];
890 yystack->yytops.yystates[yyk] = NULL;
891 }
892
893 /** Undelete the last stack that was marked as deleted. Can only be
894 done once after a deletion, and only when all other stacks have
895 been deleted. */
896 static void
897 yyundeleteLastStack (yyGLRStack* yystack)
898 {
899 if (yystack->yylastDeleted == NULL || yystack->yytops.yysize != 0)
900 return;
901 yystack->yytops.yystates[0] = yystack->yylastDeleted;
902 yystack->yytops.yysize = 1;
903 YYDPRINTF ((stderr, "Restoring last deleted stack as stack #0.\n"));
904 yystack->yylastDeleted = NULL;
905 }
906
907 static inline void
908 yyremoveDeletes (yyGLRStack* yystack)
909 {
910 size_t yyi, yyj;
911 yyi = yyj = 0;
912 while (yyj < yystack->yytops.yysize)
913 {
914 if (yystack->yytops.yystates[yyi] == NULL)
915 {
916 if (yyi == yyj)
917 YYDPRINTF ((stderr, "Removing dead stacks.\n"));
918 yystack->yytops.yysize -= 1;
919 }
920 else
921 {
922 yystack->yytops.yystates[yyj] = yystack->yytops.yystates[yyi];
923 if (yyj != yyi)
924 YYDPRINTF ((stderr, "Rename stack %d -> %d.\n", yyi, yyj));
925 yyj += 1;
926 }
927 yyi += 1;
928 }
929 }
930
931 /** Shift to a new state on stack #K of STACK, corresponding to LR state
932 * LRSTATE, at input position POSN, with (resolved) semantic value SVAL. */
933 static inline void
934 yyglrShift (yyGLRStack* yystack, int yyk, yyStateNum yylrState, size_t yyposn,
935 YYSTYPE yysval, YYLTYPE* yylocp)
936 {
937 yyGLRStackItem* yynewItem;
938
939 yynewItem = yystack->yynextFree;
940 yystack->yynextFree += 1;
941 yystack->yyspaceLeft -= 1;
942 yynewItem->yystate.yyisState = yytrue;
943 yynewItem->yystate.yylrState = yylrState;
944 yynewItem->yystate.yyposn = yyposn;
945 yynewItem->yystate.yyresolved = yytrue;
946 yynewItem->yystate.yypred = yystack->yytops.yystates[yyk];
947 yystack->yytops.yystates[yyk] = &yynewItem->yystate;
948 yynewItem->yystate.yysemantics.yysval = yysval;
949 yynewItem->yystate.yyloc = *yylocp;
950 if (yystack->yyspaceLeft < YYHEADROOM)
951 yyexpandGLRStack (yystack);
952 }
953
954 /** Shift to a new state on stack #K of STACK, to a new state
955 * corresponding to LR state LRSTATE, at input position POSN, with
956 * the (unresolved) semantic value of RHS under the action for RULE. */
957 static inline void
958 yyglrShiftDefer (yyGLRStack* yystack, int yyk, yyStateNum yylrState,
959 size_t yyposn, yyGLRState* yyrhs, yyRuleNum yyrule)
960 {
961 yyGLRStackItem* yynewItem;
962
963 yynewItem = yystack->yynextFree;
964 yynewItem->yystate.yyisState = yytrue;
965 yynewItem->yystate.yylrState = yylrState;
966 yynewItem->yystate.yyposn = yyposn;
967 yynewItem->yystate.yyresolved = yyfalse;
968 yynewItem->yystate.yypred = yystack->yytops.yystates[yyk];
969 yynewItem->yystate.yysemantics.yyfirstVal = NULL;
970 yystack->yytops.yystates[yyk] = &yynewItem->yystate;
971 yystack->yynextFree += 1;
972 yystack->yyspaceLeft -= 1;
973 yyaddDeferredAction (yystack, &yynewItem->yystate, yyrhs, yyrule);
974 }
975
976 /** Pop the symbols consumed by reduction #RULE from the top of stack
977 * #K of STACK, and perform the appropriate semantic action on their
978 * semantic values. Assumes that all ambiguities in semantic values
979 * have been previously resolved. Set *VALP to the resulting value,
980 * and *LOCP to the computed location (if any). Return value is as
981 * for userAction. */
982 static inline int
983 yydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule,
984 YYSTYPE* yyvalp, YYLTYPE* yylocp)
985 {
986 int yynrhs = yyrhsLength (yyrule);
987
988 if (yystack->yysplitPoint == NULL)
989 {
990 /* Standard special case: single stack. */
991 yyGLRStackItem* yyrhs = (yyGLRStackItem*) yystack->yytops.yystates[yyk];
992 assert (yyk == 0);
993 yystack->yynextFree -= yynrhs;
994 yystack->yyspaceLeft += yynrhs;
995 yystack->yytops.yystates[0] = & yystack->yynextFree[-1].yystate;
996 if (yynrhs == 0)
997 {
998 *yyvalp = yyval_default;
999 *yylocp = yyloc_default;
1000 }
1001 else
1002 {
1003 *yyvalp = yyrhs[1-yynrhs].yystate.yysemantics.yysval;
1004 *yylocp = yyrhs[1-yynrhs].yystate.yyloc;
1005 }
1006 return yyuserAction (yyrule, yynrhs, yyrhs, yyvalp, yylocp, yystack);
1007 }
1008 else
1009 {
1010 int yyi;
1011 yyGLRState* yys;
1012 yyGLRStackItem yyrhsVals[YYMAXRHS];
1013 for (yyi = yynrhs-1, yys = yystack->yytops.yystates[yyk]; yyi >= 0;
1014 yyi -= 1, yys = yys->yypred)
1015 {
1016 assert (yys->yypred != NULL);
1017 yyrhsVals[yyi].yystate.yyresolved = yytrue;
1018 yyrhsVals[yyi].yystate.yysemantics.yysval = yys->yysemantics.yysval;
1019 yyrhsVals[yyi].yystate.yyloc = yys->yyloc;
1020 }
1021 yyupdateSplit (yystack, yys);
1022 yystack->yytops.yystates[yyk] = yys;
1023 if (yynrhs == 0)
1024 {
1025 *yyvalp = yyval_default;
1026 *yylocp = yyloc_default;
1027 }
1028 else
1029 {
1030 *yyvalp = yyrhsVals[0].yystate.yysemantics.yysval;
1031 *yylocp = yyrhsVals[0].yystate.yyloc;
1032 }
1033 return yyuserAction (yyrule, yynrhs, yyrhsVals + (yynrhs-1),
1034 yyvalp, yylocp, yystack);
1035 }
1036 }
1037
1038 /** Pop items off stack #K of STACK according to grammar rule RULE,
1039 * and push back on the resulting nonterminal symbol. Perform the
1040 * semantic action associated with RULE and store its value with the
1041 * newly pushed state, if FORCEEVAL or if STACK is currently
1042 * unambiguous. Otherwise, store the deferred semantic action with
1043 * the new state. If the new state would have an identical input
1044 * position, LR state, and predecessor to an existing state on the stack,
1045 * it is identified with that existing state, eliminating stack #K from
1046 * the STACK. In this case, the (necessarily deferred) semantic value is
1047 * added to the options for the existing state's semantic value.
1048 */
1049 static inline YYRESULTTAG
1050 yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule,
1051 bool yyforceEval)
1052 {
1053 size_t yyposn = yystack->yytops.yystates[yyk]->yyposn;
1054
1055 if (yyforceEval || yystack->yysplitPoint == NULL)
1056 {
1057 YYSTYPE yysval;
1058 YYLTYPE yyloc;
1059
1060 YYCHK (yydoAction (yystack, yyk, yyrule, &yysval, &yyloc));
1061 yyglrShift (yystack, yyk,
1062 yyLRgotoState (yystack->yytops.yystates[yyk]->yylrState,
1063 yylhsNonterm (yyrule)),
1064 yyposn, yysval, &yyloc);
1065 YYDPRINTF ((stderr, "Reduced stack %d by rule #%d. Now in state %d.\n",
1066 yyk, yyrule-1, yystack->yytops.yystates[yyk]->yylrState));
1067 }
1068 else
1069 {
1070 size_t yyi;
1071 int yyn;
1072 yyGLRState* yys, *yys0 = yystack->yytops.yystates[yyk];
1073 yyStateNum yynewLRState;
1074
1075 for (yys = yystack->yytops.yystates[yyk], yyn = yyrhsLength (yyrule);
1076 yyn > 0; yyn -= 1)
1077 {
1078 yys = yys->yypred;
1079 assert (yys != NULL);
1080 }
1081 yyupdateSplit (yystack, yys);
1082 yynewLRState = yyLRgotoState (yys->yylrState, yylhsNonterm (yyrule));
1083 YYDPRINTF ((stderr,
1084 "Reduced stack %d by rule #%d; action deferred. "
1085 "Now in state %d.\n",
1086 yyk, yyrule-1, yynewLRState));
1087 for (yyi = 0; yyi < yystack->yytops.yysize; yyi += 1)
1088 if (yyi != yyk && yystack->yytops.yystates[yyi] != NULL)
1089 {
1090 yyGLRState* yyp, *yysplit = yystack->yysplitPoint;
1091 yyp = yystack->yytops.yystates[yyi];
1092 while (yyp != yys && yyp != yysplit && yyp->yyposn >= yyposn)
1093 {
1094 if (yyp->yylrState == yynewLRState && yyp->yypred == yys)
1095 {
1096 yyaddDeferredAction (yystack, yyp, yys0, yyrule);
1097 yymarkStackDeleted (yystack, yyk);
1098 YYDPRINTF ((stderr, "Merging stack %d into stack %d.\n",
1099 yyk, yyi));
1100 return 0;
1101 }
1102 yyp = yyp->yypred;
1103 }
1104 }
1105 yystack->yytops.yystates[yyk] = yys;
1106 yyglrShiftDefer (yystack, yyk, yynewLRState, yyposn, yys0, yyrule);
1107 }
1108 return 0;
1109 }
1110
1111 static int
1112 yysplitStack (yyGLRStack* yystack, int yyk)
1113 {
1114 if (yystack->yysplitPoint == NULL)
1115 {
1116 assert (yyk == 0);
1117 yystack->yysplitPoint = yystack->yytops.yystates[yyk];
1118 }
1119 if (yystack->yytops.yysize >= yystack->yytops.yycapacity)
1120 {
1121 yystack->yytops.yycapacity *= 2;
1122 yystack->yytops.yystates =
1123 (yyGLRState**) realloc (yystack->yytops.yystates,
1124 yystack->yytops.yycapacity
1125 * sizeof (yyGLRState*));
1126 }
1127 yystack->yytops.yystates[yystack->yytops.yysize]
1128 = yystack->yytops.yystates[yyk];
1129 yystack->yytops.yysize += 1;
1130 return yystack->yytops.yysize-1;
1131 }
1132
1133 /** True iff Y0 and Y1 represent identical options at the top level.
1134 * That is, they represent the same rule applied to RHS symbols
1135 * that produce the same terminal symbols. */
1136 static bool
1137 yyidenticalOptions (yySemanticOption* yyy0, yySemanticOption* yyy1)
1138 {
1139 if (yyy0->yyrule == yyy1->yyrule)
1140 {
1141 yyGLRState *yys0, *yys1;
1142 int yyn;
1143 for (yys0 = yyy0->yystate, yys1 = yyy1->yystate,
1144 yyn = yyrhsLength (yyy0->yyrule);
1145 yyn > 0;
1146 yys0 = yys0->yypred, yys1 = yys1->yypred, yyn -= 1)
1147 if (yys0->yyposn != yys1->yyposn)
1148 return yyfalse;
1149 return yytrue;
1150 }
1151 else
1152 return yyfalse;
1153 }
1154
1155 /** Assuming identicalOptions (Y0,Y1), (destructively) merge the
1156 * alternative semantic values for the RHS-symbols of Y1 into the
1157 * corresponding semantic value sets of the symbols of Y0. */
1158 static void
1159 yymergeOptionSets (yySemanticOption* yyy0, yySemanticOption* yyy1)
1160 {
1161 yyGLRState *yys0, *yys1;
1162 int yyn;
1163 for (yys0 = yyy0->yystate, yys1 = yyy1->yystate,
1164 yyn = yyrhsLength (yyy0->yyrule);
1165 yyn > 0;
1166 yys0 = yys0->yypred, yys1 = yys1->yypred, yyn -= 1)
1167 if (yys0 == yys1)
1168 break;
1169 else if (! yys0->yyresolved && ! yys1->yyresolved)
1170 {
1171 yySemanticOption* yyz;
1172 for (yyz = yys0->yysemantics.yyfirstVal; yyz->yynext != NULL;
1173 yyz = yyz->yynext)
1174 ;
1175 yyz->yynext = yys1->yysemantics.yyfirstVal;
1176 }
1177 }
1178
1179 /** Y0 and Y1 represent two possible actions to take in a given
1180 * parsing state; return 0 if no combination is possible,
1181 * 1 if user-mergeable, 2 if Y0 is preferred, 3 if Y1 is preferred. */
1182 static int
1183 yypreference (yySemanticOption* y0, yySemanticOption* y1)
1184 {
1185 yyRuleNum r0 = y0->yyrule, r1 = y1->yyrule;
1186 int p0 = yydprec[r0], p1 = yydprec[r1];
1187
1188 if (p0 == p1)
1189 {
1190 if (yymerger[r0] == 0 || yymerger[r0] != yymerger[r1])
1191 return 0;
1192 else
1193 return 1;
1194 }
1195 if (p0 == 0 || p1 == 0)
1196 return 0;
1197 if (p0 < p1)
1198 return 3;
1199 if (p0 > p1)
1200 return 2;
1201 return 0;
1202 }
1203
1204 static YYRESULTTAG yyresolveValue (yySemanticOption* yyoptionList,
1205 yyGLRStack* yystack, YYSTYPE* yyvalp,
1206 YYLTYPE* yylocp);
1207
1208 static YYRESULTTAG
1209 yyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack)
1210 {
1211 YYRESULTTAG yyflag;
1212 if (yyn > 0)
1213 {
1214 assert (yys->yypred != NULL);
1215 yyflag = yyresolveStates (yys->yypred, yyn-1, yystack);
1216 if (yyflag != yyok)
1217 return yyflag;
1218 if (! yys->yyresolved)
1219 {
1220 yyflag = yyresolveValue (yys->yysemantics.yyfirstVal, yystack,
1221 &yys->yysemantics.yysval, &yys->yyloc);
1222 if (yyflag != yyok)
1223 return yyflag;
1224 yys->yyresolved = yytrue;
1225 }
1226 }
1227 return yyok;
1228 }
1229
1230 static YYRESULTTAG
1231 yyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack,
1232 YYSTYPE* yyvalp, YYLTYPE* yylocp)
1233 {
1234 yyGLRStackItem yyrhsVals[YYMAXRHS];
1235 int yynrhs, yyi;
1236 yyGLRState* yys;
1237
1238 yynrhs = yyrhsLength (yyopt->yyrule);
1239 YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack));
1240 for (yyi = yynrhs-1, yys = yyopt->yystate; yyi >= 0;
1241 yyi -= 1, yys = yys->yypred)
1242 {
1243 assert (yys->yypred != NULL);
1244 yyrhsVals[yyi].yystate.yyresolved = yytrue;
1245 yyrhsVals[yyi].yystate.yysemantics.yysval = yys->yysemantics.yysval;
1246 yyrhsVals[yyi].yystate.yyloc = yys->yyloc;
1247 }
1248 return yyuserAction (yyopt->yyrule, yynrhs, yyrhsVals + (yynrhs-1),
1249 yyvalp, yylocp, yystack);
1250 }
1251
1252 #if YYDEBUG
1253 static yyGLRState YYLEFTMOST_STATE = { 0, NULL, -1, 0, { NULL } };
1254
1255 static void yyreportTree (yySemanticOption* yyx, int yyindent)
1256 {
1257 int yynrhs = yyrhsLength (yyx->yyrule);
1258 int yyi;
1259 yyGLRState* yys;
1260 yyGLRState* yystates[YYMAXRHS];
1261
1262 for (yyi = yynrhs, yys = yyx->yystate; yyi > 0; yyi -= 1, yys = yys->yypred)
1263 yystates[yyi] = yys;
1264 if (yys == NULL)
1265 yystates[0] = &YYLEFTMOST_STATE;
1266 else
1267 yystates[0] = yys;
1268
1269 if (yys->yyposn+1 > yyx->yystate->yyposn)
1270 YYFPRINTF (stderr, "%*s%s -> <Rule %d, empty>\n",
1271 yyindent, "", yytokenName (yylhsNonterm (yyx->yyrule)),
1272 yyx->yyrule);
1273 else
1274 YYFPRINTF (stderr, "%*s%s -> <Rule %d, tokens %d .. %d>\n",
1275 yyindent, "", yytokenName (yylhsNonterm (yyx->yyrule)),
1276 yyx->yyrule, yys->yyposn+1, yyx->yystate->yyposn);
1277 for (yyi = 1; yyi <= yynrhs; yyi += 1)
1278 {
1279 if (yystates[yyi]->yyresolved)
1280 {
1281 if (yystates[yyi-1]->yyposn+1 > yystates[yyi]->yyposn)
1282 YYFPRINTF (stderr, "%*s%s <empty>\n", yyindent+2, "",
1283 yytokenName (yyrhs[yyprhs[yyx->yyrule]+yyi-1]));
1284 else
1285 YYFPRINTF (stderr, "%*s%s <tokens %d .. %d>\n", yyindent+2, "",
1286 yytokenName (yyrhs[yyprhs[yyx->yyrule]+yyi-1]),
1287 yystates[yyi-1]->yyposn+1, yystates[yyi]->yyposn);
1288 }
1289 else
1290 yyreportTree (yystates[yyi]->yysemantics.yyfirstVal, yyindent+2);
1291 }
1292 }
1293 #endif
1294
1295 static void
1296 yyreportAmbiguity (yySemanticOption* yyx0, yySemanticOption* yyx1,
1297 yyGLRStack* yystack)
1298 {
1299 /* `Unused' warnings. */
1300 (void) yyx0;
1301 (void) yyx1;
1302
1303 #if YYDEBUG
1304 YYFPRINTF (stderr, "Ambiguity detected.\n");
1305 YYFPRINTF (stderr, "Option 1,\n");
1306 yyreportTree (yyx0, 2);
1307 YYFPRINTF (stderr, "\nOption 2,\n");
1308 yyreportTree (yyx1, 2);
1309 YYFPRINTF (stderr, "\n");
1310 #endif
1311 yyFail (yystack, "ambiguity detected");
1312 }
1313
1314
1315 /** Resolve the ambiguity represented by OPTIONLIST, perform the indicated
1316 * actions, and return the result. */
1317 static YYRESULTTAG
1318 yyresolveValue (yySemanticOption* yyoptionList, yyGLRStack* yystack,
1319 YYSTYPE* yyvalp, YYLTYPE* yylocp)
1320 {
1321 yySemanticOption* yybest;
1322 yySemanticOption* yyp;
1323 int yymerge;
1324
1325 yybest = yyoptionList;
1326 yymerge = 0;
1327 for (yyp = yyoptionList->yynext; yyp != NULL; yyp = yyp->yynext)
1328 {
1329 if (yyidenticalOptions (yybest, yyp))
1330 yymergeOptionSets (yybest, yyp);
1331 else
1332 switch (yypreference (yybest, yyp))
1333 {
1334 case 0:
1335 yyreportAmbiguity (yybest, yyp, yystack);
1336 break;
1337 case 1:
1338 yymerge = 1;
1339 break;
1340 case 2:
1341 break;
1342 case 3:
1343 yybest = yyp;
1344 yymerge = 0;
1345 break;
1346 }
1347 }
1348
1349 if (yymerge)
1350 {
1351 int yyprec = yydprec[yybest->yyrule];
1352 YYCHK (yyresolveAction (yybest, yystack, yyvalp, yylocp));
1353 for (yyp = yybest->yynext; yyp != NULL; yyp = yyp->yynext)
1354 {
1355 if (yyprec == yydprec[yyp->yyrule])
1356 {
1357 YYSTYPE yyval1;
1358 YYLTYPE yydummy;
1359 YYCHK (yyresolveAction (yyp, yystack, &yyval1, &yydummy));
1360 *yyvalp = yyuserMerge (yymerger[yyp->yyrule], yyvalp, &yyval1);
1361 }
1362 }
1363 return yyok;
1364 }
1365 else
1366 return yyresolveAction (yybest, yystack, yyvalp, yylocp);
1367 }
1368
1369 static YYRESULTTAG
1370 yyresolveStack (yyGLRStack* yystack)
1371 {
1372 if (yystack->yysplitPoint != NULL)
1373 {
1374 yyGLRState* yys;
1375 int yyn;
1376
1377 for (yyn = 0, yys = yystack->yytops.yystates[0];
1378 yys != yystack->yysplitPoint;
1379 yys = yys->yypred, yyn += 1)
1380 ;
1381 YYCHK (yyresolveStates (yystack->yytops.yystates[0], yyn, yystack));
1382 }
1383 return yyok;
1384 }
1385
1386 static void
1387 yycompressStack (yyGLRStack* yystack)
1388 {
1389 yyGLRState* yyp, *yyq, *yyr;
1390
1391 if (yystack->yytops.yysize != 1 || yystack->yysplitPoint == NULL)
1392 return;
1393
1394 for (yyp = yystack->yytops.yystates[0], yyq = yyp->yypred, yyr = NULL;
1395 yyp != yystack->yysplitPoint;
1396 yyr = yyp, yyp = yyq, yyq = yyp->yypred)
1397 yyp->yypred = yyr;
1398
1399 yystack->yyspaceLeft += yystack->yynextFree - yystack->yyitems;
1400 yystack->yynextFree = ((yyGLRStackItem*) yystack->yysplitPoint) + 1;
1401 yystack->yyspaceLeft -= yystack->yynextFree - yystack->yyitems;
1402 yystack->yysplitPoint = NULL;
1403 yystack->yylastDeleted = NULL;
1404
1405 while (yyr != NULL)
1406 {
1407 yystack->yynextFree->yystate = *yyr;
1408 yyr = yyr->yypred;
1409 yystack->yynextFree->yystate.yypred = & yystack->yynextFree[-1].yystate;
1410 yystack->yytops.yystates[0] = &yystack->yynextFree->yystate;
1411 yystack->yynextFree += 1;
1412 yystack->yyspaceLeft -= 1;
1413 }
1414 }
1415
1416 static YYRESULTTAG
1417 yyprocessOneStack (yyGLRStack* yystack, int yyk,
1418 size_t yyposn, YYSTYPE* yylvalp, YYLTYPE* yyllocp)
1419 {
1420 int yyaction;
1421 const short* yyconflicts;
1422 yyRuleNum yyrule;
1423 yySymbol* const yytokenp = yystack->yytokenp;
1424
1425 while (yystack->yytops.yystates[yyk] != NULL)
1426 {
1427 yyStateNum yystate = yystack->yytops.yystates[yyk]->yylrState;
1428
1429 assert (yystate != YYFINAL);
1430 if (yyisDefaultedState (yystate))
1431 {
1432 yyrule = yydefaultAction (yystate);
1433 if (yyrule == 0)
1434 {
1435 YYDPRINTF ((stderr, "Stack %d dies.\n", yyk));
1436 yymarkStackDeleted (yystack, yyk);
1437 return yyok;
1438 }
1439 YYCHK (yyglrReduce (yystack, yyk, yyrule, yyfalse));
1440 }
1441 else
1442 {
1443 if (*yytokenp == YYEMPTY)
1444 {
1445 yychar = YYLEX;
1446 *yytokenp = YYTRANSLATE(yychar);
1447 YYDPRINTF ((stderr, "Read token %s\n", yytokenName (*yytokenp)));
1448 }
1449 yygetLRActions (yystate, *yytokenp, &yyaction, &yyconflicts);
1450
1451 while (*yyconflicts != 0)
1452 {
1453 int yynewStack = yysplitStack (yystack, yyk);
1454 YYDPRINTF ((stderr, "Splitting off stack %d from %d.\n",
1455 yynewStack, yyk));
1456 YYCHK (yyglrReduce (yystack, yynewStack, *yyconflicts, yyfalse));
1457 YYCHK (yyprocessOneStack (yystack, yynewStack, yyposn,
1458 yylvalp, yyllocp));
1459 yyconflicts += 1;
1460 }
1461
1462 if (yyisShiftAction (yyaction))
1463 {
1464 YYDPRINTF ((stderr, "Shifted token %s on stack %d, ",
1465 yytokenName (*yytokenp), yyk));
1466 yyglrShift (yystack, yyk, yyaction, yyposn+1, *yylvalp, yyllocp);
1467 YYDPRINTF ((stderr, "which is now in state #%d\n",
1468 yystack->yytops.yystates[yyk]->yylrState));
1469 break;
1470 }
1471 else if (yyisErrorAction (yyaction))
1472 {
1473 YYDPRINTF ((stderr, "Stack %d dies.\n", yyk));
1474 yymarkStackDeleted (yystack, yyk);
1475 break;
1476 }
1477 else
1478 YYCHK (yyglrReduce (yystack, yyk, -yyaction, yyfalse));
1479 }
1480 }
1481 return yyok;
1482 }
1483
1484 static void
1485 yyreportParseError (yyGLRStack* yystack, YYSTYPE* yylvalp, YYLTYPE* yyllocp)
1486 {
1487 /* `Unused' warnings. */
1488 (void) yylvalp;
1489 (void) yyllocp;
1490
1491 if (yystack->yyerrState == 0)
1492 {
1493 #if YYERROR_VERBOSE
1494 yySymbol* const yytokenp = yystack->yytokenp;
1495 int yyn, yyx, yycount, yysize;
1496 const char* yyprefix;
1497 char* yyp;
1498 char* yymsg;
1499 yyn = yypact[yystack->yytops.yystates[0]->yylrState];
1500 if (yyn > YYFLAG && yyn < YYLAST)
1501 {
1502 yycount = 0;
1503 /* Start YYX at -YYN if negative to avoid negative indexes in
1504 YYCHECK. */
1505 yysize = sizeof ("parse error, unexpected ")
1506 + strlen (yytokenName (*yytokenp));
1507 yyprefix = ", expecting ";
1508 for (yyx = yyn < 0 ? -yyn : 0; yyx < yytname_size && yycount <= 5;
1509 yyx += 1)
1510 if (yycheck[yyx + yyn] == yyx)
1511 yysize += strlen (yytokenName (yyx)) + strlen (yyprefix),
1512 yycount += 1, yyprefix = " or ";
1513 yymsg = yyp = (char*) malloc (yysize);
1514 yyp += sprintf (yyp, "parse error, unexpected %s",
1515 yytokenName (*yytokenp));
1516 if (yycount < 5)
1517 {
1518 yyprefix = ", expecting ";
1519 for (yyx = yyn < 0 ? -yyn : 0; yyx < yytname_size; yyx += 1)
1520 if (yycheck[yyx + yyn] == yyx)
1521 {
1522 yyp += sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx));
1523 yyprefix = " or ";
1524 }
1525 }
1526 yyerror (yymsg);
1527 free (yymsg);
1528 }
1529 else
1530 #endif
1531 yyerror ("parse error");
1532 yynerrs += 1;
1533 }
1534 }
1535
1536 /* Recover from a syntax error on STACK, assuming that TOKENP,
1537 YYLVALP, and YYLLOCP point to the syntactic category, semantic
1538 value, and location of the lookahead.
1539 NOTE: This uses the panic-mode recovery algorithm described in the
1540 Bison documentation, which differs from what is in bison.simple.
1541 Specifically, this routine performs no reductions before shifting
1542 the error token. */
1543 static void
1544 yyrecoverParseError (yyGLRStack* yystack, YYSTYPE* yylvalp, YYLTYPE* yyllocp)
1545 {
1546 yySymbol* const yytokenp = yystack->yytokenp;
1547 size_t yyk;
1548 int yyj;
1549
1550 if (yystack->yyerrState == 0)
1551 yystack->yyerrState = 3;
1552 else if (yystack->yyerrState == 3)
1553 {
1554 /* We just shifted the error token and (perhaps) took some
1555 reductions. Skip tokens until we can proceed. */
1556 do {
1557 if (*yytokenp == YYEOF)
1558 yyFail (yystack, NULL);
1559 if (*yytokenp != YYEMPTY)
1560 YYDPRINTF ((stderr, "Discarding token %s\n",
1561 yytokenName (*yytokenp)));
1562 yychar = YYLEX;
1563 *yytokenp = YYTRANSLATE (yychar);
1564 YYDPRINTF ((stderr, "Read token %s\n", yytokenName (*yytokenp)));
1565 yyj = yypact[yystack->yytops.yystates[0]->yylrState];
1566 if (yyj == YYFLAG)
1567 /* Something's not right; we shouldn't be here */
1568 yyFail (yystack, NULL);
1569 yyj += *yytokenp;
1570 if (yyj < 0 || yyj > YYLAST || yycheck[yyj] != *yytokenp)
1571 {
1572 if (yydefact[yystack->yytops.yystates[0]->yylrState] != 0)
1573 return;
1574 }
1575 else if (yytable[yyj] != 0 && yytable[yyj] != YYFLAG)
1576 return;
1577 } while (yytrue);
1578 }
1579
1580 /* Reduce to one stack */
1581 for (yyk = 0; yyk < yystack->yytops.yysize; yyk += 1)
1582 if (yystack->yytops.yystates[yyk] != NULL)
1583 break;
1584 if (yyk >= yystack->yytops.yysize)
1585 yyFail (yystack, NULL);
1586 for (yyk += 1; yyk < yystack->yytops.yysize; yyk += 1)
1587 yymarkStackDeleted (yystack, yyk);
1588 yyremoveDeletes (yystack);
1589 yycompressStack (yystack);
1590
1591 /* Now pop stack until we find a state that shifts the error token. */
1592 while (yystack->yytops.yystates[0] != NULL)
1593 {
1594 yyj = yypact[yystack->yytops.yystates[0]->yylrState] + YYTERROR;
1595 if (yyj != YYFLAG + YYTERROR && yyj >= 0 && yyj <= YYLAST &&
1596 yycheck[yyj] == YYTERROR && yyisShiftAction (yytable[yyj]))
1597 {
1598 yyglrShift (yystack, 0, yytable[yyj],
1599 yystack->yytops.yystates[0]->yyposn, *yylvalp, yyllocp);
1600 break;
1601 }
1602 yystack->yytops.yystates[0] = yystack->yytops.yystates[0]->yypred;
1603 yystack->yynextFree -= 1;
1604 yystack->yyspaceLeft += 1;
1605 }
1606 if (yystack->yytops.yystates[0] == NULL)
1607 yyFail (yystack, NULL);
1608 }
1609
1610 #define YYCHK1(YYE) \
1611 do { \
1612 switch (YYE) { \
1613 default: \
1614 break; \
1615 case yyabort: \
1616 yystack.yyerrflag = 1; \
1617 goto yyDone; \
1618 case yyaccept: \
1619 yystack.yyerrflag = 0; \
1620 goto yyDone; \
1621 case yyerr: \
1622 goto yyuser_error; \
1623 } \
1624 } while (0)
1625
1626 int
1627 yyparse (YYPARSE_PARAM_ARG)
1628 {
1629 yySymbol yytoken;
1630 yyGLRStack yystack;
1631 size_t yyposn;
1632 ]b4_pure_if(
1633 [
1634 YYSTYPE yylval;
1635 YYLTYPE yylloc;
1636 #undef yychar
1637 #define yychar (yystack.yyrawchar)
1638 ])[
1639
1640 YYSTYPE* const yylvalp = &yylval;
1641 YYLTYPE* const yyllocp = &yylloc;
1642
1643 yyinitGLRStack (&yystack, YYINITDEPTH);
1644 yystack.yytokenp = &yytoken;
1645
1646 if (setjmp (yystack.yyexception_buffer) != 0)
1647 goto yyDone;
1648
1649 yyglrShift (&yystack, 0, 0, 0, yyval_default, &yyloc_default);
1650 yytoken = YYEMPTY;
1651 yyposn = 0;
1652
1653 while (yytrue)
1654 {
1655 /* For efficiency, we have two loops, of which the first of which
1656 * is specialized to deterministic operation (single stack, no
1657 * potential ambiguity). */
1658
1659 /* Standard mode */
1660 while (yytrue)
1661 {
1662 yyRuleNum yyrule;
1663 int yyaction;
1664 const short* yyconflicts;
1665
1666 yyStateNum yystate = yystack.yytops.yystates[0]->yylrState;
1667 if (yystate == YYFINAL)
1668 goto yyDone;
1669 if (yyisDefaultedState (yystate))
1670 {
1671 yyrule = yydefaultAction (yystate);
1672 if (yyrule == 0)
1673 {
1674 yyreportParseError (&yystack, yylvalp, yyllocp);
1675 goto yyuser_error;
1676 }
1677 YYCHK1 (yyglrReduce (&yystack, 0, yyrule, yytrue));
1678 }
1679 else
1680 {
1681 if (yytoken == YYEMPTY)
1682 {
1683 yychar = YYLEX;
1684 yytoken = YYTRANSLATE (yychar);
1685 YYDPRINTF ((stderr, "Read token %s\n",
1686 yytokenName (yytoken)));
1687 }
1688 yygetLRActions (yystate, yytoken, &yyaction, &yyconflicts);
1689 if (*yyconflicts != 0)
1690 break;
1691 if (yyisShiftAction (yyaction))
1692 {
1693 YYDPRINTF ((stderr, "Shifted token %s. ",
1694 yytokenName (yytoken)));
1695 if (yytoken != YYEOF)
1696 yytoken = YYEMPTY;
1697 yyposn += 1;
1698 yyglrShift (&yystack, 0, yyaction, yyposn, yylval, yyllocp);
1699 if (yystack.yyerrState > 0)
1700 yystack.yyerrState -= 1;
1701 YYDPRINTF ((stderr, "Now in state #%d\n",
1702 yystack.yytops.yystates[0]->yylrState));
1703 }
1704 else if (yyisErrorAction (yyaction))
1705 {
1706 yyreportParseError (&yystack, yylvalp, yyllocp);
1707 goto yyuser_error;
1708 }
1709 else
1710 YYCHK1 (yyglrReduce (&yystack, 0, -yyaction, yytrue));
1711 }
1712 }
1713
1714 while (yytrue)
1715 {
1716 int yys;
1717 int yyn = yystack.yytops.yysize;
1718 for (yys = 0; yys < yyn; yys += 1)
1719 YYCHK1 (yyprocessOneStack (&yystack, yys, yyposn,
1720 yylvalp, yyllocp));
1721 yytoken = YYEMPTY;
1722 yyposn += 1;
1723 yyremoveDeletes (&yystack);
1724 if (yystack.yytops.yysize == 0)
1725 {
1726 yyundeleteLastStack (&yystack);
1727 if (yystack.yytops.yysize == 0)
1728 yyFail (&yystack, "parse error");
1729 YYCHK1 (yyresolveStack (&yystack));
1730 YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
1731 yyreportParseError (&yystack, yylvalp, yyllocp);
1732 goto yyuser_error;
1733 }
1734 else if (yystack.yytops.yysize == 1)
1735 {
1736 YYCHK1 (yyresolveStack (&yystack));
1737 YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
1738 yycompressStack (&yystack);
1739 break;
1740 }
1741 }
1742 continue;
1743 yyuser_error:
1744 yyrecoverParseError (&yystack, yylvalp, yyllocp);
1745 yyposn = yystack.yytops.yystates[0]->yyposn;
1746 }
1747 yyDone:
1748 ;
1749
1750 yyfreeGLRStack (&yystack);
1751 return yystack.yyerrflag;
1752 }
1753
1754 /* DEBUGGING ONLY */
1755 static void yypstack (yyGLRStack* yystack, int yyk) ATTRIBUTE_UNUSED;
1756 static void yypdumpstack (yyGLRStack* yystack) ATTRIBUTE_UNUSED;
1757
1758 static void
1759 yypstates (yyGLRState* yyst)
1760 {
1761 static void
1762 yy_yypstack (yyGLRState* yys)
1763 {
1764 if (yys->yypred == NULL)
1765 fprintf (stderr, "%d@%d", yys->yylrState, yys->yyposn);
1766 else
1767 {
1768 yy_yypstack (yys->yypred);
1769 fprintf (stderr, " -> %d@%d", yys->yylrState, yys->yyposn);
1770 }
1771 }
1772
1773 if (yyst == NULL)
1774 fprintf (stderr, "<null>");
1775 else
1776 yy_yypstack (yyst);
1777 fprintf (stderr, "\n");
1778 }
1779
1780 static void
1781 yypstack (yyGLRStack* yystack, int yyk)
1782 {
1783 yypstates (yystack->yytops.yystates[yyk]);
1784 }
1785
1786 #define YYINDEX(YYX) \
1787 ((YYX) == NULL ? -1 : (yyGLRStackItem*) (YYX) - yystack->yyitems)
1788
1789
1790 static void
1791 yypdumpstack (yyGLRStack* yystack)
1792 {
1793 yyGLRStackItem* yyp;
1794 size_t yyi;
1795 for (yyp = yystack->yyitems; yyp < yystack->yynextFree; yyp += 1)
1796 {
1797 fprintf (stderr, "%3d. ", yyp - yystack->yyitems);
1798 if (*(bool*) yyp)
1799 {
1800 fprintf (stderr, "Res: %d, LR State: %d, posn: %d, pred: %d",
1801 yyp->yystate.yyresolved, yyp->yystate.yylrState,
1802 yyp->yystate.yyposn,
1803 YYINDEX(yyp->yystate.yypred));
1804 if (! yyp->yystate.yyresolved)
1805 fprintf (stderr, ", firstVal: %d",
1806 YYINDEX (yyp->yystate.yysemantics.yyfirstVal));
1807 }
1808 else
1809 {
1810 fprintf (stderr, "Option. rule: %d, state: %d, next: %d",
1811 yyp->yyoption.yyrule, YYINDEX (yyp->yyoption.yystate),
1812 YYINDEX (yyp->yyoption.yynext));
1813 }
1814 fprintf (stderr, "\n");
1815 }
1816 fprintf (stderr, "Tops:");
1817 for (yyi = 0; yyi < yystack->yytops.yysize; yyi += 1)
1818 fprintf (stderr, "%d: %d; ", yyi, YYINDEX (yystack->yytops.yystates[yyi]));
1819 fprintf (stderr, "\n");
1820 }
1821
1822 ]
1823
1824 b4_epilogue
1825 m4_if(b4_defines_flag, 0, [],
1826 [#output "b4_output_header_name"
1827 b4_copyright([Skeleton parser for GLR parsing with Bison], [2002])
1828 #ifndef b4_header_guard
1829 # define b4_header_guard
1830
1831 b4_token_defines(b4_tokens)
1832
1833 #ifndef YYSTYPE
1834 m4_ifdef([b4_stype],
1835 [#line b4_stype_line "b4_filename"
1836 typedef union b4_stype yystype;
1837 /* Line __line__ of __file__. */
1838 #line __oline__ "__ofile__"],
1839 [typedef int yystype;])
1840 # define YYSTYPE yystype
1841 #endif
1842
1843 b4_pure_if([],
1844 [extern YYSTYPE b4_prefix[]lval;])
1845
1846 b4_location_if(
1847 [#ifndef YYLTYPE
1848 typedef struct yyltype
1849 {
1850 int yyfirst_line;
1851 int yyfirst_column;
1852 int yylast_line;
1853 int yylast_column;
1854 } yyltype;
1855 # define YYLTYPE yyltype
1856 #endif
1857
1858 m4_if(b4_pure, [0],
1859 [extern YYLTYPE b4_prefix[]lloc;])
1860 ])
1861 #endif /* not b4_header_guard */
1862 ])