File Coverage

x2p/a2p.y
Criterion Covered Total %
statement 0 279 0.0
branch n/a
condition n/a
subroutine n/a
total 0 279 0.0


line stmt bran cond sub time code
1           %{
2           /* $RCSfile: a2p.y,v $$Revision: 4.1 $$Date: 92/08/07 18:29:12 $
3           *
4           * Copyright (C) 1991, 1992, 1993, 1994, 1996, 1997, 1999, 2000,
5           * by Larry Wall and others
6           *
7           * You may distribute under the terms of either the GNU General Public
8           * License or the Artistic License, as specified in the README file.
9           *
10           * $Log: a2p.y,v $
11           */
12            
13           #include "INTERN.h"
14           #include "a2p.h"
15            
16           int root;
17           int begins = Nullop;
18           int ends = Nullop;
19            
20           %}
21           %token BEGIN END
22           %token REGEX
23           %token SEMINEW NEWLINE COMMENT
24           %token FUN1 FUNN GRGR
25           %token PRINT PRINTF SPRINTF_OLD SPRINTF_NEW SPLIT
26           %token IF ELSE WHILE FOR IN
27           %token EXIT NEXT BREAK CONTINUE RET
28           %token GETLINE DO SUB GSUB MATCH
29           %token FUNCTION USERFUN DELETE
30            
31           %right ASGNOP
32           %right '?' ':'
33           %left OROR
34           %left ANDAND
35           %left IN
36           %left NUMBER VAR SUBSTR INDEX
37           %left MATCHOP
38           %left RELOP '<' '>'
39           %left OR
40           %left STRING
41           %left '+' '-'
42           %left '*' '/' '%'
43           %right UMINUS
44           %left NOT
45           %right '^'
46           %left INCR DECR
47           %left FIELD VFIELD SVFIELD
48            
49           %%
50            
51           program : junk hunks
52 0         { root = oper4(OPROG,$1,begins,$2,ends); }
53 0         ;
54            
55           begin : BEGIN '{' maybe states '}' junk
56 0         { begins = oper4(OJUNK,begins,$3,$4,$6); in_begin = FALSE;
57 0         $$ = Nullop; }
58 0         ;
59            
60           end : END '{' maybe states '}'
61 0         { ends = oper3(OJUNK,ends,$3,$4); $$ = Nullop; }
62 0         | end NEWLINE
63 0         { $$ = $1; }
64 0         ;
65            
66           hunks : hunks hunk junk
67 0         { $$ = oper3(OHUNKS,$1,$2,$3); }
68 0         | /* NULL */
69 0         { $$ = Nullop; }
70 0         ;
71            
72           hunk : patpat
73 0         { $$ = oper1(OHUNK,$1); need_entire = TRUE; }
74 0         | patpat '{' maybe states '}'
75 0         { $$ = oper2(OHUNK,$1,oper2(OJUNK,$3,$4)); }
76 0         | FUNCTION USERFUN '(' arg_list ')' maybe '{' maybe states '}'
77 0         { fixfargs($2,$4,0); $$ = oper5(OUSERDEF,$2,$4,$6,$8,$9); }
78 0         | '{' maybe states '}'
79 0         { $$ = oper2(OHUNK,Nullop,oper2(OJUNK,$2,$3)); }
80 0         | begin
81           | end
82           ;
83            
84           arg_list: expr_list
85 0         { $$ = rememberargs($$); }
86 0         ;
87            
88           patpat : cond
89 0         { $$ = oper1(OPAT,$1); }
90 0         | cond ',' cond
91 0         { $$ = oper2(ORANGE,$1,$3); }
92 0         ;
93            
94           cond : expr
95           | match
96           | rel
97           | compound_cond
98           | cond '?' expr ':' expr
99 0         { $$ = oper3(OCOND,$1,$3,$5); }
100 0         ;
101            
102           compound_cond
103           : '(' compound_cond ')'
104 0         { $$ = oper1(OCPAREN,$2); }
105 0         | cond ANDAND maybe cond
106 0         { $$ = oper3(OCANDAND,$1,$3,$4); }
107 0         | cond OROR maybe cond
108 0         { $$ = oper3(OCOROR,$1,$3,$4); }
109 0         | NOT cond
110 0         { $$ = oper1(OCNOT,$2); }
111 0         ;
112            
113           rel : expr RELOP expr
114 0         { $$ = oper3(ORELOP,$2,$1,$3); }
115 0         | expr '>' expr
116 0         { $$ = oper3(ORELOP,string(">",1),$1,$3); }
117 0         | expr '<' expr
118 0         { $$ = oper3(ORELOP,string("<",1),$1,$3); }
119 0         | '(' rel ')'
120 0         { $$ = oper1(ORPAREN,$2); }
121 0         ;
122            
123           match : expr MATCHOP expr
124 0         { $$ = oper3(OMATCHOP,$2,$1,$3); }
125 0         | expr MATCHOP REGEX
126 0         { $$ = oper3(OMATCHOP,$2,$1,oper1(OREGEX,$3)); }
127 0         | REGEX %prec MATCHOP
128 0         { $$ = oper1(OREGEX,$1); }
129 0         | '(' match ')'
130 0         { $$ = oper1(OMPAREN,$2); }
131 0         ;
132            
133           expr : term
134 0         { $$ = $1; }
135 0         | expr term
136 0         { $$ = oper2(OCONCAT,$1,$2); }
137 0         | expr '?' expr ':' expr
138 0         { $$ = oper3(OCOND,$1,$3,$5); }
139 0         | variable ASGNOP cond
140           {
141 0         $$ = oper3(OASSIGN,$2,$1,$3);
142 0         if ((ops[$1].ival & 255) == OFLD)
143 0         lval_field = TRUE;
144 0         else if ((ops[$1].ival & 255) == OVFLD)
145 0         lval_field = TRUE;
146           }
147           ;
148            
149           sprintf : SPRINTF_NEW
150           | SPRINTF_OLD ;
151            
152           term : variable
153 0         { $$ = $1; }
154 0         | NUMBER
155 0         { $$ = oper1(ONUM,$1); }
156 0         | STRING
157 0         { $$ = oper1(OSTR,$1); }
158 0         | term '+' term
159 0         { $$ = oper2(OADD,$1,$3); }
160 0         | term '-' term
161 0         { $$ = oper2(OSUBTRACT,$1,$3); }
162 0         | term '*' term
163 0         { $$ = oper2(OMULT,$1,$3); }
164 0         | term '/' term
165 0         { $$ = oper2(ODIV,$1,$3); }
166 0         | term '%' term
167 0         { $$ = oper2(OMOD,$1,$3); }
168 0         | term '^' term
169 0         { $$ = oper2(OPOW,$1,$3); }
170 0         | term IN VAR
171 0         { $$ = oper2(ODEFINED,aryrefarg($3),$1); }
172 0         | variable INCR
173           {
174 0         $$ = oper1(OPOSTINCR,$1);
175 0         if ((ops[$1].ival & 255) == OFLD)
176 0         lval_field = TRUE;
177 0         else if ((ops[$1].ival & 255) == OVFLD)
178 0         lval_field = TRUE;
179           }
180           | variable DECR
181           {
182 0         $$ = oper1(OPOSTDECR,$1);
183 0         if ((ops[$1].ival & 255) == OFLD)
184 0         lval_field = TRUE;
185 0         else if ((ops[$1].ival & 255) == OVFLD)
186 0         lval_field = TRUE;
187           }
188           | INCR variable
189           {
190 0         $$ = oper1(OPREINCR,$2);
191 0         if ((ops[$2].ival & 255) == OFLD)
192 0         lval_field = TRUE;
193 0         else if ((ops[$2].ival & 255) == OVFLD)
194 0         lval_field = TRUE;
195           }
196           | DECR variable
197           {
198 0         $$ = oper1(OPREDECR,$2);
199 0         if ((ops[$2].ival & 255) == OFLD)
200 0         lval_field = TRUE;
201 0         else if ((ops[$2].ival & 255) == OVFLD)
202 0         lval_field = TRUE;
203           }
204           | '-' term %prec UMINUS
205 0         { $$ = oper1(OUMINUS,$2); }
206 0         | '+' term %prec UMINUS
207 0         { $$ = oper1(OUPLUS,$2); }
208 0         | '(' cond ')'
209 0         { $$ = oper1(OPAREN,$2); }
210 0         | GETLINE
211 0         { $$ = oper0(OGETLINE); }
212 0         | GETLINE variable
213 0         { $$ = oper1(OGETLINE,$2); }
214 0         | GETLINE '<' expr
215 0         { $$ = oper3(OGETLINE,Nullop,string("<",1),$3);
216 0         if (ops[$3].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
217           | GETLINE variable '<' expr
218 0         { $$ = oper3(OGETLINE,$2,string("<",1),$4);
219 0         if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
220           | term 'p' GETLINE
221 0         { $$ = oper3(OGETLINE,Nullop,string("|",1),$1);
222 0         if (ops[$1].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
223           | term 'p' GETLINE variable
224 0         { $$ = oper3(OGETLINE,$4,string("|",1),$1);
225 0         if (ops[$1].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
226           | FUN1
227 0         { $$ = oper0($1); need_entire = do_chop = TRUE; }
228 0         | FUN1 '(' ')'
229 0         { $$ = oper1($1,Nullop); need_entire = do_chop = TRUE; }
230 0         | FUN1 '(' expr ')'
231 0         { $$ = oper1($1,$3); }
232 0         | FUNN '(' expr_list ')'
233 0         { $$ = oper1($1,$3); }
234 0         | USERFUN '(' expr_list ')'
235 0         { $$ = oper2(OUSERFUN,$1,$3); }
236 0         | SPRINTF_NEW '(' expr_list ')'
237 0         { $$ = oper1(OSPRINTF,$3); }
238 0         | sprintf expr_list
239 0         { $$ = oper1(OSPRINTF,$2); }
240 0         | SUBSTR '(' expr ',' expr ',' expr ')'
241 0         { $$ = oper3(OSUBSTR,$3,$5,$7); }
242 0         | SUBSTR '(' expr ',' expr ')'
243 0         { $$ = oper2(OSUBSTR,$3,$5); }
244 0         | SPLIT '(' expr ',' VAR ',' expr ')'
245 0         { $$ = oper3(OSPLIT,$3,aryrefarg(numary($5)),$7); }
246 0         | SPLIT '(' expr ',' VAR ',' REGEX ')'
247 0         { $$ = oper3(OSPLIT,$3,aryrefarg(numary($5)),oper1(OREGEX,$7));}
248 0         | SPLIT '(' expr ',' VAR ')'
249 0         { $$ = oper2(OSPLIT,$3,aryrefarg(numary($5))); }
250 0         | INDEX '(' expr ',' expr ')'
251 0         { $$ = oper2(OINDEX,$3,$5); }
252 0         | MATCH '(' expr ',' REGEX ')'
253 0         { $$ = oper2(OMATCH,$3,oper1(OREGEX,$5)); }
254 0         | MATCH '(' expr ',' expr ')'
255 0         { $$ = oper2(OMATCH,$3,$5); }
256 0         | SUB '(' expr ',' expr ')'
257 0         { $$ = oper2(OSUB,$3,$5); }
258 0         | SUB '(' REGEX ',' expr ')'
259 0         { $$ = oper2(OSUB,oper1(OREGEX,$3),$5); }
260 0         | GSUB '(' expr ',' expr ')'
261 0         { $$ = oper2(OGSUB,$3,$5); }
262 0         | GSUB '(' REGEX ',' expr ')'
263 0         { $$ = oper2(OGSUB,oper1(OREGEX,$3),$5); }
264 0         | SUB '(' expr ',' expr ',' expr ')'
265 0         { $$ = oper3(OSUB,$3,$5,$7); }
266 0         | SUB '(' REGEX ',' expr ',' expr ')'
267 0         { $$ = oper3(OSUB,oper1(OREGEX,$3),$5,$7); }
268 0         | GSUB '(' expr ',' expr ',' expr ')'
269 0         { $$ = oper3(OGSUB,$3,$5,$7); }
270 0         | GSUB '(' REGEX ',' expr ',' expr ')'
271 0         { $$ = oper3(OGSUB,oper1(OREGEX,$3),$5,$7); }
272 0         ;
273            
274           variable: VAR
275 0         { $$ = oper1(OVAR,$1); }
276 0         | VAR '[' expr_list ']'
277 0         { $$ = oper2(OVAR,aryrefarg($1),$3); }
278 0         | FIELD
279 0         { $$ = oper1(OFLD,$1); }
280 0         | SVFIELD
281 0         { $$ = oper1(OVFLD,oper1(OVAR,$1)); }
282 0         | VFIELD term
283 0         { $$ = oper1(OVFLD,$2); }
284 0         ;
285            
286           expr_list
287           : expr
288           | clist
289           | /* NULL */
290 0         { $$ = Nullop; }
291 0         ;
292            
293           clist : expr ',' maybe expr
294 0         { $$ = oper3(OCOMMA,$1,$3,$4); }
295 0         | clist ',' maybe expr
296 0         { $$ = oper3(OCOMMA,$1,$3,$4); }
297 0         | '(' clist ')' /* these parens are invisible */
298 0         { $$ = $2; }
299 0         ;
300            
301           junk : junk hunksep
302 0         { $$ = oper2(OJUNK,$1,$2); }
303 0         | /* NULL */
304 0         { $$ = Nullop; }
305 0         ;
306            
307           hunksep : ';'
308 0         { $$ = oper2(OJUNK,oper0(OSEMICOLON),oper0(ONEWLINE)); }
309 0         | SEMINEW
310 0         { $$ = oper2(OJUNK,oper0(OSEMICOLON),oper0(ONEWLINE)); }
311 0         | NEWLINE
312 0         { $$ = oper0(ONEWLINE); }
313 0         | COMMENT
314 0         { $$ = oper1(OCOMMENT,$1); }
315 0         ;
316            
317           maybe : maybe nlstuff
318 0         { $$ = oper2(OJUNK,$1,$2); }
319 0         | /* NULL */
320 0         { $$ = Nullop; }
321 0         ;
322            
323           nlstuff : NEWLINE
324 0         { $$ = oper0(ONEWLINE); }
325 0         | COMMENT
326 0         { $$ = oper1(OCOMMENT,$1); }
327 0         ;
328            
329           separator
330           : ';' maybe
331 0         { $$ = oper2(OJUNK,oper0(OSEMICOLON),$2); }
332 0         | SEMINEW maybe
333 0         { $$ = oper2(OJUNK,oper0(OSNEWLINE),$2); }
334 0         | NEWLINE maybe
335 0         { $$ = oper2(OJUNK,oper0(OSNEWLINE),$2); }
336 0         | COMMENT maybe
337 0         { $$ = oper2(OJUNK,oper1(OSCOMMENT,$1),$2); }
338 0         ;
339            
340           states : states statement
341 0         { $$ = oper2(OSTATES,$1,$2); }
342 0         | /* NULL */
343 0         { $$ = Nullop; }
344 0         ;
345            
346           statement
347           : simple separator maybe
348 0         { $$ = oper2(OJUNK,oper2(OSTATE,$1,$2),$3); }
349 0         | ';' maybe
350 0         { $$ = oper2(OSTATE,Nullop,oper2(OJUNK,oper0(OSEMICOLON),$2)); }
351 0         | SEMINEW maybe
352 0         { $$ = oper2(OSTATE,Nullop,oper2(OJUNK,oper0(OSNEWLINE),$2)); }
353 0         | compound
354           ;
355            
356           simpnull: simple
357           | /* NULL */
358 0         { $$ = Nullop; }
359 0         ;
360            
361           simple
362           : expr
363           | PRINT expr_list redir expr
364 0         { $$ = oper3(OPRINT,$2,$3,$4);
365 0         do_opens = TRUE;
366 0         saw_ORS = saw_OFS = TRUE;
367 0         if (!$2) need_entire = TRUE;
368 0         if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
369           | PRINT expr_list
370 0         { $$ = oper1(OPRINT,$2);
371 0         if (!$2) need_entire = TRUE;
372 0         saw_ORS = saw_OFS = TRUE;
373           }
374 0         | PRINTF expr_list redir expr
375 0         { $$ = oper3(OPRINTF,$2,$3,$4);
376 0         do_opens = TRUE;
377 0         if (!$2) need_entire = TRUE;
378 0         if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
379           | PRINTF expr_list
380 0         { $$ = oper1(OPRINTF,$2);
381 0         if (!$2) need_entire = TRUE;
382           }
383           | BREAK
384 0         { $$ = oper0(OBREAK); }
385 0         | NEXT
386 0         { $$ = oper0(ONEXT); }
387 0         | EXIT
388 0         { $$ = oper0(OEXIT); }
389 0         | EXIT expr
390 0         { $$ = oper1(OEXIT,$2); }
391 0         | CONTINUE
392 0         { $$ = oper0(OCONTINUE); }
393 0         | RET
394 0         { $$ = oper0(ORETURN); }
395 0         | RET expr
396 0         { $$ = oper1(ORETURN,$2); }
397 0         | DELETE VAR '[' expr_list ']'
398 0         { $$ = oper2(ODELETE,aryrefarg($2),$4); }
399 0         ;
400            
401           redir : '>' %prec FIELD
402 0         { $$ = oper1(OREDIR,string(">",1)); }
403 0         | GRGR
404 0         { $$ = oper1(OREDIR,string(">>",2)); }
405 0         | '|'
406 0         { $$ = oper1(OREDIR,string("|",1)); }
407 0         ;
408            
409           compound
410           : IF '(' cond ')' maybe statement
411 0         { $$ = oper2(OIF,$3,bl($6,$5)); }
412 0         | IF '(' cond ')' maybe statement ELSE maybe statement
413 0         { $$ = oper3(OIF,$3,bl($6,$5),bl($9,$8)); }
414 0         | WHILE '(' cond ')' maybe statement
415 0         { $$ = oper2(OWHILE,$3,bl($6,$5)); }
416 0         | DO maybe statement WHILE '(' cond ')'
417 0         { $$ = oper2(ODO,bl($3,$2),$6); }
418 0         | FOR '(' simpnull ';' cond ';' simpnull ')' maybe statement
419 0         { $$ = oper4(OFOR,$3,$5,$7,bl($10,$9)); }
420 0         | FOR '(' simpnull ';' ';' simpnull ')' maybe statement
421 0         { $$ = oper4(OFOR,$3,string("",0),$6,bl($9,$8)); }
422 0         | FOR '(' expr ')' maybe statement
423 0         { $$ = oper2(OFORIN,$3,bl($6,$5)); }
424 0         | '{' maybe states '}' maybe
425 0         { $$ = oper3(OBLOCK,oper2(OJUNK,$2,$3),Nullop,$5); }
426 0         ;
427            
428           %%
429            
430           int yyparse (void);
431            
432           #include "a2py.c"