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