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