2 /* $Header: a2p.y,v 2.0 88/06/05 00:15:38 root Exp $
5 * Revision 2.0 88/06/05 00:15:38 root
6 * Baseline version 2.0.
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 '{' maybe states '}' junk
46 { $$ = oper3(OJUNK,$3,$4,$6); in_begin = FALSE; }
51 end : END '{' maybe states '}'
52 { $$ = oper2(OJUNK,$3,$4); }
59 hunks : hunks hunk junk
60 { $$ = oper3(OHUNKS,$1,$2,$3); }
66 { $$ = oper1(OHUNK,$1); need_entire = TRUE; }
67 | patpat '{' maybe states '}'
68 { $$ = oper2(OHUNK,$1,oper2(OJUNK,$3,$4)); }
69 | '{' maybe states '}'
70 { $$ = oper2(OHUNK,Nullop,oper2(OJUNK,$2,$3)); }
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,oper1(OREGEX,$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); }
208 clist : expr ',' expr
209 { $$ = oper2(OCOMMA,$1,$3); }
211 { $$ = oper2(OCOMMA,$1,$3); }
212 | '(' clist ')' /* these parens are invisible */
217 { $$ = oper2(OJUNK,$1,$2); }
223 { $$ = oper0(OSEMICOLON); }
225 { $$ = oper0(OSEMICOLON); }
227 { $$ = oper0(ONEWLINE); }
229 { $$ = oper1(OCOMMENT,$1); }
232 maybe : maybe nlstuff
233 { $$ = oper2(OJUNK,$1,$2); }
239 { $$ = oper0(ONEWLINE); }
241 { $$ = oper1(OCOMMENT,$1); }
246 { $$ = oper2(OJUNK,oper0(OSEMICOLON),$2); }
248 { $$ = oper2(OJUNK,oper0(OSNEWLINE),$2); }
250 { $$ = oper2(OJUNK,oper0(OSNEWLINE),$2); }
252 { $$ = oper2(OJUNK,oper1(OSCOMMENT,$1),$2); }
255 states : states statement
256 { $$ = oper2(OSTATES,$1,$2); }
262 : simple separator maybe
263 { $$ = oper2(OJUNK,oper2(OSTATE,$1,$2),$3); }
265 { $$ = oper2(OSTATE,Nullop,oper2(OJUNK,oper0(OSEMICOLON),$2)); }
267 { $$ = oper2(OSTATE,Nullop,oper2(OJUNK,oper0(OSNEWLINE),$2)); }
278 | PRINT print_list redir expr
279 { $$ = oper3(OPRINT,$2,$3,$4);
281 saw_ORS = saw_OFS = TRUE;
282 if (!$2) need_entire = TRUE;
283 if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
285 { $$ = oper1(OPRINT,$2);
286 if (!$2) need_entire = TRUE;
287 saw_ORS = saw_OFS = TRUE;
289 | PRINTF print_list redir expr
290 { $$ = oper3(OPRINTF,$2,$3,$4);
292 if (!$2) need_entire = TRUE;
293 if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
295 { $$ = oper1(OPRINTF,$2);
296 if (!$2) need_entire = TRUE;
299 { $$ = oper0(OBREAK); }
301 { $$ = oper0(ONEXT); }
303 { $$ = oper0(OEXIT); }
305 { $$ = oper1(OEXIT,$2); }
307 { $$ = oper0(OCONTINUE); }
311 { $$ = oper1(OREDIR,string(">",1)); }
313 { $$ = oper1(OREDIR,string(">>",2)); }
315 { $$ = oper1(OREDIR,string("|",1)); }
319 : IF '(' cond ')' maybe statement
320 { $$ = oper2(OIF,$3,bl($6,$5)); }
321 | IF '(' cond ')' maybe statement ELSE maybe statement
322 { $$ = oper3(OIF,$3,bl($6,$5),bl($9,$8)); }
323 | WHILE '(' cond ')' maybe statement
324 { $$ = oper2(OWHILE,$3,bl($6,$5)); }
325 | FOR '(' simpnull ';' cond ';' simpnull ')' maybe statement
326 { $$ = oper4(OFOR,$3,$5,$7,bl($10,$9)); }
327 | FOR '(' simpnull ';' ';' simpnull ')' maybe statement
328 { $$ = oper4(OFOR,$3,string("",0),$6,bl($9,$8)); }
329 | FOR '(' VAR IN VAR ')' maybe statement
330 { $$ = oper3(OFORIN,$3,$5,bl($8,$7)); }
331 | '{' maybe states '}' maybe
332 { $$ = oper3(OBLOCK,oper2(OJUNK,$2,$3),Nullop,$5); }