Import byacc-20121003.
[dragonfly.git] / contrib / byacc / skeleton.c
1 /* $Id: skeleton.c,v 1.31 2011/09/07 09:37:59 tom Exp $ */
2
3 #include "defs.h"
4
5 /*  The definition of yysccsid in the banner should be replaced with    */
6 /*  a #pragma ident directive if the target C compiler supports         */
7 /*  #pragma ident directives.                                           */
8 /*                                                                      */
9 /*  If the skeleton is changed, the banner should be changed so that    */
10 /*  the altered version can be easily distinguished from the original.  */
11 /*                                                                      */
12 /*  The #defines included with the banner are there because they are    */
13 /*  useful in subsequent code.  The macros #defined in the header or    */
14 /*  the body either are not useful outside of semantic actions or       */
15 /*  are conditional.                                                    */
16
17 const char *const banner[] =
18 {
19     "#ifndef lint",
20     "static const char yysccsid[] = \"@(#)yaccpar       1.9 (Berkeley) 02/21/93\";",
21     "#endif",
22     "",
23     "#define YYBYACC 1",
24     CONCAT1("#define YYMAJOR ", YYMAJOR),
25     CONCAT1("#define YYMINOR ", YYMINOR),
26 #ifdef YYPATCH
27     CONCAT1("#define YYPATCH ", YYPATCH),
28 #endif
29     "",
30     "#define YYEMPTY        (-1)",
31     "#define yyclearin      (yychar = YYEMPTY)",
32     "#define yyerrok        (yyerrflag = 0)",
33     "#define YYRECOVERING() (yyerrflag != 0)",
34     "",
35     0
36 };
37
38 const char *const xdecls[] =
39 {
40     "",
41     "extern int YYPARSE_DECL();",
42     0
43 };
44
45 const char *const tables[] =
46 {
47     "extern short yylhs[];",
48     "extern short yylen[];",
49     "extern short yydefred[];",
50     "extern short yydgoto[];",
51     "extern short yysindex[];",
52     "extern short yyrindex[];",
53     "extern short yygindex[];",
54     "extern short yytable[];",
55     "extern short yycheck[];",
56     "",
57     "#if YYDEBUG",
58     "extern char *yyname[];",
59     "extern char *yyrule[];",
60     "#endif",
61     0
62 };
63
64 const char *const global_vars[] =
65 {
66     "",
67     "int      yydebug;",
68     "int      yynerrs;",
69     0
70 };
71
72 const char *const impure_vars[] =
73 {
74     "",
75     "int      yyerrflag;",
76     "int      yychar;",
77     "YYSTYPE  yyval;",
78     "YYSTYPE  yylval;",
79     0
80 };
81
82 const char *const hdr_defs[] =
83 {
84     "",
85     "/* define the initial stack-sizes */",
86     "#ifdef YYSTACKSIZE",
87     "#undef YYMAXDEPTH",
88     "#define YYMAXDEPTH  YYSTACKSIZE",
89     "#else",
90     "#ifdef YYMAXDEPTH",
91     "#define YYSTACKSIZE YYMAXDEPTH",
92     "#else",
93     "#define YYSTACKSIZE 500",
94     "#define YYMAXDEPTH  500",
95     "#endif",
96     "#endif",
97     "",
98     "#define YYINITSTACKSIZE 500",
99     "",
100     "typedef struct {",
101     "    unsigned stacksize;",
102     "    short    *s_base;",
103     "    short    *s_mark;",
104     "    short    *s_last;",
105     "    YYSTYPE  *l_base;",
106     "    YYSTYPE  *l_mark;",
107     "} YYSTACKDATA;",
108     0
109 };
110
111 const char *const hdr_vars[] =
112 {
113     "/* variables for the parser stack */",
114     "static YYSTACKDATA yystack;",
115     0
116 };
117
118 const char *const body_vars[] =
119 {
120     "    int      yyerrflag;",
121     "    int      yychar;",
122     "    YYSTYPE  yyval;",
123     "    YYSTYPE  yylval;",
124     "",
125     "    /* variables for the parser stack */",
126     "    YYSTACKDATA yystack;",
127     0
128 };
129
130 const char *const body_1[] =
131 {
132     "",
133     "#if YYDEBUG",
134     "#include <stdio.h>         /* needed for printf */",
135     "#endif",
136     "",
137     "#include <stdlib.h>        /* needed for malloc, etc */",
138     "#include <string.h>        /* needed for memset */",
139     "",
140     "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
141     "static int yygrowstack(YYSTACKDATA *data)",
142     "{",
143     "    int i;",
144     "    unsigned newsize;",
145     "    short *newss;",
146     "    YYSTYPE *newvs;",
147     "",
148     "    if ((newsize = data->stacksize) == 0)",
149     "        newsize = YYINITSTACKSIZE;",
150     "    else if (newsize >= YYMAXDEPTH)",
151     "        return -1;",
152     "    else if ((newsize *= 2) > YYMAXDEPTH)",
153     "        newsize = YYMAXDEPTH;",
154     "",
155     "    i = data->s_mark - data->s_base;",
156     "    newss = (short *)realloc(data->s_base, newsize * sizeof(*newss));",
157     "    if (newss == 0)",
158     "        return -1;",
159     "",
160     "    data->s_base = newss;",
161     "    data->s_mark = newss + i;",
162     "",
163     "    newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs));",
164     "    if (newvs == 0)",
165     "        return -1;",
166     "",
167     "    data->l_base = newvs;",
168     "    data->l_mark = newvs + i;",
169     "",
170     "    data->stacksize = newsize;",
171     "    data->s_last = data->s_base + newsize - 1;",
172     "    return 0;",
173     "}",
174     "",
175     "#if YYPURE || defined(YY_NO_LEAKS)",
176     "static void yyfreestack(YYSTACKDATA *data)",
177     "{",
178     "    free(data->s_base);",
179     "    free(data->l_base);",
180     "    memset(data, 0, sizeof(*data));",
181     "}",
182     "#else",
183     "#define yyfreestack(data) /* nothing */",
184     "#endif",
185     "",
186     "#define YYABORT  goto yyabort",
187     "#define YYREJECT goto yyabort",
188     "#define YYACCEPT goto yyaccept",
189     "#define YYERROR  goto yyerrlab",
190     "",
191     "int",
192     "YYPARSE_DECL()",
193     "{",
194     0
195 };
196
197 const char *const body_2[] =
198 {
199     "    int yym, yyn, yystate;",
200     "#if YYDEBUG",
201     "    const char *yys;",
202     "",
203     "    if ((yys = getenv(\"YYDEBUG\")) != 0)",
204     "    {",
205     "        yyn = *yys;",
206     "        if (yyn >= '0' && yyn <= '9')",
207     "            yydebug = yyn - '0';",
208     "    }",
209     "#endif",
210     "",
211     "    yynerrs = 0;",
212     "    yyerrflag = 0;",
213     "    yychar = YYEMPTY;",
214     "    yystate = 0;",
215     "",
216     "#if YYPURE",
217     "    memset(&yystack, 0, sizeof(yystack));",
218     "#endif",
219     "",
220     "    if (yystack.s_base == NULL && yygrowstack(&yystack)) goto yyoverflow;",
221     "    yystack.s_mark = yystack.s_base;",
222     "    yystack.l_mark = yystack.l_base;",
223     "    yystate = 0;",
224     "    *yystack.s_mark = 0;",
225     "",
226     "yyloop:",
227     "    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;",
228     "    if (yychar < 0)",
229     "    {",
230     "        if ((yychar = YYLEX) < 0) yychar = 0;",
231     "#if YYDEBUG",
232     "        if (yydebug)",
233     "        {",
234     "            yys = 0;",
235     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
236     "            if (!yys) yys = \"illegal-symbol\";",
237     "            printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
238     "                    YYPREFIX, yystate, yychar, yys);",
239     "        }",
240     "#endif",
241     "    }",
242     "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
243     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
244     "    {",
245     "#if YYDEBUG",
246     "        if (yydebug)",
247     "            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
248     "                    YYPREFIX, yystate, yytable[yyn]);",
249     "#endif",
250     "        if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))",
251     "        {",
252     "            goto yyoverflow;",
253     "        }",
254     "        yystate = yytable[yyn];",
255     "        *++yystack.s_mark = yytable[yyn];",
256     "        *++yystack.l_mark = yylval;",
257     "        yychar = YYEMPTY;",
258     "        if (yyerrflag > 0)  --yyerrflag;",
259     "        goto yyloop;",
260     "    }",
261     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
262     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
263     "    {",
264     "        yyn = yytable[yyn];",
265     "        goto yyreduce;",
266     "    }",
267     "    if (yyerrflag) goto yyinrecovery;",
268     "",
269     0
270 };
271
272 const char *const body_3[] =
273 {
274     "",
275     "    goto yyerrlab;",
276     "",
277     "yyerrlab:",
278     "    ++yynerrs;",
279     "",
280     "yyinrecovery:",
281     "    if (yyerrflag < 3)",
282     "    {",
283     "        yyerrflag = 3;",
284     "        for (;;)",
285     "        {",
286     "            if ((yyn = yysindex[*yystack.s_mark]) && (yyn += YYERRCODE) >= 0 &&",
287     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
288     "            {",
289     "#if YYDEBUG",
290     "                if (yydebug)",
291     "                    printf(\"%sdebug: state %d, error recovery shifting\\",
292     " to state %d\\n\", YYPREFIX, *yystack.s_mark, yytable[yyn]);",
293     "#endif",
294     "                if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))",
295     "                {",
296     "                    goto yyoverflow;",
297     "                }",
298     "                yystate = yytable[yyn];",
299     "                *++yystack.s_mark = yytable[yyn];",
300     "                *++yystack.l_mark = yylval;",
301     "                goto yyloop;",
302     "            }",
303     "            else",
304     "            {",
305     "#if YYDEBUG",
306     "                if (yydebug)",
307     "                    printf(\"%sdebug: error recovery discarding state %d\
308 \\n\",",
309     "                            YYPREFIX, *yystack.s_mark);",
310     "#endif",
311     "                if (yystack.s_mark <= yystack.s_base) goto yyabort;",
312     "                --yystack.s_mark;",
313     "                --yystack.l_mark;",
314     "            }",
315     "        }",
316     "    }",
317     "    else",
318     "    {",
319     "        if (yychar == 0) goto yyabort;",
320     "#if YYDEBUG",
321     "        if (yydebug)",
322     "        {",
323     "            yys = 0;",
324     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
325     "            if (!yys) yys = \"illegal-symbol\";",
326     "            printf(\"%sdebug: state %d, error recovery discards token %d\
327  (%s)\\n\",",
328     "                    YYPREFIX, yystate, yychar, yys);",
329     "        }",
330     "#endif",
331     "        yychar = YYEMPTY;",
332     "        goto yyloop;",
333     "    }",
334     "",
335     "yyreduce:",
336     "#if YYDEBUG",
337     "    if (yydebug)",
338     "        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
339     "                YYPREFIX, yystate, yyn, yyrule[yyn]);",
340     "#endif",
341     "    yym = yylen[yyn];",
342     "    if (yym)",
343     "        yyval = yystack.l_mark[1-yym];",
344     "    else",
345     "        memset(&yyval, 0, sizeof yyval);",
346     "    switch (yyn)",
347     "    {",
348     0
349 };
350
351 const char *const trailer[] =
352 {
353     "    }",
354     "    yystack.s_mark -= yym;",
355     "    yystate = *yystack.s_mark;",
356     "    yystack.l_mark -= yym;",
357     "    yym = yylhs[yyn];",
358     "    if (yystate == 0 && yym == 0)",
359     "    {",
360     "#if YYDEBUG",
361     "        if (yydebug)",
362     "            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
363     " state %d\\n\", YYPREFIX, YYFINAL);",
364     "#endif",
365     "        yystate = YYFINAL;",
366     "        *++yystack.s_mark = YYFINAL;",
367     "        *++yystack.l_mark = yyval;",
368     "        if (yychar < 0)",
369     "        {",
370     "            if ((yychar = YYLEX) < 0) yychar = 0;",
371     "#if YYDEBUG",
372     "            if (yydebug)",
373     "            {",
374     "                yys = 0;",
375     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
376     "                if (!yys) yys = \"illegal-symbol\";",
377     "                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
378     "                        YYPREFIX, YYFINAL, yychar, yys);",
379     "            }",
380     "#endif",
381     "        }",
382     "        if (yychar == 0) goto yyaccept;",
383     "        goto yyloop;",
384     "    }",
385     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
386     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
387     "        yystate = yytable[yyn];",
388     "    else",
389     "        yystate = yydgoto[yym];",
390     "#if YYDEBUG",
391     "    if (yydebug)",
392     "        printf(\"%sdebug: after reduction, shifting from state %d \\",
393     "to state %d\\n\", YYPREFIX, *yystack.s_mark, yystate);",
394     "#endif",
395     "    if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))",
396     "    {",
397     "        goto yyoverflow;",
398     "    }",
399     "    *++yystack.s_mark = (short) yystate;",
400     "    *++yystack.l_mark = yyval;",
401     "    goto yyloop;",
402     "",
403     "yyoverflow:",
404     0
405 };
406
407 const char *const trailer_2[] =
408 {
409     "",
410     "yyabort:",
411     "    yyfreestack(&yystack);",
412     "    return (1);",
413     "",
414     "yyaccept:",
415     "    yyfreestack(&yystack);",
416     "    return (0);",
417     "}",
418     0
419 };
420
421 void
422 write_section(FILE * fp, const char *const section[])
423 {
424     int c;
425     int i;
426     const char *s;
427
428     for (i = 0; (s = section[i]) != 0; ++i)
429     {
430         while ((c = *s) != 0)
431         {
432             putc(c, fp);
433             ++s;
434         }
435         if (fp == code_file)
436             ++outline;
437         putc('\n', fp);
438     }
439 }