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