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