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