xref: /openbsd-src/usr.bin/yacc/skeleton.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: skeleton.c,v 1.39 2015/12/31 23:22:39 guenther 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 	NULL
63 };
64 
65 
66 char *tables[] =
67 {
68 	"extern const short yylhs[];",
69 	"extern const short yylen[];",
70 	"extern const short yydefred[];",
71 	"extern const short yydgoto[];",
72 	"extern const short yysindex[];",
73 	"extern const short yyrindex[];",
74 	"extern const short yygindex[];",
75 	"extern const short yytable[];",
76 	"extern const short yycheck[];",
77 	"#if YYDEBUG",
78 	"extern const char *const yyname[];",
79 	"extern const char *const yyrule[];",
80 	"#endif",
81 	NULL
82 };
83 
84 
85 char *header[] =
86 {
87 	"#ifdef YYSTACKSIZE",
88 	"#undef YYMAXDEPTH",
89 	"#define YYMAXDEPTH YYSTACKSIZE",
90 	"#else",
91 	"#ifdef YYMAXDEPTH",
92 	"#define YYSTACKSIZE YYMAXDEPTH",
93 	"#else",
94 	"#define YYSTACKSIZE 10000",
95 	"#define YYMAXDEPTH 10000",
96 	"#endif",
97 	"#endif",
98 	"#define YYINITSTACKSIZE 200",
99 	"/* LINTUSED */",
100 	"int yydebug;",
101 	"int yynerrs;",
102 	"int yyerrflag;",
103 	"int yychar;",
104 	"short *yyssp;",
105 	"YYSTYPE *yyvsp;",
106 	"YYSTYPE yyval;",
107 	"YYSTYPE yylval;",
108 	"short *yyss;",
109 	"short *yysslim;",
110 	"YYSTYPE *yyvs;",
111 	"unsigned int yystacksize;",
112 	"int yyparse(void);",
113 	NULL
114 };
115 
116 
117 char *body[] =
118 {
119 	"/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
120 	"static int yygrowstack(void)",
121 	"{",
122 	"    unsigned int newsize;",
123 	"    long sslen;",
124 	"    short *newss;",
125 	"    YYSTYPE *newvs;",
126 	"",
127 	"    if ((newsize = yystacksize) == 0)",
128 	"        newsize = YYINITSTACKSIZE;",
129 	"    else if (newsize >= YYMAXDEPTH)",
130 	"        return -1;",
131 	"    else if ((newsize *= 2) > YYMAXDEPTH)",
132 	"        newsize = YYMAXDEPTH;",
133 	"    sslen = yyssp - yyss;",
134 	"#ifdef SIZE_MAX",
135 	"#define YY_SIZE_MAX SIZE_MAX",
136 	"#else",
137 	"#define YY_SIZE_MAX 0xffffffffU",
138 	"#endif",
139 	"    if (newsize && YY_SIZE_MAX / newsize < sizeof *newss)",
140 	"        goto bail;",
141 	"    newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :",
142 	"      (short *)malloc(newsize * sizeof *newss); /* overflow check above */",
143 	"    if (newss == NULL)",
144 	"        goto bail;",
145 	"    yyss = newss;",
146 	"    yyssp = newss + sslen;",
147 	"    if (newsize && YY_SIZE_MAX / newsize < sizeof *newvs)",
148 	"        goto bail;",
149 	"    newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :",
150 	"      (YYSTYPE *)malloc(newsize * sizeof *newvs); /* overflow check above */",
151 	"    if (newvs == NULL)",
152 	"        goto bail;",
153 	"    yyvs = newvs;",
154 	"    yyvsp = newvs + sslen;",
155 	"    yystacksize = newsize;",
156 	"    yysslim = yyss + newsize - 1;",
157 	"    return 0;",
158 	"bail:",
159 	"    if (yyss)",
160 	"            free(yyss);",
161 	"    if (yyvs)",
162 	"            free(yyvs);",
163 	"    yyss = yyssp = NULL;",
164 	"    yyvs = yyvsp = NULL;",
165 	"    yystacksize = 0;",
166 	"    return -1;",
167 	"}",
168 	"",
169 	"#define YYABORT goto yyabort",
170 	"#define YYREJECT goto yyabort",
171 	"#define YYACCEPT goto yyaccept",
172 	"#define YYERROR goto yyerrlab",
173 	"int",
174 	"yyparse(void)",
175 	"{",
176 	"    int yym, yyn, yystate;",
177 	"#if YYDEBUG",
178 	"    const char *yys;",
179 	"",
180 	"    if ((yys = getenv(\"YYDEBUG\")))",
181 	"    {",
182 	"        yyn = *yys;",
183 	"        if (yyn >= '0' && yyn <= '9')",
184 	"            yydebug = yyn - '0';",
185 	"    }",
186 	"#endif /* YYDEBUG */",
187 	"",
188 	"    yynerrs = 0;",
189 	"    yyerrflag = 0;",
190 	"    yychar = (-1);",
191 	"",
192 	"    if (yyss == NULL && yygrowstack()) goto yyoverflow;",
193 	"    yyssp = yyss;",
194 	"    yyvsp = yyvs;",
195 	"    *yyssp = yystate = 0;",
196 	"",
197 	"yyloop:",
198 	"    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;",
199 	"    if (yychar < 0)",
200 	"    {",
201 	"        if ((yychar = yylex()) < 0) yychar = 0;",
202 	"#if YYDEBUG",
203 	"        if (yydebug)",
204 	"        {",
205 	"            yys = 0;",
206 	"            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
207 	"            if (!yys) yys = \"illegal-symbol\";",
208 	"            printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
209 	"                    YYPREFIX, yystate, yychar, yys);",
210 	"        }",
211 	"#endif",
212 	"    }",
213 	"    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
214 	"            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
215 	"    {",
216 	"#if YYDEBUG",
217 	"        if (yydebug)",
218 	"            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
219 	"                    YYPREFIX, yystate, yytable[yyn]);",
220 	"#endif",
221 	"        if (yyssp >= yysslim && yygrowstack())",
222 	"        {",
223 	"            goto yyoverflow;",
224 	"        }",
225 	"        *++yyssp = yystate = yytable[yyn];",
226 	"        *++yyvsp = yylval;",
227 	"        yychar = (-1);",
228 	"        if (yyerrflag > 0)  --yyerrflag;",
229 	"        goto yyloop;",
230 	"    }",
231 	"    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
232 	"            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
233 	"    {",
234 	"        yyn = yytable[yyn];",
235 	"        goto yyreduce;",
236 	"    }",
237 	"    if (yyerrflag) goto yyinrecovery;",
238 	"#if defined(__GNUC__)",
239 	"    goto yynewerror;",
240 	"#endif",
241 	"yynewerror:",
242 	"    yyerror(\"syntax error\");",
243 	"#if defined(__GNUC__)",
244 	"    goto yyerrlab;",
245 	"#endif",
246 	"yyerrlab:",
247 	"    ++yynerrs;",
248 	"yyinrecovery:",
249 	"    if (yyerrflag < 3)",
250 	"    {",
251 	"        yyerrflag = 3;",
252 	"        for (;;)",
253 	"        {",
254 	"            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
255 	"                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
256 	"            {",
257 	"#if YYDEBUG",
258 	"                if (yydebug)",
259 	"                    printf(\"%sdebug: state %d, error recovery shifting\\",
260 	" to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
261 	"#endif",
262 	"                if (yyssp >= yysslim && yygrowstack())",
263 	"                {",
264 	"                    goto yyoverflow;",
265 	"                }",
266 	"                *++yyssp = yystate = yytable[yyn];",
267 	"                *++yyvsp = yylval;",
268 	"                goto yyloop;",
269 	"            }",
270 	"            else",
271 	"            {",
272 	"#if YYDEBUG",
273 	"                if (yydebug)",
274 	"                    printf(\"%sdebug: error recovery discarding state %d\
275 \\n\",",
276 	"                            YYPREFIX, *yyssp);",
277 	"#endif",
278 	"                if (yyssp <= yyss) goto yyabort;",
279 	"                --yyssp;",
280 	"                --yyvsp;",
281 	"            }",
282 	"        }",
283 	"    }",
284 	"    else",
285 	"    {",
286 	"        if (yychar == 0) goto yyabort;",
287 	"#if YYDEBUG",
288 	"        if (yydebug)",
289 	"        {",
290 	"            yys = 0;",
291 	"            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
292 	"            if (!yys) yys = \"illegal-symbol\";",
293 	"            printf(\"%sdebug: state %d, error recovery discards token %d\
294  (%s)\\n\",",
295 	"                    YYPREFIX, yystate, yychar, yys);",
296 	"        }",
297 	"#endif",
298 	"        yychar = (-1);",
299 	"        goto yyloop;",
300 	"    }",
301 	"yyreduce:",
302 	"#if YYDEBUG",
303 	"    if (yydebug)",
304 	"        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
305 	"                YYPREFIX, yystate, yyn, yyrule[yyn]);",
306 	"#endif",
307 	"    yym = yylen[yyn];",
308 	"    if (yym)",
309 	"        yyval = yyvsp[1-yym];",
310 	"    else",
311 	"        memset(&yyval, 0, sizeof yyval);",
312 	"    switch (yyn)",
313 	"    {",
314 	NULL
315 };
316 
317 
318 char *trailer[] =
319 {
320 	"    }",
321 	"    yyssp -= yym;",
322 	"    yystate = *yyssp;",
323 	"    yyvsp -= yym;",
324 	"    yym = yylhs[yyn];",
325 	"    if (yystate == 0 && yym == 0)",
326 	"    {",
327 	"#if YYDEBUG",
328 	"        if (yydebug)",
329 	"            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
330 	" state %d\\n\", YYPREFIX, YYFINAL);",
331 	"#endif",
332 	"        yystate = YYFINAL;",
333 	"        *++yyssp = YYFINAL;",
334 	"        *++yyvsp = yyval;",
335 	"        if (yychar < 0)",
336 	"        {",
337 	"            if ((yychar = yylex()) < 0) yychar = 0;",
338 	"#if YYDEBUG",
339 	"            if (yydebug)",
340 	"            {",
341 	"                yys = 0;",
342 	"                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
343 	"                if (!yys) yys = \"illegal-symbol\";",
344 	"                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
345 	"                        YYPREFIX, YYFINAL, yychar, yys);",
346 	"            }",
347 	"#endif",
348 	"        }",
349 	"        if (yychar == 0) goto yyaccept;",
350 	"        goto yyloop;",
351 	"    }",
352 	"    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
353 	"            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
354 	"        yystate = yytable[yyn];",
355 	"    else",
356 	"        yystate = yydgoto[yym];",
357 	"#if YYDEBUG",
358 	"    if (yydebug)",
359 	"        printf(\"%sdebug: after reduction, shifting from state %d \\",
360 	"to state %d\\n\", YYPREFIX, *yyssp, yystate);",
361 	"#endif",
362 	"    if (yyssp >= yysslim && yygrowstack())",
363 	"    {",
364 	"        goto yyoverflow;",
365 	"    }",
366 	"    *++yyssp = yystate;",
367 	"    *++yyvsp = yyval;",
368 	"    goto yyloop;",
369 	"yyoverflow:",
370 	"    yyerror(\"yacc stack overflow\");",
371 	"yyabort:",
372 	"    if (yyss)",
373 	"            free(yyss);",
374 	"    if (yyvs)",
375 	"            free(yyvs);",
376 	"    yyss = yyssp = NULL;",
377 	"    yyvs = yyvsp = NULL;",
378 	"    yystacksize = 0;",
379 	"    return (1);",
380 	"yyaccept:",
381 	"    if (yyss)",
382 	"            free(yyss);",
383 	"    if (yyvs)",
384 	"            free(yyvs);",
385 	"    yyss = yyssp = NULL;",
386 	"    yyvs = yyvsp = NULL;",
387 	"    yystacksize = 0;",
388 	"    return (0);",
389 	"}",
390 	NULL
391 };
392 
393 
394 void
395 write_section(char *section[])
396 {
397 	int i;
398 	char *s;
399 
400 	for (i = 0; (s = section[i]); ++i) {
401 		++outline;
402 		fputs(s, code_file);
403 		putc('\n', code_file);
404 	}
405 }
406