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