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