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