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