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