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