xref: /illumos-gate/usr/src/cmd/sgs/yacc/common/yaccpar (revision a97db1b7)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/* Copyright (c) 1988 AT&T */
28/* All Rights Reserved */
29
30/*
31** Skeleton parser driver for yacc output
32*/
33
34/*
35** yacc user known macros and defines
36*/
37#define YYERROR		goto yyerrlab
38#define YYACCEPT	return(0)
39#define YYABORT		return(1)
40#define YYBACKUP( newtoken, newvalue )\
41{\
42	if ( yychar >= 0 || ( yyr2[ yytmp ] >> 1 ) != 1 )\
43	{\
44		yyerror( "syntax error - cannot backup" );\
45		goto yyerrlab;\
46	}\
47	yychar = newtoken;\
48	yystate = *yyps;\
49	yylval = newvalue;\
50	goto yynewstate;\
51}
52#define YYRECOVERING()	(!!yyerrflag)
53#define YYNEW(type)	malloc(sizeof(type) * yynewmax)
54#define YYCOPY(to, from, type) \
55	(type *) memcpy(to, (char *) from, yymaxdepth * sizeof (type))
56#define YYENLARGE( from, type) \
57	(type *) realloc((char *) from, yynewmax * sizeof(type))
58#ifndef YYDEBUG
59#	define YYDEBUG	1	/* make debugging available */
60#endif
61
62/*
63** user known globals
64*/
65int yydebug;			/* set to 1 to get debugging */
66
67/*
68** driver internal defines
69*/
70#define YYFLAG		(-10000000)
71
72/*
73** global variables used by the parser
74*/
75YYSTYPE *yypv;			/* top of value stack */
76int *yyps;			/* top of state stack */
77
78int yystate;			/* current state */
79int yytmp;			/* extra var (lasts between blocks) */
80
81int yynerrs;			/* number of errors */
82int yyerrflag;			/* error recovery flag */
83int yychar;			/* current input token number */
84
85
86
87#ifdef YYNMBCHARS
88#define YYLEX()		yycvtok(yylex())
89/*
90** yycvtok - return a token if i is a wchar_t value that exceeds 255.
91**	If i<255, i itself is the token.  If i>255 but the neither
92**	of the 30th or 31st bit is on, i is already a token.
93*/
94int yycvtok(int i)
95{
96	int first = 0;
97	int last = YYNMBCHARS - 1;
98	int mid;
99	wchar_t j;
100
101	if(i&0x60000000){/*Must convert to a token. */
102		if( yymbchars[last].character < i ){
103			return i;/*Giving up*/
104		}
105		while ((last>=first)&&(first>=0)) {/*Binary search loop*/
106			mid = (first+last)/2;
107			j = yymbchars[mid].character;
108			if( j==i ){/*Found*/
109				return yymbchars[mid].tvalue;
110			}else if( j<i ){
111				first = mid + 1;
112			}else{
113				last = mid -1;
114			}
115		}
116		/*No entry in the table.*/
117		return i;/* Giving up.*/
118	}else{/* i is already a token. */
119		return i;
120	}
121}
122#else/*!YYNMBCHARS*/
123#define YYLEX()		yylex()
124#endif/*!YYNMBCHARS*/
125
126/*
127** yyparse - return 0 if worked, 1 if syntax error not recovered from
128*/
129int yyparse(void)
130{
131	YYSTYPE *yypvt = 0;	/* top of value stack for $vars */
132
133#if defined(__cplusplus) || defined(lint)
134/*
135	hacks to please C++ and lint - goto's inside
136	switch should never be executed
137*/
138	static int __yaccpar_lint_hack__ = 0;
139	switch (__yaccpar_lint_hack__)
140	{
141		case 1: goto yyerrlab;
142		case 2: goto yynewstate;
143	}
144#endif
145
146	/*
147	** Initialize externals - yyparse may be called more than once
148	*/
149	yypv = &yyv[-1];
150	yyps = &yys[-1];
151	yystate = 0;
152	yytmp = 0;
153	yynerrs = 0;
154	yyerrflag = 0;
155	yychar = -1;
156
157#if YYMAXDEPTH <= 0
158	if (yymaxdepth <= 0)
159	{
160		if ((yymaxdepth = YYEXPAND(0)) <= 0)
161		{
162			yyerror("yacc initialization error");
163			YYABORT;
164		}
165	}
166#endif
167
168	{
169		YYSTYPE *yy_pv;	/* top of value stack */
170		int *yy_ps;		/* top of state stack */
171		int yy_state;		/* current state */
172		int  yy_n;		/* internal state number info */
173	goto yystack;	/* moved from 6 lines above to here to please C++ */
174
175		/*
176		** get globals into registers.
177		** branch to here only if YYBACKUP was called.
178		*/
179	yynewstate:
180		yy_pv = yypv;
181		yy_ps = yyps;
182		yy_state = yystate;
183		goto yy_newstate;
184
185		/*
186		** get globals into registers.
187		** either we just started, or we just finished a reduction
188		*/
189	yystack:
190		yy_pv = yypv;
191		yy_ps = yyps;
192		yy_state = yystate;
193
194		/*
195		** top of for (;;) loop while no reductions done
196		*/
197	yy_stack:
198		/*
199		** put a state and value onto the stacks
200		*/
201#if YYDEBUG
202		/*
203		** if debugging, look up token value in list of value vs.
204		** name pairs.  0 and negative (-1) are special values.
205		** Note: linear search is used since time is not a real
206		** consideration while debugging.
207		*/
208		if ( yydebug )
209		{
210			int yy_i;
211
212			printf( "State %d, token ", yy_state );
213			if ( yychar == 0 )
214				printf( "end-of-file\n" );
215			else if ( yychar < 0 )
216				printf( "-none-\n" );
217			else
218			{
219				for ( yy_i = 0; yytoks[yy_i].t_val >= 0;
220					yy_i++ )
221				{
222					if ( yytoks[yy_i].t_val == yychar )
223						break;
224				}
225				printf( "%s\n", yytoks[yy_i].t_name );
226			}
227		}
228#endif /* YYDEBUG */
229		if ( ++yy_ps >= &yys[ yymaxdepth ] )	/* room on stack? */
230		{
231			/*
232			** reallocate and recover.  Note that pointers
233			** have to be reset, or bad things will happen
234			*/
235			long yyps_index = (yy_ps - yys);
236			long yypv_index = (yy_pv - yyv);
237			long yypvt_index = (yypvt - yyv);
238			int yynewmax;
239#ifdef YYEXPAND
240			yynewmax = YYEXPAND(yymaxdepth);
241#else
242			yynewmax = 2 * yymaxdepth;	/* double table size */
243			if (yymaxdepth == YYMAXDEPTH)	/* first time growth */
244			{
245				char *newyys = (char *)YYNEW(int);
246				char *newyyv = (char *)YYNEW(YYSTYPE);
247				if (newyys != 0 && newyyv != 0)
248				{
249					yys = YYCOPY(newyys, yys, int);
250					yyv = YYCOPY(newyyv, yyv, YYSTYPE);
251				}
252				else
253					yynewmax = 0;	/* failed */
254			}
255			else				/* not first time */
256			{
257				yys = YYENLARGE(yys, int);
258				yyv = YYENLARGE(yyv, YYSTYPE);
259				if (yys == 0 || yyv == 0)
260					yynewmax = 0;	/* failed */
261			}
262#endif
263			if (yynewmax <= yymaxdepth)	/* tables not expanded */
264			{
265				yyerror( "yacc stack overflow" );
266				YYABORT;
267			}
268			yymaxdepth = yynewmax;
269
270			yy_ps = yys + yyps_index;
271			yy_pv = yyv + yypv_index;
272			yypvt = yyv + yypvt_index;
273		}
274		*yy_ps = yy_state;
275		*++yy_pv = yyval;
276
277		/*
278		** we have a new state - find out what to do
279		*/
280	yy_newstate:
281		if ( ( yy_n = yypact[ yy_state ] ) <= YYFLAG )
282			goto yydefault;		/* simple state */
283#if YYDEBUG
284		/*
285		** if debugging, need to mark whether new token grabbed
286		*/
287		yytmp = yychar < 0;
288#endif
289		if ( ( yychar < 0 ) && ( ( yychar = YYLEX() ) < 0 ) )
290			yychar = 0;		/* reached EOF */
291#if YYDEBUG
292		if ( yydebug && yytmp )
293		{
294			int yy_i;
295
296			printf( "Received token " );
297			if ( yychar == 0 )
298				printf( "end-of-file\n" );
299			else if ( yychar < 0 )
300				printf( "-none-\n" );
301			else
302			{
303				for ( yy_i = 0; yytoks[yy_i].t_val >= 0;
304					yy_i++ )
305				{
306					if ( yytoks[yy_i].t_val == yychar )
307						break;
308				}
309				printf( "%s\n", yytoks[yy_i].t_name );
310			}
311		}
312#endif /* YYDEBUG */
313		if ( ( ( yy_n += yychar ) < 0 ) || ( yy_n >= YYLAST ) )
314			goto yydefault;
315		if ( yychk[ yy_n = yyact[ yy_n ] ] == yychar )	/*valid shift*/
316		{
317			yychar = -1;
318			yyval = yylval;
319			yy_state = yy_n;
320			if ( yyerrflag > 0 )
321				yyerrflag--;
322			goto yy_stack;
323		}
324
325	yydefault:
326		if ( ( yy_n = yydef[ yy_state ] ) == -2 )
327		{
328#if YYDEBUG
329			yytmp = yychar < 0;
330#endif
331			if ( ( yychar < 0 ) && ( ( yychar = YYLEX() ) < 0 ) )
332				yychar = 0;		/* reached EOF */
333#if YYDEBUG
334			if ( yydebug && yytmp )
335			{
336				int yy_i;
337
338				printf( "Received token " );
339				if ( yychar == 0 )
340					printf( "end-of-file\n" );
341				else if ( yychar < 0 )
342					printf( "-none-\n" );
343				else
344				{
345					for ( yy_i = 0;
346						yytoks[yy_i].t_val >= 0;
347						yy_i++ )
348					{
349						if ( yytoks[yy_i].t_val
350							== yychar )
351						{
352							break;
353						}
354					}
355					printf( "%s\n", yytoks[yy_i].t_name );
356				}
357			}
358#endif /* YYDEBUG */
359			/*
360			** look through exception table
361			*/
362			{
363				YYCONST int *yyxi = yyexca;
364
365				while ( ( *yyxi != -1 ) ||
366					( yyxi[1] != yy_state ) )
367				{
368					yyxi += 2;
369				}
370				while ( ( *(yyxi += 2) >= 0 ) &&
371					( *yyxi != yychar ) )
372					;
373				if ( ( yy_n = yyxi[1] ) < 0 )
374					YYACCEPT;
375			}
376		}
377
378		/*
379		** check for syntax error
380		*/
381		if ( yy_n == 0 )	/* have an error */
382		{
383			/* no worry about speed here! */
384			switch ( yyerrflag )
385			{
386			case 0:		/* new error */
387				yyerror( "syntax error" );
388				goto skip_init;
389			yyerrlab:
390				/*
391				** get globals into registers.
392				** we have a user generated syntax type error
393				*/
394				yy_pv = yypv;
395				yy_ps = yyps;
396				yy_state = yystate;
397			skip_init:
398				yynerrs++;
399				/* FALLTHRU */
400			case 1:
401			case 2:		/* incompletely recovered error */
402					/* try again... */
403				yyerrflag = 3;
404				/*
405				** find state where "error" is a legal
406				** shift action
407				*/
408				while ( yy_ps >= yys )
409				{
410					yy_n = yypact[ *yy_ps ] + YYERRCODE;
411					if ( yy_n >= 0 && yy_n < YYLAST &&
412						yychk[yyact[yy_n]] == YYERRCODE)					{
413						/*
414						** simulate shift of "error"
415						*/
416						yy_state = yyact[ yy_n ];
417						goto yy_stack;
418					}
419					/*
420					** current state has no shift on
421					** "error", pop stack
422					*/
423#if YYDEBUG
424#	define _POP_ "Error recovery pops state %d, uncovers state %d\n"
425					if ( yydebug )
426						printf( _POP_, *yy_ps,
427							yy_ps[-1] );
428#	undef _POP_
429#endif
430					yy_ps--;
431					yy_pv--;
432				}
433				/*
434				** there is no state on stack with "error" as
435				** a valid shift.  give up.
436				*/
437				YYABORT;
438			case 3:		/* no shift yet; eat a token */
439#if YYDEBUG
440				/*
441				** if debugging, look up token in list of
442				** pairs.  0 and negative shouldn't occur,
443				** but since timing doesn't matter when
444				** debugging, it doesn't hurt to leave the
445				** tests here.
446				*/
447				if ( yydebug )
448				{
449					int yy_i;
450
451					printf( "Error recovery discards " );
452					if ( yychar == 0 )
453						printf( "token end-of-file\n" );
454					else if ( yychar < 0 )
455						printf( "token -none-\n" );
456					else
457					{
458						for ( yy_i = 0;
459							yytoks[yy_i].t_val >= 0;
460							yy_i++ )
461						{
462							if ( yytoks[yy_i].t_val
463								== yychar )
464							{
465								break;
466							}
467						}
468						printf( "token %s\n",
469							yytoks[yy_i].t_name );
470					}
471				}
472#endif /* YYDEBUG */
473				if ( yychar == 0 )	/* reached EOF. quit */
474					YYABORT;
475				yychar = -1;
476				goto yy_newstate;
477			}
478		}/* end if ( yy_n == 0 ) */
479		/*
480		** reduction by production yy_n
481		** put stack tops, etc. so things right after switch
482		*/
483#if YYDEBUG
484		/*
485		** if debugging, print the string that is the user's
486		** specification of the reduction which is just about
487		** to be done.
488		*/
489		if ( yydebug )
490			printf( "Reduce by (%d) \"%s\"\n",
491				yy_n, yyreds[ yy_n ] );
492#endif
493		yytmp = yy_n;			/* value to switch over */
494		yypvt = yy_pv;			/* $vars top of value stack */
495		/*
496		** Look in goto table for next state
497		** Sorry about using yy_state here as temporary
498		** register variable, but why not, if it works...
499		** If yyr2[ yy_n ] doesn't have the low order bit
500		** set, then there is no action to be done for
501		** this reduction.  So, no saving & unsaving of
502		** registers done.  The only difference between the
503		** code just after the if and the body of the if is
504		** the goto yy_stack in the body.  This way the test
505		** can be made before the choice of what to do is needed.
506		*/
507		{
508			/* length of production doubled with extra bit */
509			int yy_len = yyr2[ yy_n ];
510
511			if ( !( yy_len & 01 ) )
512			{
513				yy_len >>= 1;
514				yyval = ( yy_pv -= yy_len )[1];	/* $$ = $1 */
515				yy_state = yypgo[ yy_n = yyr1[ yy_n ] ] +
516					*( yy_ps -= yy_len ) + 1;
517				if ( yy_state >= YYLAST ||
518					yychk[ yy_state =
519					yyact[ yy_state ] ] != -yy_n )
520				{
521					yy_state = yyact[ yypgo[ yy_n ] ];
522				}
523				goto yy_stack;
524			}
525			yy_len >>= 1;
526			yyval = ( yy_pv -= yy_len )[1];	/* $$ = $1 */
527			yy_state = yypgo[ yy_n = yyr1[ yy_n ] ] +
528				*( yy_ps -= yy_len ) + 1;
529			if ( yy_state >= YYLAST ||
530				yychk[ yy_state = yyact[ yy_state ] ] != -yy_n )
531			{
532				yy_state = yyact[ yypgo[ yy_n ] ];
533			}
534		}
535					/* save until reenter driver code */
536		yystate = yy_state;
537		yyps = yy_ps;
538		yypv = yy_pv;
539	}
540	/*
541	** code supplied by user is placed in this switch
542	*/
543	switch( yytmp )
544	{
545		$A
546	}
547	goto yystack;		/* reset registers in driver code */
548}
549