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