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