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