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