This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PATCH: [perl #133620] heap buffer overflow
[perl5.git] / perly.y
1 /*    perly.y
2  *
3  *    Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
4  *    Copyright (c) 2007, 2008, 2009, 2010, 2011 by Larry Wall and others
5  *
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.
8  *
9  */
10
11 /*
12  * 'I see,' laughed Strider.  'I look foul and feel fair.  Is that it?
13  *  All that is gold does not glitter, not all those who wander are lost.'
14  *
15  *     [p.171 of _The Lord of the Rings_, I/x: "Strider"]
16  */
17
18 /*
19  * This file holds the grammar for the Perl language. If edited, you need
20  * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
21  * and perly.act which are derived from this.
22  *
23  * The main job of of this grammar is to call the various newFOO()
24  * functions in op.c to build a syntax tree of OP structs.
25  * It relies on the lexer in toke.c to do the tokenizing.
26  *
27  * Note: due to the way that the cleanup code works WRT to freeing ops on
28  * the parse stack, it is dangerous to assign to the $n variables within
29  * an action.
30  */
31
32 /*  Make the parser re-entrant. */
33
34 %pure-parser
35
36 %start grammar
37
38 %union {
39     I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
40                                 must always be 1st union member) */
41     char *pval;
42     OP *opval;
43     GV *gvval;
44 }
45
46 %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
47
48 %token <ival> '{' '}' '[' ']' '-' '+' '@' '%' '&' '=' '.'
49
50 %token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
51 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
52 %token <opval> PLUGEXPR PLUGSTMT
53 %token <pval> LABEL
54 %token <ival> FORMAT SUB SIGSUB ANONSUB ANON_SIGSUB PACKAGE USE
55 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
56 %token <ival> GIVEN WHEN DEFAULT
57 %token <ival> LOOPEX DOTDOT YADAYADA
58 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
59 %token <ival> RELOP EQOP MULOP ADDOP
60 %token <ival> DOLSHARP DO HASHBRACK NOAMP
61 %token <ival> LOCAL MY REQUIRE
62 %token <ival> COLONATTR FORMLBRACK FORMRBRACK
63
64 %type <ival> grammar remember mremember
65 %type <ival>  startsub startanonsub startformsub
66
67 %type <ival> mintro
68
69 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
70 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
71 %type <opval> sliceme kvslice gelem
72 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
73 %type <opval> optlistexpr optexpr optrepl indirob listop method
74 %type <opval> formname subname proto cont my_scalar my_var
75 %type <opval> refgen_topic formblock
76 %type <opval> subattrlist myattrlist myattrterm myterm
77 %type <opval> termbinop termunop anonymous termdo
78 %type <ival>  sigslurpsigil
79 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
80 %type <opval> sigelem siglist siglistornull subsignature optsubsignature
81 %type <opval> subbody optsubbody sigsubbody optsigsubbody
82 %type <opval> formstmtseq formline formarg
83
84 %nonassoc <ival> PREC_LOW
85 %nonassoc LOOPEX
86
87 %left <ival> OROP DOROP
88 %left <ival> ANDOP
89 %right <ival> NOTOP
90 %nonassoc LSTOP LSTOPSUB
91 %left <ival> ','
92 %right <ival> ASSIGNOP
93 %right <ival> '?' ':'
94 %nonassoc DOTDOT
95 %left <ival> OROR DORDOR
96 %left <ival> ANDAND
97 %left <ival> BITOROP
98 %left <ival> BITANDOP
99 %nonassoc EQOP
100 %nonassoc RELOP
101 %nonassoc UNIOP UNIOPSUB
102 %nonassoc REQUIRE
103 %left <ival> SHIFTOP
104 %left ADDOP
105 %left MULOP
106 %left <ival> MATCHOP
107 %right <ival> '!' '~' UMINUS REFGEN
108 %right <ival> POWOP
109 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
110 %left <ival> ARROW
111 %nonassoc <ival> ')'
112 %left <ival> '('
113 %left '[' '{'
114
115 %% /* RULES */
116
117 /* Top-level choice of what kind of thing yyparse was called to parse */
118 grammar :       GRAMPROG
119                         {
120                           parser->expect = XSTATE;
121                           $<ival>$ = 0;
122                         }
123                 remember stmtseq
124                         {
125                           newPROG(block_end($3,$4));
126                           PL_compiling.cop_seq = 0;
127                           $$ = 0;
128                         }
129         |       GRAMEXPR
130                         {
131                           parser->expect = XTERM;
132                           $<ival>$ = 0;
133                         }
134                 optexpr
135                         {
136                           PL_eval_root = $3;
137                           $$ = 0;
138                         }
139         |       GRAMBLOCK
140                         {
141                           parser->expect = XBLOCK;
142                           $<ival>$ = 0;
143                         }
144                 block
145                         {
146                           PL_pad_reset_pending = TRUE;
147                           PL_eval_root = $3;
148                           $$ = 0;
149                           yyunlex();
150                           parser->yychar = yytoken = YYEOF;
151                         }
152         |       GRAMBARESTMT
153                         {
154                           parser->expect = XSTATE;
155                           $<ival>$ = 0;
156                         }
157                 barestmt
158                         {
159                           PL_pad_reset_pending = TRUE;
160                           PL_eval_root = $3;
161                           $$ = 0;
162                           yyunlex();
163                           parser->yychar = yytoken = YYEOF;
164                         }
165         |       GRAMFULLSTMT
166                         {
167                           parser->expect = XSTATE;
168                           $<ival>$ = 0;
169                         }
170                 fullstmt
171                         {
172                           PL_pad_reset_pending = TRUE;
173                           PL_eval_root = $3;
174                           $$ = 0;
175                           yyunlex();
176                           parser->yychar = yytoken = YYEOF;
177                         }
178         |       GRAMSTMTSEQ
179                         {
180                           parser->expect = XSTATE;
181                           $<ival>$ = 0;
182                         }
183                 stmtseq
184                         {
185                           PL_eval_root = $3;
186                           $$ = 0;
187                         }
188         ;
189
190 /* An ordinary block */
191 block   :       '{' remember stmtseq '}'
192                         { if (parser->copline > (line_t)$1)
193                               parser->copline = (line_t)$1;
194                           $$ = block_end($2, $3);
195                         }
196         ;
197
198 /* format body */
199 formblock:      '=' remember ';' FORMRBRACK formstmtseq ';' '.'
200                         { if (parser->copline > (line_t)$1)
201                               parser->copline = (line_t)$1;
202                           $$ = block_end($2, $5);
203                         }
204         ;
205
206 remember:       /* NULL */      /* start a full lexical scope */
207                         { $$ = block_start(TRUE);
208                           parser->parsed_sub = 0; }
209         ;
210
211 mblock  :       '{' mremember stmtseq '}'
212                         { if (parser->copline > (line_t)$1)
213                               parser->copline = (line_t)$1;
214                           $$ = block_end($2, $3);
215                         }
216         ;
217
218 mremember:      /* NULL */      /* start a partial lexical scope */
219                         { $$ = block_start(FALSE);
220                           parser->parsed_sub = 0; }
221         ;
222
223 /* A sequence of statements in the program */
224 stmtseq :       /* NULL */
225                         { $$ = NULL; }
226         |       stmtseq fullstmt
227                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
228                             PL_pad_reset_pending = TRUE;
229                             if ($1 && $2)
230                                 PL_hints |= HINT_BLOCK_SCOPE;
231                         }
232         ;
233
234 /* A sequence of format lines */
235 formstmtseq:    /* NULL */
236                         { $$ = NULL; }
237         |       formstmtseq formline
238                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
239                             PL_pad_reset_pending = TRUE;
240                             if ($1 && $2)
241                                 PL_hints |= HINT_BLOCK_SCOPE;
242                         }
243         ;
244
245 /* A statement in the program, including optional labels */
246 fullstmt:       barestmt
247                         {
248                           $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
249                         }
250         |       labfullstmt
251                         { $$ = $1; }
252         ;
253
254 labfullstmt:    LABEL barestmt
255                         {
256                           $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
257                         }
258         |       LABEL labfullstmt
259                         {
260                           $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
261                         }
262         ;
263
264 /* A bare statement, lacking label and other aspects of state op */
265 barestmt:       PLUGSTMT
266                         { $$ = $1; }
267         |       FORMAT startformsub formname formblock
268                         {
269                           CV *fmtcv = PL_compcv;
270                           newFORM($2, $3, $4);
271                           $$ = NULL;
272                           if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
273                               pad_add_weakref(fmtcv);
274                           }
275                           parser->parsed_sub = 1;
276                         }
277         |       SUB subname startsub
278                     /* sub declaration or definition not within scope
279                        of 'use feature "signatures"'*/
280                         {
281                           init_named_cv(PL_compcv, $2);
282                           parser->in_my = 0;
283                           parser->in_my_stash = NULL;
284                         }
285                     proto subattrlist optsubbody
286                         {
287                           SvREFCNT_inc_simple_void(PL_compcv);
288                           $2->op_type == OP_CONST
289                               ? newATTRSUB($3, $2, $5, $6, $7)
290                               : newMYSUB($3, $2, $5, $6, $7)
291                           ;
292                           $$ = NULL;
293                           intro_my();
294                           parser->parsed_sub = 1;
295                         }
296         |       SIGSUB subname startsub
297                     /* sub declaration or definition under 'use feature
298                      * "signatures"'. (Note that a signature isn't
299                      * allowed in a declaration)
300                      */
301                         {
302                           init_named_cv(PL_compcv, $2);
303                           parser->in_my = 0;
304                           parser->in_my_stash = NULL;
305                         }
306                     subattrlist optsigsubbody
307                         {
308                           SvREFCNT_inc_simple_void(PL_compcv);
309                           $2->op_type == OP_CONST
310                               ? newATTRSUB($3, $2, NULL, $5, $6)
311                               : newMYSUB(  $3, $2, NULL, $5, $6)
312                           ;
313                           $$ = NULL;
314                           intro_my();
315                           parser->parsed_sub = 1;
316                         }
317         |       PACKAGE BAREWORD BAREWORD ';'
318                         {
319                           package($3);
320                           if ($2)
321                               package_version($2);
322                           $$ = NULL;
323                         }
324         |       USE startsub
325                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
326                 BAREWORD BAREWORD optlistexpr ';'
327                         {
328                           SvREFCNT_inc_simple_void(PL_compcv);
329                           utilize($1, $2, $4, $5, $6);
330                           parser->parsed_sub = 1;
331                           $$ = NULL;
332                         }
333         |       IF '(' remember mexpr ')' mblock else
334                         {
335                           $$ = block_end($3,
336                               newCONDOP(0, $4, op_scope($6), $7));
337                           parser->copline = (line_t)$1;
338                         }
339         |       UNLESS '(' remember mexpr ')' mblock else
340                         {
341                           $$ = block_end($3,
342                               newCONDOP(0, $4, $7, op_scope($6)));
343                           parser->copline = (line_t)$1;
344                         }
345         |       GIVEN '(' remember mexpr ')' mblock
346                         {
347                           $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
348                           parser->copline = (line_t)$1;
349                         }
350         |       WHEN '(' remember mexpr ')' mblock
351                         { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
352         |       DEFAULT block
353                         { $$ = newWHENOP(0, op_scope($2)); }
354         |       WHILE '(' remember texpr ')' mintro mblock cont
355                         {
356                           $$ = block_end($3,
357                                   newWHILEOP(0, 1, NULL,
358                                       $4, $7, $8, $6));
359                           parser->copline = (line_t)$1;
360                         }
361         |       UNTIL '(' remember iexpr ')' mintro mblock cont
362                         {
363                           $$ = block_end($3,
364                                   newWHILEOP(0, 1, NULL,
365                                       $4, $7, $8, $6));
366                           parser->copline = (line_t)$1;
367                         }
368         |       FOR '(' remember mnexpr ';'
369                         { parser->expect = XTERM; }
370                 texpr ';'
371                         { parser->expect = XTERM; }
372                 mintro mnexpr ')'
373                 mblock
374                         {
375                           OP *initop = $4;
376                           OP *forop = newWHILEOP(0, 1, NULL,
377                                       scalar($7), $13, $11, $10);
378                           if (initop) {
379                               forop = op_prepend_elem(OP_LINESEQ, initop,
380                                   op_append_elem(OP_LINESEQ,
381                                       newOP(OP_UNSTACK, OPf_SPECIAL),
382                                       forop));
383                           }
384                           PL_hints |= HINT_BLOCK_SCOPE;
385                           $$ = block_end($3, forop);
386                           parser->copline = (line_t)$1;
387                         }
388         |       FOR MY remember my_scalar '(' mexpr ')' mblock cont
389                         {
390                           $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
391                           parser->copline = (line_t)$1;
392                         }
393         |       FOR scalar '(' remember mexpr ')' mblock cont
394                         {
395                           $$ = block_end($4, newFOROP(0,
396                                       op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
397                           parser->copline = (line_t)$1;
398                         }
399         |       FOR my_refgen remember my_var
400                         { parser->in_my = 0; $<opval>$ = my($4); }
401                 '(' mexpr ')' mblock cont
402                         {
403                           $$ = block_end(
404                                 $3,
405                                 newFOROP(0,
406                                          op_lvalue(
407                                             newUNOP(OP_REFGEN, 0,
408                                                     $<opval>5),
409                                             OP_ENTERLOOP),
410                                          $7, $9, $10)
411                           );
412                           parser->copline = (line_t)$1;
413                         }
414         |       FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
415                         {
416                           $$ = block_end($5, newFOROP(
417                                 0, op_lvalue(newUNOP(OP_REFGEN, 0,
418                                                      $3),
419                                              OP_ENTERLOOP), $6, $8, $9));
420                           parser->copline = (line_t)$1;
421                         }
422         |       FOR '(' remember mexpr ')' mblock cont
423                         {
424                           $$ = block_end($3,
425                                   newFOROP(0, NULL, $4, $6, $7));
426                           parser->copline = (line_t)$1;
427                         }
428         |       block cont
429                         {
430                           /* a block is a loop that happens once */
431                           $$ = newWHILEOP(0, 1, NULL,
432                                   NULL, $1, $2, 0);
433                         }
434         |       PACKAGE BAREWORD BAREWORD '{' remember
435                         {
436                           package($3);
437                           if ($2) {
438                               package_version($2);
439                           }
440                         }
441                 stmtseq '}'
442                         {
443                           /* a block is a loop that happens once */
444                           $$ = newWHILEOP(0, 1, NULL,
445                                   NULL, block_end($5, $7), NULL, 0);
446                           if (parser->copline > (line_t)$4)
447                               parser->copline = (line_t)$4;
448                         }
449         |       sideff ';'
450                         {
451                           $$ = $1;
452                         }
453         |       YADAYADA ';'
454                         {
455                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
456                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
457                         }
458         |       ';'
459                         {
460                           $$ = NULL;
461                           parser->copline = NOLINE;
462                         }
463         ;
464
465 /* Format line */
466 formline:       THING formarg
467                         { OP *list;
468                           if ($2) {
469                               OP *term = $2;
470                               list = op_append_elem(OP_LIST, $1, term);
471                           }
472                           else {
473                               list = $1;
474                           }
475                           if (parser->copline == NOLINE)
476                                parser->copline = CopLINE(PL_curcop)-1;
477                           else parser->copline--;
478                           $$ = newSTATEOP(0, NULL,
479                                           op_convert_list(OP_FORMLINE, 0, list));
480                         }
481         ;
482
483 formarg :       /* NULL */
484                         { $$ = NULL; }
485         |       FORMLBRACK stmtseq FORMRBRACK
486                         { $$ = op_unscope($2); }
487         ;
488
489 /* An expression which may have a side-effect */
490 sideff  :       error
491                         { $$ = NULL; }
492         |       expr
493                         { $$ = $1; }
494         |       expr IF expr
495                         { $$ = newLOGOP(OP_AND, 0, $3, $1); }
496         |       expr UNLESS expr
497                         { $$ = newLOGOP(OP_OR, 0, $3, $1); }
498         |       expr WHILE expr
499                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
500         |       expr UNTIL iexpr
501                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
502         |       expr FOR expr
503                         { $$ = newFOROP(0, NULL, $3, $1, NULL);
504                           parser->copline = (line_t)$2; }
505         |       expr WHEN expr
506                         { $$ = newWHENOP($3, op_scope($1)); }
507         ;
508
509 /* else and elsif blocks */
510 else    :       /* NULL */
511                         { $$ = NULL; }
512         |       ELSE mblock
513                         {
514                           ($2)->op_flags |= OPf_PARENS;
515                           $$ = op_scope($2);
516                         }
517         |       ELSIF '(' mexpr ')' mblock else
518                         { parser->copline = (line_t)$1;
519                             $$ = newCONDOP(0,
520                                 newSTATEOP(OPf_SPECIAL,NULL,$3),
521                                 op_scope($5), $6);
522                           PL_hints |= HINT_BLOCK_SCOPE;
523                         }
524         ;
525
526 /* Continue blocks */
527 cont    :       /* NULL */
528                         { $$ = NULL; }
529         |       CONTINUE block
530                         { $$ = op_scope($2); }
531         ;
532
533 /* determine whether there are any new my declarations */
534 mintro  :       /* NULL */
535                         { $$ = (PL_min_intro_pending &&
536                             PL_max_intro_pending >=  PL_min_intro_pending);
537                           intro_my(); }
538
539 /* Normal expression */
540 nexpr   :       /* NULL */
541                         { $$ = NULL; }
542         |       sideff
543         ;
544
545 /* Boolean expression */
546 texpr   :       /* NULL means true */
547                         { YYSTYPE tmplval;
548                           (void)scan_num("1", &tmplval);
549                           $$ = tmplval.opval; }
550         |       expr
551         ;
552
553 /* Inverted boolean expression */
554 iexpr   :       expr
555                         { $$ = invert(scalar($1)); }
556         ;
557
558 /* Expression with its own lexical scope */
559 mexpr   :       expr
560                         { $$ = $1; intro_my(); }
561         ;
562
563 mnexpr  :       nexpr
564                         { $$ = $1; intro_my(); }
565         ;
566
567 formname:       BAREWORD        { $$ = $1; }
568         |       /* NULL */      { $$ = NULL; }
569         ;
570
571 startsub:       /* NULL */      /* start a regular subroutine scope */
572                         { $$ = start_subparse(FALSE, 0);
573                             SAVEFREESV(PL_compcv); }
574
575         ;
576
577 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
578                         { $$ = start_subparse(FALSE, CVf_ANON);
579                             SAVEFREESV(PL_compcv); }
580         ;
581
582 startformsub:   /* NULL */      /* start a format subroutine scope */
583                         { $$ = start_subparse(TRUE, 0);
584                             SAVEFREESV(PL_compcv); }
585         ;
586
587 /* Name of a subroutine - must be a bareword, could be special */
588 subname :       BAREWORD
589         |       PRIVATEREF
590         ;
591
592 /* Subroutine prototype */
593 proto   :       /* NULL */
594                         { $$ = NULL; }
595         |       THING
596         ;
597
598 /* Optional list of subroutine attributes */
599 subattrlist:    /* NULL */
600                         { $$ = NULL; }
601         |       COLONATTR THING
602                         { $$ = $2; }
603         |       COLONATTR
604                         { $$ = NULL; }
605         ;
606
607 /* List of attributes for a "my" variable declaration */
608 myattrlist:     COLONATTR THING
609                         { $$ = $2; }
610         |       COLONATTR
611                         { $$ = NULL; }
612         ;
613
614
615
616 /* --------------------------------------
617  * subroutine signature parsing
618  */
619
620 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable  */
621 sigvarname:     /* NULL */
622                         { parser->in_my = 0; $$ = NULL; }
623         |       PRIVATEREF
624                         { parser->in_my = 0; $$ = $1; }
625         ;
626
627 sigslurpsigil:
628                 '@'
629                         { $$ = '@'; }
630         |       '%'
631                         { $$ = '%'; }
632
633 /* @, %, @foo, %foo */
634 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */ 
635                         {
636                             I32 sigil   = $1;
637                             OP *var     = $2;
638                             OP *defexpr = $3;
639
640                             if (parser->sig_slurpy)
641                                 yyerror("Multiple slurpy parameters not allowed");
642                             parser->sig_slurpy = (char)sigil;
643
644                             if (defexpr)
645                                 yyerror("A slurpy parameter may not have "
646                                         "a default value");
647
648                             $$ = var ? newSTATEOP(0, NULL, var) : NULL;
649                         }
650         ;
651
652 /* default part of sub signature scalar element: i.e. '= default_expr' */
653 sigdefault:     /* NULL */
654                         { $$ = NULL; }
655         |       ASSIGNOP
656                         { $$ = newOP(OP_NULL, 0); }
657         |       ASSIGNOP term
658                         { $$ = $2; }
659
660
661 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
662 sigscalarelem:
663                 '$' sigvarname sigdefault
664                         {
665                             OP *var     = $2;
666                             OP *defexpr = $3;
667
668                             if (parser->sig_slurpy)
669                                 yyerror("Slurpy parameter not last");
670
671                             parser->sig_elems++;
672
673                             if (defexpr) {
674                                 parser->sig_optelems++;
675
676                                 if (   defexpr->op_type == OP_NULL
677                                     && !(defexpr->op_flags & OPf_KIDS))
678                                 {
679                                     /* handle '$=' special case */
680                                     if (var)
681                                         yyerror("Optional parameter "
682                                                     "lacks default expression");
683                                     op_free(defexpr);
684                                 }
685                                 else { 
686                                     /* a normal '=default' expression */ 
687                                     OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
688                                                         defexpr,
689                                                         LINKLIST(defexpr));
690                                     /* re-purpose op_targ to hold @_ index */
691                                     defop->op_targ =
692                                         (PADOFFSET)(parser->sig_elems - 1);
693
694                                     if (var) {
695                                         var->op_flags |= OPf_STACKED;
696                                         (void)op_sibling_splice(var,
697                                                         NULL, 0, defop);
698                                         scalar(defop);
699                                     }
700                                     else
701                                         var = newUNOP(OP_NULL, 0, defop);
702
703                                     LINKLIST(var);
704                                     /* NB: normally the first child of a
705                                      * logop is executed before the logop,
706                                      * and it pushes a boolean result
707                                      * ready for the logop. For ARGDEFELEM,
708                                      * the op itself does the boolean
709                                      * calculation, so set the first op to
710                                      * it instead.
711                                      */
712                                     var->op_next = defop;
713                                     defexpr->op_next = var;
714                                 }
715                             }
716                             else {
717                                 if (parser->sig_optelems)
718                                     yyerror("Mandatory parameter "
719                                             "follows optional parameter");
720                             }
721
722                             $$ = var ? newSTATEOP(0, NULL, var) : NULL;
723                         }
724         ;
725
726
727 /* subroutine signature element: e.g. '$x = $default' or '%h' */
728 sigelem:        sigscalarelem
729                         { parser->in_my = KEY_sigvar; $$ = $1; }
730         |       sigslurpelem
731                         { parser->in_my = KEY_sigvar; $$ = $1; }
732         ;
733
734 /* list of subroutine signature elements */
735 siglist:
736                 siglist ','
737                         { $$ = $1; }
738         |       siglist ',' sigelem
739                         {
740                           $$ = op_append_list(OP_LINESEQ, $1, $3);
741                         }
742         |       sigelem  %prec PREC_LOW
743                         { $$ = $1; }
744         ;
745
746 /* () or (....) */
747 siglistornull:          /* NULL */
748                         { $$ = NULL; }
749         |       siglist
750                         { $$ = $1; }
751
752 /* optional subroutine signature */
753 optsubsignature:        /* NULL */
754                         { $$ = NULL; }
755         |       subsignature
756                         { $$ = $1; }
757
758 /* Subroutine signature */
759 subsignature:   '('
760                         {
761                             ENTER;
762                             SAVEIV(parser->sig_elems);
763                             SAVEIV(parser->sig_optelems);
764                             SAVEI8(parser->sig_slurpy);
765                             parser->sig_elems    = 0;
766                             parser->sig_optelems = 0;
767                             parser->sig_slurpy   = 0;
768                             parser->in_my        = KEY_sigvar;
769                         }
770                 siglistornull
771                 ')'
772                         {
773                             OP            *sigops = $3;
774                             UNOP_AUX_item *aux;
775                             OP            *check;
776
777                             if (!FEATURE_SIGNATURES_IS_ENABLED)
778                                 Perl_croak(aTHX_ "Experimental "
779                                     "subroutine signatures not enabled");
780
781                             /* We shouldn't get here otherwise */
782                             Perl_ck_warner_d(aTHX_
783                                 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
784                                 "The signatures feature is experimental");
785
786                             aux = (UNOP_AUX_item*)PerlMemShared_malloc(
787                                 sizeof(UNOP_AUX_item) * 3);
788                             aux[0].iv = parser->sig_elems;
789                             aux[1].iv = parser->sig_optelems;
790                             aux[2].iv = parser->sig_slurpy;
791                             check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
792                             sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
793                             sigops = op_prepend_elem(OP_LINESEQ,
794                                                 newSTATEOP(0, NULL, NULL),
795                                                 sigops);
796                             /* a nextstate at the end handles context
797                              * correctly for an empty sub body */
798                             $$ = op_append_elem(OP_LINESEQ,
799                                                 sigops,
800                                                 newSTATEOP(0, NULL, NULL));
801
802                             parser->in_my = 0;
803                             /* tell the toker that attrributes can follow
804                              * this sig, but only so that the toker
805                              * can skip through any (illegal) trailing
806                              * attribute text then give a useful error
807                              * message about "attributes before sig",
808                              * rather than falling over ina mess at
809                              * unrecognised syntax.
810                              */
811                             parser->expect = XATTRBLOCK;
812                             parser->sig_seen = TRUE;
813                             LEAVE;
814                         }
815         ;
816
817 /* Optional subroutine body (for named subroutine declaration) */
818 optsubbody:     subbody { $$ = $1; }
819         |       ';'     { $$ = NULL; }
820         ;
821
822
823 /* Subroutine body (without signature) */
824 subbody:        remember  '{' stmtseq '}'
825                         {
826                           if (parser->copline > (line_t)$2)
827                               parser->copline = (line_t)$2;
828                           $$ = block_end($1, $3);
829                         }
830         ;
831
832
833 /* optional [ Subroutine body with optional signature ] (for named
834  * subroutine declaration) */
835 optsigsubbody:  sigsubbody { $$ = $1; }
836         |       ';'        { $$ = NULL; }
837
838 /* Subroutine body with optional signature */
839 sigsubbody:     remember optsubsignature '{' stmtseq '}'
840                         {
841                           if (parser->copline > (line_t)$3)
842                               parser->copline = (line_t)$3;
843                           $$ = block_end($1,
844                                 op_append_list(OP_LINESEQ, $2, $4));
845                         }
846         ;
847
848
849 /* Ordinary expressions; logical combinations */
850 expr    :       expr ANDOP expr
851                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
852         |       expr OROP expr
853                         { $$ = newLOGOP($2, 0, $1, $3); }
854         |       expr DOROP expr
855                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
856         |       listexpr %prec PREC_LOW
857         ;
858
859 /* Expressions are a list of terms joined by commas */
860 listexpr:       listexpr ','
861                         { $$ = $1; }
862         |       listexpr ',' term
863                         {
864                           OP* term = $3;
865                           $$ = op_append_elem(OP_LIST, $1, term);
866                         }
867         |       term %prec PREC_LOW
868         ;
869
870 /* List operators */
871 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
872                         { $$ = op_convert_list($1, OPf_STACKED,
873                                 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
874                         }
875         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
876                         { $$ = op_convert_list($1, OPf_STACKED,
877                                 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
878                         }
879         |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
880                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
881                                 op_append_elem(OP_LIST,
882                                     op_prepend_elem(OP_LIST, scalar($1), $5),
883                                     newMETHOP(OP_METHOD, 0, $3)));
884                         }
885         |       term ARROW method                     /* $foo->bar */
886                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
887                                 op_append_elem(OP_LIST, scalar($1),
888                                     newMETHOP(OP_METHOD, 0, $3)));
889                         }
890         |       METHOD indirob optlistexpr           /* new Class @args */
891                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
892                                 op_append_elem(OP_LIST,
893                                     op_prepend_elem(OP_LIST, $2, $3),
894                                     newMETHOP(OP_METHOD, 0, $1)));
895                         }
896         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
897                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
898                                 op_append_elem(OP_LIST,
899                                     op_prepend_elem(OP_LIST, $2, $4),
900                                     newMETHOP(OP_METHOD, 0, $1)));
901                         }
902         |       LSTOP optlistexpr                    /* print @args */
903                         { $$ = op_convert_list($1, 0, $2); }
904         |       FUNC '(' optexpr ')'                 /* print (@args) */
905                         { $$ = op_convert_list($1, 0, $3); }
906         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
907                         { SvREFCNT_inc_simple_void(PL_compcv);
908                           $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
909                     optlistexpr         %prec LSTOP  /* ... @bar */
910                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
911                                  op_append_elem(OP_LIST,
912                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
913                         }
914         ;
915
916 /* Names of methods. May use $object->$methodname */
917 method  :       METHOD
918         |       scalar
919         ;
920
921 /* Some kind of subscripted expression */
922 subscripted:    gelem '{' expr ';' '}'        /* *main::{something} */
923                         /* In this and all the hash accessors, ';' is
924                          * provided by the tokeniser */
925                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
926         |       scalar '[' expr ']'          /* $array[$element] */
927                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
928                         }
929         |       term ARROW '[' expr ']'      /* somearef->[$element] */
930                         { $$ = newBINOP(OP_AELEM, 0,
931                                         ref(newAVREF($1),OP_RV2AV),
932                                         scalar($4));
933                         }
934         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
935                         { $$ = newBINOP(OP_AELEM, 0,
936                                         ref(newAVREF($1),OP_RV2AV),
937                                         scalar($3));
938                         }
939         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
940                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
941                         }
942         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
943                         { $$ = newBINOP(OP_HELEM, 0,
944                                         ref(newHVREF($1),OP_RV2HV),
945                                         jmaybe($4)); }
946         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
947                         { $$ = newBINOP(OP_HELEM, 0,
948                                         ref(newHVREF($1),OP_RV2HV),
949                                         jmaybe($3)); }
950         |       term ARROW '(' ')'          /* $subref->() */
951                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
952                                    newCVREF(0, scalar($1)));
953                           if (parser->expect == XBLOCK)
954                               parser->expect = XOPERATOR;
955                         }
956         |       term ARROW '(' expr ')'     /* $subref->(@args) */
957                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
958                                    op_append_elem(OP_LIST, $4,
959                                        newCVREF(0, scalar($1))));
960                           if (parser->expect == XBLOCK)
961                               parser->expect = XOPERATOR;
962                         }
963
964         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
965                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
966                                    op_append_elem(OP_LIST, $3,
967                                                newCVREF(0, scalar($1))));
968                           if (parser->expect == XBLOCK)
969                               parser->expect = XOPERATOR;
970                         }
971         |       subscripted '(' ')'        /* $foo->{bar}->() */
972                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
973                                    newCVREF(0, scalar($1)));
974                           if (parser->expect == XBLOCK)
975                               parser->expect = XOPERATOR;
976                         }
977         |       '(' expr ')' '[' expr ']'            /* list slice */
978                         { $$ = newSLICEOP(0, $5, $2); }
979         |       QWLIST '[' expr ']'            /* list literal slice */
980                         { $$ = newSLICEOP(0, $3, $1); }
981         |       '(' ')' '[' expr ']'                 /* empty list slice! */
982                         { $$ = newSLICEOP(0, $4, NULL); }
983     ;
984
985 /* Binary operators between terms */
986 termbinop:      term ASSIGNOP term                     /* $x = $y, $x += $y */
987                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
988         |       term POWOP term                        /* $x ** $y */
989                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
990         |       term MULOP term                        /* $x * $y, $x x $y */
991                         {   if ($2 != OP_REPEAT)
992                                 scalar($1);
993                             $$ = newBINOP($2, 0, $1, scalar($3));
994                         }
995         |       term ADDOP term                        /* $x + $y */
996                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
997         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
998                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
999         |       term RELOP term                        /* $x > $y, etc. */
1000                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1001         |       term EQOP term                         /* $x == $y, $x eq $y */
1002                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1003         |       term BITANDOP term                     /* $x & $y */
1004                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1005         |       term BITOROP term                      /* $x | $y */
1006                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1007         |       term DOTDOT term                       /* $x..$y, $x...$y */
1008                         { $$ = newRANGE($2, scalar($1), scalar($3)); }
1009         |       term ANDAND term                       /* $x && $y */
1010                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
1011         |       term OROR term                         /* $x || $y */
1012                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
1013         |       term DORDOR term                       /* $x // $y */
1014                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
1015         |       term MATCHOP term                      /* $x =~ /$y/ */
1016                         { $$ = bind_match($2, $1, $3); }
1017     ;
1018
1019 /* Unary operators and terms */
1020 termunop : '-' term %prec UMINUS                       /* -$x */
1021                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
1022         |       '+' term %prec UMINUS                  /* +$x */
1023                         { $$ = $2; }
1024
1025         |       '!' term                               /* !$x */
1026                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1027         |       '~' term                               /* ~$x */
1028                         { $$ = newUNOP($1, 0, scalar($2)); }
1029         |       term POSTINC                           /* $x++ */
1030                         { $$ = newUNOP(OP_POSTINC, 0,
1031                                         op_lvalue(scalar($1), OP_POSTINC)); }
1032         |       term POSTDEC                           /* $x-- */
1033                         { $$ = newUNOP(OP_POSTDEC, 0,
1034                                         op_lvalue(scalar($1), OP_POSTDEC));}
1035         |       term POSTJOIN    /* implicit join after interpolated ->@ */
1036                         { $$ = op_convert_list(OP_JOIN, 0,
1037                                        op_append_elem(
1038                                         OP_LIST,
1039                                         newSVREF(scalar(
1040                                             newSVOP(OP_CONST,0,
1041                                                     newSVpvs("\""))
1042                                         )),
1043                                         $1
1044                                        ));
1045                         }
1046         |       PREINC term                            /* ++$x */
1047                         { $$ = newUNOP(OP_PREINC, 0,
1048                                         op_lvalue(scalar($2), OP_PREINC)); }
1049         |       PREDEC term                            /* --$x */
1050                         { $$ = newUNOP(OP_PREDEC, 0,
1051                                         op_lvalue(scalar($2), OP_PREDEC)); }
1052
1053     ;
1054
1055 /* Constructors for anonymous data */
1056 anonymous:      '[' expr ']'
1057                         { $$ = newANONLIST($2); }
1058         |       '[' ']'
1059                         { $$ = newANONLIST(NULL);}
1060         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
1061                         { $$ = newANONHASH($2); }
1062         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
1063                         { $$ = newANONHASH(NULL); }
1064         |       ANONSUB     startanonsub proto subattrlist subbody    %prec '('
1065                         { SvREFCNT_inc_simple_void(PL_compcv);
1066                           $$ = newANONATTRSUB($2, $3, $4, $5); }
1067         |       ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '('
1068                         { SvREFCNT_inc_simple_void(PL_compcv);
1069                           $$ = newANONATTRSUB($2, NULL, $3, $4); }
1070     ;
1071
1072 /* Things called with "do" */
1073 termdo  :       DO term %prec UNIOP                     /* do $filename */
1074                         { $$ = dofile($2, $1);}
1075         |       DO block        %prec '('               /* do { code */
1076                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
1077         ;
1078
1079 term    :       termbinop
1080         |       termunop
1081         |       anonymous
1082         |       termdo
1083         |       term '?' term ':' term
1084                         { $$ = newCONDOP(0, $1, $3, $5); }
1085         |       REFGEN term                          /* \$x, \@y, \%z */
1086                         { $$ = newUNOP(OP_REFGEN, 0, $2); }
1087         |       MY REFGEN term
1088                         { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
1089         |       myattrterm      %prec UNIOP
1090                         { $$ = $1; }
1091         |       LOCAL term      %prec UNIOP
1092                         { $$ = localize($2,0); }
1093         |       '(' expr ')'
1094                         { $$ = sawparens($2); }
1095         |       QWLIST
1096                         { $$ = $1; }
1097         |       '(' ')'
1098                         { $$ = sawparens(newNULLLIST()); }
1099         |       scalar  %prec '('
1100                         { $$ = $1; }
1101         |       star    %prec '('
1102                         { $$ = $1; }
1103         |       hsh     %prec '('
1104                         { $$ = $1; }
1105         |       ary     %prec '('
1106                         { $$ = $1; }
1107         |       arylen  %prec '('                    /* $#x, $#{ something } */
1108                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1109         |       subscripted
1110                         { $$ = $1; }
1111         |       sliceme '[' expr ']'                     /* array slice */
1112                         { $$ = op_prepend_elem(OP_ASLICE,
1113                                 newOP(OP_PUSHMARK, 0),
1114                                     newLISTOP(OP_ASLICE, 0,
1115                                         list($3),
1116                                         ref($1, OP_ASLICE)));
1117                           if ($$ && $1)
1118                               $$->op_private |=
1119                                   $1->op_private & OPpSLICEWARNING;
1120                         }
1121         |       kvslice '[' expr ']'                 /* array key/value slice */
1122                         { $$ = op_prepend_elem(OP_KVASLICE,
1123                                 newOP(OP_PUSHMARK, 0),
1124                                     newLISTOP(OP_KVASLICE, 0,
1125                                         list($3),
1126                                         ref(oopsAV($1), OP_KVASLICE)));
1127                           if ($$ && $1)
1128                               $$->op_private |=
1129                                   $1->op_private & OPpSLICEWARNING;
1130                         }
1131         |       sliceme '{' expr ';' '}'                 /* @hash{@keys} */
1132                         { $$ = op_prepend_elem(OP_HSLICE,
1133                                 newOP(OP_PUSHMARK, 0),
1134                                     newLISTOP(OP_HSLICE, 0,
1135                                         list($3),
1136                                         ref(oopsHV($1), OP_HSLICE)));
1137                           if ($$ && $1)
1138                               $$->op_private |=
1139                                   $1->op_private & OPpSLICEWARNING;
1140                         }
1141         |       kvslice '{' expr ';' '}'                 /* %hash{@keys} */
1142                         { $$ = op_prepend_elem(OP_KVHSLICE,
1143                                 newOP(OP_PUSHMARK, 0),
1144                                     newLISTOP(OP_KVHSLICE, 0,
1145                                         list($3),
1146                                         ref($1, OP_KVHSLICE)));
1147                           if ($$ && $1)
1148                               $$->op_private |=
1149                                   $1->op_private & OPpSLICEWARNING;
1150                         }
1151         |       THING   %prec '('
1152                         { $$ = $1; }
1153         |       amper                                /* &foo; */
1154                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1155         |       amper '(' ')'                 /* &foo() or foo() */
1156                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1157                         }
1158         |       amper '(' expr ')'          /* &foo(@args) or foo(@args) */
1159                         {
1160                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1161                                 op_append_elem(OP_LIST, $3, scalar($1)));
1162                         }
1163         |       NOAMP subname optlistexpr       /* foo @args (no parens) */
1164                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1165                             op_append_elem(OP_LIST, $3, scalar($2)));
1166                         }
1167         |       term ARROW '$' '*'
1168                         { $$ = newSVREF($1); }
1169         |       term ARROW '@' '*'
1170                         { $$ = newAVREF($1); }
1171         |       term ARROW '%' '*'
1172                         { $$ = newHVREF($1); }
1173         |       term ARROW '&' '*'
1174                         { $$ = newUNOP(OP_ENTERSUB, 0,
1175                                        scalar(newCVREF($3,$1))); }
1176         |       term ARROW '*' '*'      %prec '('
1177                         { $$ = newGVREF(0,$1); }
1178         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1179                         { $$ = newOP($1, OPf_SPECIAL);
1180                             PL_hints |= HINT_BLOCK_SCOPE; }
1181         |       LOOPEX term
1182                         { $$ = newLOOPEX($1,$2); }
1183         |       NOTOP listexpr                       /* not $foo */
1184                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1185         |       UNIOP                                /* Unary op, $_ implied */
1186                         { $$ = newOP($1, 0); }
1187         |       UNIOP block                          /* eval { foo }* */
1188                         { $$ = newUNOP($1, 0, $2); }
1189         |       UNIOP term                           /* Unary op */
1190                         { $$ = newUNOP($1, 0, $2); }
1191         |       REQUIRE                              /* require, $_ implied */
1192                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1193         |       REQUIRE term                         /* require Foo */
1194                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1195         |       UNIOPSUB
1196                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1197         |       UNIOPSUB term                        /* Sub treated as unop */
1198                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1199                             op_append_elem(OP_LIST, $2, scalar($1))); }
1200         |       FUNC0                                /* Nullary operator */
1201                         { $$ = newOP($1, 0); }
1202         |       FUNC0 '(' ')'
1203                         { $$ = newOP($1, 0);}
1204         |       FUNC0OP       /* Same as above, but op created in toke.c */
1205                         { $$ = $1; }
1206         |       FUNC0OP '(' ')'
1207                         { $$ = $1; }
1208         |       FUNC0SUB                             /* Sub treated as nullop */
1209                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1210         |       FUNC1 '(' ')'                        /* not () */
1211                         { $$ = ($1 == OP_NOT)
1212                           ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1213                           : newOP($1, OPf_SPECIAL); }
1214         |       FUNC1 '(' expr ')'                   /* not($foo) */
1215                         { $$ = newUNOP($1, 0, $3); }
1216         |       PMFUNC /* m//, s///, qr//, tr/// */
1217                         {
1218                             if (   $1->op_type != OP_TRANS
1219                                 && $1->op_type != OP_TRANSR
1220                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1221                             {
1222                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1223                                 SAVEFREESV(PL_compcv);
1224                             } else
1225                                 $<ival>$ = 0;
1226                         }
1227                     '(' listexpr optrepl ')'
1228                         { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1229         |       BAREWORD
1230         |       listop
1231         |       PLUGEXPR
1232         ;
1233
1234 /* "my" declarations, with optional attributes */
1235 myattrterm:     MY myterm myattrlist
1236                         { $$ = my_attrs($2,$3); }
1237         |       MY myterm
1238                         { $$ = localize($2,1); }
1239         |       MY REFGEN myterm myattrlist
1240                         { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
1241         ;
1242
1243 /* Things that can be "my"'d */
1244 myterm  :       '(' expr ')'
1245                         { $$ = sawparens($2); }
1246         |       '(' ')'
1247                         { $$ = sawparens(newNULLLIST()); }
1248
1249         |       scalar  %prec '('
1250                         { $$ = $1; }
1251         |       hsh     %prec '('
1252                         { $$ = $1; }
1253         |       ary     %prec '('
1254                         { $$ = $1; }
1255         ;
1256
1257 /* Basic list expressions */
1258 optlistexpr:    /* NULL */ %prec PREC_LOW
1259                         { $$ = NULL; }
1260         |       listexpr    %prec PREC_LOW
1261                         { $$ = $1; }
1262         ;
1263
1264 optexpr:        /* NULL */
1265                         { $$ = NULL; }
1266         |       expr
1267                         { $$ = $1; }
1268         ;
1269
1270 optrepl:        /* NULL */
1271                         { $$ = NULL; }
1272         |       '/' expr
1273                         { $$ = $2; }
1274         ;
1275
1276 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1277    lexical */
1278 my_scalar:      scalar
1279                         { parser->in_my = 0; $$ = my($1); }
1280         ;
1281
1282 my_var  :       scalar
1283         |       ary
1284         |       hsh
1285         ;
1286
1287 refgen_topic:   my_var
1288         |       amper
1289         ;
1290
1291 my_refgen:      MY REFGEN
1292         |       REFGEN MY
1293         ;
1294
1295 amper   :       '&' indirob
1296                         { $$ = newCVREF($1,$2); }
1297         ;
1298
1299 scalar  :       '$' indirob
1300                         { $$ = newSVREF($2); }
1301         ;
1302
1303 ary     :       '@' indirob
1304                         { $$ = newAVREF($2);
1305                           if ($$) $$->op_private |= $1;
1306                         }
1307         ;
1308
1309 hsh     :       '%' indirob
1310                         { $$ = newHVREF($2);
1311                           if ($$) $$->op_private |= $1;
1312                         }
1313         ;
1314
1315 arylen  :       DOLSHARP indirob
1316                         { $$ = newAVREF($2); }
1317         |       term ARROW DOLSHARP '*'
1318                         { $$ = newAVREF($1); }
1319         ;
1320
1321 star    :       '*' indirob
1322                         { $$ = newGVREF(0,$2); }
1323         ;
1324
1325 sliceme :       ary
1326         |       term ARROW '@'
1327                         { $$ = newAVREF($1); }
1328         ;
1329
1330 kvslice :       hsh
1331         |       term ARROW '%'
1332                         { $$ = newHVREF($1); }
1333         ;
1334
1335 gelem   :       star
1336         |       term ARROW '*'
1337                         { $$ = newGVREF(0,$1); }
1338         ;
1339
1340 /* Indirect objects */
1341 indirob :       BAREWORD
1342                         { $$ = scalar($1); }
1343         |       scalar %prec PREC_LOW
1344                         { $$ = scalar($1); }
1345         |       block
1346                         { $$ = op_scope($1); }
1347
1348         |       PRIVATEREF
1349                         { $$ = $1; }
1350         ;