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