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