X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/ca98bf57582952c898808b61e32bc8e2b30bc4b0..366eea36d3a9aaccf80a78c2fcd8c3cd6b563e25:/data/bison.simple diff --git a/data/bison.simple b/data/bison.simple index d54d3a27..906f3139 100644 --- a/data/bison.simple +++ b/data/bison.simple @@ -33,6 +33,20 @@ m4_define([b4_rhs_value], [yyvsp@<:@m4_eval([$2 - $1])@:>@m4_ifval([$3], [.$3])]) + +## ----------- ## +## 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 @$. @@ -48,6 +62,24 @@ m4_define([b4_rhs_location], [yylsp@<:@m4_eval([$2 - $1])@:>@]) + +## -------------- ## +## %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. ## +## ------------------- ## + m4_define_default([b4_input_suffix], [.y]) m4_define_default([b4_output_parser_suffix], @@ -171,10 +203,7 @@ m4_if(b4_prefix[], [yy], [], #define yychar b4_prefix[]char #define yydebug b4_prefix[]debug #define yynerrs b4_prefix[]nerrs -#if YYLSP_NEEDED -# define yylloc b4_prefix[]lloc -#endif -]) +b4_location_if([#define yylloc b4_prefix[]lloc])]) /* Copy the first part of user declarations. */ b4_pre_prologue @@ -264,9 +293,8 @@ union yyalloc { short yyss; YYSTYPE yyvs; -# if YYLSP_NEEDED - YYLTYPE yyls; -# endif + b4_location_if([ YYLTYPE yyls; +])dnl }; /* The size of the maximum gap between one aligned stack and the next. */ @@ -274,15 +302,13 @@ union yyalloc /* The size of an array large to enough to hold all stacks, each with N elements. */ -# if YYLSP_NEEDED -# define YYSTACK_BYTES(N) \ +b4_location_if( +[# define YYSTACK_BYTES(N) \ ((N) * (sizeof (short) + sizeof (YYSTYPE) + sizeof (YYLTYPE)) \ - + 2 * YYSTACK_GAP_MAX) -# else -# define YYSTACK_BYTES(N) \ + + 2 * YYSTACK_GAP_MAX)], +[# define YYSTACK_BYTES(N) \ ((N) * (sizeof (short) + sizeof (YYSTYPE)) \ - + YYSTACK_GAP_MAX) -# endif + + YYSTACK_GAP_MAX)]) /* Copy COUNT objects from FROM to TO. The source and destination do not overlap. */ @@ -509,23 +535,13 @@ while (0) /* YYLEX -- calling `yylex' with the right arguments. */ -#if YYPURE -# if YYLSP_NEEDED -# ifdef YYLEX_PARAM -# define YYLEX yylex (&yylval, &yylloc, YYLEX_PARAM) -# else -# define YYLEX yylex (&yylval, &yylloc) -# endif -# else /* !YYLSP_NEEDED */ -# ifdef YYLEX_PARAM -# define YYLEX yylex (&yylval, YYLEX_PARAM) -# else -# define YYLEX yylex (&yylval) -# endif -# endif /* !YYLSP_NEEDED */ -#else /* !YYPURE */ -# define YYLEX yylex () -#endif /* !YYPURE */ +b4_pure_if( +[#ifdef YYLEX_PARAM +# define YYLEX yylex (&yylval, b4_location_if([&yylloc, ])YYLEX_PARAM) +#else +# define YYLEX yylex (&yylval, b4_location_if([&yylloc, ])) +#endif], +[#define YYLEX yylex ()]) /* Enable debugging if requested. */ #if YYDEBUG @@ -651,47 +667,43 @@ int yyparse (void *); int yyparse (void); # endif #endif -static void yydestructor (int symbol_type, YYSTYPE symbol_value); - -/* YY_DECL_VARIABLES -- depending whether we use a pure parser, - variables are global, or local to YYPARSE. */ - -#define YY_DECL_NON_LSP_VARIABLES \ -/* The lookahead symbol. */ \ -int yychar; \ - \ -/* The semantic value of the lookahead symbol. */ \ -YYSTYPE yylval; \ - \ -/* Number of parse errors so far. */ \ -int yynerrs; - -#if YYLSP_NEEDED -# define YY_DECL_VARIABLES \ -YY_DECL_NON_LSP_VARIABLES \ - \ -/* Location data for the lookahead symbol. */ \ -YYLTYPE yylloc; -#else -# define YY_DECL_VARIABLES \ -YY_DECL_NON_LSP_VARIABLES + +#if defined (__STDC__) || defined (__cplusplus) +static void yydestructor (int yytype, + YYSTYPE yyvalue[]b4_location_if([, YYLTYPE yylocation])); +# if YYDEBUG +static void yysymprint (FILE* out, int yytype, + YYSTYPE yyvalue[]b4_location_if([, YYLTYPE yylocation])); +# endif #endif -/* If nonreentrant, generate the variables here. */ +m4_divert_push([KILL])# ======================== M4 code. +# b4_declare_parser_variables +# --------------------------- +# Declare the variables that are global, or local to YYPARSE if +# pure-parser +m4_define([b4_declare_parser_variables], +[/* The lookahead symbol. */ +int yychar; + +/* The semantic value of the lookahead symbol. */ +YYSTYPE yylval; + +/* Number of parse errors so far. */ +int yynerrs;b4_location_if([ +/* Location data for the lookahead symbol. */ +YYLTYPE yylloc;]) +]) +m4_divert_pop([KILL])dnl# ====================== End of M4 code. -#if !YYPURE -YY_DECL_VARIABLES -#endif /* !YYPURE */ +b4_pure_if([], + [b4_declare_parser_variables]) int yyparse (YYPARSE_PARAM_ARG) YYPARSE_PARAM_DECL {[ - /* If reentrant, generate the variables here. */ -#if YYPURE - YY_DECL_VARIABLES -#endif /* !YYPURE */ - + ]b4_pure_if([b4_declare_parser_variables])[ register int yystate; register int yyn; int yyresult; @@ -718,27 +730,20 @@ yyparse (YYPARSE_PARAM_ARG) YYSTYPE *yyvs = yyvsa; register YYSTYPE *yyvsp; -#if YYLSP_NEEDED - /* The location stack. */ +]b4_location_if( +[[ /* The location stack. */ YYLTYPE yylsa[YYINITDEPTH]; YYLTYPE *yyls = yylsa; - YYLTYPE *yylsp; -#endif + YYLTYPE *yylsp;]])[ -#if YYLSP_NEEDED -# define YYPOPSTACK (yyvsp--, yyssp--, yylsp--) -#else -# define YYPOPSTACK (yyvsp--, yyssp--) -#endif +#define YYPOPSTACK (yyvsp--, yyssp--]b4_location_if([, yylsp--])[) YYSIZE_T yystacksize = YYINITDEPTH; /* The variables used to return semantic value and location from the action routines. */ YYSTYPE yyval; -#if YYLSP_NEEDED - YYLTYPE yyloc; -#endif +]b4_location_if([ YYLTYPE yyloc;])[ /* When reducing, the number of symbols on the RHS of the reduced rule. */ @@ -758,9 +763,7 @@ yyparse (YYPARSE_PARAM_ARG) yyssp = yyss; yyvsp = yyvs; -#if YYLSP_NEEDED - yylsp = yyls; -#endif +]b4_location_if([ yylsp = yyls;])[ goto yysetstate; /*------------------------------------------------------------. @@ -787,25 +790,18 @@ yyparse (YYPARSE_PARAM_ARG) memory. */ YYSTYPE *yyvs1 = yyvs; short *yyss1 = yyss; +]b4_location_if([ YYLTYPE *yyls1 = yyls;])[ /* Each stack pointer address is followed by the size of the - data in use in that stack, in bytes. */ -# if YYLSP_NEEDED - YYLTYPE *yyls1 = yyls; - /* This used to be a conditional around just the two extra args, - but that might be undefined if yyoverflow is a macro. */ - yyoverflow ("parser stack overflow", - &yyss1, yysize * sizeof (*yyssp), - &yyvs1, yysize * sizeof (*yyvsp), - &yyls1, yysize * sizeof (*yylsp), - &yystacksize); - yyls = yyls1; -# else + data in use in that stack, in bytes. This used to be a + conditional around just the two extra args, but that might + be undefined if yyoverflow is a macro. */ yyoverflow ("parser stack overflow", &yyss1, yysize * sizeof (*yyssp), &yyvs1, yysize * sizeof (*yyvsp), +]b4_location_if([ &yyls1, yysize * sizeof (*yylsp),])[ &yystacksize); -# endif +]b4_location_if([ yyls = yyls1;])[ yyss = yyss1; yyvs = yyvs1; } @@ -828,9 +824,7 @@ yyparse (YYPARSE_PARAM_ARG) goto yyoverflowlab; YYSTACK_RELOCATE (yyss); YYSTACK_RELOCATE (yyvs); -# if YYLSP_NEEDED - YYSTACK_RELOCATE (yyls); -# endif +]b4_location_if([ YYSTACK_RELOCATE (yyls);])[ # undef YYSTACK_RELOCATE if (yyss1 != yyssa) YYSTACK_FREE (yyss1); @@ -840,9 +834,7 @@ yyparse (YYPARSE_PARAM_ARG) yyssp = yyss + yysize - 1; yyvsp = yyvs + yysize - 1; -#if YYLSP_NEEDED - yylsp = yyls + yysize - 1; -#endif +]b4_location_if([ yylsp = yyls + yysize - 1;])[ YYDPRINTF ((stderr, "Stack size increased to %lu\n", (unsigned long int) yystacksize)); @@ -899,14 +891,9 @@ yybackup: which are defined only if `YYDEBUG' is set. */ if (yydebug) { - YYFPRINTF (stderr, "Next token is %d (%s", - yychar, yytname[yychar1]); - /* Give the individual parser a way to print the precise - meaning of a token, for further debugging info. */ -# ifdef YYPRINT - YYPRINT (stderr, yychar, yylval); -# endif - YYFPRINTF (stderr, ")\n"); + YYFPRINTF (stderr, "Next token is "); + yysymprint (stderr, yychar1, yylval]b4_location_if([, yyloc])[); + YYFPRINTF (stderr, "\n"); } #endif } @@ -946,9 +933,7 @@ yybackup: yychar = YYEMPTY; *++yyvsp = yylval; -#if YYLSP_NEEDED - *++yylsp = yylloc; -#endif +]b4_location_if([ *++yylsp = yylloc;])[ /* Count tokens shifted since error; after three, turn off error status. */ @@ -986,10 +971,9 @@ yyreduce: GCC warning that YYVAL may be used uninitialized. */ yyval = yyvsp[1-yylen]; -#if YYLSP_NEEDED - /* Default location. */ - YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen); -#endif +]b4_location_if( +[ /* Default location. */ + YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen);])[ #if YYDEBUG /* We have to keep this `#if YYDEBUG', since we use variables which @@ -1017,9 +1001,7 @@ yyreduce: [ yyvsp -= yylen; yyssp -= yylen; -#if YYLSP_NEEDED - yylsp -= yylen; -#endif +]b4_location_if([ yylsp -= yylen;])[ #if YYDEBUG if (yydebug) @@ -1033,9 +1015,7 @@ yyreduce: #endif *++yyvsp = yyval; -#if YYLSP_NEEDED - *++yylsp = yyloc; -#endif +]b4_location_if([ *++yylsp = yyloc;])[ /* Now `shift' the result of the reduction. Determine what state that goes to, based on the state we popped back to and the rule @@ -1132,24 +1112,14 @@ yyerrlab1: #if YYDEBUG if (yydebug) { - if (yystos[*yyssp] < YYNTOKENS) - { - YYFPRINTF (stderr, "Error: popping token %d (%s", - yytoknum[yystos[*yyssp]], - yytname[yystos[*yyssp]]); -# ifdef YYPRINT - YYPRINT (stderr, yytoknum[yystos[*yyssp]], *yyvsp); -# endif - YYFPRINTF (stderr, ")\n"); - } - else - { - YYFPRINTF (stderr, "Error: popping nonterminal (%s)\n", - yytname[yystos[*yyssp]]); - } + YYFPRINTF (stderr, "Error: popping "); + yysymprint (stderr, + yystos[*yyssp], + *yyvsp]b4_location_if([, *yylsp])[); + YYFPRINTF (stderr, "\n"); } #endif - yydestructor (yystos[*yyssp], *yyvsp); + yydestructor (yystos[*yyssp], *yyvsp]b4_location_if([, *yylsp])[); YYPOPSTACK; } YYABORT; @@ -1157,7 +1127,7 @@ yyerrlab1: YYDPRINTF ((stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1])); - yydestructor (yychar1, yylval); + yydestructor (yychar1, yylval]b4_location_if([, yylloc])[); yychar = YYEMPTY; } @@ -1187,29 +1157,18 @@ yyerrlab1: #if YYDEBUG if (yydebug) { - if (yystos[yystate] < YYNTOKENS) - { - YYFPRINTF (stderr, "Error: popping token %d (%s", - yytoknum[yystos[yystate]], yytname[yystos[yystate]]); -# ifdef YYPRINT - YYPRINT (stderr, yytoknum[yystos[yystate]], *yyvsp); -# endif - YYFPRINTF (stderr, ")\n"); - } - else - { - YYFPRINTF (stderr, "Error: popping nonterminal (%s)\n", - yytname[yystos[yystate]]); - } + YYFPRINTF (stderr, "Error: popping "); + yysymprint (stderr, + yystos[*yyssp], + *yyvsp]b4_location_if([, *yylsp])[); + YYFPRINTF (stderr, "\n"); } #endif - yydestructor (yystos[yystate], *yyvsp); + yydestructor (yystos[yystate], *yyvsp]b4_location_if([, *yylsp])[); yyvsp--; yystate = *--yyssp; -#if YYLSP_NEEDED - yylsp--; -#endif +]b4_location_if([ yylsp--;])[ #if YYDEBUG if (yydebug) @@ -1229,9 +1188,7 @@ yyerrlab1: YYDPRINTF ((stderr, "Shifting error token, ")); *++yyvsp = yylval; -#if YYLSP_NEEDED - *++yylsp = yylloc; -#endif +]b4_location_if([ *++yylsp = yylloc;])[ yystate = yyn; goto yynewstate; @@ -1270,44 +1227,80 @@ yyreturn: ]} -/*-------------------------------------------------. -| Release the memory associated to SYMBOL-NUMBER. | -`-------------------------------------------------*/ +/*-----------------------------------------------. +| Release the memory associated to this symbol. | +`-----------------------------------------------*/ m4_divert_push([KILL])# M4 code. -# b4_eval -# ------- -# FIXME: This is really wrong, we no longer guarantee we don't evaluate -# the user's input. This demonstrates that decoding actions (BRACED_CODE) -# ought to be done when output, not when read. -m4_define([b4_eval], -[$*]) - # b4_symbol_destructor(SYMBOL-NUMBER, DESTRUCTOR, TYPE-NAME) # ---------------------------------------------------------- m4_define([b4_symbol_destructor], -[m4_pushdef([b4_dollar_dollar], [symbol_value.$6])dnl +[m4_pushdef([b4_dollar_dollar], [yyvalue.$6])dnl +m4_pushdef([b4_at_dollar], [yylocation])dnl case $4: /* $3 */ #line $2 "$1" - b4_eval($5); + $5; #line __oline__ "__ofile__" break; +m4_popdef([b4_at_dollar])dnl m4_popdef([b4_dollar_dollar])]) m4_divert_pop([KILL])dnl# End of M4 code. static void -yydestructor (int symbol_type, YYSTYPE symbol_value) +yydestructor (int yytype, + YYSTYPE yyvalue[]b4_location_if([, YYLTYPE yylocation])) { - switch (symbol_type) + switch (yytype) { m4_map([b4_symbol_destructor], m4_defn([b4_symbol_destructors]))dnl default: - YYDPRINTF ((stderr, "yydestructor: unknown symbol type: %d (%s)\n", - symbol_type, yytname[[symbol_type]])); break; } } + +#if YYDEBUG +/*---------------------------. +| Print this symbol on OUT. | +`---------------------------*/ + +m4_divert_push([KILL])# M4 code. +# b4_symbol_printer(SYMBOL-NUMBER, PRINTER, TYPE-NAME) +# ---------------------------------------------------------- +m4_define([b4_symbol_printer], +[m4_pushdef([b4_dollar_dollar], [yyvalue.$6])dnl +m4_pushdef([b4_at_dollar], [yylocation])dnl + case $4: /* $3 */ +#line $2 "$1" + $5; +#line __oline__ "__ofile__" + break; +m4_popdef([b4_at_dollar])dnl +m4_popdef([b4_dollar_dollar])]) + +m4_divert_pop([KILL])dnl# End of M4 code. +static void +yysymprint (FILE* out, int yytype, + YYSTYPE yyvalue[]b4_location_if([, YYLTYPE yylocation])) +{ + if (yytype < YYNTOKENS) + YYFPRINTF (out, "token %d (%s ", yytoknum[[yytype]], yytname[[yytype]]); + else + YYFPRINTF (out, "nterm %s (", yytname[[yytype]]); +# ifdef YYPRINT + YYPRINT (out, yytype, yyvalue); +# else + switch (yytype) + { +m4_map([b4_symbol_printer], m4_defn([b4_symbol_printers]))dnl + default: + break; + } +# endif /* !defined YYPRINT. */ + YYFPRINTF (out, ")"); +} +#endif /* YYDEBUG. */ + b4_epilogue m4_if(b4_defines_flag, 0, [], [#output "b4_output_header_name" @@ -1326,10 +1319,10 @@ typedef union b4_stype yystype; # define YYSTYPE yystype #endif -m4_if(b4_pure, [0], +b4_pure_if([], [extern YYSTYPE b4_prefix[]lval;]) -m4_if(b4_locations_flag, [0], [], +b4_location_if( [#ifndef YYLTYPE typedef struct yyltype {