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