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