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