xref: /freebsd/contrib/byacc/yaccpar.skel (revision 74d9553e43cfafc29448d0bb836916aa21dea0de)
1/* $Id: yaccpar.skel,v 1.8 2016/12/02 21:44:42 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 || defined(yytname)
42extern char *yyname[];
43#endif
44#if YYDEBUG
45extern char *yyrule[];
46#endif
47%% global_vars
48
49int      yydebug;
50int      yynerrs;
51%% impure_vars
52
53int      yyerrflag;
54int      yychar;
55YYSTYPE  yyval;
56YYSTYPE  yylval;
57%% hdr_defs
58
59/* define the initial stack-sizes */
60#ifdef YYSTACKSIZE
61#undef YYMAXDEPTH
62#define YYMAXDEPTH  YYSTACKSIZE
63#else
64#ifdef YYMAXDEPTH
65#define YYSTACKSIZE YYMAXDEPTH
66#else
67#define YYSTACKSIZE 10000
68#define YYMAXDEPTH  10000
69#endif
70#endif
71
72#define YYINITSTACKSIZE 200
73
74typedef struct {
75    unsigned stacksize;
76    YYINT    *s_base;
77    YYINT    *s_mark;
78    YYINT    *s_last;
79    YYSTYPE  *l_base;
80    YYSTYPE  *l_mark;
81} YYSTACKDATA;
82%% hdr_vars
83/* variables for the parser stack */
84static YYSTACKDATA yystack;
85%% body_vars
86    int      yyerrflag;
87    int      yychar;
88    YYSTYPE  yyval;
89    YYSTYPE  yylval;
90
91    /* variables for the parser stack */
92    YYSTACKDATA yystack;
93%% body_1
94
95#if YYDEBUG
96#include <stdio.h>	/* needed for printf */
97#endif
98
99#include <stdlib.h>	/* needed for malloc, etc */
100#include <string.h>	/* needed for memset */
101
102/* allocate initial stack or double stack size, up to YYMAXDEPTH */
103static int yygrowstack(YYSTACKDATA *data)
104{
105    int i;
106    unsigned newsize;
107    YYINT *newss;
108    YYSTYPE *newvs;
109
110    if ((newsize = data->stacksize) == 0)
111        newsize = YYINITSTACKSIZE;
112    else if (newsize >= YYMAXDEPTH)
113        return YYENOMEM;
114    else if ((newsize *= 2) > YYMAXDEPTH)
115        newsize = YYMAXDEPTH;
116
117    i = (int) (data->s_mark - data->s_base);
118    newss = (YYINT *)realloc(data->s_base, newsize * sizeof(*newss));
119    if (newss == 0)
120        return YYENOMEM;
121
122    data->s_base = newss;
123    data->s_mark = newss + i;
124
125    newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs));
126    if (newvs == 0)
127        return YYENOMEM;
128
129    data->l_base = newvs;
130    data->l_mark = newvs + i;
131
132    data->stacksize = newsize;
133    data->s_last = data->s_base + newsize - 1;
134    return 0;
135}
136
137#if YYPURE || defined(YY_NO_LEAKS)
138static void yyfreestack(YYSTACKDATA *data)
139{
140    free(data->s_base);
141    free(data->l_base);
142    memset(data, 0, sizeof(*data));
143}
144#else
145#define yyfreestack(data) /* nothing */
146#endif
147
148#define YYABORT  goto yyabort
149#define YYREJECT goto yyabort
150#define YYACCEPT goto yyaccept
151#define YYERROR  goto yyerrlab
152
153int
154YYPARSE_DECL()
155{
156%% body_2
157    int yym, yyn, yystate;
158#if YYDEBUG
159    const char *yys;
160
161    if ((yys = getenv("YYDEBUG")) != 0)
162    {
163        yyn = *yys;
164        if (yyn >= '0' && yyn <= '9')
165            yydebug = yyn - '0';
166    }
167#endif
168
169%% body_3
170    yym = 0;
171    yyn = 0;
172    yynerrs = 0;
173    yyerrflag = 0;
174    yychar = YYEMPTY;
175    yystate = 0;
176
177#if YYPURE
178    memset(&yystack, 0, sizeof(yystack));
179#endif
180
181    if (yystack.s_base == NULL && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow;
182    yystack.s_mark = yystack.s_base;
183    yystack.l_mark = yystack.l_base;
184    yystate = 0;
185    *yystack.s_mark = 0;
186
187yyloop:
188    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
189    if (yychar < 0)
190    {
191        yychar = YYLEX;
192        if (yychar < 0) yychar = YYEOF;
193#if YYDEBUG
194        if (yydebug)
195        {
196            if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN];
197            printf("%sdebug: state %d, reading %d (%s)\n",
198                    YYPREFIX, yystate, yychar, yys);
199        }
200#endif
201    }
202    if (((yyn = yysindex[yystate]) != 0) && (yyn += yychar) >= 0 &&
203            yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar)
204    {
205#if YYDEBUG
206        if (yydebug)
207            printf("%sdebug: state %d, shifting to state %d\n",
208                    YYPREFIX, yystate, yytable[yyn]);
209#endif
210        if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow;
211        yystate = yytable[yyn];
212        *++yystack.s_mark = yytable[yyn];
213        *++yystack.l_mark = yylval;
214        yychar = YYEMPTY;
215        if (yyerrflag > 0)  --yyerrflag;
216        goto yyloop;
217    }
218    if (((yyn = yyrindex[yystate]) != 0) && (yyn += yychar) >= 0 &&
219            yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar)
220    {
221        yyn = yytable[yyn];
222        goto yyreduce;
223    }
224    if (yyerrflag != 0) goto yyinrecovery;
225
226    YYERROR_CALL("syntax error");
227
228    goto yyerrlab; /* redundant goto avoids 'unused label' warning */
229yyerrlab:
230    ++yynerrs;
231
232yyinrecovery:
233    if (yyerrflag < 3)
234    {
235        yyerrflag = 3;
236        for (;;)
237        {
238            if (((yyn = yysindex[*yystack.s_mark]) != 0) && (yyn += YYERRCODE) >= 0 &&
239                    yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) YYERRCODE)
240            {
241#if YYDEBUG
242                if (yydebug)
243                    printf("%sdebug: state %d, error recovery shifting\
244 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]);
245#endif
246                if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow;
247                yystate = yytable[yyn];
248                *++yystack.s_mark = yytable[yyn];
249                *++yystack.l_mark = yylval;
250                goto yyloop;
251            }
252            else
253            {
254#if YYDEBUG
255                if (yydebug)
256                    printf("%sdebug: error recovery discarding state %d\n",
257                            YYPREFIX, *yystack.s_mark);
258#endif
259                if (yystack.s_mark <= yystack.s_base) goto yyabort;
260                --yystack.s_mark;
261                --yystack.l_mark;
262            }
263        }
264    }
265    else
266    {
267        if (yychar == YYEOF) goto yyabort;
268#if YYDEBUG
269        if (yydebug)
270        {
271            if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN];
272            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
273                    YYPREFIX, yystate, yychar, yys);
274        }
275#endif
276        yychar = YYEMPTY;
277        goto yyloop;
278    }
279
280yyreduce:
281#if YYDEBUG
282    if (yydebug)
283        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
284                YYPREFIX, yystate, yyn, yyrule[yyn]);
285#endif
286    yym = yylen[yyn];
287    if (yym > 0)
288        yyval = yystack.l_mark[1-yym];
289    else
290        memset(&yyval, 0, sizeof yyval);
291
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            yychar = YYLEX;
313            if (yychar < 0) yychar = YYEOF;
314#if YYDEBUG
315            if (yydebug)
316            {
317                if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN];
318                printf("%sdebug: state %d, reading %d (%s)\n",
319                        YYPREFIX, YYFINAL, yychar, yys);
320            }
321#endif
322        }
323        if (yychar == YYEOF) goto yyaccept;
324        goto yyloop;
325    }
326    if (((yyn = yygindex[yym]) != 0) && (yyn += yystate) >= 0 &&
327            yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yystate)
328        yystate = yytable[yyn];
329    else
330        yystate = yydgoto[yym];
331#if YYDEBUG
332    if (yydebug)
333        printf("%sdebug: after reduction, shifting from state %d \
334to state %d\n", YYPREFIX, *yystack.s_mark, yystate);
335#endif
336    if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow;
337    *++yystack.s_mark = (YYINT) yystate;
338    *++yystack.l_mark = yyval;
339    goto yyloop;
340
341yyoverflow:
342    YYERROR_CALL("yacc stack overflow");
343
344yyabort:
345    yyfreestack(&yystack);
346    return (1);
347
348yyaccept:
349    yyfreestack(&yystack);
350    return (0);
351}
352