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