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