This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Allow more tries for t/op/time.t test 2.
[perl5.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
0de566d7 3 * Copyright (c) 1991-2002, 2003, 2004 Larry Wall
a687059c 4 *
9ef589d8
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8d063cd8 7 *
a0d0e21e
LW
8 */
9
10/*
11 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
14a38fd5 12 * All that is gold does not glitter, not all those who wander are lost.'
8d063cd8
LW
13 */
14
166f8a29
DM
15/* This file holds the grammar for the Perl language. If edited, you need
16 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
17 * and perly.act which are derived from this.
18 *
19 * The main job of of this grammar is to call the various newFOO()
20 * functions in op.c to build a syntax tree of OP structs.
61296642 21 * It relies on the lexer in toke.c to do the tokenizing.
166f8a29
DM
22 */
23
0de566d7 24/* Make the parser re-entrant. */
8d063cd8 25
0de566d7 26%pure_parser
8d063cd8 27
0de566d7 28%start prog
9d116dd7 29
8d063cd8 30%union {
79072805
LW
31 I32 ival;
32 char *pval;
33 OP *opval;
34 GV *gvval;
8d063cd8
LW
35}
36
fad39ff1 37%token <ival> '{'
f0fcb552 38
a0d0e21e 39%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
4633a7c4 40%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
79072805 41%token <pval> LABEL
a0d0e21e 42%token <ival> FORMAT SUB ANONSUB PACKAGE USE
79072805
LW
43%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
44%token <ival> LOOPEX DOTDOT
36477c24 45%token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
79072805 46%token <ival> RELOP EQOP MULOP ADDOP
55497cff 47%token <ival> DOLSHARP DO HASHBRACK NOAMP
09bef843
SB
48%token <ival> LOCAL MY MYSUB
49%token COLONATTR
79072805 50
09bef843 51%type <ival> prog decl format startsub startanonsub startformsub
500bedb6 52%type <ival> progstart remember mremember '&' savescope
bbce6d69 53%type <opval> block mblock lineseq line loop cond else
fad39ff1 54%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
bbce6d69 55%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
44a8e56a 56%type <opval> listexpr listexprcom indirob listop method
57%type <opval> formname subname proto subbody cont my_scalar
09bef843 58%type <opval> subattrlist myattrlist mysubrout myattrterm myterm
891be019 59%type <opval> termbinop termunop anonymous termdo
79072805 60%type <pval> label
79072805 61
fad39ff1
SM
62%nonassoc PREC_LOW
63%nonassoc LOOPEX
64
c963b151 65%left <ival> OROP DOROP
463ee0b2 66%left ANDOP
c07a80fd 67%right NOTOP
36477c24 68%nonassoc LSTOP LSTOPSUB
8d063cd8 69%left ','
a0d0e21e 70%right <ival> ASSIGNOP
8d063cd8
LW
71%right '?' ':'
72%nonassoc DOTDOT
c963b151 73%left OROR DORDOR
8d063cd8 74%left ANDAND
79072805
LW
75%left <ival> BITOROP
76%left <ival> BITANDOP
a687059c
LW
77%nonassoc EQOP
78%nonassoc RELOP
36477c24 79%nonassoc UNIOP UNIOPSUB
79072805 80%left <ival> SHIFTOP
a687059c
LW
81%left ADDOP
82%left MULOP
8990e307 83%left <ival> MATCHOP
79072805
LW
84%right '!' '~' UMINUS REFGEN
85%right <ival> POWOP
86%nonassoc PREINC PREDEC POSTINC POSTDEC
8990e307 87%left ARROW
fad39ff1 88%nonassoc <ival> ')'
8d063cd8 89%left '('
fad39ff1 90%left '[' '{'
8d063cd8
LW
91
92%% /* RULES */
93
891be019 94/* The whole program */
ecb2f335 95prog : progstart
ae986130 96 /*CONTINUED*/ lineseq
ecb2f335 97 { $$ = $1; newPROG(block_end($1,$2)); }
8d063cd8
LW
98 ;
99
891be019 100/* An ordinary block */
a687059c 101block : '{' remember lineseq '}'
3280af22 102 { if (PL_copline > (line_t)$1)
eb160463 103 PL_copline = (line_t)$1;
36477c24 104 $$ = block_end($2, $3); }
a0d0e21e
LW
105 ;
106
55497cff 107remember: /* NULL */ /* start a full lexical scope */
108 { $$ = block_start(TRUE); }
109 ;
110
ecb2f335
NIS
111progstart:
112 {
ecb2f335
NIS
113 PL_expect = XSTATE; $$ = block_start(TRUE);
114 }
115 ;
116
117
bbce6d69 118mblock : '{' mremember lineseq '}'
3280af22 119 { if (PL_copline > (line_t)$1)
eb160463 120 PL_copline = (line_t)$1;
36477c24 121 $$ = block_end($2, $3); }
55497cff 122 ;
123
124mremember: /* NULL */ /* start a partial lexical scope */
125 { $$ = block_start(FALSE); }
8d063cd8
LW
126 ;
127
500bedb6
DM
128savescope: /* NULL */ /* remember stack pos in case of error */
129 { $$ = PL_savestack_ix; }
130
891be019 131/* A collection of "lines" in the program */
8d063cd8 132lineseq : /* NULL */
79072805
LW
133 { $$ = Nullop; }
134 | lineseq decl
135 { $$ = $1; }
500bedb6
DM
136 | lineseq savescope line
137 { LEAVE_SCOPE($2);
138 $$ = append_list(OP_LINESEQ,
139 (LISTOP*)$1, (LISTOP*)$3);
3280af22 140 PL_pad_reset_pending = TRUE;
500bedb6 141 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
8d063cd8
LW
142 ;
143
891be019 144/* A "line" in the program */
79072805
LW
145line : label cond
146 { $$ = newSTATEOP(0, $1, $2); }
8d063cd8
LW
147 | loop /* loops add their own labels */
148 | label ';'
149 { if ($1 != Nullch) {
79072805 150 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
450a55e4
LW
151 }
152 else {
79072805 153 $$ = Nullop;
3280af22 154 PL_copline = NOLINE;
32c2e4fb 155 }
3280af22 156 PL_expect = XSTATE; }
8d063cd8 157 | label sideff ';'
79072805 158 { $$ = newSTATEOP(0, $1, $2);
3280af22 159 PL_expect = XSTATE; }
8d063cd8
LW
160 ;
161
891be019 162/* An expression which may have a side-effect */
a687059c 163sideff : error
79072805 164 { $$ = Nullop; }
a687059c 165 | expr
79072805 166 { $$ = $1; }
a687059c 167 | expr IF expr
79072805 168 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
a687059c 169 | expr UNLESS expr
79072805 170 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
a687059c 171 | expr WHILE expr
8990e307 172 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
55497cff 173 | expr UNTIL iexpr
174 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
ecca16b0 175 | expr FOR expr
eb160463 176 { $$ = newFOROP(0, Nullch, (line_t)$2,
ecca16b0 177 Nullop, $3, $1, Nullop); }
79072805
LW
178 ;
179
891be019 180/* else and elsif blocks */
79072805
LW
181else : /* NULL */
182 { $$ = Nullop; }
55497cff 183 | ELSE mblock
38a230cb 184 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
55497cff 185 | ELSIF '(' mexpr ')' mblock else
eb160463 186 { PL_copline = (line_t)$1;
2c15bef3 187 $$ = newCONDOP(0, $3, scope($5), $6);
3280af22 188 PL_hints |= HINT_BLOCK_SCOPE; }
79072805
LW
189 ;
190
891be019 191/* Real conditional expressions */
55497cff 192cond : IF '(' remember mexpr ')' mblock else
eb160463 193 { PL_copline = (line_t)$1;
36477c24 194 $$ = block_end($3,
bbce6d69 195 newCONDOP(0, $4, scope($6), $7)); }
55497cff 196 | UNLESS '(' remember miexpr ')' mblock else
eb160463 197 { PL_copline = (line_t)$1;
36477c24 198 $$ = block_end($3,
bbce6d69 199 newCONDOP(0, $4, scope($6), $7)); }
79072805
LW
200 ;
201
891be019 202/* Continue blocks */
79072805
LW
203cont : /* NULL */
204 { $$ = Nullop; }
205 | CONTINUE block
206 { $$ = scope($2); }
207 ;
208
891be019 209/* Loops: while, until, for, and a bare block */
55497cff 210loop : label WHILE '(' remember mtexpr ')' mblock cont
eb160463 211 { PL_copline = (line_t)$2;
36477c24 212 $$ = block_end($4,
55497cff 213 newSTATEOP(0, $1,
bbce6d69 214 newWHILEOP(0, 1, (LOOP*)Nullop,
fb73857a 215 $2, $5, $7, $8))); }
55497cff 216 | label UNTIL '(' remember miexpr ')' mblock cont
eb160463 217 { PL_copline = (line_t)$2;
36477c24 218 $$ = block_end($4,
55497cff 219 newSTATEOP(0, $1,
bbce6d69 220 newWHILEOP(0, 1, (LOOP*)Nullop,
fb73857a 221 $2, $5, $7, $8))); }
bbce6d69 222 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
36477c24 223 { $$ = block_end($4,
eb160463 224 newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); }
bbce6d69 225 | label FOR scalar '(' remember mexpr ')' mblock cont
36477c24 226 { $$ = block_end($5,
eb160463 227 newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP),
bbce6d69 228 $6, $8, $9)); }
229 | label FOR '(' remember mexpr ')' mblock cont
36477c24 230 { $$ = block_end($4,
eb160463 231 newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); }
bbce6d69 232 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
8d063cd8 233 /* basically fake up an initialize-while lineseq */
36c66720 234 { OP *forop;
eb160463 235 PL_copline = (line_t)$2;
36c66720
RH
236 forop = newSTATEOP(0, $1,
237 newWHILEOP(0, 1, (LOOP*)Nullop,
238 $2, scalar($7),
239 $11, $9));
240 if ($5) {
241 forop = append_elem(OP_LINESEQ,
242 newSTATEOP(0, ($1?savepv($1):Nullch),
243 $5),
244 forop);
245 }
246
247 $$ = block_end($4, forop); }
79072805 248 | label block cont /* a block is a loop that happens once */
fb73857a 249 { $$ = newSTATEOP(0, $1,
250 newWHILEOP(0, 1, (LOOP*)Nullop,
251 NOLINE, Nullop, $2, $3)); }
8d063cd8
LW
252 ;
253
891be019 254/* Normal expression */
8d063cd8 255nexpr : /* NULL */
79072805 256 { $$ = Nullop; }
8d063cd8
LW
257 | sideff
258 ;
259
891be019 260/* Boolean expression */
8d063cd8 261texpr : /* NULL means true */
b73d6f50 262 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
8d063cd8
LW
263 | expr
264 ;
265
891be019 266/* Inverted boolean expression */
55497cff 267iexpr : expr
268 { $$ = invert(scalar($1)); }
269 ;
270
891be019 271/* Expression with its own lexical scope */
55497cff 272mexpr : expr
bbce6d69 273 { $$ = $1; intro_my(); }
274 ;
275
276mnexpr : nexpr
277 { $$ = $1; intro_my(); }
55497cff 278 ;
279
280mtexpr : texpr
bbce6d69 281 { $$ = $1; intro_my(); }
55497cff 282 ;
283
284miexpr : iexpr
bbce6d69 285 { $$ = $1; intro_my(); }
55497cff 286 ;
287
891be019 288/* Optional "MAIN:"-style loop labels */
8d063cd8
LW
289label : /* empty */
290 { $$ = Nullch; }
32c2e4fb 291 | LABEL
8d063cd8
LW
292 ;
293
891be019 294/* Some kind of declaration - does not take part in the parse tree */
8d063cd8
LW
295decl : format
296 { $$ = 0; }
297 | subrout
298 { $$ = 0; }
09bef843
SB
299 | mysubrout
300 { $$ = 0; }
a687059c
LW
301 | package
302 { $$ = 0; }
a0d0e21e 303 | use
85e6fe83 304 { $$ = 0; }
8d063cd8
LW
305 ;
306
44a8e56a 307format : FORMAT startformsub formname block
a0d0e21e 308 { newFORM($2, $3, $4); }
44a8e56a 309 ;
310
311formname: WORD { $$ = $1; }
312 | /* NULL */ { $$ = Nullop; }
8d063cd8
LW
313 ;
314
891be019 315/* Unimplemented "my sub foo { }" */
09bef843
SB
316mysubrout: MYSUB startsub subname proto subattrlist subbody
317 { newMYSUB($2, $3, $4, $5, $6); }
318 ;
319
891be019 320/* Subroutine definition */
09bef843
SB
321subrout : SUB startsub subname proto subattrlist subbody
322 { newATTRSUB($2, $3, $4, $5, $6); }
28757baa 323 ;
324
fa83b5b6 325startsub: /* NULL */ /* start a regular subroutine scope */
774d564b 326 { $$ = start_subparse(FALSE, 0); }
28757baa 327 ;
328
329startanonsub: /* NULL */ /* start an anonymous subroutine scope */
774d564b 330 { $$ = start_subparse(FALSE, CVf_ANON); }
28757baa 331 ;
332
44a8e56a 333startformsub: /* NULL */ /* start a format subroutine scope */
774d564b 334 { $$ = start_subparse(TRUE, 0); }
44a8e56a 335 ;
336
891be019 337/* Name of a subroutine - must be a bareword, could be special */
2d8e6c8d 338subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
e858de61 339 if (strEQ(name, "BEGIN") || strEQ(name, "END")
7d30b5c4 340 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
1aff0e91 341 CvSPECIAL_on(PL_compcv);
28757baa 342 $$ = $1; }
a0d0e21e
LW
343 ;
344
891be019 345/* Subroutine prototype */
4633a7c4
LW
346proto : /* NULL */
347 { $$ = Nullop; }
348 | THING
349 ;
28757baa 350
891be019 351/* Optional list of subroutine attributes */
09bef843
SB
352subattrlist: /* NULL */
353 { $$ = Nullop; }
354 | COLONATTR THING
355 { $$ = $2; }
356 | COLONATTR
357 { $$ = Nullop; }
358 ;
359
891be019 360/* List of attributes for a "my" variable declaration */
09bef843
SB
361myattrlist: COLONATTR THING
362 { $$ = $2; }
363 | COLONATTR
364 { $$ = Nullop; }
365 ;
366
891be019 367/* Subroutine body - either null or a block */
28757baa 368subbody : block { $$ = $1; }
3280af22 369 | ';' { $$ = Nullop; PL_expect = XSTATE; }
8d063cd8
LW
370 ;
371
a687059c 372package : PACKAGE WORD ';'
79072805 373 { package($2); }
a687059c
LW
374 ;
375
28757baa 376use : USE startsub
1aff0e91 377 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa 378 WORD WORD listexpr ';'
379 { utilize($1, $2, $4, $5, $6); }
85e6fe83
LW
380 ;
381
891be019 382/* Ordinary expressions; logical combinations */
a0d0e21e
LW
383expr : expr ANDOP expr
384 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
385 | expr OROP expr
386 { $$ = newLOGOP($2, 0, $1, $3); }
c963b151
BD
387 | expr DOROP expr
388 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
fad39ff1 389 | argexpr %prec PREC_LOW
a0d0e21e
LW
390 ;
391
891be019 392/* Expressions are a list of terms joined by commas */
a0d0e21e
LW
393argexpr : argexpr ','
394 { $$ = $1; }
395 | argexpr ',' term
79072805 396 { $$ = append_elem(OP_LIST, $1, $3); }
fad39ff1 397 | term %prec PREC_LOW
8d063cd8
LW
398 ;
399
891be019
SC
400/* List operators */
401listop : LSTOP indirob argexpr /* print $fh @args */
79072805 402 { $$ = convert($1, OPf_STACKED,
a0d0e21e 403 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
891be019 404 | FUNC '(' indirob expr ')' /* print ($fh @args */
79072805 405 { $$ = convert($1, OPf_STACKED,
a0d0e21e 406 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
891be019 407 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
4633a7c4 408 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 409 append_elem(OP_LIST,
55497cff 410 prepend_elem(OP_LIST, scalar($1), $5),
a0d0e21e 411 newUNOP(OP_METHOD, 0, $3))); }
891be019 412 | term ARROW method /* $foo->bar */
b1524f17
SM
413 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
414 append_elem(OP_LIST, scalar($1),
415 newUNOP(OP_METHOD, 0, $3))); }
891be019 416 | METHOD indirob listexpr /* new Class @args */
4633a7c4 417 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 418 append_elem(OP_LIST,
4633a7c4 419 prepend_elem(OP_LIST, $2, $3),
a0d0e21e 420 newUNOP(OP_METHOD, 0, $1))); }
891be019 421 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
4633a7c4 422 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 423 append_elem(OP_LIST,
4633a7c4 424 prepend_elem(OP_LIST, $2, $4),
a0d0e21e 425 newUNOP(OP_METHOD, 0, $1))); }
891be019 426 | LSTOP listexpr /* print @args */
79072805 427 { $$ = convert($1, 0, $2); }
891be019 428 | FUNC '(' listexprcom ')' /* print (@args) */
79072805 429 { $$ = convert($1, 0, $3); }
ecb2f335 430 | LSTOPSUB startanonsub block /* map { foo } ... */
09bef843 431 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
891be019 432 listexpr %prec LSTOP /* ... @bar */
4633a7c4 433 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
28757baa 434 append_elem(OP_LIST,
435 prepend_elem(OP_LIST, $3, $5), $1)); }
a687059c
LW
436 ;
437
891be019 438/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
439method : METHOD
440 | scalar
441 ;
442
891be019
SC
443/* Some kind of subscripted expression */
444subscripted: star '{' expr ';' '}' /* *main::{something} */
445 /* In this and all the hash accessors, ';' is
446 * provided by the tokeniser */
264e1af3
RGS
447 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
448 PL_expect = XOPERATOR; }
891be019 449 | scalar '[' expr ']' /* $array[$element] */
fad39ff1 450 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
891be019 451 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
452 { $$ = newBINOP(OP_AELEM, 0,
453 ref(newAVREF($1),OP_RV2AV),
454 scalar($4));}
891be019 455 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
456 { $$ = newBINOP(OP_AELEM, 0,
457 ref(newAVREF($1),OP_RV2AV),
458 scalar($3));}
891be019 459 | scalar '{' expr ';' '}' /* $foo->{bar();} */
fad39ff1
SM
460 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
461 PL_expect = XOPERATOR; }
891be019 462 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
463 { $$ = newBINOP(OP_HELEM, 0,
464 ref(newHVREF($1),OP_RV2HV),
465 jmaybe($4));
466 PL_expect = XOPERATOR; }
891be019 467 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
468 { $$ = newBINOP(OP_HELEM, 0,
469 ref(newHVREF($1),OP_RV2HV),
470 jmaybe($3));
471 PL_expect = XOPERATOR; }
891be019 472 | term ARROW '(' ')' /* $subref->() */
fad39ff1
SM
473 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
474 newCVREF(0, scalar($1))); }
891be019 475 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1
SM
476 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
477 append_elem(OP_LIST, $4,
478 newCVREF(0, scalar($1)))); }
479
891be019 480 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1
SM
481 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
482 append_elem(OP_LIST, $3,
483 newCVREF(0, scalar($1)))); }
891be019 484 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1
SM
485 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
486 newCVREF(0, scalar($1))); }
891be019 487 ;
fad39ff1 488
891be019
SC
489/* Binary operators between terms */
490termbinop : term ASSIGNOP term /* $x = $y */
a0d0e21e 491 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
891be019 492 | term POWOP term /* $x ** $y */
79072805 493 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 494 | term MULOP term /* $x * $y, $x x $y */
79072805
LW
495 { if ($2 != OP_REPEAT)
496 scalar($1);
497 $$ = newBINOP($2, 0, $1, scalar($3)); }
891be019 498 | term ADDOP term /* $x + $y */
79072805 499 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 500 | term SHIFTOP term /* $x >> $y, $x << $y */
79072805 501 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 502 | term RELOP term /* $x > $y, etc. */
79072805 503 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 504 | term EQOP term /* $x == $y, $x eq $y */
79072805 505 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 506 | term BITANDOP term /* $x & $y */
79072805 507 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 508 | term BITOROP term /* $x | $y */
79072805 509 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 510 | term DOTDOT term /* $x..$y, $x...$y */
79072805 511 { $$ = newRANGE($2, scalar($1), scalar($3));}
891be019 512 | term ANDAND term /* $x && $y */
463ee0b2 513 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
891be019 514 | term OROR term /* $x || $y */
463ee0b2 515 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
c963b151
BD
516 | term DORDOR term /* $x // $y */
517 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
891be019 518 | term MATCHOP term /* $x =~ /$y/ */
79072805 519 { $$ = bind_match($2, $1, $3); }
891be019 520 ;
8d063cd8 521
891be019
SC
522/* Unary operators and terms */
523termunop : '-' term %prec UMINUS /* -$x */
79072805 524 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
891be019 525 | '+' term %prec UMINUS /* +$x */
a687059c 526 { $$ = $2; }
891be019 527 | '!' term /* !$x */
79072805 528 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 529 | '~' term /* ~$x */
79072805 530 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
891be019 531 | term POSTINC /* $x++ */
79072805 532 { $$ = newUNOP(OP_POSTINC, 0,
463ee0b2 533 mod(scalar($1), OP_POSTINC)); }
891be019 534 | term POSTDEC /* $x-- */
79072805 535 { $$ = newUNOP(OP_POSTDEC, 0,
463ee0b2 536 mod(scalar($1), OP_POSTDEC)); }
891be019 537 | PREINC term /* ++$x */
79072805 538 { $$ = newUNOP(OP_PREINC, 0,
463ee0b2 539 mod(scalar($2), OP_PREINC)); }
891be019 540 | PREDEC term /* --$x */
79072805 541 { $$ = newUNOP(OP_PREDEC, 0,
463ee0b2 542 mod(scalar($2), OP_PREDEC)); }
891be019
SC
543
544 ;
545
546/* Constructors for anonymous data */
547anonymous: '[' expr ']'
548 { $$ = newANONLIST($2); }
549 | '[' ']'
550 { $$ = newANONLIST(Nullop); }
551 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
ecb2f335 552 { $$ = newANONHASH($2); }
891be019
SC
553 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
554 { $$ = newANONHASH(Nullop); }
555 | ANONSUB startanonsub proto subattrlist block %prec '('
556 { $$ = newANONATTRSUB($2, $3, $4, $5); }
557
558 ;
559
560/* Things called with "do" */
561termdo : DO term %prec UNIOP /* do $filename */
562 { $$ = dofile($2); }
563 | DO block %prec '(' /* do { code */
564 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
565 | DO WORD '(' ')' /* do somesub() */
566 { $$ = newUNOP(OP_ENTERSUB,
567 OPf_SPECIAL|OPf_STACKED,
568 prepend_elem(OP_LIST,
569 scalar(newCVREF(
570 (OPpENTERSUB_AMPER<<8),
571 scalar($2)
572 )),Nullop)); dep();}
573 | DO WORD '(' expr ')' /* do somesub(@args) */
574 { $$ = newUNOP(OP_ENTERSUB,
575 OPf_SPECIAL|OPf_STACKED,
576 append_elem(OP_LIST,
577 $4,
578 scalar(newCVREF(
579 (OPpENTERSUB_AMPER<<8),
580 scalar($2)
581 )))); dep();}
582 | DO scalar '(' ')' /* do $subref () */
583 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
584 prepend_elem(OP_LIST,
585 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
586 | DO scalar '(' expr ')' /* do $subref (@args) */
587 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
588 prepend_elem(OP_LIST,
589 $4,
590 scalar(newCVREF(0,scalar($2))))); dep();}
591
592 ;
593
594term : termbinop
595 | termunop
596 | anonymous
597 | termdo
e9bdcc27 598 | term '?' term ':' term
891be019
SC
599 { $$ = newCONDOP(0, $1, $3, $5); }
600 | REFGEN term /* \$x, \@y, \%z */
601 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
09bef843
SB
602 | myattrterm %prec UNIOP
603 { $$ = $1; }
604 | LOCAL term %prec UNIOP
93a17b20 605 { $$ = localize($2,$1); }
a0d0e21e 606 | '(' expr ')'
79072805 607 { $$ = sawparens($2); }
8d063cd8 608 | '(' ')'
8990e307 609 { $$ = sawparens(newNULLLIST()); }
79072805 610 | scalar %prec '('
8d063cd8 611 { $$ = $1; }
79072805 612 | star %prec '('
8d063cd8 613 { $$ = $1; }
79072805 614 | hsh %prec '('
8d063cd8 615 { $$ = $1; }
79072805 616 | ary %prec '('
8d063cd8 617 { $$ = $1; }
891be019 618 | arylen %prec '(' /* $#x, $#{ something } */
79072805 619 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
620 | subscripted
621 { $$ = $1; }
891be019 622 | '(' expr ')' '[' expr ']' /* list slice */
79072805 623 { $$ = newSLICEOP(0, $5, $2); }
891be019 624 | '(' ')' '[' expr ']' /* empty list slice! */
79072805 625 { $$ = newSLICEOP(0, $4, Nullop); }
891be019 626 | ary '[' expr ']' /* array slice */
79072805
LW
627 { $$ = prepend_elem(OP_ASLICE,
628 newOP(OP_PUSHMARK, 0),
79072805
LW
629 newLISTOP(OP_ASLICE, 0,
630 list($3),
a0d0e21e 631 ref($1, OP_ASLICE))); }
891be019 632 | ary '{' expr ';' '}' /* @hash{@keys} */
79072805
LW
633 { $$ = prepend_elem(OP_HSLICE,
634 newOP(OP_PUSHMARK, 0),
79072805
LW
635 newLISTOP(OP_HSLICE, 0,
636 list($3),
a0d0e21e 637 ref(oopsHV($1), OP_HSLICE)));
3280af22 638 PL_expect = XOPERATOR; }
79072805
LW
639 | THING %prec '('
640 { $$ = $1; }
891be019 641 | amper /* &foo; */
c07a80fd 642 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ecb2f335 643 | amper '(' ')' /* &foo() */
a0d0e21e 644 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 645 | amper '(' expr ')' /* &foo(@args) */
a0d0e21e 646 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 647 append_elem(OP_LIST, $3, scalar($1))); }
891be019 648 | NOAMP WORD listexpr /* foo(@args) */
a0d0e21e 649 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
a5f75d66 650 append_elem(OP_LIST, $3, scalar($2))); }
891be019 651 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
85e6fe83 652 { $$ = newOP($1, OPf_SPECIAL);
3280af22 653 PL_hints |= HINT_BLOCK_SCOPE; }
a0d0e21e 654 | LOOPEX term
8990e307 655 { $$ = newLOOPEX($1,$2); }
891be019 656 | NOTOP argexpr /* not $foo */
c07a80fd 657 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 658 | UNIOP /* Unary op, $_ implied */
79072805 659 { $$ = newOP($1, 0); }
30173ab5 660 | UNIOP block /* eval { foo } */
79072805 661 { $$ = newUNOP($1, 0, $2); }
891be019 662 | UNIOP term /* Unary op */
79072805 663 { $$ = newUNOP($1, 0, $2); }
891be019 664 | UNIOPSUB term /* Sub treated as unop */
4633a7c4
LW
665 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
666 append_elem(OP_LIST, $2, scalar($1))); }
891be019 667 | FUNC0 /* Nullary operator */
79072805 668 { $$ = newOP($1, 0); }
ae986130 669 | FUNC0 '(' ')'
79072805 670 { $$ = newOP($1, 0); }
891be019 671 | FUNC0SUB /* Sub treated as nullop */
28757baa 672 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 673 scalar($1)); }
891be019 674 | FUNC1 '(' ')' /* not () */
79072805 675 { $$ = newOP($1, OPf_SPECIAL); }
891be019 676 | FUNC1 '(' expr ')' /* not($foo) */
79072805 677 { $$ = newUNOP($1, 0, $3); }
131b3ad0
DM
678 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
679 { $$ = pmruntime($1, $3, 1); }
79072805 680 | WORD
378cc40b 681 | listop
8d063cd8
LW
682 ;
683
891be019 684/* "my" declarations, with optional attributes */
09bef843
SB
685myattrterm: MY myterm myattrlist
686 { $$ = my_attrs($2,$3); }
687 | MY myterm
688 { $$ = localize($2,$1); }
689 ;
690
891be019 691/* Things that can be "my"'d */
09bef843
SB
692myterm : '(' expr ')'
693 { $$ = sawparens($2); }
694 | '(' ')'
695 { $$ = sawparens(newNULLLIST()); }
696 | scalar %prec '('
697 { $$ = $1; }
698 | hsh %prec '('
699 { $$ = $1; }
700 | ary %prec '('
701 { $$ = $1; }
702 ;
703
891be019 704/* Basic list expressions */
fad39ff1 705listexpr: /* NULL */ %prec PREC_LOW
8990e307 706 { $$ = Nullop; }
fad39ff1 707 | argexpr %prec PREC_LOW
a0d0e21e
LW
708 { $$ = $1; }
709 ;
710
711listexprcom: /* NULL */
712 { $$ = Nullop; }
79072805
LW
713 | expr
714 { $$ = $1; }
a0d0e21e
LW
715 | expr ','
716 { $$ = $1; }
79072805
LW
717 ;
718
891be019
SC
719/* A little bit of trickery to make "for my $foo (@bar)" actually be
720 lexical */
55497cff 721my_scalar: scalar
3280af22 722 { PL_in_my = 0; $$ = my($1); }
55497cff 723 ;
724
79072805 725amper : '&' indirob
c07a80fd 726 { $$ = newCVREF($1,$2); }
a687059c
LW
727 ;
728
79072805
LW
729scalar : '$' indirob
730 { $$ = newSVREF($2); }
a687059c
LW
731 ;
732
79072805
LW
733ary : '@' indirob
734 { $$ = newAVREF($2); }
735 ;
736
737hsh : '%' indirob
738 { $$ = newHVREF($2); }
739 ;
740
741arylen : DOLSHARP indirob
742 { $$ = newAVREF($2); }
743 ;
744
745star : '*' indirob
a0d0e21e 746 { $$ = newGVREF(0,$2); }
79072805
LW
747 ;
748
891be019 749/* Indirect objects */
79072805
LW
750indirob : WORD
751 { $$ = scalar($1); }
fad39ff1 752 | scalar %prec PREC_LOW
463ee0b2 753 { $$ = scalar($1); }
79072805 754 | block
a0d0e21e 755 { $$ = scope($1); }
79072805 756
93a17b20
LW
757 | PRIVATEREF
758 { $$ = $1; }
8d063cd8 759 ;