# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
+
+## ---------------- ##
+## Default values. ##
+## ---------------- ##
+
+# Stack parameters.
+m4_define_default([b4_stack_depth_max], [10000])
+m4_define_default([b4_stack_depth_init], [200])
+
+# Location type.
+m4_define_default([b4_location_type], [yyltype])
+
+# Accumule in b4_lex_param all the yylex arguments.
+# Yes, this is quite ugly...
+m4_define([b4_lex_param],
+m4_dquote(b4_pure_if([[[[YYSTYPE *]], [[yylvalp]]][]dnl
+b4_location_if([, [[YYLTYPE *], [yyllocp]]])])dnl
+m4_ifdef([b4_lex_param], [, ]b4_lex_param)))
+
+# Yes, this is quite ugly...
+ m4_define_default([b4_parse_param])
+m4_ifdef([b4_parse_param],
+[m4_define([b4_parse_param],
+ b4_parse_param)])
+
+
+
+## ----------------- ##
+## Semantic Values. ##
+## ----------------- ##
+
+
# b4_lhs_value([TYPE])
# --------------------
# Expansion of $<TYPE>$.
## Locations. ##
## ----------- ##
-# b4_location_if(IF-TRUE, IF-FALSE)
-# ---------------------------------
-# Expand IF-TRUE, if locations are used, IF-FALSE otherwise.
-m4_define([b4_location_if],
-[m4_if(b4_locations_flag, [1],
- [$1],
- [$2])])
-
-
# b4_lhs_location()
# -----------------
# Expansion of @$.
-## -------------- ##
-## %pure-parser. ##
-## -------------- ##
-
-# b4_pure_if(IF-TRUE, IF-FALSE)
-# -----------------------------
-# Expand IF-TRUE, if %pure-parser, IF-FALSE otherwise.
-m4_define([b4_pure_if],
-[m4_if(b4_pure, [1],
- [$1],
- [$2])])
-
-
## ------------------- ##
## Output file names. ##
## ------------------- ##
[[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]], [_])])
+# We do want M4 expansion after # for CPP macros.
+m4_changecom()
m4_divert(0)dnl
#output "b4_output_parser_name"
b4_copyright([Skeleton parser for GLR parsing with Bison], [2002])
#ifndef YYLTYPE
typedef struct yyltype
{
-]b4_location_if([
int first_line;
int first_column;
int last_line;
- int last_column;])[
+ int last_column;
} yyltype;
-# define YYLTYPE ]b4_ltype[
+# define YYLTYPE ]b4_location_type[
# define YYLTYPE_IS_TRIVIAL 1
#endif
/* YYFINAL -- State number of the termination state. */
#define YYFINAL ]b4_final_state_number[
+/* YYLAST -- Last index in YYTABLE. */
#define YYLAST ]b4_last[
/* YYNTOKENS -- Number of terminals. */
};
\f
-/* The user can define YYPARSE_PARAM as the name of an argument to be passed
- into yyparse. The argument should have type void *.
- It should actually point to an object.
- Grammar actions can access the variable by casting it
- to the proper pointer type. */
-
-#ifdef YYPARSE_PARAM
-# define YYPARSE_PARAM_ARG void *YYPARSE_PARAM
-#else /* !YYPARSE_PARAM */
-# define YYPARSE_PARAM_ARG void
-#endif /* !YYPARSE_PARAM */
-
-/* Prevent warning if -Wstrict-prototypes. */
-#ifdef __GNUC__
-# ifdef YYPARSE_PARAM
-int yyparse (void *);
-# else
-int yyparse (void);
-# endif
-#endif
+/* Prevent warning if -Wmissing-prototypes. */
+]b4_c_ansi_function_decl([yyparse], [int], b4_parse_param)
+
+m4_ifset([b4_parse_param],
+[#define YY_USER_FORMALS , b4_c_ansi_formals(b4_parse_param)
+#define YY_USER_ARGS , b4_c_args(b4_parse_param)],
+[#define YY_USER_FORMALS
+#define YY_USER_ARGS])
+
-/* Error token number */
+[/* Error token number */
#define YYTERROR 1
/* YYLLOC_DEFAULT -- Compute the default location (before the actions
#endif
/* YYLEX -- calling `yylex' with the right arguments. */
+#define YYLEX ]b4_c_function_call([yylex], [int], b4_lex_param)[
]b4_pure_if(
[
-#ifdef YYLEX_PARAM
-# define YYLEX yylex (yylvalp, b4_location_if([yyllocp, ])YYLEX_PARAM)
-#else
-# define YYLEX yylex (yylvalp[]b4_location_if([, yyllocp]))
-#endif],
-[#define YYLEX yylex ()])
-
-b4_pure_if(
-[
#undef yynerrs
#define yynerrs (yystack->yyerrcnt)
#undef yychar
/* YYINITDEPTH -- initial size of the parser's stacks. */
#ifndef YYINITDEPTH
-# define YYINITDEPTH ]b4_initdepth[
+# define YYINITDEPTH ]b4_stack_depth_init[
#endif
/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
#endif
#ifndef YYMAXDEPTH
-# define YYMAXDEPTH ]b4_maxdepth[
+# define YYMAXDEPTH ]b4_stack_depth_max[
#endif
/* Minimum number of free items on the stack allowed after an
* yyerr for YYERROR, yyabort for YYABORT. */
static YYRESULTTAG
yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp,
- YYSTYPE* yyvalp, YYLTYPE* yylocp, yyGLRStack* yystack)
+ YYSTYPE* yyvalp, YYLTYPE* yylocp, yyGLRStack* yystack
+ YY_USER_FORMALS)
{
/* Avoid `unused' warnings in there are no $n. */
(void) yystack;
static inline bool
yyisShiftAction (int yyaction)
{
- return yyaction > 0;
+ return 0 < yyaction;
}
static inline bool
size_t yysize, yynewSize;
size_t yyn;
yysize = yystack->yynextFree - yystack->yyitems;
- if (yysize >= YYMAXDEPTH)
+ if (YYMAXDEPTH <= yysize)
yyFail (yystack, "parsing stack overflow (%d items)", yysize);
yynewSize = 2*yysize;
- if (yynewSize > YYMAXDEPTH)
+ if (YYMAXDEPTH < yynewSize)
yynewSize = YYMAXDEPTH;
yyinitGLRStack (&yynewStack, yynewSize);
for (yyp0 = yystack->yyitems, yyp1 = yynewStack.yyitems, yyn = yysize;
- yyn > 0;
+ 0 < yyn;
yyn -= 1, yyp0 += 1, yyp1 += 1)
{
*yyp1 = *yyp0;
* for userAction. */
static inline int
yydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule,
- YYSTYPE* yyvalp, YYLTYPE* yylocp)
+ YYSTYPE* yyvalp, YYLTYPE* yylocp YY_USER_FORMALS)
{
int yynrhs = yyrhsLength (yyrule);
*yyvalp = rhs[1-yynrhs].yystate.yysemantics.yysval;
*yylocp = rhs[1-yynrhs].yystate.yyloc;
}
- return yyuserAction (yyrule, yynrhs, rhs, yyvalp, yylocp, yystack);
+ return yyuserAction (yyrule, yynrhs, rhs,
+ yyvalp, yylocp, yystack YY_USER_ARGS);
}
else
{
int yyi;
yyGLRState* yys;
yyGLRStackItem yyrhsVals[YYMAXRHS];
- for (yyi = yynrhs-1, yys = yystack->yytops.yystates[yyk]; yyi >= 0;
+ for (yyi = yynrhs-1, yys = yystack->yytops.yystates[yyk]; 0 <= yyi;
yyi -= 1, yys = yys->yypred)
{
assert (yys->yypred != NULL);
*yylocp = yyrhsVals[0].yystate.yyloc;
}
return yyuserAction (yyrule, yynrhs, yyrhsVals + (yynrhs-1),
- yyvalp, yylocp, yystack);
+ yyvalp, yylocp, yystack YY_USER_ARGS);
}
}
+#if !YYDEBUG
+# define YY_REDUCE_PRINT(K, Rule)
+#else
+# define YY_REDUCE_PRINT(K, Rule) \
+do { \
+ if (yydebug) \
+ yy_reduce_print (K, Rule); \
+} while (0)
+
+/*----------------------------------------------------------.
+| Report that the RULE is going to be reduced on stack #K. |
+`----------------------------------------------------------*/
+
+static inline void
+yy_reduce_print (size_t yyk, yyRuleNum yyrule)
+{
+ int yyi;
+ YYDPRINTF ((stderr, "Reducing stack %d by rule %d (line %d),",
+ yyk, yyrule - 1, yyrline[yyrule]));
+ /* Print the symbols being reduced, and their result. */
+ for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++)
+ YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi]));
+ YYFPRINTF (stderr, " -> %s\n", yytokenName (yyr1[yyrule]));
+}
+#endif
+
/** Pop items off stack #K of STACK according to grammar rule RULE,
* and push back on the resulting nonterminal symbol. Perform the
* semantic action associated with RULE and store its value with the
*/
static inline YYRESULTTAG
yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule,
- bool yyforceEval)
+ bool yyforceEval YY_USER_FORMALS)
{
size_t yyposn = yystack->yytops.yystates[yyk]->yyposn;
YYSTYPE yysval;
YYLTYPE yyloc;
-#if YYDEBUG
- if (yydebug)
- {
- int yyi;
- YYDPRINTF ((stderr, "Reducing stack %d by rule %d (line %d),",
- yyk, yyrule - 1, yyrline[yyrule]));
- /* Print the symbols being reduced, and their result. */
- for (yyi = yyprhs[yyrule]; yyrhs[yyi] >= 0; yyi++)
- YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi]));
- YYFPRINTF (stderr, " -> %s\n", yytokenName (yyr1[yyrule]));
- }
-#endif
- YYCHK (yydoAction (yystack, yyk, yyrule, &yysval, &yyloc));
+ YY_REDUCE_PRINT (yyk, yyrule);
+ YYCHK (yydoAction (yystack, yyk, yyrule, &yysval, &yyloc YY_USER_ARGS));
yyglrShift (yystack, yyk,
yyLRgotoState (yystack->yytops.yystates[yyk]->yylrState,
yylhsNonterm (yyrule)),
yyStateNum yynewLRState;
for (yys = yystack->yytops.yystates[yyk], yyn = yyrhsLength (yyrule);
- yyn > 0; yyn -= 1)
+ 0 < yyn; yyn -= 1)
{
yys = yys->yypred;
assert (yys != NULL);
return 0;
if (p0 < p1)
return 3;
- if (p0 > p1)
+ if (p1 < p0)
return 2;
return 0;
}
static YYRESULTTAG yyresolveValue (yySemanticOption* yyoptionList,
yyGLRStack* yystack, YYSTYPE* yyvalp,
- YYLTYPE* yylocp);
+ YYLTYPE* yylocp YY_USER_FORMALS);
static YYRESULTTAG
-yyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack)
+yyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack YY_USER_FORMALS)
{
YYRESULTTAG yyflag;
- if (yyn > 0)
+ if (0 < yyn)
{
assert (yys->yypred != NULL);
- yyflag = yyresolveStates (yys->yypred, yyn-1, yystack);
+ yyflag = yyresolveStates (yys->yypred, yyn-1, yystack YY_USER_ARGS);
if (yyflag != yyok)
return yyflag;
if (! yys->yyresolved)
{
yyflag = yyresolveValue (yys->yysemantics.yyfirstVal, yystack,
- &yys->yysemantics.yysval, &yys->yyloc);
+ &yys->yysemantics.yysval, &yys->yyloc
+ YY_USER_ARGS);
if (yyflag != yyok)
return yyflag;
yys->yyresolved = yytrue;
static YYRESULTTAG
yyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack,
- YYSTYPE* yyvalp, YYLTYPE* yylocp)
+ YYSTYPE* yyvalp, YYLTYPE* yylocp YY_USER_FORMALS)
{
yyGLRStackItem yyrhsVals[YYMAXRHS];
int yynrhs, yyi;
yyGLRState* yys;
yynrhs = yyrhsLength (yyopt->yyrule);
- YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack));
- for (yyi = yynrhs-1, yys = yyopt->yystate; yyi >= 0;
+ YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack YY_USER_ARGS));
+ for (yyi = yynrhs-1, yys = yyopt->yystate; 0 <= yyi;
yyi -= 1, yys = yys->yypred)
{
assert (yys->yypred != NULL);
yyrhsVals[yyi].yystate.yyloc = yys->yyloc;
}
return yyuserAction (yyopt->yyrule, yynrhs, yyrhsVals + (yynrhs-1),
- yyvalp, yylocp, yystack);
+ yyvalp, yylocp, yystack YY_USER_ARGS);
}
#if YYDEBUG
-/* FIXME: How can we initialize the lloc bit? */
-static yyGLRState YYLEFTMOST_STATE = { 0, 0, -1, NULL, 0, { NULL } };
-
-static void yyreportTree (yySemanticOption* yyx, int yyindent)
+static void
+yyreportTree (yySemanticOption* yyx, int yyindent)
{
int yynrhs = yyrhsLength (yyx->yyrule);
int yyi;
yyGLRState* yys;
yyGLRState* yystates[YYMAXRHS];
+ yyGLRState yyleftmost_state;
- for (yyi = yynrhs, yys = yyx->yystate; yyi > 0; yyi -= 1, yys = yys->yypred)
+ for (yyi = yynrhs, yys = yyx->yystate; 0 < yyi; yyi -= 1, yys = yys->yypred)
yystates[yyi] = yys;
if (yys == NULL)
- yystates[0] = &YYLEFTMOST_STATE;
+ {
+ yyleftmost_state.yyposn = 0;
+ yystates[0] = &yyleftmost_state;
+ }
else
yystates[0] = yys;
- if (yys->yyposn+1 > yyx->yystate->yyposn)
+ if (yyx->yystate->yyposn < yys->yyposn + 1)
YYFPRINTF (stderr, "%*s%s -> <Rule %d, empty>\n",
yyindent, "", yytokenName (yylhsNonterm (yyx->yyrule)),
yyx->yyrule);
* actions, and return the result. */
static YYRESULTTAG
yyresolveValue (yySemanticOption* yyoptionList, yyGLRStack* yystack,
- YYSTYPE* yyvalp, YYLTYPE* yylocp)
+ YYSTYPE* yyvalp, YYLTYPE* yylocp YY_USER_FORMALS)
{
yySemanticOption* yybest;
yySemanticOption* yyp;
if (yymerge)
{
int yyprec = yydprec[yybest->yyrule];
- YYCHK (yyresolveAction (yybest, yystack, yyvalp, yylocp));
+ YYCHK (yyresolveAction (yybest, yystack, yyvalp, yylocp YY_USER_ARGS));
for (yyp = yybest->yynext; yyp != NULL; yyp = yyp->yynext)
{
if (yyprec == yydprec[yyp->yyrule])
{
YYSTYPE yyval1;
YYLTYPE yydummy;
- YYCHK (yyresolveAction (yyp, yystack, &yyval1, &yydummy));
+ YYCHK (yyresolveAction (yyp, yystack, &yyval1, &yydummy YY_USER_ARGS));
*yyvalp = yyuserMerge (yymerger[yyp->yyrule], yyvalp, &yyval1);
}
}
return yyok;
}
else
- return yyresolveAction (yybest, yystack, yyvalp, yylocp);
+ return yyresolveAction (yybest, yystack, yyvalp, yylocp YY_USER_ARGS);
}
static YYRESULTTAG
-yyresolveStack (yyGLRStack* yystack)
+yyresolveStack (yyGLRStack* yystack YY_USER_FORMALS)
{
if (yystack->yysplitPoint != NULL)
{
yys != yystack->yysplitPoint;
yys = yys->yypred, yyn += 1)
;
- YYCHK (yyresolveStates (yystack->yytops.yystates[0], yyn, yystack));
+ YYCHK (yyresolveStates (yystack->yytops.yystates[0], yyn, yystack
+ YY_USER_ARGS));
}
return yyok;
}
static YYRESULTTAG
yyprocessOneStack (yyGLRStack* yystack, int yyk,
- size_t yyposn, YYSTYPE* yylvalp, YYLTYPE* yyllocp)
+ size_t yyposn, YYSTYPE* yylvalp, YYLTYPE* yyllocp
+ YY_USER_FORMALS)
{
int yyaction;
const short* yyconflicts;
yymarkStackDeleted (yystack, yyk);
return yyok;
}
- YYCHK (yyglrReduce (yystack, yyk, yyrule, yyfalse));
+ YYCHK (yyglrReduce (yystack, yyk, yyrule, yyfalse YY_USER_ARGS));
}
else
{
int yynewStack = yysplitStack (yystack, yyk);
YYDPRINTF ((stderr, "Splitting off stack %d from %d.\n",
yynewStack, yyk));
- YYCHK (yyglrReduce (yystack, yynewStack, *yyconflicts, yyfalse));
+ YYCHK (yyglrReduce (yystack, yynewStack,
+ *yyconflicts, yyfalse YY_USER_ARGS));
YYCHK (yyprocessOneStack (yystack, yynewStack, yyposn,
- yylvalp, yyllocp));
+ yylvalp, yyllocp YY_USER_ARGS));
yyconflicts += 1;
}
break;
}
else
- YYCHK (yyglrReduce (yystack, yyk, -yyaction, yyfalse));
+ YYCHK (yyglrReduce (yystack, yyk, -yyaction, yyfalse YY_USER_ARGS));
}
}
return yyok;
yysize += strlen (yytokenName (yyx)) + strlen (yyprefix),
yycount += 1, yyprefix = " or ";
yymsg = yyp = (char*) malloc (yysize);
- yyp += sprintf (yyp, "parse error, unexpected %s",
- yytokenName (*yytokenp));
+ sprintf (yyp, "parse error, unexpected %s",
+ yytokenName (*yytokenp));
+ yyp += strlen (yyp);
if (yycount < 5)
{
yyprefix = ", expecting ";
for (yyx = yyn < 0 ? -yyn : 0; yyx < yytname_size; yyx += 1)
if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
{
- yyp += sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx));
+ sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx));
+ yyp += strlen (yyp);
yyprefix = " or ";
}
}
} \
} while (0)
-int
-yyparse (YYPARSE_PARAM_ARG)
+
+/*----------.
+| yyparse. |
+`----------*/
+
+]b4_c_ansi_function_def([yyparse], [int], b4_parse_param)[
{
yySymbol yytoken;
yyGLRStack yystack;
while (yytrue)
{
- /* For efficiency, we have two loops, of which the first of which
- * is specialized to deterministic operation (single stack, no
- * potential ambiguity). */
+ /* For efficiency, we have two loops, the first of which is
+ specialized to deterministic operation (single stack, no
+ potential ambiguity). */
/* Standard mode */
while (yytrue)
yyreportParseError (&yystack, yylvalp, yyllocp);
goto yyuser_error;
}
- YYCHK1 (yyglrReduce (&yystack, 0, yyrule, yytrue));
+ YYCHK1 (yyglrReduce (&yystack, 0, yyrule, yytrue YY_USER_ARGS));
}
else
{
yytoken = YYEMPTY;
yyposn += 1;
yyglrShift (&yystack, 0, yyaction, yyposn, yylval, yyllocp);
- if (yystack.yyerrState > 0)
+ if (0 < yystack.yyerrState)
yystack.yyerrState -= 1;
YYDPRINTF ((stderr, "Entering state %d\n",
yystack.yytops.yystates[0]->yylrState));
goto yyuser_error;
}
else
- YYCHK1 (yyglrReduce (&yystack, 0, -yyaction, yytrue));
+ YYCHK1 (yyglrReduce (&yystack, 0, -yyaction, yytrue YY_USER_ARGS));
}
}
int yyn = yystack.yytops.yysize;
for (yys = 0; yys < yyn; yys += 1)
YYCHK1 (yyprocessOneStack (&yystack, yys, yyposn,
- yylvalp, yyllocp));
+ yylvalp, yyllocp YY_USER_ARGS));
yytoken = YYEMPTY;
yyposn += 1;
yyremoveDeletes (&yystack);
yyundeleteLastStack (&yystack);
if (yystack.yytops.yysize == 0)
yyFail (&yystack, "parse error");
- YYCHK1 (yyresolveStack (&yystack));
+ YYCHK1 (yyresolveStack (&yystack YY_USER_ARGS));
YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
yyreportParseError (&yystack, yylvalp, yyllocp);
goto yyuser_error;
}
else if (yystack.yytops.yysize == 1)
{
- YYCHK1 (yyresolveStack (&yystack));
+ YYCHK1 (yyresolveStack (&yystack YY_USER_ARGS));
YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
yycompressStack (&yystack);
break;
(long) YYINDEX (yystack->yytops.yystates[yyi]));
fprintf (stderr, "\n");
}
-
]
b4_epilogue