xref: /original-bsd/usr.bin/yacc/skeleton.c (revision 0842ddeb)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)skeleton.c	5.8 (Berkeley) 04/29/95";
13 #endif /* not lint */
14 
15 #include "defs.h"
16 
17 /*  The definition of yysccsid in the banner should be replaced with	*/
18 /*  a #pragma ident directive if the target C compiler supports		*/
19 /*  #pragma ident directives.						*/
20 /*									*/
21 /*  If the skeleton is changed, the banner should be changed so that	*/
22 /*  the altered version can be easily distinguished from the original.	*/
23 /*									*/
24 /*  The #defines included with the banner are there because they are	*/
25 /*  useful in subsequent code.  The macros #defined in the header or	*/
26 /*  the body either are not useful outside of semantic actions or	*/
27 /*  are conditional.							*/
28 
29 char *banner[] =
30 {
31     "#ifndef lint",
32     "static char yysccsid[] = \"@(#)yaccpar	1.9 (Berkeley) 02/21/93\";",
33     "#endif",
34     "#include <stdlib.h>",
35     "#define YYBYACC 1",
36     "#define YYMAJOR 1",
37     "#define YYMINOR 9",
38     "#define yyclearin (yychar=(-1))",
39     "#define yyerrok (yyerrflag=0)",
40     "#define YYRECOVERING (yyerrflag!=0)",
41     0
42 };
43 
44 
45 char *tables[] =
46 {
47     "extern short yylhs[];",
48     "extern short yylen[];",
49     "extern short yydefred[];",
50     "extern short yydgoto[];",
51     "extern short yysindex[];",
52     "extern short yyrindex[];",
53     "extern short yygindex[];",
54     "extern short yytable[];",
55     "extern short yycheck[];",
56     "#if YYDEBUG",
57     "extern char *yyname[];",
58     "extern char *yyrule[];",
59     "#endif",
60     0
61 };
62 
63 
64 char *header[] =
65 {
66     "#ifdef YYSTACKSIZE",
67     "#undef YYMAXDEPTH",
68     "#define YYMAXDEPTH YYSTACKSIZE",
69     "#else",
70     "#ifdef YYMAXDEPTH",
71     "#define YYSTACKSIZE YYMAXDEPTH",
72     "#else",
73     "#define YYSTACKSIZE 10000",
74     "#define YYMAXDEPTH 10000",
75     "#endif",
76     "#endif",
77     "#define YYINITSTACKSIZE 200",
78     "int yydebug;",
79     "int yynerrs;",
80     "int yyerrflag;",
81     "int yychar;",
82     "short *yyssp;",
83     "YYSTYPE *yyvsp;",
84     "YYSTYPE yyval;",
85     "YYSTYPE yylval;",
86     "short *yyss;",
87     "short *yysslim;",
88     "YYSTYPE *yyvs;",
89     "int yystacksize;",
90     0
91 };
92 
93 
94 char *body[] =
95 {
96     "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
97     "static int yygrowstack()",
98     "{",
99     "    int newsize, i;",
100     "    short *newss;",
101     "    YYSTYPE *newvs;",
102     "",
103     "    if ((newsize = yystacksize) == 0)",
104     "        newsize = YYINITSTACKSIZE;",
105     "    else if (newsize >= YYMAXDEPTH)",
106     "        return -1;",
107     "    else if ((newsize *= 2) > YYMAXDEPTH)",
108     "        newsize = YYMAXDEPTH;",
109     "    i = yyssp - yyss;",
110     "    if ((newss = realloc(yyss, newsize * sizeof *newss)) == NULL)",
111     "        return -1;",
112     "    yyss = newss;",
113     "    yyssp = newss + i;",
114     "    if ((newvs = realloc(yyvs, newsize * sizeof *newvs)) == NULL)",
115     "        return -1;",
116     "    yyvs = newvs;",
117     "    yyvsp = newvs + i;",
118     "    yystacksize = newsize;",
119     "    yysslim = yyss + newsize - 1;",
120     "    return 0;",
121     "}",
122     "",
123     "#define YYABORT goto yyabort",
124     "#define YYREJECT goto yyabort",
125     "#define YYACCEPT goto yyaccept",
126     "#define YYERROR goto yyerrlab",
127     "int",
128     "yyparse()",
129     "{",
130     "    register int yym, yyn, yystate;",
131     "#if YYDEBUG",
132     "    register char *yys;",
133     "    extern char *getenv();",
134     "",
135     "    if (yys = getenv(\"YYDEBUG\"))",
136     "    {",
137     "        yyn = *yys;",
138     "        if (yyn >= '0' && yyn <= '9')",
139     "            yydebug = yyn - '0';",
140     "    }",
141     "#endif",
142     "",
143     "    yynerrs = 0;",
144     "    yyerrflag = 0;",
145     "    yychar = (-1);",
146     "",
147     "    if (yyss == NULL && yygrowstack()) goto yyoverflow;",
148     "    yyssp = yyss;",
149     "    yyvsp = yyvs;",
150     "    *yyssp = yystate = 0;",
151     "",
152     "yyloop:",
153     "    if (yyn = yydefred[yystate]) goto yyreduce;",
154     "    if (yychar < 0)",
155     "    {",
156     "        if ((yychar = yylex()) < 0) yychar = 0;",
157     "#if YYDEBUG",
158     "        if (yydebug)",
159     "        {",
160     "            yys = 0;",
161     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
162     "            if (!yys) yys = \"illegal-symbol\";",
163     "            printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
164     "                    YYPREFIX, yystate, yychar, yys);",
165     "        }",
166     "#endif",
167     "    }",
168     "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
169     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
170     "    {",
171     "#if YYDEBUG",
172     "        if (yydebug)",
173     "            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
174     "                    YYPREFIX, yystate, yytable[yyn]);",
175     "#endif",
176     "        if (yyssp >= yysslim && yygrowstack())",
177     "        {",
178     "            goto yyoverflow;",
179     "        }",
180     "        *++yyssp = yystate = yytable[yyn];",
181     "        *++yyvsp = yylval;",
182     "        yychar = (-1);",
183     "        if (yyerrflag > 0)  --yyerrflag;",
184     "        goto yyloop;",
185     "    }",
186     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
187     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
188     "    {",
189     "        yyn = yytable[yyn];",
190     "        goto yyreduce;",
191     "    }",
192     "    if (yyerrflag) goto yyinrecovery;",
193     "#ifdef lint",
194     "    goto yynewerror;",
195     "#endif",
196     "yynewerror:",
197     "    yyerror(\"syntax error\");",
198     "#ifdef lint",
199     "    goto yyerrlab;",
200     "#endif",
201     "yyerrlab:",
202     "    ++yynerrs;",
203     "yyinrecovery:",
204     "    if (yyerrflag < 3)",
205     "    {",
206     "        yyerrflag = 3;",
207     "        for (;;)",
208     "        {",
209     "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
210     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
211     "            {",
212     "#if YYDEBUG",
213     "                if (yydebug)",
214     "                    printf(\"%sdebug: state %d, error recovery shifting\\",
215     " to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
216     "#endif",
217     "                if (yyssp >= yysslim && yygrowstack())",
218     "                {",
219     "                    goto yyoverflow;",
220     "                }",
221     "                *++yyssp = yystate = yytable[yyn];",
222     "                *++yyvsp = yylval;",
223     "                goto yyloop;",
224     "            }",
225     "            else",
226     "            {",
227     "#if YYDEBUG",
228     "                if (yydebug)",
229     "                    printf(\"%sdebug: error recovery discarding state %d\
230 \\n\",",
231     "                            YYPREFIX, *yyssp);",
232     "#endif",
233     "                if (yyssp <= yyss) goto yyabort;",
234     "                --yyssp;",
235     "                --yyvsp;",
236     "            }",
237     "        }",
238     "    }",
239     "    else",
240     "    {",
241     "        if (yychar == 0) goto yyabort;",
242     "#if YYDEBUG",
243     "        if (yydebug)",
244     "        {",
245     "            yys = 0;",
246     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
247     "            if (!yys) yys = \"illegal-symbol\";",
248     "            printf(\"%sdebug: state %d, error recovery discards token %d\
249  (%s)\\n\",",
250     "                    YYPREFIX, yystate, yychar, yys);",
251     "        }",
252     "#endif",
253     "        yychar = (-1);",
254     "        goto yyloop;",
255     "    }",
256     "yyreduce:",
257     "#if YYDEBUG",
258     "    if (yydebug)",
259     "        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
260     "                YYPREFIX, yystate, yyn, yyrule[yyn]);",
261     "#endif",
262     "    yym = yylen[yyn];",
263     "    yyval = yyvsp[1-yym];",
264     "    switch (yyn)",
265     "    {",
266     0
267 };
268 
269 
270 char *trailer[] =
271 {
272     "    }",
273     "    yyssp -= yym;",
274     "    yystate = *yyssp;",
275     "    yyvsp -= yym;",
276     "    yym = yylhs[yyn];",
277     "    if (yystate == 0 && yym == 0)",
278     "    {",
279     "#if YYDEBUG",
280     "        if (yydebug)",
281     "            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
282     " state %d\\n\", YYPREFIX, YYFINAL);",
283     "#endif",
284     "        yystate = YYFINAL;",
285     "        *++yyssp = YYFINAL;",
286     "        *++yyvsp = yyval;",
287     "        if (yychar < 0)",
288     "        {",
289     "            if ((yychar = yylex()) < 0) yychar = 0;",
290     "#if YYDEBUG",
291     "            if (yydebug)",
292     "            {",
293     "                yys = 0;",
294     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
295     "                if (!yys) yys = \"illegal-symbol\";",
296     "                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
297     "                        YYPREFIX, YYFINAL, yychar, yys);",
298     "            }",
299     "#endif",
300     "        }",
301     "        if (yychar == 0) goto yyaccept;",
302     "        goto yyloop;",
303     "    }",
304     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
305     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
306     "        yystate = yytable[yyn];",
307     "    else",
308     "        yystate = yydgoto[yym];",
309     "#if YYDEBUG",
310     "    if (yydebug)",
311     "        printf(\"%sdebug: after reduction, shifting from state %d \\",
312     "to state %d\\n\", YYPREFIX, *yyssp, yystate);",
313     "#endif",
314     "    if (yyssp >= yysslim && yygrowstack())",
315     "    {",
316     "        goto yyoverflow;",
317     "    }",
318     "    *++yyssp = yystate;",
319     "    *++yyvsp = yyval;",
320     "    goto yyloop;",
321     "yyoverflow:",
322     "    yyerror(\"yacc stack overflow\");",
323     "yyabort:",
324     "    return (1);",
325     "yyaccept:",
326     "    return (0);",
327     "}",
328     0
329 };
330 
331 
332 write_section(section)
333 char *section[];
334 {
335     register int c;
336     register int i;
337     register char *s;
338     register FILE *f;
339 
340     f = code_file;
341     for (i = 0; s = section[i]; ++i)
342     {
343 	++outline;
344 	while (c = *s)
345 	{
346 	    putc(c, f);
347 	    ++s;
348 	}
349 	putc('\n', f);
350     }
351 }
352