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