Imported Upstream version 20101229
[platform/upstream/byacc.git] / skeleton.c
1 /* $Id: skeleton.c,v 1.30 2010/12/29 20:57:21 tom Exp $ */
2
3 #include "defs.h"
4
5 /*  The definition of yysccsid in the banner should be replaced with    */
6 /*  a #pragma ident directive if the target C compiler supports         */
7 /*  #pragma ident directives.                                           */
8 /*                                                                      */
9 /*  If the skeleton is changed, the banner should be changed so that    */
10 /*  the altered version can be easily distinguished from the original.  */
11 /*                                                                      */
12 /*  The #defines included with the banner are there because they are    */
13 /*  useful in subsequent code.  The macros #defined in the header or    */
14 /*  the body either are not useful outside of semantic actions or       */
15 /*  are conditional.                                                    */
16
17 const char *const banner[] =
18 {
19     "#ifndef lint",
20     "static const char yysccsid[] = \"@(#)yaccpar       1.9 (Berkeley) 02/21/93\";",
21     "#endif",
22     "",
23     "#define YYBYACC 1",
24     CONCAT1("#define YYMAJOR ", YYMAJOR),
25     CONCAT1("#define YYMINOR ", YYMINOR),
26 #ifdef YYPATCH
27     CONCAT1("#define YYPATCH ", YYPATCH),
28 #endif
29     "",
30     "#define YYEMPTY        (-1)",
31     "#define yyclearin      (yychar = YYEMPTY)",
32     "#define yyerrok        (yyerrflag = 0)",
33     "#define YYRECOVERING() (yyerrflag != 0)",
34     "",
35     0
36 };
37
38 const char *const xdecls[] =
39 {
40     "extern int YYPARSE_DECL();",
41     "",
42     0
43 };
44
45 const char *const 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     "",
57     "#if YYDEBUG",
58     "extern char *yyname[];",
59     "extern char *yyrule[];",
60     "#endif",
61     0
62 };
63
64 const char *const hdr_defs[] =
65 {
66     "/* define the initial stack-sizes */",
67     "#ifdef YYSTACKSIZE",
68     "#undef YYMAXDEPTH",
69     "#define YYMAXDEPTH  YYSTACKSIZE",
70     "#else",
71     "#ifdef YYMAXDEPTH",
72     "#define YYSTACKSIZE YYMAXDEPTH",
73     "#else",
74     "#define YYSTACKSIZE 500",
75     "#define YYMAXDEPTH  500",
76     "#endif",
77     "#endif",
78     "",
79     "#define YYINITSTACKSIZE 500",
80     "",
81     "int      yydebug;",
82     "int      yynerrs;",
83     "",
84     "typedef struct {",
85     "    unsigned stacksize;",
86     "    short    *s_base;",
87     "    short    *s_mark;",
88     "    short    *s_last;",
89     "    YYSTYPE  *l_base;",
90     "    YYSTYPE  *l_mark;",
91     "} YYSTACKDATA;",
92     0
93 };
94
95 const char *const hdr_vars[] =
96 {
97     "int      yyerrflag;",
98     "int      yychar;",
99     "YYSTYPE  yyval;",
100     "YYSTYPE  yylval;",
101     "",
102     "/* variables for the parser stack */",
103     "static YYSTACKDATA yystack;",
104     0
105 };
106
107 const char *const body_vars[] =
108 {
109     "    int      yyerrflag;",
110     "    int      yychar;",
111     "    YYSTYPE  yyval;",
112     "    YYSTYPE  yylval;",
113     "",
114     "    /* variables for the parser stack */",
115     "    YYSTACKDATA yystack;",
116     0
117 };
118
119 const char *const body_1[] =
120 {
121     "",
122     "#if YYDEBUG",
123     "#include <stdio.h>         /* needed for printf */",
124     "#endif",
125     "",
126     "#include <stdlib.h>        /* needed for malloc, etc */",
127     "#include <string.h>        /* needed for memset */",
128     "",
129     "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
130     "static int yygrowstack(YYSTACKDATA *data)",
131     "{",
132     "    int i;",
133     "    unsigned newsize;",
134     "    short *newss;",
135     "    YYSTYPE *newvs;",
136     "",
137     "    if ((newsize = data->stacksize) == 0)",
138     "        newsize = YYINITSTACKSIZE;",
139     "    else if (newsize >= YYMAXDEPTH)",
140     "        return -1;",
141     "    else if ((newsize *= 2) > YYMAXDEPTH)",
142     "        newsize = YYMAXDEPTH;",
143     "",
144     "    i = data->s_mark - data->s_base;",
145     "    newss = (short *)realloc(data->s_base, newsize * sizeof(*newss));",
146     "    if (newss == 0)",
147     "        return -1;",
148     "",
149     "    data->s_base = newss;",
150     "    data->s_mark = newss + i;",
151     "",
152     "    newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs));",
153     "    if (newvs == 0)",
154     "        return -1;",
155     "",
156     "    data->l_base = newvs;",
157     "    data->l_mark = newvs + i;",
158     "",
159     "    data->stacksize = newsize;",
160     "    data->s_last = data->s_base + newsize - 1;",
161     "    return 0;",
162     "}",
163     "",
164     "#if YYPURE || defined(YY_NO_LEAKS)",
165     "static void yyfreestack(YYSTACKDATA *data)",
166     "{",
167     "    free(data->s_base);",
168     "    free(data->l_base);",
169     "    memset(data, 0, sizeof(*data));",
170     "}",
171     "#else",
172     "#define yyfreestack(data) /* nothing */",
173     "#endif",
174     "",
175     "#define YYABORT  goto yyabort",
176     "#define YYREJECT goto yyabort",
177     "#define YYACCEPT goto yyaccept",
178     "#define YYERROR  goto yyerrlab",
179     "",
180     "int",
181     "YYPARSE_DECL()",
182     "{",
183     0
184 };
185
186 const char *const body_2[] =
187 {
188     "    int yym, yyn, yystate;",
189     "#if YYDEBUG",
190     "    const char *yys;",
191     "",
192     "    if ((yys = getenv(\"YYDEBUG\")) != 0)",
193     "    {",
194     "        yyn = *yys;",
195     "        if (yyn >= '0' && yyn <= '9')",
196     "            yydebug = yyn - '0';",
197     "    }",
198     "#endif",
199     "",
200     "    yynerrs = 0;",
201     "    yyerrflag = 0;",
202     "    yychar = YYEMPTY;",
203     "    yystate = 0;",
204     "",
205     "#if YYPURE",
206     "    memset(&yystack, 0, sizeof(yystack));",
207     "#endif",
208     "",
209     "    if (yystack.s_base == NULL && yygrowstack(&yystack)) goto yyoverflow;",
210     "    yystack.s_mark = yystack.s_base;",
211     "    yystack.l_mark = yystack.l_base;",
212     "    yystate = 0;",
213     "    *yystack.s_mark = 0;",
214     "",
215     "yyloop:",
216     "    if ((yyn = yydefred[yystate]) != 0) 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 (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))",
240     "        {",
241     "            goto yyoverflow;",
242     "        }",
243     "        yystate = yytable[yyn];",
244     "        *++yystack.s_mark = yytable[yyn];",
245     "        *++yystack.l_mark = yylval;",
246     "        yychar = YYEMPTY;",
247     "        if (yyerrflag > 0)  --yyerrflag;",
248     "        goto yyloop;",
249     "    }",
250     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
251     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
252     "    {",
253     "        yyn = yytable[yyn];",
254     "        goto yyreduce;",
255     "    }",
256     "    if (yyerrflag) goto yyinrecovery;",
257     "",
258     0
259 };
260
261 const char *const body_3[] =
262 {
263     "",
264     "    goto yyerrlab;",
265     "",
266     "yyerrlab:",
267     "    ++yynerrs;",
268     "",
269     "yyinrecovery:",
270     "    if (yyerrflag < 3)",
271     "    {",
272     "        yyerrflag = 3;",
273     "        for (;;)",
274     "        {",
275     "            if ((yyn = yysindex[*yystack.s_mark]) && (yyn += YYERRCODE) >= 0 &&",
276     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
277     "            {",
278     "#if YYDEBUG",
279     "                if (yydebug)",
280     "                    printf(\"%sdebug: state %d, error recovery shifting\\",
281     " to state %d\\n\", YYPREFIX, *yystack.s_mark, yytable[yyn]);",
282     "#endif",
283     "                if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))",
284     "                {",
285     "                    goto yyoverflow;",
286     "                }",
287     "                yystate = yytable[yyn];",
288     "                *++yystack.s_mark = yytable[yyn];",
289     "                *++yystack.l_mark = yylval;",
290     "                goto yyloop;",
291     "            }",
292     "            else",
293     "            {",
294     "#if YYDEBUG",
295     "                if (yydebug)",
296     "                    printf(\"%sdebug: error recovery discarding state %d\
297 \\n\",",
298     "                            YYPREFIX, *yystack.s_mark);",
299     "#endif",
300     "                if (yystack.s_mark <= yystack.s_base) goto yyabort;",
301     "                --yystack.s_mark;",
302     "                --yystack.l_mark;",
303     "            }",
304     "        }",
305     "    }",
306     "    else",
307     "    {",
308     "        if (yychar == 0) goto yyabort;",
309     "#if YYDEBUG",
310     "        if (yydebug)",
311     "        {",
312     "            yys = 0;",
313     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
314     "            if (!yys) yys = \"illegal-symbol\";",
315     "            printf(\"%sdebug: state %d, error recovery discards token %d\
316  (%s)\\n\",",
317     "                    YYPREFIX, yystate, yychar, yys);",
318     "        }",
319     "#endif",
320     "        yychar = YYEMPTY;",
321     "        goto yyloop;",
322     "    }",
323     "",
324     "yyreduce:",
325     "#if YYDEBUG",
326     "    if (yydebug)",
327     "        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
328     "                YYPREFIX, yystate, yyn, yyrule[yyn]);",
329     "#endif",
330     "    yym = yylen[yyn];",
331     "    if (yym)",
332     "        yyval = yystack.l_mark[1-yym];",
333     "    else",
334     "        memset(&yyval, 0, sizeof yyval);",
335     "    switch (yyn)",
336     "    {",
337     0
338 };
339
340 const char *const trailer[] =
341 {
342     "    }",
343     "    yystack.s_mark -= yym;",
344     "    yystate = *yystack.s_mark;",
345     "    yystack.l_mark -= yym;",
346     "    yym = yylhs[yyn];",
347     "    if (yystate == 0 && yym == 0)",
348     "    {",
349     "#if YYDEBUG",
350     "        if (yydebug)",
351     "            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
352     " state %d\\n\", YYPREFIX, YYFINAL);",
353     "#endif",
354     "        yystate = YYFINAL;",
355     "        *++yystack.s_mark = YYFINAL;",
356     "        *++yystack.l_mark = yyval;",
357     "        if (yychar < 0)",
358     "        {",
359     "            if ((yychar = YYLEX) < 0) yychar = 0;",
360     "#if YYDEBUG",
361     "            if (yydebug)",
362     "            {",
363     "                yys = 0;",
364     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
365     "                if (!yys) yys = \"illegal-symbol\";",
366     "                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
367     "                        YYPREFIX, YYFINAL, yychar, yys);",
368     "            }",
369     "#endif",
370     "        }",
371     "        if (yychar == 0) goto yyaccept;",
372     "        goto yyloop;",
373     "    }",
374     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
375     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
376     "        yystate = yytable[yyn];",
377     "    else",
378     "        yystate = yydgoto[yym];",
379     "#if YYDEBUG",
380     "    if (yydebug)",
381     "        printf(\"%sdebug: after reduction, shifting from state %d \\",
382     "to state %d\\n\", YYPREFIX, *yystack.s_mark, yystate);",
383     "#endif",
384     "    if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))",
385     "    {",
386     "        goto yyoverflow;",
387     "    }",
388     "    *++yystack.s_mark = (short) yystate;",
389     "    *++yystack.l_mark = yyval;",
390     "    goto yyloop;",
391     "",
392     "yyoverflow:",
393     0
394 };
395
396 const char *const trailer_2[] =
397 {
398     "",
399     "yyabort:",
400     "    yyfreestack(&yystack);",
401     "    return (1);",
402     "",
403     "yyaccept:",
404     "    yyfreestack(&yystack);",
405     "    return (0);",
406     "}",
407     0
408 };
409
410 void
411 write_section(const char *const section[])
412 {
413     int c;
414     int i;
415     const char *s;
416     FILE *f;
417
418     f = code_file;
419     for (i = 0; (s = section[i]) != 0; ++i)
420     {
421         ++outline;
422         while ((c = *s) != 0)
423         {
424             putc(c, f);
425             ++s;
426         }
427         putc('\n', f);
428     }
429 }