Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/m4/m4y.c

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



#line	2	"/usr/steve/m4/m4y.y"
extern long	evalval;
#define	YYSTYPE	long
extern	int	yyerrflag;
#ifndef	YYMAXDEPTH
#define	YYMAXDEPTH	150
#endif
#ifndef	YYSTYPE
#define	YYSTYPE	int
#endif
YYSTYPE	yylval;
YYSTYPE	yyval;
#define	DIGITS	57346
#define	GT	57347
#define	GE	57348
#define	LT	57349
#define	LE	57350
#define	NE	57351
#define	EQ	57352
#define	POWER	57353
#define	UMINUS	57354
#define YYEOFCODE 1
#define YYERRCODE 2

#line	42	"/usr/steve/m4/m4y.y"


int
peek(int c, int r1, int r2)
{
	extern char *pe;

	if (*++pe != c)
		return(r2);
	++pe;
	return(r1);
}

int
yylex(void)
{
	extern char *pe;

	while (*pe==' ' || *pe=='\t' || *pe=='\n')
		pe++;
	switch(*pe) {
	case '\0':
	case '+':
	case '-':
	case '/':
	case '%':
	case '(':
	case ')':
		return(*pe++);
	case '^':
		pe++;
		return(POWER);
	case '*':
		return(peek('*', POWER, '*'));
	case '>':
		return(peek('=', GE, GT));
	case '<':
		return(peek('=', LE, LT));
	case '=':
		return(peek('=', EQ, EQ));
	case '|':
		return(peek('|', '|', '|'));
	case '&':
		return(peek('&', '&', '&'));
	case '!':
		return(peek('=', NE, '!'));
	default:
		evalval = 0;
		while (*pe >= '0' && *pe <= '9')
			evalval = evalval*10 + *pe++ - '0';
		return(DIGITS);
	}
}

int
yyerror(char *s)
{
	USED(s);
	return 0;
}
short	yyexca[] =
{-1, 1,
	1, -1,
	-2, 0,
-1, 28,
	8, 0,
	9, 0,
	10, 0,
	11, 0,
	12, 0,
	13, 0,
	-2, 6,
-1, 29,
	8, 0,
	9, 0,
	10, 0,
	11, 0,
	12, 0,
	13, 0,
	-2, 7,
-1, 30,
	8, 0,
	9, 0,
	10, 0,
	11, 0,
	12, 0,
	13, 0,
	-2, 8,
-1, 31,
	8, 0,
	9, 0,
	10, 0,
	11, 0,
	12, 0,
	13, 0,
	-2, 9,
-1, 32,
	8, 0,
	9, 0,
	10, 0,
	11, 0,
	12, 0,
	13, 0,
	-2, 10,
-1, 33,
	8, 0,
	9, 0,
	10, 0,
	11, 0,
	12, 0,
	13, 0,
	-2, 11,
};
#define	YYNPROD	22
#define	YYPRIVATE 57344
#define	YYLAST	97
short	yyact[] =
{
   2,  21,   1,   0,  22,  23,  24,  25,   0,  26,
  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
  37,  38,  39,   8,   9,   0,  12,  13,  14,  15,
  11,  10,  16,  17,  18,  19,  20,  21,   8,   9,
  40,  12,  13,  14,  15,  11,  10,  16,  17,  18,
  19,  20,  21,   9,   0,  12,  13,  14,  15,  11,
  10,  16,  17,  18,  19,  20,  21,  12,  13,  14,
  15,  11,  10,  16,  17,  18,  19,  20,  21,   7,
   0,   0,   3,  16,  17,  18,  19,  20,  21,   6,
   5,   0,  18,  19,  20,  21,   4
};
short	yypact[] =
{
  75,-1000,  33,  75,  75,  75,  75,-1000,  75,  75,
  75,  75,  75,  75,  75,  75,  75,  75,  75,  75,
  75,  75,  59,  18,-1000,-1000,  47,  59,  69,  69,
  69,  69,  69,  69,  76,  76, -18, -18, -18, -18,
-1000
};
short	yypgo[] =
{
   0,   2,   0
};
short	yyr1[] =
{
   0,   1,   1,   2,   2,   2,   2,   2,   2,   2,
   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,
   2,   2
};
short	yyr2[] =
{
   0,   1,   0,   3,   3,   2,   3,   3,   3,   3,
   3,   3,   3,   3,   3,   3,   3,   3,   3,   2,
   2,   1
};
short	yychk[] =
{
-1000,  -1,  -2,   7,  21,  15,  14,   4,   5,   6,
  13,  12,   8,   9,  10,  11,  14,  15,  16,  17,
  18,  19,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,
  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,
  22
};
short	yydef[] =
{
   2,  -2,   1,   0,   0,   0,   0,  21,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   5,   0,  19,  20,   3,   4,  -2,  -2,
  -2,  -2,  -2,  -2,  12,  13,  14,  15,  16,  18,
  17
};
short	yytok1[] =
{
   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   7,   0,   0,   0,  18,   6,   0,
  21,  22,  16,  14,   0,  15,   0,  17,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   5
};
short	yytok2[] =
{
   2,   3,   4,   8,   9,  10,  11,  12,  13,  19,
  20
};
long	yytok3[] =
{
   0
};
#define YYFLAG 		-1000
#define	yyclearin	yychar = -1
#define	yyerrok		yyerrflag = 0

#ifdef	yydebug
#include	"y.debug"
#else
#define	yydebug		0
char*	yytoknames[1];		/* for debugging */
char*	yystates[1];		/* for debugging */
#endif

/*	parser for yacc output	*/

int	yynerrs = 0;		/* number of errors */
int	yyerrflag = 0;		/* error recovery flag */

extern	int	fprint(int, char*, ...);
extern	int	sprint(char*, char*, ...);

char*
yytokname(int yyc)
{
	static char x[10];

	if(yyc > 0 && yyc <= sizeof(yytoknames)/sizeof(yytoknames[0]))
	if(yytoknames[yyc-1])
		return yytoknames[yyc-1];
	sprint(x, "<%d>", yyc);
	return x;
}

char*
yystatname(int yys)
{
	static char x[10];

	if(yys >= 0 && yys < sizeof(yystates)/sizeof(yystates[0]))
	if(yystates[yys])
		return yystates[yys];
	sprint(x, "<%d>\n", yys);
	return x;
}

long
yylex1(void)
{
	long yychar;
	long *t3p;
	int c;

	yychar = yylex();
	if(yychar <= 0) {
		c = yytok1[0];
		goto out;
	}
	if(yychar < sizeof(yytok1)/sizeof(yytok1[0])) {
		c = yytok1[yychar];
		goto out;
	}
	if(yychar >= YYPRIVATE)
		if(yychar < YYPRIVATE+sizeof(yytok2)/sizeof(yytok2[0])) {
			c = yytok2[yychar-YYPRIVATE];
			goto out;
		}
	for(t3p=yytok3;; t3p+=2) {
		c = t3p[0];
		if(c == yychar) {
			c = t3p[1];
			goto out;
		}
		if(c == 0)
			break;
	}
	c = 0;

out:
	if(c == 0)
		c = yytok2[1];	/* unknown char */
	if(yydebug >= 3)
		fprint(2, "lex %.4lux %s\n", yychar, yytokname(c));
	return c;
}

int
yyparse(void)
{
	struct
	{
		YYSTYPE	yyv;
		int	yys;
	} yys[YYMAXDEPTH], *yyp, *yypt;
	short *yyxi;
	int yyj, yym, yystate, yyn, yyg;
	long yychar;
	YYSTYPE save1, save2;
	int save3, save4;

	save1 = yylval;
	save2 = yyval;
	save3 = yynerrs;
	save4 = yyerrflag;

	yystate = 0;
	yychar = -1;
	yynerrs = 0;
	yyerrflag = 0;
	yyp = &yys[-1];
	goto yystack;

ret0:
	yyn = 0;
	goto ret;

ret1:
	yyn = 1;
	goto ret;

ret:
	yylval = save1;
	yyval = save2;
	yynerrs = save3;
	yyerrflag = save4;
	return yyn;

yystack:
	/* put a state and value onto the stack */
	if(yydebug >= 4)
		fprint(2, "char %s in %s", yytokname(yychar), yystatname(yystate));

	yyp++;
	if(yyp >= &yys[YYMAXDEPTH]) {
		yyerror("yacc stack overflow");
		goto ret1;
	}
	yyp->yys = yystate;
	yyp->yyv = yyval;

yynewstate:
	yyn = yypact[yystate];
	if(yyn <= YYFLAG)
		goto yydefault; /* simple state */
	if(yychar < 0)
		yychar = yylex1();
	yyn += yychar;
	if(yyn < 0 || yyn >= YYLAST)
		goto yydefault;
	yyn = yyact[yyn];
	if(yychk[yyn] == yychar) { /* valid shift */
		yychar = -1;
		yyval = yylval;
		yystate = yyn;
		if(yyerrflag > 0)
			yyerrflag--;
		goto yystack;
	}

yydefault:
	/* default state action */
	yyn = yydef[yystate];
	if(yyn == -2) {
		if(yychar < 0)
			yychar = yylex1();

		/* look through exception table */
		for(yyxi=yyexca;; yyxi+=2)
			if(yyxi[0] == -1 && yyxi[1] == yystate)
				break;
		for(yyxi += 2;; yyxi += 2) {
			yyn = yyxi[0];
			if(yyn < 0 || yyn == yychar)
				break;
		}
		yyn = yyxi[1];
		if(yyn < 0)
			goto ret0;
	}
	if(yyn == 0) {
		/* error ... attempt to resume parsing */
		switch(yyerrflag) {
		case 0:   /* brand new error */
			yyerror("syntax error");
			yynerrs++;
			if(yydebug >= 1) {
				fprint(2, "%s", yystatname(yystate));
				fprint(2, "saw %s\n", yytokname(yychar));
			}

		case 1:
		case 2: /* incompletely recovered error ... try again */
			yyerrflag = 3;

			/* find a state where "error" is a legal shift action */
			while(yyp >= yys) {
				yyn = yypact[yyp->yys] + YYERRCODE;
				if(yyn >= 0 && yyn < YYLAST) {
					yystate = yyact[yyn];  /* simulate a shift of "error" */
					if(yychk[yystate] == YYERRCODE)
						goto yystack;
				}

				/* the current yyp has no shift onn "error", pop stack */
				if(yydebug >= 2)
					fprint(2, "error recovery pops state %d, uncovers %d\n",
						yyp->yys, (yyp-1)->yys );
				yyp--;
			}
			/* there is no state on the stack with an error shift ... abort */
			goto ret1;

		case 3:  /* no shift yet; clobber input char */
			if(yydebug >= 2)
				fprint(2, "error recovery discards %s\n", yytokname(yychar));
			if(yychar == YYEOFCODE)
				goto ret1;
			yychar = -1;
			goto yynewstate;   /* try again in the same state */
		}
	}

	/* reduction by production yyn */
	if(yydebug >= 2)
		fprint(2, "reduce %d in:\n\t%s", yyn, yystatname(yystate));

	yypt = yyp;
	yyp -= yyr2[yyn];
	yyval = (yyp+1)->yyv;
	yym = yyn;

	/* consult goto table to find next state */
	yyn = yyr1[yyn];
	yyg = yypgo[yyn];
	yyj = yyg + yyp->yys + 1;

	if(yyj >= YYLAST || yychk[yystate=yyact[yyj]] != -yyn)
		yystate = yyact[yyg];
	switch(yym) {
		
case 1:
#line	17	"/usr/steve/m4/m4y.y"
{ evalval = yypt[-0].yyv; } break;
case 2:
#line	18	"/usr/steve/m4/m4y.y"
{ evalval = 0; } break;
case 3:
#line	21	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-2].yyv!=0 || yypt[-0].yyv!=0) ? 1 : 0; } break;
case 4:
#line	22	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-2].yyv!=0 && yypt[-0].yyv!=0) ? 1 : 0; } break;
case 5:
#line	23	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-0].yyv == 0; } break;
case 6:
#line	24	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-2].yyv == yypt[-0].yyv; } break;
case 7:
#line	25	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-2].yyv != yypt[-0].yyv; } break;
case 8:
#line	26	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-2].yyv > yypt[-0].yyv; } break;
case 9:
#line	27	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-2].yyv >= yypt[-0].yyv; } break;
case 10:
#line	28	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-2].yyv < yypt[-0].yyv; } break;
case 11:
#line	29	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-2].yyv <= yypt[-0].yyv; } break;
case 12:
#line	30	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-2].yyv+yypt[-0].yyv); } break;
case 13:
#line	31	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-2].yyv-yypt[-0].yyv); } break;
case 14:
#line	32	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-2].yyv*yypt[-0].yyv); } break;
case 15:
#line	33	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-2].yyv/yypt[-0].yyv); } break;
case 16:
#line	34	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-2].yyv%yypt[-0].yyv); } break;
case 17:
#line	35	"/usr/steve/m4/m4y.y"
{ yyval = (yypt[-1].yyv); } break;
case 18:
#line	36	"/usr/steve/m4/m4y.y"
{ for (yyval=1; yypt[-0].yyv-->0; yyval *= yypt[-2].yyv); } break;
case 19:
#line	37	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-0].yyv-1; yyval = -yypt[-0].yyv; } break;
case 20:
#line	38	"/usr/steve/m4/m4y.y"
{ yyval = yypt[-0].yyv-1; yyval = yypt[-0].yyv; } break;
case 21:
#line	39	"/usr/steve/m4/m4y.y"
{ yyval = evalval; } break;
	}
	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 webmaster@9p.io.