Node *primary(void) { Node *np; switch (rtok) { case CHAR: np = op2(CHAR, NIL, itonp(rlxval)); rtok = relex(); return (unary(np)); case ALL: rtok = relex(); return (unary(op2(ALL, NIL, NIL))); case EMPTYRE: rtok = relex(); return (unary(op2(ALL, NIL, NIL))); case DOT: rtok = relex(); return (unary(op2(DOT, NIL, NIL))); case CCL: np = op2(CCL, NIL, (Node*) cclenter((char *) rlxstr)); rtok = relex(); return (unary(np)); case NCCL: np = op2(NCCL, NIL, (Node *) cclenter((char *) rlxstr)); rtok = relex(); return (unary(np)); case '^': rtok = relex(); return (unary(op2(CHAR, NIL, itonp(HAT)))); case '$': rtok = relex(); return (unary(op2(CHAR, NIL, NIL))); case '(': rtok = relex(); if (rtok == ')') { /* special pleading for () */ rtok = relex(); return unary(op2(CCL, NIL, (Node *) tostring(""))); } np = regexp(); if (rtok == ')') { rtok = relex(); return (unary(np)); } else FATAL("syntax error in regular expression %s at %s", lastre, prestr); default: FATAL("illegal primary in regular expression %s at %s", lastre, prestr); } return 0; /*NOTREACHED*/ }
yyparse() { short yys[YYMAXDEPTH]; short yyj, yym; register YYSTYPE *yypvt; register short yystate, *yyps, yyn; register YYSTYPE *yypv; register short *yyxi; yystate = 0; yychar = -1; yynerrs = 0; yyerrflag = 0; yyps= &yys[-1]; yypv= &yyv[-1]; yystack: /* put a state and value onto the stack */ #ifdef YYDEBUG if( yydebug ) printf( "state %d, char 0%o\n", yystate, yychar ); #endif 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 ) if( (yychar=yylex())<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 ) if( (yychar=yylex())<0 ) yychar = 0; /* look through exception table */ for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */ while( *(yyxi+=2) >= 0 ) { if( *yyxi == yychar ) break; } if( (yyn = yyxi[1]) < 0 ) return(0); /* accept */ } if( yyn == 0 ) { /* error */ /* error ... attempt to resume parsing */ switch( yyerrflag ) { case 0: /* brand new error */ yyerror( "syntax error" ); yyerrlab: ++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; } yyn = yypact[*yyps]; /* the current yyps has no shift onn "error", pop stack */ #ifdef YYDEBUG if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] ); #endif --yyps; --yypv; } /* there is no state on the stack with an error shift ... abort */ yyabort: return(1); case 3: /* no shift yet; clobber input char */ #ifdef YYDEBUG if( yydebug ) printf( "error recovery discards char %d\n", yychar ); #endif if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */ yychar = -1; goto yynewstate; /* try again in the same state */ } } /* reduction by production yyn */ #ifdef YYDEBUG if( yydebug ) printf("reduce %d\n",yyn); #endif 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) { case 1: # line 44 "awk.g.y" { if (errorflag==0) winner = (node *)stat3(PROGRAM, yypvt[-2], yypvt[-1], yypvt[-0]); } break; case 2: # line 45 "awk.g.y" { yyclearin; yyerror("bailing out"); } break; case 3: # line 49 "awk.g.y" { PUTS("XBEGIN list"); yyval = yypvt[-1]; } break; case 5: # line 51 "awk.g.y" { PUTS("empty XBEGIN"); yyval = (hack)nullstat; } break; case 6: # line 55 "awk.g.y" { PUTS("XEND list"); yyval = yypvt[-1]; } break; case 8: # line 57 "awk.g.y" { PUTS("empty END"); yyval = (hack)nullstat; } break; case 9: # line 61 "awk.g.y" { PUTS("cond||cond"); yyval = op2(BOR, yypvt[-2], yypvt[-0]); } break; case 10: # line 62 "awk.g.y" { PUTS("cond&&cond"); yyval = op2(AND, yypvt[-2], yypvt[-0]); } break; case 11: # line 63 "awk.g.y" { PUTS("!cond"); yyval = op1(NOT, yypvt[-0]); } break; case 12: # line 64 "awk.g.y" { yyval = yypvt[-1]; } break; case 13: # line 68 "awk.g.y" { PUTS("pat||pat"); yyval = op2(BOR, yypvt[-2], yypvt[-0]); } break; case 14: # line 69 "awk.g.y" { PUTS("pat&&pat"); yyval = op2(AND, yypvt[-2], yypvt[-0]); } break; case 15: # line 70 "awk.g.y" { PUTS("!pat"); yyval = op1(NOT, yypvt[-0]); } break; case 16: # line 71 "awk.g.y" { yyval = yypvt[-1]; } break; case 17: # line 75 "awk.g.y" { PUTS("expr"); yyval = op2(NE, yypvt[-0], valtonode(lookup("$zero&null", symtab, 0), CCON)); } break; case 18: # line 76 "awk.g.y" { PUTS("relexpr"); } break; case 19: # line 77 "awk.g.y" { PUTS("lexexpr"); } break; case 20: # line 78 "awk.g.y" { PUTS("compcond"); } break; case 21: # line 82 "awk.g.y" { PUTS("else"); } break; case 22: # line 86 "awk.g.y" { PUTS("field"); yyval = valtonode(yypvt[-0], CFLD); } break; case 23: # line 87 "awk.g.y" { PUTS("ind field"); yyval = op1(INDIRECT, yypvt[-0]); } break; case 24: # line 91 "awk.g.y" { PUTS("if(cond)"); yyval = yypvt[-2]; } break; case 25: # line 95 "awk.g.y" { PUTS("expr~re"); yyval = op2(yypvt[-1], yypvt[-2], makedfa(yypvt[-0])); } break; case 26: # line 96 "awk.g.y" { PUTS("(lex_expr)"); yyval = yypvt[-1]; } break; case 27: # line 100 "awk.g.y" { PUTS("number"); yyval = valtonode(yypvt[-0], CCON); } break; case 28: # line 101 "awk.g.y" { PUTS("string"); yyval = valtonode(yypvt[-0], CCON); } break; case 29: # line 102 "awk.g.y" { PUTS("var"); yyval = valtonode(yypvt[-0], CVAR); } break; case 30: # line 103 "awk.g.y" { PUTS("array[]"); yyval = op2(ARRAY, yypvt[-3], yypvt[-1]); } break; case 33: # line 108 "awk.g.y" { PUTS("getline"); yyval = op1(GETLINE, 0); } break; case 34: # line 109 "awk.g.y" { PUTS("func"); yyval = op2(FNCN, yypvt[-0], valtonode(lookup("$record", symtab, 0), CFLD)); } break; case 35: # line 112 "awk.g.y" { PUTS("func()"); yyval = op2(FNCN, yypvt[-2], valtonode(lookup("$record", symtab, 0), CFLD)); } break; case 36: # line 115 "awk.g.y" { PUTS("func(expr)"); yyval = op2(FNCN, yypvt[-3], yypvt[-1]); } break; case 37: # line 116 "awk.g.y" { PUTS("sprintf"); yyval = op1(yypvt[-1], yypvt[-0]); } break; case 38: # line 118 "awk.g.y" { PUTS("substr(e,e,e)"); yyval = op3(SUBSTR, yypvt[-5], yypvt[-3], yypvt[-1]); } break; case 39: # line 120 "awk.g.y" { PUTS("substr(e,e,e)"); yyval = op3(SUBSTR, yypvt[-3], yypvt[-1], nullstat); } break; case 40: # line 122 "awk.g.y" { PUTS("split(e,e,e)"); yyval = op3(SPLIT, yypvt[-5], yypvt[-3], yypvt[-1]); } break; case 41: # line 124 "awk.g.y" { PUTS("split(e,e,e)"); yyval = op3(SPLIT, yypvt[-3], yypvt[-1], nullstat); } break; case 42: # line 126 "awk.g.y" { PUTS("index(e,e)"); yyval = op2(INDEX, yypvt[-3], yypvt[-1]); } break; case 43: # line 127 "awk.g.y" { PUTS("(expr)"); yyval = yypvt[-1]; } break; case 44: # line 128 "awk.g.y" { PUTS("t+t"); yyval = op2(ADD, yypvt[-2], yypvt[-0]); } break; case 45: # line 129 "awk.g.y" { PUTS("t-t"); yyval = op2(MINUS, yypvt[-2], yypvt[-0]); } break; case 46: # line 130 "awk.g.y" { PUTS("t*t"); yyval = op2(MULT, yypvt[-2], yypvt[-0]); } break; case 47: # line 131 "awk.g.y" { PUTS("t/t"); yyval = op2(DIVIDE, yypvt[-2], yypvt[-0]); } break; case 48: # line 132 "awk.g.y" { PUTS("t%t"); yyval = op2(MOD, yypvt[-2], yypvt[-0]); } break; case 49: # line 133 "awk.g.y" { PUTS("-term"); yyval = op1(UMINUS, yypvt[-0]); } break; case 50: # line 134 "awk.g.y" { PUTS("+term"); yyval = yypvt[-0]; } break; case 51: # line 135 "awk.g.y" { PUTS("++var"); yyval = op1(PREINCR, yypvt[-0]); } break; case 52: # line 136 "awk.g.y" { PUTS("--var"); yyval = op1(PREDECR, yypvt[-0]); } break; case 53: # line 137 "awk.g.y" { PUTS("var++"); yyval= op1(POSTINCR, yypvt[-1]); } break; case 54: # line 138 "awk.g.y" { PUTS("var--"); yyval= op1(POSTDECR, yypvt[-1]); } break; case 55: # line 142 "awk.g.y" { PUTS("term"); } break; case 56: # line 143 "awk.g.y" { PUTS("expr term"); yyval = op2(CAT, yypvt[-1], yypvt[-0]); } break; case 57: # line 144 "awk.g.y" { PUTS("var=expr"); yyval = stat2(yypvt[-1], yypvt[-2], yypvt[-0]); } break; case 60: # line 153 "awk.g.y" { PUTS("pattern"); yyval = stat2(PASTAT, yypvt[-0], genprint()); } break; case 61: # line 154 "awk.g.y" { PUTS("pattern {...}"); yyval = stat2(PASTAT, yypvt[-3], yypvt[-1]); } break; case 62: # line 155 "awk.g.y" { PUTS("srch,srch"); yyval = pa2stat(yypvt[-2], yypvt[-0], genprint()); } break; case 63: # line 157 "awk.g.y" { PUTS("srch, srch {...}"); yyval = pa2stat(yypvt[-5], yypvt[-3], yypvt[-1]); } break; case 64: # line 158 "awk.g.y" { PUTS("null pattern {...}"); yyval = stat2(PASTAT, nullstat, yypvt[-1]); } break; case 65: # line 162 "awk.g.y" { PUTS("pa_stats pa_stat"); yyval = linkum(yypvt[-2], yypvt[-1]); } break; case 66: # line 163 "awk.g.y" { PUTS("null pa_stat"); yyval = (hack)nullstat; } break; case 67: # line 164 "awk.g.y" { PUTS("pa_stats pa_stat"); yyval = linkum(yypvt[-1], yypvt[-0]); } break; case 68: # line 168 "awk.g.y" { PUTS("regex"); yyval = op2(MATCH, valtonode(lookup("$record", symtab, 0), CFLD), makedfa(yypvt[-0])); } break; case 69: # line 171 "awk.g.y" { PUTS("relexpr"); } break; case 70: # line 172 "awk.g.y" { PUTS("lexexpr"); } break; case 71: # line 173 "awk.g.y" { PUTS("comp pat"); } break; case 72: # line 177 "awk.g.y" { PUTS("expr"); } break; case 73: # line 178 "awk.g.y" { PUTS("pe_list"); } break; case 74: # line 179 "awk.g.y" { PUTS("null print_list"); yyval = valtonode(lookup("$record", symtab, 0), CFLD); } break; case 75: # line 183 "awk.g.y" { yyval = linkum(yypvt[-2], yypvt[-0]); } break; case 76: # line 184 "awk.g.y" { yyval = linkum(yypvt[-2], yypvt[-0]); } break; case 77: # line 185 "awk.g.y" { yyval = yypvt[-1]; } break; case 80: # line 194 "awk.g.y" { startreg(); } break; case 81: # line 196 "awk.g.y" { PUTS("/r/"); yyval = yypvt[-1]; } break; case 82: # line 200 "awk.g.y" { PUTS("regex CHAR"); yyval = op2(CHAR, (node *) 0, yypvt[-0]); } break; case 83: # line 201 "awk.g.y" { PUTS("regex DOT"); yyval = op2(DOT, (node *) 0, (node *) 0); } break; case 84: # line 202 "awk.g.y" { PUTS("regex CCL"); yyval = op2(CCL, (node *) 0, cclenter(yypvt[-0])); } break; case 85: # line 203 "awk.g.y" { PUTS("regex NCCL"); yyval = op2(NCCL, (node *) 0, cclenter(yypvt[-0])); } break; case 86: # line 204 "awk.g.y" { PUTS("regex ^"); yyval = op2(CHAR, (node *) 0, HAT); } break; case 87: # line 205 "awk.g.y" { PUTS("regex $"); yyval = op2(CHAR, (node *) 0 ,(node *) 0); } break; case 88: # line 206 "awk.g.y" { PUTS("regex OR"); yyval = op2(OR, yypvt[-2], yypvt[-0]); } break; case 89: # line 208 "awk.g.y" { PUTS("regex CAT"); yyval = op2(CAT, yypvt[-1], yypvt[-0]); } break; case 90: # line 209 "awk.g.y" { PUTS("regex STAR"); yyval = op2(STAR, yypvt[-1], (node *) 0); } break; case 91: # line 210 "awk.g.y" { PUTS("regex PLUS"); yyval = op2(PLUS, yypvt[-1], (node *) 0); } break; case 92: # line 211 "awk.g.y" { PUTS("regex QUEST"); yyval = op2(QUEST, yypvt[-1], (node *) 0); } break; case 93: # line 212 "awk.g.y" { PUTS("(regex)"); yyval = yypvt[-1]; } break; case 94: # line 217 "awk.g.y" { PUTS("expr relop expr"); yyval = op2(yypvt[-1], yypvt[-2], yypvt[-0]); } break; case 95: # line 219 "awk.g.y" { PUTS("(relexpr)"); yyval = yypvt[-1]; } break; case 98: # line 229 "awk.g.y" { PUTS("print>stat"); yyval = stat3(yypvt[-3], yypvt[-2], yypvt[-1], yypvt[-0]); } break; case 99: # line 231 "awk.g.y" { PUTS("print list"); yyval = stat3(yypvt[-1], yypvt[-0], nullstat, nullstat); } break; case 100: # line 233 "awk.g.y" { PUTS("printf>stat"); yyval = stat3(yypvt[-3], yypvt[-2], yypvt[-1], yypvt[-0]); } break; case 101: # line 235 "awk.g.y" { PUTS("printf list"); yyval = stat3(yypvt[-1], yypvt[-0], nullstat, nullstat); } break; case 102: # line 236 "awk.g.y" { PUTS("expr"); yyval = exptostat(yypvt[-0]); } break; case 103: # line 237 "awk.g.y" { PUTS("null simple statement"); yyval = (hack)nullstat; } break; case 104: # line 238 "awk.g.y" { yyclearin; yyerror("illegal statement"); } break; case 105: # line 242 "awk.g.y" { PUTS("simple stat"); } break; case 106: # line 243 "awk.g.y" { PUTS("if stat"); yyval = stat3(IF, yypvt[-1], yypvt[-0], nullstat); } break; case 107: # line 245 "awk.g.y" { PUTS("if-else stat"); yyval = stat3(IF, yypvt[-3], yypvt[-2], yypvt[-0]); } break; case 108: # line 246 "awk.g.y" { PUTS("while stat"); yyval = stat2(WHILE, yypvt[-1], yypvt[-0]); } break; case 109: # line 247 "awk.g.y" { PUTS("for stat"); } break; case 110: # line 248 "awk.g.y" { PUTS("next"); yyval = stat1(NEXT, 0); } break; case 111: # line 249 "awk.g.y" { PUTS("exit"); yyval = stat1(EXIT, 0); } break; case 112: # line 250 "awk.g.y" { PUTS("exit"); yyval = stat1(EXIT, yypvt[-1]); } break; case 113: # line 251 "awk.g.y" { PUTS("break"); yyval = stat1(BREAK, 0); } break; case 114: # line 252 "awk.g.y" { PUTS("continue"); yyval = stat1(CONTINUE, 0); } break; case 115: # line 253 "awk.g.y" { PUTS("{statlist}"); yyval = yypvt[-1]; } break; case 116: # line 257 "awk.g.y" { PUTS("stat_list stat"); yyval = linkum(yypvt[-1], yypvt[-0]); } break; case 117: # line 258 "awk.g.y" { PUTS("null stat list"); yyval = (hack)nullstat; } break; case 118: # line 262 "awk.g.y" { PUTS("while(cond)"); yyval = yypvt[-2]; } break; case 119: # line 267 "awk.g.y" { PUTS("for(e;e;e)"); yyval = stat4(FOR, yypvt[-7], yypvt[-5], yypvt[-3], yypvt[-0]); } break; case 120: # line 269 "awk.g.y" { PUTS("for(e;e;e)"); yyval = stat4(FOR, yypvt[-6], nullstat, yypvt[-3], yypvt[-0]); } break; case 121: # line 271 "awk.g.y" { PUTS("for(v in v)"); yyval = stat3(IN, yypvt[-5], yypvt[-3], yypvt[-0]); } break; } goto yystack; /* stack new state and value */ }