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