2 /* $Header: a2p.y,v 1.0 87/12/18 13:07:05 root Exp $
5 * Revision 1.0 87/12/18 13:07:05 root
18 %token SEMINEW NEWLINE COMMENT
20 %token PRINT PRINTF SPRINTF SPLIT
21 %token IF ELSE WHILE FOR IN
22 %token EXIT NEXT BREAK CONTINUE
28 %left NUMBER VAR SUBSTR INDEX
30 %nonassoc RELOP MATCHOP
41 program : junk begin hunks end
42 { root = oper4(OPROG,$1,$2,$3,$4); }
45 begin : BEGIN '{' states '}' junk
46 { $$ = oper2(OJUNK,$3,$5); in_begin = FALSE; }
51 end : END '{' states '}'
59 hunks : hunks hunk junk
60 { $$ = oper3(OHUNKS,$1,$2,$3); }
66 { $$ = oper1(OHUNK,$1); need_entire = TRUE; }
67 | patpat '{' states '}'
68 { $$ = oper2(OHUNK,$1,$3); }
70 { $$ = oper2(OHUNK,Nullop,$2); }
74 { $$ = oper1(OPAT,$1); }
76 { $$ = oper2(ORANGE,$1,$3); }
80 { $$ = oper1(OREGEX,$1); }
87 : '(' compound_pat ')'
88 { $$ = oper1(OPPAREN,$2); }
90 { $$ = oper2(OPANDAND,$1,$3); }
92 { $$ = oper2(OPOROR,$1,$3); }
94 { $$ = oper1(OPNOT,$2); }
104 : '(' compound_cond ')'
105 { $$ = oper1(OCPAREN,$2); }
107 { $$ = oper2(OCANDAND,$1,$3); }
109 { $$ = oper2(OCOROR,$1,$3); }
111 { $$ = oper1(OCNOT,$2); }
114 rel : expr RELOP expr
115 { $$ = oper3(ORELOP,$2,$1,$3); }
117 { $$ = oper1(ORPAREN,$2); }
120 match : expr MATCHOP REGEX
121 { $$ = oper3(OMATCHOP,$2,$1,$3); }
123 { $$ = oper1(OMPAREN,$2); }
129 { $$ = oper2(OCONCAT,$1,$2); }
130 | variable ASGNOP expr
131 { $$ = oper3(OASSIGN,$2,$1,$3);
132 if ((ops[$1].ival & 255) == OFLD)
134 if ((ops[$1].ival & 255) == OVFLD)
142 { $$ = oper2(OADD,$1,$3); }
144 { $$ = oper2(OSUB,$1,$3); }
146 { $$ = oper2(OMULT,$1,$3); }
148 { $$ = oper2(ODIV,$1,$3); }
150 { $$ = oper2(OMOD,$1,$3); }
152 { $$ = oper1(OPOSTINCR,$1); }
154 { $$ = oper1(OPOSTDECR,$1); }
156 { $$ = oper1(OPREINCR,$2); }
158 { $$ = oper1(OPREDECR,$2); }
159 | '-' term %prec UMINUS
160 { $$ = oper1(OUMINUS,$2); }
161 | '+' term %prec UMINUS
162 { $$ = oper1(OUPLUS,$2); }
164 { $$ = oper1(OPAREN,$2); }
166 { $$ = oper0(OGETLINE); }
168 { $$ = oper0($1); need_entire = do_chop = TRUE; }
170 { $$ = oper1($1,Nullop); need_entire = do_chop = TRUE; }
172 { $$ = oper1($1,$3); }
174 { $$ = oper1(OSPRINTF,$2); }
175 | SUBSTR '(' expr ',' expr ',' expr ')'
176 { $$ = oper3(OSUBSTR,$3,$5,$7); }
177 | SUBSTR '(' expr ',' expr ')'
178 { $$ = oper2(OSUBSTR,$3,$5); }
179 | SPLIT '(' expr ',' VAR ',' expr ')'
180 { $$ = oper3(OSPLIT,$3,numary($5),$7); }
181 | SPLIT '(' expr ',' VAR ')'
182 { $$ = oper2(OSPLIT,$3,numary($5)); }
183 | INDEX '(' expr ',' expr ')'
184 { $$ = oper2(OINDEX,$3,$5); }
188 { $$ = oper1(ONUM,$1); }
190 { $$ = oper1(OSTR,$1); }
192 { $$ = oper1(OVAR,$1); }
194 { $$ = oper2(OVAR,$1,$3); }
196 { $$ = oper1(OFLD,$1); }
198 { $$ = oper1(OVFLD,$2); }
202 { $$ = oper0(ONEWLINE); }
206 { $$ = oper1(OCOMMENT,$1); }
216 clist : expr ',' expr
217 { $$ = oper2(OCOMMA,$1,$3); }
219 { $$ = oper2(OCOMMA,$1,$3); }
220 | '(' clist ')' /* these parens are invisible */
225 { $$ = oper2(OJUNK,$1,$2); }
231 { $$ = oper0(OSEMICOLON); }
233 { $$ = oper0(OSEMICOLON); }
235 { $$ = oper0(ONEWLINE); }
237 { $$ = oper1(OCOMMENT,$1); }
242 { $$ = oper0(OSEMICOLON); }
244 { $$ = oper0(OSNEWLINE); }
246 { $$ = oper0(OSNEWLINE); }
248 { $$ = oper1(OSCOMMENT,$1); }
251 states : states statement
252 { $$ = oper2(OSTATES,$1,$2); }
259 { $$ = oper2(OSTATE,$1,$2); }
265 | PRINT print_list redir expr
266 { $$ = oper3(OPRINT,$2,$3,$4);
268 saw_ORS = saw_OFS = TRUE;
269 if (!$2) need_entire = TRUE;
270 if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
272 { $$ = oper1(OPRINT,$2);
273 if (!$2) need_entire = TRUE;
274 saw_ORS = saw_OFS = TRUE;
276 | PRINTF print_list redir expr
277 { $$ = oper3(OPRINTF,$2,$3,$4);
279 if (!$2) need_entire = TRUE;
280 if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
282 { $$ = oper1(OPRINTF,$2);
283 if (!$2) need_entire = TRUE;
286 { $$ = oper0(OBREAK); }
288 { $$ = oper0(ONEXT); }
290 { $$ = oper0(OEXIT); }
292 { $$ = oper1(OEXIT,$2); }
294 { $$ = oper0(OCONTINUE); }
300 { $$ = oper1(OREDIR,string(">",1)); }
302 { $$ = oper1(OREDIR,string(">>",2)); }
304 { $$ = oper1(OREDIR,string("|",1)); }
308 : IF '(' cond ')' maybe statement
309 { $$ = oper2(OIF,$3,bl($6,$5)); }
310 | IF '(' cond ')' maybe statement ELSE maybe statement
311 { $$ = oper3(OIF,$3,bl($6,$5),bl($9,$8)); }
312 | WHILE '(' cond ')' maybe statement
313 { $$ = oper2(OWHILE,$3,bl($6,$5)); }
314 | FOR '(' simple ';' cond ';' simple ')' maybe statement
315 { $$ = oper4(OFOR,$3,$5,$7,bl($10,$9)); }
316 | FOR '(' simple ';' ';' simple ')' maybe statement
317 { $$ = oper4(OFOR,$3,string("",0),$6,bl($9,$8)); }
318 | FOR '(' VAR IN VAR ')' maybe statement
319 { $$ = oper3(OFORIN,$3,$5,bl($8,$7)); }
321 { $$ = oper1(OBLOCK,$2); }