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