xref: /openbsd-src/usr.bin/yacc/skeleton.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: skeleton.c,v 1.29 2008/07/08 15:06:50 otto Exp $	*/
2 /*	$NetBSD: skeleton.c,v 1.10 1996/03/25 00:36:18 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1989 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Robert Paul Corbett.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)skeleton.c	5.8 (Berkeley) 4/29/95";
39 #else
40 static char rcsid[] = "$NetBSD: skeleton.c,v 1.10 1996/03/25 00:36:18 mrg Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include "defs.h"
45 
46 /*  The definition of yysccsid in the banner should be replaced with	*/
47 /*  a #pragma ident directive if the target C compiler supports		*/
48 /*  #pragma ident directives.						*/
49 /*									*/
50 /*  If the skeleton is changed, the banner should be changed so that	*/
51 /*  the altered version can be easily distinguished from the original.	*/
52 /*									*/
53 /*  The #defines included with the banner are there because they are	*/
54 /*  useful in subsequent code.  The macros #defined in the header or	*/
55 /*  the body either are not useful outside of semantic actions or	*/
56 /*  are conditional.							*/
57 
58 char *banner[] =
59 {
60     "#ifndef lint",
61     "/*static char yysccsid[] = \"from: @(#)yaccpar	1.9 (Berkeley) 02/21/93\";*/",
62     "static char yyrcsid[]",
63     "#if __GNUC__ >= 2",
64     "  __attribute__ ((unused))",
65     "#endif /* __GNUC__ >= 2 */",
66     "  = \"$OpenBSD: skeleton.c,v 1.29 2008/07/08 15:06:50 otto Exp $\";",
67     "#endif",
68     "#include <stdlib.h>",
69     "#include <string.h>",
70     "#define YYBYACC 1",
71     "#define YYMAJOR 1",
72     "#define YYMINOR 9",
73     "#define YYLEX yylex()",
74     "#define YYEMPTY -1",
75     "#define yyclearin (yychar=(YYEMPTY))",
76     "#define yyerrok (yyerrflag=0)",
77     "#define YYRECOVERING() (yyerrflag!=0)",
78     0
79 };
80 
81 
82 char *tables[] =
83 {
84     "#if defined(__cplusplus) || defined(__STDC__)",
85     "extern const short yylhs[];",
86     "extern const short yylen[];",
87     "extern const short yydefred[];",
88     "extern const short yydgoto[];",
89     "extern const short yysindex[];",
90     "extern const short yyrindex[];",
91     "extern const short yygindex[];",
92     "extern const short yytable[];",
93     "extern const short yycheck[];",
94     "#if YYDEBUG",
95     "extern const char *const yyname[];",
96     "extern const char *const yyrule[];",
97     "#endif",
98     "#else /* !(defined(__cplusplus) || defined(__STDC__)) */",
99     "extern short yylhs[];",
100     "extern short yylen[];",
101     "extern short yydefred[];",
102     "extern short yydgoto[];",
103     "extern short yysindex[];",
104     "extern short yyrindex[];",
105     "extern short yygindex[];",
106     "extern short yytable[];",
107     "extern short yycheck[];",
108     "#if YYDEBUG",
109     "extern char *yyname[];",
110     "extern char *yyrule[];",
111     "#endif /* YYDEBUG */",
112     "#endif /* !(defined(__cplusplus) || defined(__STDC__)) */",
113     0
114 };
115 
116 
117 char *header[] =
118 {
119     "#ifdef YYSTACKSIZE",
120     "#undef YYMAXDEPTH",
121     "#define YYMAXDEPTH YYSTACKSIZE",
122     "#else",
123     "#ifdef YYMAXDEPTH",
124     "#define YYSTACKSIZE YYMAXDEPTH",
125     "#else",
126     "#define YYSTACKSIZE 10000",
127     "#define YYMAXDEPTH 10000",
128     "#endif",
129     "#endif",
130     "#define YYINITSTACKSIZE 200",
131     "/* LINTUSED */",
132     "int yydebug;",
133     "int yynerrs;",
134     "int yyerrflag;",
135     "int yychar;",
136     "short *yyssp;",
137     "YYSTYPE *yyvsp;",
138     "YYSTYPE yyval;",
139     "YYSTYPE yylval;",
140     "short *yyss;",
141     "short *yysslim;",
142     "YYSTYPE *yyvs;",
143     "int yystacksize;",
144     0
145 };
146 
147 
148 char *body[] =
149 {
150     "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
151     "#if defined(__cplusplus) || defined(__STDC__)",
152     "static int yygrowstack(void)",
153     "#else",
154     "static int yygrowstack()",
155     "#endif",
156     "{",
157     "    int newsize, i;",
158     "    short *newss;",
159     "    YYSTYPE *newvs;",
160     "",
161     "    if ((newsize = yystacksize) == 0)",
162     "        newsize = YYINITSTACKSIZE;",
163     "    else if (newsize >= YYMAXDEPTH)",
164     "        return -1;",
165     "    else if ((newsize *= 2) > YYMAXDEPTH)",
166     "        newsize = YYMAXDEPTH;",
167     "    i = yyssp - yyss;",
168     "#ifdef SIZE_MAX",
169     "#define YY_SIZE_MAX SIZE_MAX",
170     "#else",
171     "#define YY_SIZE_MAX 0xffffffffU",
172     "#endif",
173     "    if (newsize && YY_SIZE_MAX / newsize < sizeof *newss)",
174     "        goto bail;",
175     "    newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :",
176     "      (short *)malloc(newsize * sizeof *newss); /* overflow check above */",
177     "    if (newss == NULL)",
178     "        goto bail;",
179     "    yyss = newss;",
180     "    yyssp = newss + i;",
181     "    if (newsize && YY_SIZE_MAX / newsize < sizeof *newvs)",
182     "        goto bail;",
183     "    newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :",
184     "      (YYSTYPE *)malloc(newsize * sizeof *newvs); /* overflow check above */",
185     "    if (newvs == NULL)",
186     "        goto bail;",
187     "    yyvs = newvs;",
188     "    yyvsp = newvs + i;",
189     "    yystacksize = newsize;",
190     "    yysslim = yyss + newsize - 1;",
191     "    return 0;",
192     "bail:",
193     "    if (yyss)",
194     "            free(yyss);",
195     "    if (yyvs)",
196     "            free(yyvs);",
197     "    yyss = yyssp = NULL;",
198     "    yyvs = yyvsp = NULL;",
199     "    yystacksize = 0;",
200     "    return -1;",
201     "}",
202     "",
203     "#define YYABORT goto yyabort",
204     "#define YYREJECT goto yyabort",
205     "#define YYACCEPT goto yyaccept",
206     "#define YYERROR goto yyerrlab",
207     "int",
208     "#if defined(__cplusplus) || defined(__STDC__)",
209     "yyparse(void)",
210     "#else",
211     "yyparse()",
212     "#endif",
213     "{",
214     "    int yym, yyn, yystate;",
215     "#if YYDEBUG",
216     "#if defined(__cplusplus) || defined(__STDC__)",
217     "    const char *yys;",
218     "#else /* !(defined(__cplusplus) || defined(__STDC__)) */",
219     "    char *yys;",
220     "#endif /* !(defined(__cplusplus) || defined(__STDC__)) */",
221     "",
222     "    if ((yys = getenv(\"YYDEBUG\")))",
223     "    {",
224     "        yyn = *yys;",
225     "        if (yyn >= '0' && yyn <= '9')",
226     "            yydebug = yyn - '0';",
227     "    }",
228     "#endif /* YYDEBUG */",
229     "",
230     "    yynerrs = 0;",
231     "    yyerrflag = 0;",
232     "    yychar = (-1);",
233     "",
234     "    if (yyss == NULL && yygrowstack()) goto yyoverflow;",
235     "    yyssp = yyss;",
236     "    yyvsp = yyvs;",
237     "    *yyssp = yystate = 0;",
238     "",
239     "yyloop:",
240     "    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;",
241     "    if (yychar < 0)",
242     "    {",
243     "        if ((yychar = yylex()) < 0) yychar = 0;",
244     "#if YYDEBUG",
245     "        if (yydebug)",
246     "        {",
247     "            yys = 0;",
248     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
249     "            if (!yys) yys = \"illegal-symbol\";",
250     "            printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
251     "                    YYPREFIX, yystate, yychar, yys);",
252     "        }",
253     "#endif",
254     "    }",
255     "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
256     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
257     "    {",
258     "#if YYDEBUG",
259     "        if (yydebug)",
260     "            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
261     "                    YYPREFIX, yystate, yytable[yyn]);",
262     "#endif",
263     "        if (yyssp >= yysslim && yygrowstack())",
264     "        {",
265     "            goto yyoverflow;",
266     "        }",
267     "        *++yyssp = yystate = yytable[yyn];",
268     "        *++yyvsp = yylval;",
269     "        yychar = (-1);",
270     "        if (yyerrflag > 0)  --yyerrflag;",
271     "        goto yyloop;",
272     "    }",
273     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
274     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
275     "    {",
276     "        yyn = yytable[yyn];",
277     "        goto yyreduce;",
278     "    }",
279     "    if (yyerrflag) goto yyinrecovery;",
280     "#if defined(lint) || defined(__GNUC__)",
281     "    goto yynewerror;",
282     "#endif",
283     "yynewerror:",
284     "    yyerror(\"syntax error\");",
285     "#if defined(lint) || defined(__GNUC__)",
286     "    goto yyerrlab;",
287     "#endif",
288     "yyerrlab:",
289     "    ++yynerrs;",
290     "yyinrecovery:",
291     "    if (yyerrflag < 3)",
292     "    {",
293     "        yyerrflag = 3;",
294     "        for (;;)",
295     "        {",
296     "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
297     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
298     "            {",
299     "#if YYDEBUG",
300     "                if (yydebug)",
301     "                    printf(\"%sdebug: state %d, error recovery shifting\\",
302     " to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
303     "#endif",
304     "                if (yyssp >= yysslim && yygrowstack())",
305     "                {",
306     "                    goto yyoverflow;",
307     "                }",
308     "                *++yyssp = yystate = yytable[yyn];",
309     "                *++yyvsp = yylval;",
310     "                goto yyloop;",
311     "            }",
312     "            else",
313     "            {",
314     "#if YYDEBUG",
315     "                if (yydebug)",
316     "                    printf(\"%sdebug: error recovery discarding state %d\
317 \\n\",",
318     "                            YYPREFIX, *yyssp);",
319     "#endif",
320     "                if (yyssp <= yyss) goto yyabort;",
321     "                --yyssp;",
322     "                --yyvsp;",
323     "            }",
324     "        }",
325     "    }",
326     "    else",
327     "    {",
328     "        if (yychar == 0) goto yyabort;",
329     "#if YYDEBUG",
330     "        if (yydebug)",
331     "        {",
332     "            yys = 0;",
333     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
334     "            if (!yys) yys = \"illegal-symbol\";",
335     "            printf(\"%sdebug: state %d, error recovery discards token %d\
336  (%s)\\n\",",
337     "                    YYPREFIX, yystate, yychar, yys);",
338     "        }",
339     "#endif",
340     "        yychar = (-1);",
341     "        goto yyloop;",
342     "    }",
343     "yyreduce:",
344     "#if YYDEBUG",
345     "    if (yydebug)",
346     "        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
347     "                YYPREFIX, yystate, yyn, yyrule[yyn]);",
348     "#endif",
349     "    yym = yylen[yyn];",
350     "    if (yym)",
351     "        yyval = yyvsp[1-yym];",
352     "    else",
353     "        memset(&yyval, 0, sizeof yyval);",
354     "    switch (yyn)",
355     "    {",
356     0
357 };
358 
359 
360 char *trailer[] =
361 {
362     "    }",
363     "    yyssp -= yym;",
364     "    yystate = *yyssp;",
365     "    yyvsp -= yym;",
366     "    yym = yylhs[yyn];",
367     "    if (yystate == 0 && yym == 0)",
368     "    {",
369     "#if YYDEBUG",
370     "        if (yydebug)",
371     "            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
372     " state %d\\n\", YYPREFIX, YYFINAL);",
373     "#endif",
374     "        yystate = YYFINAL;",
375     "        *++yyssp = YYFINAL;",
376     "        *++yyvsp = yyval;",
377     "        if (yychar < 0)",
378     "        {",
379     "            if ((yychar = yylex()) < 0) yychar = 0;",
380     "#if YYDEBUG",
381     "            if (yydebug)",
382     "            {",
383     "                yys = 0;",
384     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
385     "                if (!yys) yys = \"illegal-symbol\";",
386     "                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
387     "                        YYPREFIX, YYFINAL, yychar, yys);",
388     "            }",
389     "#endif",
390     "        }",
391     "        if (yychar == 0) goto yyaccept;",
392     "        goto yyloop;",
393     "    }",
394     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
395     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
396     "        yystate = yytable[yyn];",
397     "    else",
398     "        yystate = yydgoto[yym];",
399     "#if YYDEBUG",
400     "    if (yydebug)",
401     "        printf(\"%sdebug: after reduction, shifting from state %d \\",
402     "to state %d\\n\", YYPREFIX, *yyssp, yystate);",
403     "#endif",
404     "    if (yyssp >= yysslim && yygrowstack())",
405     "    {",
406     "        goto yyoverflow;",
407     "    }",
408     "    *++yyssp = yystate;",
409     "    *++yyvsp = yyval;",
410     "    goto yyloop;",
411     "yyoverflow:",
412     "    yyerror(\"yacc stack overflow\");",
413     "yyabort:",
414     "    if (yyss)",
415     "            free(yyss);",
416     "    if (yyvs)",
417     "            free(yyvs);",
418     "    yyss = yyssp = NULL;",
419     "    yyvs = yyvsp = NULL;",
420     "    yystacksize = 0;",
421     "    return (1);",
422     "yyaccept:",
423     "    if (yyss)",
424     "            free(yyss);",
425     "    if (yyvs)",
426     "            free(yyvs);",
427     "    yyss = yyssp = NULL;",
428     "    yyvs = yyvsp = NULL;",
429     "    yystacksize = 0;",
430     "    return (0);",
431     "}",
432     0
433 };
434 
435 
436 void
437 write_section(char *section[])
438 {
439     int c;
440     int i;
441     char *s;
442     FILE *f;
443 
444     f = code_file;
445     for (i = 0; (s = section[i]); ++i)
446     {
447 	++outline;
448 	while ((c = *s))
449 	{
450 	    putc(c, f);
451 	    ++s;
452 	}
453 	putc('\n', f);
454     }
455 }
456