9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

yaccpar (5229B)


      1 #define YYFLAG 		-1000
      2 #define YYERROR		goto yyerrlab
      3 #define YYACCEPT	return(0)
      4 #define YYABORT		return(1)
      5 #define	yyclearin	yychar = -1
      6 #define	yyerrok		yyerrflag = 0
      7 
      8 #ifdef	yydebug
      9 #include	"y.debug"
     10 #else
     11 #define	yydebug		0
     12 static	const	char*	yytoknames[1];		/* for debugging */
     13 static	const	char*	yystates[1];		/* for debugging */
     14 #endif
     15 
     16 /*	parser for yacc output	*/
     17 #ifdef YYARG
     18 #define	yynerrs		yyarg->yynerrs
     19 #define	yyerrflag	yyarg->yyerrflag
     20 #define yyval		yyarg->yyval
     21 #define yylval		yyarg->yylval
     22 #else
     23 int	yynerrs = 0;		/* number of errors */
     24 int	yyerrflag = 0;		/* error recovery flag */
     25 #endif
     26 
     27 extern	int	fprint(int, char*, ...);
     28 extern	int	sprint(char*, char*, ...);
     29 
     30 static const char*
     31 yytokname(int yyc)
     32 {
     33 	static char x[10];
     34 
     35 	if(yyc > 0 && yyc <= sizeof(yytoknames)/sizeof(yytoknames[0]))
     36 	if(yytoknames[yyc-1])
     37 		return yytoknames[yyc-1];
     38 	sprint(x, "<%d>", yyc);
     39 	return x;
     40 }
     41 
     42 static const char*
     43 yystatname(int yys)
     44 {
     45 	static char x[10];
     46 
     47 	if(yys >= 0 && yys < sizeof(yystates)/sizeof(yystates[0]))
     48 	if(yystates[yys])
     49 		return yystates[yys];
     50 	sprint(x, "<%d>\n", yys);
     51 	return x;
     52 }
     53 
     54 static long
     55 #ifdef YYARG
     56 yylex1(struct Yyarg *yyarg)
     57 #else
     58 yylex1(void)
     59 #endif
     60 {
     61 	long yychar;
     62 	const long *t3p;
     63 	int c;
     64 
     65 #ifdef YYARG	
     66 	yychar = yylex(yyarg);
     67 #else
     68 	yychar = yylex();
     69 #endif
     70 	if(yychar <= 0) {
     71 		c = yytok1[0];
     72 		goto out;
     73 	}
     74 	if(yychar < sizeof(yytok1)/sizeof(yytok1[0])) {
     75 		c = yytok1[yychar];
     76 		goto out;
     77 	}
     78 	if(yychar >= YYPRIVATE)
     79 		if(yychar < YYPRIVATE+sizeof(yytok2)/sizeof(yytok2[0])) {
     80 			c = yytok2[yychar-YYPRIVATE];
     81 			goto out;
     82 		}
     83 	for(t3p=yytok3;; t3p+=2) {
     84 		c = t3p[0];
     85 		if(c == yychar) {
     86 			c = t3p[1];
     87 			goto out;
     88 		}
     89 		if(c == 0)
     90 			break;
     91 	}
     92 	c = 0;
     93 
     94 out:
     95 	if(c == 0)
     96 		c = yytok2[1];	/* unknown char */
     97 	if(yydebug >= 3)
     98 		fprint(2, "lex %.4lux %s\n", yychar, yytokname(c));
     99 	return c;
    100 }
    101 
    102 int
    103 #ifdef YYARG
    104 yyparse(struct Yyarg *yyarg)
    105 #else
    106 yyparse(void)
    107 #endif
    108 {
    109 	struct
    110 	{
    111 		YYSTYPE	yyv;
    112 		int	yys;
    113 	} yys[YYMAXDEPTH], *yyp, *yypt;
    114 	const short *yyxi;
    115 	int yyj, yym, yystate, yyn, yyg;
    116 	long yychar;
    117 #ifndef YYARG
    118 	YYSTYPE save1, save2;
    119 	int save3, save4;
    120 
    121 	save1 = yylval;
    122 	save2 = yyval;
    123 	save3 = yynerrs;
    124 	save4 = yyerrflag;
    125 #endif
    126 
    127 	yystate = 0;
    128 	yychar = -1;
    129 	yynerrs = 0;
    130 	yyerrflag = 0;
    131 	yyp = &yys[-1];
    132 	goto yystack;
    133 
    134 ret0:
    135 	yyn = 0;
    136 	goto ret;
    137 
    138 ret1:
    139 	yyn = 1;
    140 	goto ret;
    141 
    142 ret:
    143 #ifndef YYARG
    144 	yylval = save1;
    145 	yyval = save2;
    146 	yynerrs = save3;
    147 	yyerrflag = save4;
    148 #endif
    149 	return yyn;
    150 
    151 yystack:
    152 	/* put a state and value onto the stack */
    153 	if(yydebug >= 4)
    154 		fprint(2, "char %s in %s", yytokname(yychar), yystatname(yystate));
    155 
    156 	yyp++;
    157 	if(yyp >= &yys[YYMAXDEPTH]) {
    158 		yyerror("yacc stack overflow");
    159 		goto ret1;
    160 	}
    161 	yyp->yys = yystate;
    162 	yyp->yyv = yyval;
    163 
    164 yynewstate:
    165 	yyn = yypact[yystate];
    166 	if(yyn <= YYFLAG)
    167 		goto yydefault; /* simple state */
    168 	if(yychar < 0)
    169 #ifdef YYARG
    170 		yychar = yylex1(yyarg);
    171 #else
    172 		yychar = yylex1();
    173 #endif
    174 	yyn += yychar;
    175 	if(yyn < 0 || yyn >= YYLAST)
    176 		goto yydefault;
    177 	yyn = yyact[yyn];
    178 	if(yychk[yyn] == yychar) { /* valid shift */
    179 		yychar = -1;
    180 		yyval = yylval;
    181 		yystate = yyn;
    182 		if(yyerrflag > 0)
    183 			yyerrflag--;
    184 		goto yystack;
    185 	}
    186 
    187 yydefault:
    188 	/* default state action */
    189 	yyn = yydef[yystate];
    190 	if(yyn == -2) {
    191 		if(yychar < 0)
    192 #ifdef YYARG
    193 		yychar = yylex1(yyarg);
    194 #else
    195 		yychar = yylex1();
    196 #endif
    197 
    198 		/* look through exception table */
    199 		for(yyxi=yyexca;; yyxi+=2)
    200 			if(yyxi[0] == -1 && yyxi[1] == yystate)
    201 				break;
    202 		for(yyxi += 2;; yyxi += 2) {
    203 			yyn = yyxi[0];
    204 			if(yyn < 0 || yyn == yychar)
    205 				break;
    206 		}
    207 		yyn = yyxi[1];
    208 		if(yyn < 0)
    209 			goto ret0;
    210 	}
    211 	if(yyn == 0) {
    212 		/* error ... attempt to resume parsing */
    213 		switch(yyerrflag) {
    214 		case 0:   /* brand new error */
    215 			yyerror("syntax error");
    216 			if(yydebug >= 1) {
    217 				fprint(2, "%s", yystatname(yystate));
    218 				fprint(2, "saw %s\n", yytokname(yychar));
    219 			}
    220 			goto yyerrlab;
    221 		yyerrlab:
    222 			yynerrs++;
    223 
    224 		case 1:
    225 		case 2: /* incompletely recovered error ... try again */
    226 			yyerrflag = 3;
    227 
    228 			/* find a state where "error" is a legal shift action */
    229 			while(yyp >= yys) {
    230 				yyn = yypact[yyp->yys] + YYERRCODE;
    231 				if(yyn >= 0 && yyn < YYLAST) {
    232 					yystate = yyact[yyn];  /* simulate a shift of "error" */
    233 					if(yychk[yystate] == YYERRCODE)
    234 						goto yystack;
    235 				}
    236 
    237 				/* the current yyp has no shift onn "error", pop stack */
    238 				if(yydebug >= 2)
    239 					fprint(2, "error recovery pops state %d, uncovers %d\n",
    240 						yyp->yys, (yyp-1)->yys );
    241 				yyp--;
    242 			}
    243 			/* there is no state on the stack with an error shift ... abort */
    244 			goto ret1;
    245 
    246 		case 3:  /* no shift yet; clobber input char */
    247 			if(yydebug >= 2)
    248 				fprint(2, "error recovery discards %s\n", yytokname(yychar));
    249 			if(yychar == YYEOFCODE)
    250 				goto ret1;
    251 			yychar = -1;
    252 			goto yynewstate;   /* try again in the same state */
    253 		}
    254 	}
    255 
    256 	/* reduction by production yyn */
    257 	if(yydebug >= 2)
    258 		fprint(2, "reduce %d in:\n\t%s", yyn, yystatname(yystate));
    259 
    260 	yypt = yyp;
    261 	yyp -= yyr2[yyn];
    262 	yyval = (yyp+1)->yyv;
    263 	yym = yyn;
    264 
    265 	/* consult goto table to find next state */
    266 	yyn = yyr1[yyn];
    267 	yyg = yypgo[yyn];
    268 	yyj = yyg + yyp->yys + 1;
    269 
    270 	if(yyj >= YYLAST || yychk[yystate=yyact[yyj]] != -yyn)
    271 		yystate = yyact[yyg];
    272 	switch(yym) {
    273 		$A
    274 	}
    275 	goto yystack;  /* stack new state and value */
    276 }