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