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