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