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