xref: /freebsd/contrib/byacc/yaccpar.skel (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1/* $Id: yaccpar.skel,v 1.7 2016/06/06 23:35:55 Tom.Shields 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    yym = 0;
170    yyn = 0;
171    yynerrs = 0;
172    yyerrflag = 0;
173    yychar = YYEMPTY;
174    yystate = 0;
175
176#if YYPURE
177    memset(&yystack, 0, sizeof(yystack));
178#endif
179
180    if (yystack.s_base == NULL && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow;
181    yystack.s_mark = yystack.s_base;
182    yystack.l_mark = yystack.l_base;
183    yystate = 0;
184    *yystack.s_mark = 0;
185
186yyloop:
187    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
188    if (yychar < 0)
189    {
190        yychar = YYLEX;
191        if (yychar < 0) yychar = YYEOF;
192#if YYDEBUG
193        if (yydebug)
194        {
195            if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN];
196            printf("%sdebug: state %d, reading %d (%s)\n",
197                    YYPREFIX, yystate, yychar, yys);
198        }
199#endif
200    }
201    if (((yyn = yysindex[yystate]) != 0) && (yyn += yychar) >= 0 &&
202            yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar)
203    {
204#if YYDEBUG
205        if (yydebug)
206            printf("%sdebug: state %d, shifting to state %d\n",
207                    YYPREFIX, yystate, yytable[yyn]);
208#endif
209        if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow;
210        yystate = yytable[yyn];
211        *++yystack.s_mark = yytable[yyn];
212        *++yystack.l_mark = yylval;
213        yychar = YYEMPTY;
214        if (yyerrflag > 0)  --yyerrflag;
215        goto yyloop;
216    }
217    if (((yyn = yyrindex[yystate]) != 0) && (yyn += yychar) >= 0 &&
218            yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar)
219    {
220        yyn = yytable[yyn];
221        goto yyreduce;
222    }
223    if (yyerrflag != 0) goto yyinrecovery;
224
225    YYERROR_CALL("syntax error");
226
227    goto yyerrlab; /* redundant goto avoids 'unused label' warning */
228yyerrlab:
229    ++yynerrs;
230
231yyinrecovery:
232    if (yyerrflag < 3)
233    {
234        yyerrflag = 3;
235        for (;;)
236        {
237            if (((yyn = yysindex[*yystack.s_mark]) != 0) && (yyn += YYERRCODE) >= 0 &&
238                    yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) YYERRCODE)
239            {
240#if YYDEBUG
241                if (yydebug)
242                    printf("%sdebug: state %d, error recovery shifting\
243 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]);
244#endif
245                if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow;
246                yystate = yytable[yyn];
247                *++yystack.s_mark = yytable[yyn];
248                *++yystack.l_mark = yylval;
249                goto yyloop;
250            }
251            else
252            {
253#if YYDEBUG
254                if (yydebug)
255                    printf("%sdebug: error recovery discarding state %d\n",
256                            YYPREFIX, *yystack.s_mark);
257#endif
258                if (yystack.s_mark <= yystack.s_base) goto yyabort;
259                --yystack.s_mark;
260                --yystack.l_mark;
261            }
262        }
263    }
264    else
265    {
266        if (yychar == YYEOF) goto yyabort;
267#if YYDEBUG
268        if (yydebug)
269        {
270            if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN];
271            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
272                    YYPREFIX, yystate, yychar, yys);
273        }
274#endif
275        yychar = YYEMPTY;
276        goto yyloop;
277    }
278
279yyreduce:
280#if YYDEBUG
281    if (yydebug)
282        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
283                YYPREFIX, yystate, yyn, yyrule[yyn]);
284#endif
285    yym = yylen[yyn];
286    if (yym > 0)
287        yyval = yystack.l_mark[1-yym];
288    else
289        memset(&yyval, 0, sizeof yyval);
290
291    switch (yyn)
292    {
293%% trailer
294    }
295    yystack.s_mark -= yym;
296    yystate = *yystack.s_mark;
297    yystack.l_mark -= yym;
298    yym = yylhs[yyn];
299    if (yystate == 0 && yym == 0)
300    {
301#if YYDEBUG
302        if (yydebug)
303            printf("%sdebug: after reduction, shifting from state 0 to\
304 state %d\n", YYPREFIX, YYFINAL);
305#endif
306        yystate = YYFINAL;
307        *++yystack.s_mark = YYFINAL;
308        *++yystack.l_mark = yyval;
309        if (yychar < 0)
310        {
311            yychar = YYLEX;
312            if (yychar < 0) yychar = YYEOF;
313#if YYDEBUG
314            if (yydebug)
315            {
316                if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN];
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]) != 0) && (yyn += yystate) >= 0 &&
326            yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) 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) goto yyoverflow;
336    *++yystack.s_mark = (YYINT) yystate;
337    *++yystack.l_mark = yyval;
338    goto yyloop;
339
340yyoverflow:
341    YYERROR_CALL("yacc stack overflow");
342
343yyabort:
344    yyfreestack(&yystack);
345    return (1);
346
347yyaccept:
348    yyfreestack(&yystack);
349    return (0);
350}
351