1: %token FIRSTTOKEN /*must be first*/ 2: %token FINAL FATAL 3: %token LT LE GT GE EQ NE 4: %token MATCH NOTMATCH 5: %token APPEND 6: %token ADD MINUS MULT DIVIDE MOD UMINUS 7: %token ASSIGN ADDEQ SUBEQ MULTEQ DIVEQ MODEQ 8: %token JUMP 9: %token XBEGIN XEND 10: %token NL 11: %token PRINT PRINTF SPRINTF SPLIT 12: %token IF ELSE WHILE FOR IN NEXT EXIT BREAK CONTINUE 13: %token PROGRAM PASTAT PASTAT2 14: 15: %right ASGNOP 16: %left BOR 17: %left AND 18: %left NOT 19: %left NUMBER VAR ARRAY FNCN SUBSTR LSUBSTR INDEX 20: %nonassoc RELOP MATCHOP 21: %left OR 22: %left STRING DOT CCL NCCL CHAR 23: %left '(' '^' '$' 24: %left CAT 25: %left '+' '-' 26: %left '*' '/' '%' 27: %left STAR PLUS QUEST 28: %left POSTINCR PREINCR POSTDECR PREDECR INCR DECR 29: %left FIELD INDIRECT 30: %token LASTTOKEN /* has to be last */ 31: 32: %{ 33: #include "awk.def" 34: #ifndef DEBUG 35: # define PUTS(x) 36: #endif 37: %} 38: %% 39: 40: program: 41: begin pa_stats end { if (errorflag==0) winner = stat3(PROGRAM, $1, $2, $3); } 42: | error { yyclearin; yyerror("bailing out"); } 43: ; 44: 45: begin: 46: XBEGIN '{' stat_list '}' { PUTS("XBEGIN list"); $$ = $3; } 47: | begin NL 48: | { PUTS("empty XBEGIN"); $$ = nullstat; } 49: ; 50: 51: end: 52: XEND '{' stat_list '}' { PUTS("XEND list"); $$ = $3; } 53: | end NL 54: | { PUTS("empty END"); $$ = nullstat; } 55: ; 56: 57: compound_conditional: 58: conditional BOR conditional { PUTS("cond||cond"); $$ = op2(BOR, $1, $3); } 59: | conditional AND conditional { PUTS("cond&&cond"); $$ = op2(AND, $1, $3); } 60: | NOT conditional { PUTS("!cond"); $$ = op1(NOT, $2); } 61: | '(' compound_conditional ')' { $$ = $2; } 62: ; 63: 64: compound_pattern: 65: pattern BOR pattern { PUTS("pat||pat"); $$ = op2(BOR, $1, $3); } 66: | pattern AND pattern { PUTS("pat&&pat"); $$ = op2(AND, $1, $3); } 67: | NOT pattern { PUTS("!pat"); $$ = op1(NOT, $2); } 68: | '(' compound_pattern ')' { $$ = $2; } 69: ; 70: 71: conditional: 72: expr { PUTS("expr"); $$ = op2(NE, $1, valtonode(lookup("0", symtab), CCON)); } 73: | rel_expr { PUTS("relexpr"); } 74: | lex_expr { PUTS("lexexpr"); } 75: | compound_conditional { PUTS("compcond"); } 76: ; 77: 78: else: 79: ELSE optNL { PUTS("else"); } 80: ; 81: 82: field: 83: FIELD { PUTS("field"); $$ = valtonode($1, CFLD); } 84: | INDIRECT term { PUTS("ind field"); $$ = op1(INDIRECT, $2); } 85: ; 86: 87: if: 88: IF '(' conditional ')' optNL { PUTS("if(cond)"); $$ = $3; } 89: ; 90: 91: lex_expr: 92: expr MATCHOP regular_expr { PUTS("expr~re"); $$ = op2($2, $1, makedfa($3)); } 93: | '(' lex_expr ')' { PUTS("(lex_expr)"); $$ = $2; } 94: ; 95: 96: var: 97: NUMBER {PUTS("number"); $$ = valtonode($1, CCON); } 98: | STRING { PUTS("string"); $$ = valtonode($1, CCON); } 99: | VAR { PUTS("var"); $$ = valtonode($1, CVAR); } 100: | VAR '[' expr ']' { PUTS("array[]"); $$ = op2(ARRAY, $1, $3); } 101: | field 102: ; 103: term: 104: var 105: | FNCN { PUTS("func"); 106: $$ = op2(FNCN, $1, valtonode(lookup("$record", symtab), CFLD)); 107: } 108: | FNCN '(' ')' { PUTS("func()"); 109: $$ = op2(FNCN, $1, valtonode(lookup("$record", symtab), CFLD)); 110: } 111: | FNCN '(' expr ')' { PUTS("func(expr)"); $$ = op2(FNCN, $1, $3); } 112: | SPRINTF print_list { PUTS("sprintf"); $$ = op1($1, $2); } 113: | SUBSTR '(' expr ',' expr ',' expr ')' 114: { PUTS("substr(e,e,e)"); $$ = op3(SUBSTR, $3, $5, $7); } 115: | SUBSTR '(' expr ',' expr ')' 116: { PUTS("substr(e,e,e)"); $$ = op3(SUBSTR, $3, $5, nullstat); } 117: | SPLIT '(' expr ',' VAR ',' expr ')' 118: { PUTS("split(e,e,e)"); $$ = op3(SPLIT, $3, $5, $7); } 119: | SPLIT '(' expr ',' VAR ')' 120: { PUTS("split(e,e,e)"); $$ = op3(SPLIT, $3, $5, nullstat); } 121: | INDEX '(' expr ',' expr ')' 122: { PUTS("index(e,e)"); $$ = op2(INDEX, $3, $5); } 123: | '(' expr ')' {PUTS("(expr)"); $$ = $2; } 124: | term '+' term { PUTS("t+t"); $$ = op2(ADD, $1, $3); } 125: | term '-' term { PUTS("t-t"); $$ = op2(MINUS, $1, $3); } 126: | term '*' term { PUTS("t*t"); $$ = op2(MULT, $1, $3); } 127: | term '/' term { PUTS("t/t"); $$ = op2(DIVIDE, $1, $3); } 128: | term '%' term { PUTS("t%t"); $$ = op2(MOD, $1, $3); } 129: | '-' term %prec QUEST { PUTS("-term"); $$ = op1(UMINUS, $2); } 130: | '+' term %prec QUEST { PUTS("+term"); $$ = $2; } 131: | INCR var { PUTS("++var"); $$ = op1(PREINCR, $2); } 132: | DECR var { PUTS("--var"); $$ = op1(PREDECR, $2); } 133: | var INCR { PUTS("var++"); $$= op1(POSTINCR, $1); } 134: | var DECR { PUTS("var--"); $$= op1(POSTDECR, $1); } 135: ; 136: 137: expr: 138: term { PUTS("term"); } 139: | expr term { PUTS("expr term"); $$ = op2(CAT, $1, $2); } 140: | var ASGNOP expr { PUTS("var=expr"); $$ = stat2($2, $1, $3); } 141: ; 142: 143: optNL: 144: NL 145: | 146: ; 147: 148: pa_stat: 149: pattern { PUTS("pattern"); $$ = stat2(PASTAT, $1, genprint()); } 150: | pattern '{' stat_list '}' { PUTS("pattern {...}"); $$ = stat2(PASTAT, $1, $3); } 151: | pattern ',' pattern { PUTS("srch,srch"); $$ = pa2stat($1, $3, genprint()); } 152: | pattern ',' pattern '{' stat_list '}' 153: { PUTS("srch, srch {...}"); $$ = pa2stat($1, $3, $5); } 154: | '{' stat_list '}' { PUTS("null pattern {...}"); $$ = stat2(PASTAT, nullstat, $2); } 155: ; 156: 157: pa_stats: 158: pa_stats pa_stat st { PUTS("pa_stats pa_stat"); $$ = linkum($1, $2); } 159: | { PUTS("null pa_stat"); $$ = nullstat; } 160: | pa_stats pa_stat {PUTS("pa_stats pa_stat"); $$ = linkum($1, $2); } 161: ; 162: 163: pattern: 164: regular_expr { PUTS("regex"); 165: $$ = op2(MATCH, valtonode(lookup("$record", symtab), CFLD), makedfa($1)); 166: } 167: | rel_expr { PUTS("relexpr"); } 168: | lex_expr { PUTS("lexexpr"); } 169: | compound_pattern { PUTS("comp pat"); } 170: ; 171: 172: print_list: 173: expr { PUTS("expr"); } 174: | pe_list { PUTS("pe_list"); } 175: | { PUTS("null print_list"); $$ = valtonode(lookup("$record", symtab), CFLD); } 176: ; 177: 178: pe_list: 179: expr ',' expr {$$ = linkum($1, $3); } 180: | pe_list ',' expr {$$ = linkum($1, $3); } 181: | '(' pe_list ')' {$$ = $2; } 182: ; 183: 184: redir: 185: RELOP 186: | '|' 187: ; 188: 189: regular_expr: 190: '/' { startreg(); } 191: r '/' 192: { PUTS("/r/"); $$ = $3; } 193: ; 194: 195: r: 196: CHAR { PUTS("regex CHAR"); $$ = op2(CHAR, (node *) 0, $1); } 197: | DOT { PUTS("regex DOT"); $$ = op2(DOT, (node *) 0, (node *) 0); } 198: | CCL { PUTS("regex CCL"); $$ = op2(CCL, (node *) 0, cclenter($1)); } 199: | NCCL { PUTS("regex NCCL"); $$ = op2(NCCL, (node *) 0, cclenter($1)); } 200: | '^' { PUTS("regex ^"); $$ = op2(CHAR, (node *) 0, HAT); } 201: | '$' { PUTS("regex $"); $$ = op2(CHAR, (node *) 0 ,(node *) 0); } 202: | r OR r { PUTS("regex OR"); $$ = op2(OR, $1, $3); } 203: | r r %prec CAT 204: { PUTS("regex CAT"); $$ = op2(CAT, $1, $2); } 205: | r STAR { PUTS("regex STAR"); $$ = op2(STAR, $1, (node *) 0); } 206: | r PLUS { PUTS("regex PLUS"); $$ = op2(PLUS, $1, (node *) 0); } 207: | r QUEST { PUTS("regex QUEST"); $$ = op2(QUEST, $1, (node *) 0); } 208: | '(' r ')' { PUTS("(regex)"); $$ = $2; } 209: ; 210: 211: rel_expr: 212: expr RELOP expr 213: { PUTS("expr relop expr"); $$ = op2($2, $1, $3); } 214: | '(' rel_expr ')' 215: { PUTS("(relexpr)"); $$ = $2; } 216: ; 217: 218: st: 219: NL 220: | ';' 221: ; 222: 223: simple_stat: 224: PRINT print_list redir expr 225: { PUTS("print>stat"); $$ = stat3($1, $2, $3, $4); } 226: | PRINT print_list 227: { PUTS("print list"); $$ = stat3($1, $2, nullstat, nullstat); } 228: | PRINTF print_list redir expr 229: { PUTS("printf>stat"); $$ = stat3($1, $2, $3, $4); } 230: | PRINTF print_list 231: { PUTS("printf list"); $$ = stat3($1, $2, nullstat, nullstat); } 232: | expr { PUTS("expr"); $$ = exptostat($1); } 233: | { PUTS("null simple statement"); $$ = nullstat; } 234: | error { yyclearin; yyerror("illegal statement"); } 235: ; 236: 237: statement: 238: simple_stat st { PUTS("simple stat"); } 239: | if statement { PUTS("if stat"); $$ = stat3(IF, $1, $2, nullstat); } 240: | if statement else statement 241: { PUTS("if-else stat"); $$ = stat3(IF, $1, $2, $4); } 242: | while statement { PUTS("while stat"); $$ = stat2(WHILE, $1, $2); } 243: | for { PUTS("for stat"); } 244: | NEXT st { PUTS("next"); $$ = genjump(NEXT); } 245: | EXIT st { PUTS("exit"); $$ = genjump(EXIT); } 246: | BREAK st { PUTS("break"); $$ = genjump(BREAK); } 247: | CONTINUE st { PUTS("continue"); $$ = genjump(CONTINUE); } 248: | '{' stat_list '}' { PUTS("{statlist}"); $$ = $2; } 249: ; 250: 251: stat_list: 252: stat_list statement { PUTS("stat_list stat"); $$ = linkum($1, $2); } 253: | { PUTS("null stat list"); $$ = nullstat; } 254: ; 255: 256: while: 257: WHILE '(' conditional ')' optNL { PUTS("while(cond)"); $$ = $3; } 258: ; 259: 260: for: 261: FOR '(' simple_stat ';' conditional ';' simple_stat ')' optNL statement 262: { PUTS("for(e;e;e)"); $$ = stat4(FOR, $3, $5, $7, $10); } 263: | FOR '(' simple_stat ';' ';' simple_stat ')' optNL statement 264: { PUTS("for(e;e;e)"); $$ = stat4(FOR, $3, nullstat, $6, $9); } 265: | FOR '(' VAR IN VAR ')' optNL statement 266: { PUTS("for(v in v)"); $$ = stat3(IN, $3, $5, $8); } 267: ; 268: 269: %%