Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/c++/cfront/yaccpars

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#define YYFLAG 		-1000
#define YYERROR		goto yyerrlab
#define YYACCEPT	return(0)
#define YYABORT		return(1)
#define	yydebug		0

/*	parser for yacc output	*/

YYSTYPE yyv[YYMAXDEPTH]; 	/* where the values are stored */
int	yychar = -1; 		/* current input token number */
int	yynerrs = 0;		/* number of errors */
short	yyerrflag = 0;		/* error recovery flag */

char	*yytoknames[1];		/* for debugging */
char	*yystates[1];		/* for debugging */

int
yyparse(void)
{
	short yys[YYMAXDEPTH], *yyps, *yyxi;
	int yyj, yym, yystate, yyn;
	YYSTYPE *yypvt, *yypv;

	yystate = 0;
	yychar = -1;
	yynerrs = 0;
	yyerrflag = 0;
	yyps = &yys[-1];
	yypv = &yyv[-1];

yystack:
	/* put a state and value onto the stack */
	if(yydebug >= 3)
		if(yychar < 0 || yytoknames[yychar] == 0)
			printf("char %d in %s", yychar, yystates[yystate]);
		else
			printf("%s in %s", yytoknames[yychar], yystates[yystate]);
	if(++yyps >= &yys[YYMAXDEPTH]) { 
		yyerror("yacc stack overflow"); 
		return 1; 
	}
	*yyps = yystate;
	++yypv;
	*yypv = yyval;

yynewstate:
	yyn = yypact[yystate];
	if(yyn <= YYFLAG)
		goto yydefault; /* simple state */
	if(yychar < 0) {
		yychar = yylex();
		if(yydebug >= 2) {
			if(yychar <= 0)
				printf("lex EOF\n");
			else
			if(yytoknames[yychar])
				printf("lex %s\n", yytoknames[yychar]);
			else
				printf("lex (%c)\n", yychar);
		}
		if(yychar < 0)
			yychar = 0;
	}
	if((yyn += yychar) < 0 || yyn >= YYLAST)
		goto yydefault;
	if(yychk[yyn=yyact[yyn]] == yychar) { /* valid shift */
		yychar = -1;
		yyval = yylval;
		yystate = yyn;
		if(yyerrflag > 0)
			--yyerrflag;
		goto yystack;
	}

yydefault:
	/* default state action */
	if((yyn=yydef[yystate]) == -2) {
		if(yychar < 0) {
			yychar = yylex();
			if(yydebug >= 2)
				if(yychar < 0)
					printf("lex EOF\n");
				else
					printf("lex %s\n", yytoknames[yychar]);
			if(yychar < 0)
				yychar = 0;
		}
		/* look through exception table */
		for(yyxi = yyexca; *yyxi != -1 || yyxi[1] != yystate; yyxi += 2)
			;
		while(*(yyxi+=2) >= 0)
			if(*yyxi == yychar)
				break;
		if((yyn = yyxi[1]) < 0)
			return 0;   /* accept */
	}
	if(yyn == 0) {
		/* error ... attempt to resume parsing */
		switch(yyerrflag) {
		case 0:   /* brand new error */
			yyerror("syntax error");
			if(yydebug) {
				printf("%s", yystates[yystate]);
				if(yytoknames[yychar])
					printf("saw %s\n", yytoknames[yychar]);
				else
				if(yychar >= ' ' && yychar < 0177)
					printf("saw `%c'\n", yychar);
				else
				if(yychar == 0)
					printf("saw EOF\n");
				else
					printf("saw char 0%o\n", yychar);
			}
			++yynerrs;

		case 1:
		case 2: /* incompletely recovered error ... try again */
			yyerrflag = 3;
			/* find a state where "error" is a legal shift action */
			while(yyps >= yys) {
				yyn = yypact[*yyps] + YYERRCODE;
				if(yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE) {
					yystate = yyact[yyn];  /* simulate a shift of "error" */
					goto yystack;
				}
				/* the current yyps has no shift onn "error", pop stack */
				if(yydebug)
					printf("error recovery pops state %d, uncovers %d\n",
						*yyps, yyps[-1] );
				--yyps;
				--yypv;
			}
			/* there is no state on the stack with an error shift ... abort */
			return 1;

		case 3:  /* no shift yet; clobber input char */
			if(yydebug) {
				printf("error recovery discards ");
				if(yytoknames[yychar])
					printf("%s\n", yytoknames[yychar]);
				else
				if(yychar >= ' ' && yychar < 0177)
					printf("`%c'\n", yychar);
				else
				if(yychar == 0)
					printf("EOF\n");
				else
					printf("char 0%o\n", yychar);
			}
			if(yychar == 0)
				return 1; /* don't discard EOF, quit */
			yychar = -1;
			goto yynewstate;   /* try again in the same state */
		}
	}
	/* reduction by production yyn */
	if(yydebug) {
		char *s;

		printf("reduce %d in:\n\t", yyn);
		for(s = yystates[yystate]; *s; s++) {
			printf("%c", *s);
			if(*s == '\n' && *(s+1))
				printf("\t");
		}
	}
	yyps -= yyr2[yyn];
	yypvt = yypv;
	yypv -= yyr2[yyn];
	yyval = yypv[1];
	yym = yyn;

	/* consult goto table to find next state */
	yyn = yyr1[yyn];
	yyj = yypgo[yyn] + *yyps + 1;
	if(yyj >= YYLAST || yychk[yystate=yyact[yyj]] != -yyn)
		yystate = yyact[yypgo[yyn]];
	switch(yym) {
		$A
	}
	goto yystack;  /* stack new state and value */
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].