This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for 890b5089ee
[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 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 %pure_parser
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
47
48 %token <ival> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.'
49
50 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
51 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
52 %token <opval> PLUGEXPR PLUGSTMT
53 %token <pval> LABEL
54 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
55 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
56 %token <ival> GIVEN WHEN DEFAULT
57 %token <ival> LOOPEX DOTDOT YADAYADA
58 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
59 %token <ival> RELOP EQOP MULOP ADDOP
60 %token <ival> DOLSHARP DO HASHBRACK NOAMP
61 %token <ival> LOCAL MY REQUIRE
62 %token <ival> COLONATTR FORMLBRACK FORMRBRACK
63
64 %type <ival> grammar remember mremember
65 %type <ival>  startsub startanonsub startformsub
66
67 %type <ival> mintro
68
69 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
70 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
71 %type <opval> sliceme kvslice gelem
72 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
73 %type <opval> optlistexpr optexpr indirob listop method
74 %type <opval> formname subname proto optsubbody cont my_scalar my_var
75 %type <opval> refgen_topic formblock
76 %type <opval> subattrlist myattrlist myattrterm myterm
77 %type <opval> realsubbody subsignature termbinop termunop anonymous termdo
78 %type <opval> formstmtseq formline formarg
79
80 %nonassoc <ival> PREC_LOW
81 %nonassoc LOOPEX
82
83 %left <ival> OROP DOROP
84 %left <ival> ANDOP
85 %right <ival> NOTOP
86 %nonassoc LSTOP LSTOPSUB
87 %left <ival> ','
88 %right <ival> ASSIGNOP
89 %right <ival> '?' ':'
90 %nonassoc DOTDOT YADAYADA
91 %left <ival> OROR DORDOR
92 %left <ival> ANDAND
93 %left <ival> BITOROP
94 %left <ival> BITANDOP
95 %nonassoc EQOP
96 %nonassoc RELOP
97 %nonassoc UNIOP UNIOPSUB
98 %nonassoc REQUIRE
99 %left <ival> SHIFTOP
100 %left ADDOP
101 %left MULOP
102 %left <ival> MATCHOP
103 %right <ival> '!' '~' UMINUS REFGEN
104 %right <ival> POWOP
105 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
106 %left <ival> ARROW
107 %nonassoc <ival> ')'
108 %left <ival> '('
109 %left '[' '{'
110
111 %% /* RULES */
112
113 /* Top-level choice of what kind of thing yyparse was called to parse */
114 grammar :       GRAMPROG
115                         {
116                           parser->expect = XSTATE;
117                         }
118                 remember stmtseq
119                         {
120                           newPROG(block_end($3,$4));
121                           PL_compiling.cop_seq = 0;
122                           $$ = 0;
123                         }
124         |       GRAMEXPR
125                         {
126                           parser->expect = XTERM;
127                         }
128                 optexpr
129                         {
130                           PL_eval_root = $3;
131                           $$ = 0;
132                         }
133         |       GRAMBLOCK
134                         {
135                           parser->expect = XBLOCK;
136                         }
137                 block
138                         {
139                           PL_pad_reset_pending = TRUE;
140                           PL_eval_root = $3;
141                           $$ = 0;
142                           yyunlex();
143                           parser->yychar = YYEOF;
144                         }
145         |       GRAMBARESTMT
146                         {
147                           parser->expect = XSTATE;
148                         }
149                 barestmt
150                         {
151                           PL_pad_reset_pending = TRUE;
152                           PL_eval_root = $3;
153                           $$ = 0;
154                           yyunlex();
155                           parser->yychar = YYEOF;
156                         }
157         |       GRAMFULLSTMT
158                         {
159                           parser->expect = XSTATE;
160                         }
161                 fullstmt
162                         {
163                           PL_pad_reset_pending = TRUE;
164                           PL_eval_root = $3;
165                           $$ = 0;
166                           yyunlex();
167                           parser->yychar = YYEOF;
168                         }
169         |       GRAMSTMTSEQ
170                         {
171                           parser->expect = XSTATE;
172                         }
173                 stmtseq
174                         {
175                           PL_eval_root = $3;
176                           $$ = 0;
177                         }
178         ;
179
180 /* An ordinary block */
181 block   :       '{' remember stmtseq '}'
182                         { if (parser->copline > (line_t)$1)
183                               parser->copline = (line_t)$1;
184                           $$ = block_end($2, $3);
185                         }
186         ;
187
188 /* format body */
189 formblock:      '=' remember ';' FORMRBRACK formstmtseq ';' '.'
190                         { if (parser->copline > (line_t)$1)
191                               parser->copline = (line_t)$1;
192                           $$ = block_end($2, $5);
193                         }
194         ;
195
196 remember:       /* NULL */      /* start a full lexical scope */
197                         { $$ = block_start(TRUE); }
198         ;
199
200 mblock  :       '{' mremember stmtseq '}'
201                         { if (parser->copline > (line_t)$1)
202                               parser->copline = (line_t)$1;
203                           $$ = block_end($2, $3);
204                         }
205         ;
206
207 mremember:      /* NULL */      /* start a partial lexical scope */
208                         { $$ = block_start(FALSE); }
209         ;
210
211 /* A sequence of statements in the program */
212 stmtseq :       /* NULL */
213                         { $$ = (OP*)NULL; }
214         |       stmtseq fullstmt
215                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
216                             PL_pad_reset_pending = TRUE;
217                             if ($1 && $2)
218                                 PL_hints |= HINT_BLOCK_SCOPE;
219                         }
220         ;
221
222 /* A sequence of format lines */
223 formstmtseq:    /* NULL */
224                         { $$ = (OP*)NULL; }
225         |       formstmtseq formline
226                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
227                             PL_pad_reset_pending = TRUE;
228                             if ($1 && $2)
229                                 PL_hints |= HINT_BLOCK_SCOPE;
230                         }
231         ;
232
233 /* A statement in the program, including optional labels */
234 fullstmt:       barestmt
235                         {
236                           $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
237                         }
238         |       labfullstmt
239                         { $$ = $1; }
240         ;
241
242 labfullstmt:    LABEL barestmt
243                         {
244                           $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
245                         }
246         |       LABEL labfullstmt
247                         {
248                           $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
249                         }
250         ;
251
252 /* A bare statement, lacking label and other aspects of state op */
253 barestmt:       PLUGSTMT
254                         { $$ = $1; }
255         |       FORMAT startformsub formname formblock
256                         {
257                           CV *fmtcv = PL_compcv;
258                           newFORM($2, $3, $4);
259                           $$ = (OP*)NULL;
260                           if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
261                               SvREFCNT_inc_simple_void(fmtcv);
262                               pad_add_anon(fmtcv, OP_NULL);
263                           }
264                         }
265         |       SUB subname startsub
266                         {
267                           if ($2->op_type == OP_CONST) {
268                             const char *const name =
269                                 SvPV_nolen_const(((SVOP*)$2)->op_sv);
270                             if (strEQ(name, "BEGIN") || strEQ(name, "END")
271                               || strEQ(name, "INIT") || strEQ(name, "CHECK")
272                               || strEQ(name, "UNITCHECK"))
273                               CvSPECIAL_on(PL_compcv);
274                           }
275                           else
276                           /* State subs inside anonymous subs need to be
277                              clonable themselves. */
278                           if (CvANON(CvOUTSIDE(PL_compcv))
279                            || CvCLONE(CvOUTSIDE(PL_compcv))
280                            || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
281                                                 CvOUTSIDE(PL_compcv)
282                                              ))[$2->op_targ]))
283                               CvCLONE_on(PL_compcv);
284                           parser->in_my = 0;
285                           parser->in_my_stash = NULL;
286                         }
287                 proto subattrlist optsubbody
288                         {
289                           SvREFCNT_inc_simple_void(PL_compcv);
290                           $2->op_type == OP_CONST
291                               ? newATTRSUB($3, $2, $5, $6, $7)
292                               : newMYSUB($3, $2, $5, $6, $7)
293                           ;
294                           $$ = (OP*)NULL;
295                           intro_my();
296                         }
297         |       PACKAGE WORD WORD ';'
298                         {
299                           package($3);
300                           if ($2)
301                               package_version($2);
302                           $$ = (OP*)NULL;
303                         }
304         |       USE startsub
305                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
306                 WORD WORD optlistexpr ';'
307                         {
308                           SvREFCNT_inc_simple_void(PL_compcv);
309                           utilize($1, $2, $4, $5, $6);
310                           $$ = (OP*)NULL;
311                         }
312         |       IF '(' remember mexpr ')' mblock else
313                         {
314                           $$ = block_end($3,
315                               newCONDOP(0, $4, op_scope($6), $7));
316                           parser->copline = (line_t)$1;
317                         }
318         |       UNLESS '(' remember miexpr ')' mblock else
319                         {
320                           $$ = block_end($3,
321                               newCONDOP(0, $4, op_scope($6), $7));
322                           parser->copline = (line_t)$1;
323                         }
324         |       GIVEN '(' remember mexpr ')' mblock
325                         {
326                           const PADOFFSET offset = pad_findmy_pvs("$_", 0);
327                           $$ = block_end($3,
328                                   newGIVENOP($4, op_scope($6),
329                                     offset == NOT_IN_PAD
330                                     || PAD_COMPNAME_FLAGS_isOUR(offset)
331                                       ? 0
332                                       : offset));
333                           parser->copline = (line_t)$1;
334                         }
335         |       WHEN '(' remember mexpr ')' mblock
336                         { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
337         |       DEFAULT block
338                         { $$ = newWHENOP(0, op_scope($2)); }
339         |       WHILE '(' remember texpr ')' mintro mblock cont
340                         {
341                           $$ = block_end($3,
342                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
343                                       $4, $7, $8, $6));
344                           parser->copline = (line_t)$1;
345                         }
346         |       UNTIL '(' remember iexpr ')' mintro mblock cont
347                         {
348                           $$ = block_end($3,
349                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
350                                       $4, $7, $8, $6));
351                           parser->copline = (line_t)$1;
352                         }
353         |       FOR '(' remember mnexpr ';'
354                         { parser->expect = XTERM; }
355                 texpr ';'
356                         { parser->expect = XTERM; }
357                 mintro mnexpr ')'
358                 mblock
359                         {
360                           OP *initop = $4;
361                           OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
362                                       scalar($7), $13, $11, $10);
363                           if (initop) {
364                               forop = op_prepend_elem(OP_LINESEQ, initop,
365                                   op_append_elem(OP_LINESEQ,
366                                       newOP(OP_UNSTACK, OPf_SPECIAL),
367                                       forop));
368                           }
369                           $$ = block_end($3, forop);
370                           parser->copline = (line_t)$1;
371                         }
372         |       FOR MY remember my_scalar '(' mexpr ')' mblock cont
373                         {
374                           $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
375                           parser->copline = (line_t)$1;
376                         }
377         |       FOR scalar '(' remember mexpr ')' mblock cont
378                         {
379                           $$ = block_end($4, newFOROP(0,
380                                       op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
381                           parser->copline = (line_t)$1;
382                         }
383         |       FOR REFGEN MY remember my_var
384                         { parser->in_my = 0; $<opval>$ = my($5); }
385                 '(' mexpr ')' mblock cont
386                         {
387                           $$ = block_end(
388                                 $4,
389                                 newFOROP(0,
390                                          op_lvalue(
391                                             newUNOP(OP_REFGEN, 0,
392                                                     $<opval>6),
393                                             OP_ENTERLOOP),
394                                          $8, $10, $11)
395                           );
396                           parser->copline = (line_t)$1;
397                         }
398         |       FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
399                         {
400                           $$ = block_end($5, newFOROP(
401                                 0, op_lvalue(newUNOP(OP_REFGEN, 0,
402                                                      $3),
403                                              OP_ENTERLOOP), $6, $8, $9));
404                           parser->copline = (line_t)$1;
405                         }
406         |       FOR '(' remember mexpr ')' mblock cont
407                         {
408                           $$ = block_end($3,
409                                   newFOROP(0, (OP*)NULL, $4, $6, $7));
410                           parser->copline = (line_t)$1;
411                         }
412         |       block cont
413                         {
414                           /* a block is a loop that happens once */
415                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
416                                   (OP*)NULL, $1, $2, 0);
417                         }
418         |       PACKAGE WORD WORD '{' remember
419                         {
420                           package($3);
421                           if ($2) {
422                               package_version($2);
423                           }
424                         }
425                 stmtseq '}'
426                         {
427                           /* a block is a loop that happens once */
428                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
429                                   (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
430                           if (parser->copline > (line_t)$4)
431                               parser->copline = (line_t)$4;
432                         }
433         |       sideff ';'
434                         {
435                           $$ = $1;
436                         }
437         |       ';'
438                         {
439                           $$ = (OP*)NULL;
440                           parser->copline = NOLINE;
441                         }
442         ;
443
444 /* Format line */
445 formline:       THING formarg
446                         { OP *list;
447                           if ($2) {
448                               OP *term = $2;
449                               list = op_append_elem(OP_LIST, $1, term);
450                           }
451                           else {
452                               list = $1;
453                           }
454                           if (parser->copline == NOLINE)
455                                parser->copline = CopLINE(PL_curcop)-1;
456                           else parser->copline--;
457                           $$ = newSTATEOP(0, NULL,
458                                           op_convert_list(OP_FORMLINE, 0, list));
459                         }
460         ;
461
462 formarg :       /* NULL */
463                         { $$ = NULL; }
464         |       FORMLBRACK stmtseq FORMRBRACK
465                         { $$ = op_unscope($2); }
466         ;
467
468 /* An expression which may have a side-effect */
469 sideff  :       error
470                         { $$ = (OP*)NULL; }
471         |       expr
472                         { $$ = $1; }
473         |       expr IF expr
474                         { $$ = newLOGOP(OP_AND, 0, $3, $1); }
475         |       expr UNLESS expr
476                         { $$ = newLOGOP(OP_OR, 0, $3, $1); }
477         |       expr WHILE expr
478                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
479         |       expr UNTIL iexpr
480                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
481         |       expr FOR expr
482                         { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
483                           parser->copline = (line_t)$2; }
484         |       expr WHEN expr
485                         { $$ = newWHENOP($3, op_scope($1)); }
486         ;
487
488 /* else and elsif blocks */
489 else    :       /* NULL */
490                         { $$ = (OP*)NULL; }
491         |       ELSE mblock
492                         {
493                           ($2)->op_flags |= OPf_PARENS;
494                           $$ = op_scope($2);
495                         }
496         |       ELSIF '(' mexpr ')' mblock else
497                         { parser->copline = (line_t)$1;
498                             $$ = newCONDOP(0,
499                                 newSTATEOP(OPf_SPECIAL,NULL,$3),
500                                 op_scope($5), $6);
501                           PL_hints |= HINT_BLOCK_SCOPE;
502                         }
503         ;
504
505 /* Continue blocks */
506 cont    :       /* NULL */
507                         { $$ = (OP*)NULL; }
508         |       CONTINUE block
509                         { $$ = op_scope($2); }
510         ;
511
512 /* determine whether there are any new my declarations */
513 mintro  :       /* NULL */
514                         { $$ = (PL_min_intro_pending &&
515                             PL_max_intro_pending >=  PL_min_intro_pending);
516                           intro_my(); }
517
518 /* Normal expression */
519 nexpr   :       /* NULL */
520                         { $$ = (OP*)NULL; }
521         |       sideff
522         ;
523
524 /* Boolean expression */
525 texpr   :       /* NULL means true */
526                         { YYSTYPE tmplval;
527                           (void)scan_num("1", &tmplval);
528                           $$ = tmplval.opval; }
529         |       expr
530         ;
531
532 /* Inverted boolean expression */
533 iexpr   :       expr
534                         { $$ = invert(scalar($1)); }
535         ;
536
537 /* Expression with its own lexical scope */
538 mexpr   :       expr
539                         { $$ = $1; intro_my(); }
540         ;
541
542 mnexpr  :       nexpr
543                         { $$ = $1; intro_my(); }
544         ;
545
546 miexpr  :       iexpr
547                         { $$ = $1; intro_my(); }
548         ;
549
550 formname:       WORD            { $$ = $1; }
551         |       /* NULL */      { $$ = (OP*)NULL; }
552         ;
553
554 startsub:       /* NULL */      /* start a regular subroutine scope */
555                         { $$ = start_subparse(FALSE, 0);
556                             SAVEFREESV(PL_compcv); }
557
558         ;
559
560 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
561                         { $$ = start_subparse(FALSE, CVf_ANON);
562                             SAVEFREESV(PL_compcv); }
563         ;
564
565 startformsub:   /* NULL */      /* start a format subroutine scope */
566                         { $$ = start_subparse(TRUE, 0);
567                             SAVEFREESV(PL_compcv); }
568         ;
569
570 /* Name of a subroutine - must be a bareword, could be special */
571 subname :       WORD
572         |       PRIVATEREF
573         ;
574
575 /* Subroutine prototype */
576 proto   :       /* NULL */
577                         { $$ = (OP*)NULL; }
578         |       THING
579         ;
580
581 /* Optional list of subroutine attributes */
582 subattrlist:    /* NULL */
583                         { $$ = (OP*)NULL; }
584         |       COLONATTR THING
585                         { $$ = $2; }
586         |       COLONATTR
587                         { $$ = (OP*)NULL; }
588         ;
589
590 /* List of attributes for a "my" variable declaration */
591 myattrlist:     COLONATTR THING
592                         { $$ = $2; }
593         |       COLONATTR
594                         { $$ = (OP*)NULL; }
595         ;
596
597 /* Optional subroutine signature */
598 subsignature:   /* NULL */ { $$ = (OP*)NULL; }
599         |       '('
600                         {
601                           if (!FEATURE_SIGNATURES_IS_ENABLED)
602                             Perl_croak(aTHX_ "Experimental "
603                                 "subroutine signatures not enabled");
604                           Perl_ck_warner_d(aTHX_
605                                 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
606                                 "The signatures feature is experimental");
607                           $<opval>$ = parse_subsignature();
608                         }
609                 ')'
610                         {
611                           $$ = op_append_list(OP_LINESEQ, $<opval>2,
612                                 newSTATEOP(0, NULL, sawparens(newNULLLIST())));
613                           parser->expect = XBLOCK;
614                         }
615         ;
616
617 /* Subroutine body - block with optional signature */
618 realsubbody:    remember subsignature '{' stmtseq '}'
619                         {
620                           if (parser->copline > (line_t)$3)
621                               parser->copline = (line_t)$3;
622                           $$ = block_end($1,
623                                 op_append_list(OP_LINESEQ, $2, $4));
624                         }
625         ;
626
627 /* Optional subroutine body, for named subroutine declaration */
628 optsubbody:     realsubbody { $$ = $1; }
629         |       ';'     { $$ = (OP*)NULL; }
630         ;
631
632 /* Ordinary expressions; logical combinations */
633 expr    :       expr ANDOP expr
634                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
635         |       expr OROP expr
636                         { $$ = newLOGOP($2, 0, $1, $3); }
637         |       expr DOROP expr
638                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
639         |       listexpr %prec PREC_LOW
640         ;
641
642 /* Expressions are a list of terms joined by commas */
643 listexpr:       listexpr ','
644                         { $$ = $1; }
645         |       listexpr ',' term
646                         { 
647                           OP* term = $3;
648                           $$ = op_append_elem(OP_LIST, $1, term);
649                         }
650         |       term %prec PREC_LOW
651         ;
652
653 /* List operators */
654 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
655                         { $$ = op_convert_list($1, OPf_STACKED,
656                                 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
657                         }
658         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
659                         { $$ = op_convert_list($1, OPf_STACKED,
660                                 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
661                         }
662         |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
663                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
664                                 op_append_elem(OP_LIST,
665                                     op_prepend_elem(OP_LIST, scalar($1), $5),
666                                     newMETHOP(OP_METHOD, 0, $3)));
667                         }
668         |       term ARROW method                     /* $foo->bar */
669                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
670                                 op_append_elem(OP_LIST, scalar($1),
671                                     newMETHOP(OP_METHOD, 0, $3)));
672                         }
673         |       METHOD indirob optlistexpr           /* new Class @args */
674                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
675                                 op_append_elem(OP_LIST,
676                                     op_prepend_elem(OP_LIST, $2, $3),
677                                     newMETHOP(OP_METHOD, 0, $1)));
678                         }
679         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
680                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
681                                 op_append_elem(OP_LIST,
682                                     op_prepend_elem(OP_LIST, $2, $4),
683                                     newMETHOP(OP_METHOD, 0, $1)));
684                         }
685         |       LSTOP optlistexpr                    /* print @args */
686                         { $$ = op_convert_list($1, 0, $2); }
687         |       FUNC '(' optexpr ')'                 /* print (@args) */
688                         { $$ = op_convert_list($1, 0, $3); }
689         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
690                         { SvREFCNT_inc_simple_void(PL_compcv);
691                           $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
692                     optlistexpr         %prec LSTOP  /* ... @bar */
693                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
694                                  op_append_elem(OP_LIST,
695                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
696                         }
697         ;
698
699 /* Names of methods. May use $object->$methodname */
700 method  :       METHOD
701         |       scalar
702         ;
703
704 /* Some kind of subscripted expression */
705 subscripted:    gelem '{' expr ';' '}'        /* *main::{something} */
706                         /* In this and all the hash accessors, ';' is
707                          * provided by the tokeniser */
708                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
709         |       scalar '[' expr ']'          /* $array[$element] */
710                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
711                         }
712         |       term ARROW '[' expr ']'      /* somearef->[$element] */
713                         { $$ = newBINOP(OP_AELEM, 0,
714                                         ref(newAVREF($1),OP_RV2AV),
715                                         scalar($4));
716                         }
717         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
718                         { $$ = newBINOP(OP_AELEM, 0,
719                                         ref(newAVREF($1),OP_RV2AV),
720                                         scalar($3));
721                         }
722         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
723                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
724                         }
725         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
726                         { $$ = newBINOP(OP_HELEM, 0,
727                                         ref(newHVREF($1),OP_RV2HV),
728                                         jmaybe($4)); }
729         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
730                         { $$ = newBINOP(OP_HELEM, 0,
731                                         ref(newHVREF($1),OP_RV2HV),
732                                         jmaybe($3)); }
733         |       term ARROW '(' ')'          /* $subref->() */
734                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
735                                    newCVREF(0, scalar($1))); }
736         |       term ARROW '(' expr ')'     /* $subref->(@args) */
737                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
738                                    op_append_elem(OP_LIST, $4,
739                                        newCVREF(0, scalar($1)))); }
740
741         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
742                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
743                                    op_append_elem(OP_LIST, $3,
744                                                newCVREF(0, scalar($1)))); }
745         |       subscripted '(' ')'        /* $foo->{bar}->() */
746                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
747                                    newCVREF(0, scalar($1))); }
748         |       '(' expr ')' '[' expr ']'            /* list slice */
749                         { $$ = newSLICEOP(0, $5, $2); }
750         |       QWLIST '[' expr ']'            /* list literal slice */
751                         { $$ = newSLICEOP(0, $3, $1); }
752         |       '(' ')' '[' expr ']'                 /* empty list slice! */
753                         { $$ = newSLICEOP(0, $4, (OP*)NULL); }
754     ;
755
756 /* Binary operators between terms */
757 termbinop:      term ASSIGNOP term                     /* $x = $y */
758                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
759         |       term POWOP term                        /* $x ** $y */
760                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
761         |       term MULOP term                        /* $x * $y, $x x $y */
762                         {   if ($2 != OP_REPEAT)
763                                 scalar($1);
764                             $$ = newBINOP($2, 0, $1, scalar($3));
765                         }
766         |       term ADDOP term                        /* $x + $y */
767                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
768         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
769                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
770         |       term RELOP term                        /* $x > $y, etc. */
771                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
772         |       term EQOP term                         /* $x == $y, $x eq $y */
773                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
774         |       term BITANDOP term                     /* $x & $y */
775                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
776         |       term BITOROP term                      /* $x | $y */
777                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
778         |       term DOTDOT term                       /* $x..$y, $x...$y */
779                         { $$ = newRANGE($2, scalar($1), scalar($3)); }
780         |       term ANDAND term                       /* $x && $y */
781                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
782         |       term OROR term                         /* $x || $y */
783                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
784         |       term DORDOR term                       /* $x // $y */
785                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
786         |       term MATCHOP term                      /* $x =~ /$y/ */
787                         { $$ = bind_match($2, $1, $3); }
788     ;
789
790 /* Unary operators and terms */
791 termunop : '-' term %prec UMINUS                       /* -$x */
792                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
793         |       '+' term %prec UMINUS                  /* +$x */
794                         { $$ = $2; }
795
796         |       '!' term                               /* !$x */
797                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
798         |       '~' term                               /* ~$x */
799                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2)); }
800         |       term POSTINC                           /* $x++ */
801                         { $$ = newUNOP(OP_POSTINC, 0,
802                                         op_lvalue(scalar($1), OP_POSTINC)); }
803         |       term POSTDEC                           /* $x-- */
804                         { $$ = newUNOP(OP_POSTDEC, 0,
805                                         op_lvalue(scalar($1), OP_POSTDEC));}
806         |       term POSTJOIN    /* implicit join after interpolated ->@ */
807                         { $$ = op_convert_list(OP_JOIN, 0,
808                                        op_append_elem(
809                                         OP_LIST,
810                                         newSVREF(scalar(
811                                             newSVOP(OP_CONST,0,
812                                                     newSVpvs("\""))
813                                         )),
814                                         $1
815                                        ));
816                         }
817         |       PREINC term                            /* ++$x */
818                         { $$ = newUNOP(OP_PREINC, 0,
819                                         op_lvalue(scalar($2), OP_PREINC)); }
820         |       PREDEC term                            /* --$x */
821                         { $$ = newUNOP(OP_PREDEC, 0,
822                                         op_lvalue(scalar($2), OP_PREDEC)); }
823
824     ;
825
826 /* Constructors for anonymous data */
827 anonymous:      '[' expr ']'
828                         { $$ = newANONLIST($2); }
829         |       '[' ']'
830                         { $$ = newANONLIST((OP*)NULL);}
831         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
832                         { $$ = newANONHASH($2); }
833         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
834                         { $$ = newANONHASH((OP*)NULL); }
835         |       ANONSUB startanonsub proto subattrlist realsubbody      %prec '('
836                         { SvREFCNT_inc_simple_void(PL_compcv);
837                           $$ = newANONATTRSUB($2, $3, $4, $5); }
838
839     ;
840
841 /* Things called with "do" */
842 termdo  :       DO term %prec UNIOP                     /* do $filename */
843                         { $$ = dofile($2, $1);}
844         |       DO block        %prec '('               /* do { code */
845                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
846         ;
847
848 term    :       termbinop
849         |       termunop
850         |       anonymous
851         |       termdo
852         |       term '?' term ':' term
853                         { $$ = newCONDOP(0, $1, $3, $5); }
854         |       REFGEN term                          /* \$x, \@y, \%z */
855                         { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN)); }
856         |       myattrterm      %prec UNIOP
857                         { $$ = $1; }
858         |       LOCAL term      %prec UNIOP
859                         { $$ = localize($2,$1); }
860         |       '(' expr ')'
861                         { $$ = sawparens($2); }
862         |       QWLIST
863                         { $$ = $1; }
864         |       '(' ')'
865                         { $$ = sawparens(newNULLLIST()); }
866         |       scalar  %prec '('
867                         { $$ = $1; }
868         |       star    %prec '('
869                         { $$ = $1; }
870         |       hsh     %prec '('
871                         { $$ = $1; }
872         |       ary     %prec '('
873                         { $$ = $1; }
874         |       arylen  %prec '('                    /* $#x, $#{ something } */
875                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
876         |       subscripted
877                         { $$ = $1; }
878         |       sliceme '[' expr ']'                     /* array slice */
879                         { $$ = op_prepend_elem(OP_ASLICE,
880                                 newOP(OP_PUSHMARK, 0),
881                                     newLISTOP(OP_ASLICE, 0,
882                                         list($3),
883                                         ref($1, OP_ASLICE)));
884                           if ($$ && $1)
885                               $$->op_private |=
886                                   $1->op_private & OPpSLICEWARNING;
887                         }
888         |       kvslice '[' expr ']'                 /* array key/value slice */
889                         { $$ = op_prepend_elem(OP_KVASLICE,
890                                 newOP(OP_PUSHMARK, 0),
891                                     newLISTOP(OP_KVASLICE, 0,
892                                         list($3),
893                                         ref(oopsAV($1), OP_KVASLICE)));
894                           if ($$ && $1)
895                               $$->op_private |=
896                                   $1->op_private & OPpSLICEWARNING;
897                         }
898         |       sliceme '{' expr ';' '}'                 /* @hash{@keys} */
899                         { $$ = op_prepend_elem(OP_HSLICE,
900                                 newOP(OP_PUSHMARK, 0),
901                                     newLISTOP(OP_HSLICE, 0,
902                                         list($3),
903                                         ref(oopsHV($1), OP_HSLICE)));
904                           if ($$ && $1)
905                               $$->op_private |=
906                                   $1->op_private & OPpSLICEWARNING;
907                         }
908         |       kvslice '{' expr ';' '}'                 /* %hash{@keys} */
909                         { $$ = op_prepend_elem(OP_KVHSLICE,
910                                 newOP(OP_PUSHMARK, 0),
911                                     newLISTOP(OP_KVHSLICE, 0,
912                                         list($3),
913                                         ref($1, OP_KVHSLICE)));
914                           if ($$ && $1)
915                               $$->op_private |=
916                                   $1->op_private & OPpSLICEWARNING;
917                         }
918         |       THING   %prec '('
919                         { $$ = $1; }
920         |       amper                                /* &foo; */
921                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
922         |       amper '(' ')'                 /* &foo() or foo() */
923                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
924                         }
925         |       amper '(' expr ')'          /* &foo(@args) or foo(@args) */
926                         {
927                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
928                                 op_append_elem(OP_LIST, $3, scalar($1)));
929                         }
930         |       NOAMP subname optlistexpr       /* foo @args (no parens) */
931                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
932                             op_append_elem(OP_LIST, $3, scalar($2)));
933                         }
934         |       term ARROW '$' '*'
935                         { $$ = newSVREF($1); }
936         |       term ARROW '@' '*'
937                         { $$ = newAVREF($1); }
938         |       term ARROW '%' '*'
939                         { $$ = newHVREF($1); }
940         |       term ARROW '&' '*'
941                         { $$ = newUNOP(OP_ENTERSUB, 0,
942                                        scalar(newCVREF($3,$1))); }
943         |       term ARROW '*' '*'      %prec '('
944                         { $$ = newGVREF(0,$1); }
945         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
946                         { $$ = newOP($1, OPf_SPECIAL);
947                             PL_hints |= HINT_BLOCK_SCOPE; }
948         |       LOOPEX term
949                         { $$ = newLOOPEX($1,$2); }
950         |       NOTOP listexpr                       /* not $foo */
951                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
952         |       UNIOP                                /* Unary op, $_ implied */
953                         { $$ = newOP($1, 0); }
954         |       UNIOP block                          /* eval { foo }* */
955                         { $$ = newUNOP($1, 0, $2); }
956         |       UNIOP term                           /* Unary op */
957                         { $$ = newUNOP($1, 0, $2); }
958         |       REQUIRE                              /* require, $_ implied */
959                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
960         |       REQUIRE term                         /* require Foo */
961                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
962         |       UNIOPSUB
963                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
964         |       UNIOPSUB term                        /* Sub treated as unop */
965                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
966                             op_append_elem(OP_LIST, $2, scalar($1))); }
967         |       FUNC0                                /* Nullary operator */
968                         { $$ = newOP($1, 0); }
969         |       FUNC0 '(' ')'
970                         { $$ = newOP($1, 0);}
971         |       FUNC0OP       /* Same as above, but op created in toke.c */
972                         { $$ = $1; }
973         |       FUNC0OP '(' ')'
974                         { $$ = $1; }
975         |       FUNC0SUB                             /* Sub treated as nullop */
976                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
977         |       FUNC1 '(' ')'                        /* not () */
978                         { $$ = ($1 == OP_NOT)
979                           ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
980                           : newOP($1, OPf_SPECIAL); }
981         |       FUNC1 '(' expr ')'                   /* not($foo) */
982                         { $$ = newUNOP($1, 0, $3); }
983         |       PMFUNC /* m//, s///, qr//, tr/// */
984                         {
985                             if (   $1->op_type != OP_TRANS
986                                 && $1->op_type != OP_TRANSR
987                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
988                             {
989                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
990                                 SAVEFREESV(PL_compcv);
991                             } else
992                                 $<ival>$ = 0;
993                         }
994                     '(' listexpr ')'
995                         { $$ = pmruntime($1, $4, 1, $<ival>2); }
996         |       WORD
997         |       listop
998         |       YADAYADA
999                         {
1000                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1001                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1002                         }
1003         |       PLUGEXPR
1004         ;
1005
1006 /* "my" declarations, with optional attributes */
1007 myattrterm:     MY myterm myattrlist
1008                         { $$ = my_attrs($2,$3); }
1009         |       MY myterm
1010                         { $$ = localize($2,$1); }
1011         ;
1012
1013 /* Things that can be "my"'d */
1014 myterm  :       '(' expr ')'
1015                         { $$ = sawparens($2); }
1016         |       '(' ')'
1017                         { $$ = sawparens(newNULLLIST()); }
1018
1019         |       scalar  %prec '('
1020                         { $$ = $1; }
1021         |       hsh     %prec '('
1022                         { $$ = $1; }
1023         |       ary     %prec '('
1024                         { $$ = $1; }
1025         ;
1026
1027 /* Basic list expressions */
1028 optlistexpr:    /* NULL */ %prec PREC_LOW
1029                         { $$ = (OP*)NULL; }
1030         |       listexpr    %prec PREC_LOW
1031                         { $$ = $1; }
1032         ;
1033
1034 optexpr:        /* NULL */
1035                         { $$ = (OP*)NULL; }
1036         |       expr
1037                         { $$ = $1; }
1038         ;
1039
1040 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1041    lexical */
1042 my_scalar:      scalar
1043                         { parser->in_my = 0; $$ = my($1); }
1044         ;
1045
1046 my_var  :       scalar
1047         |       ary
1048         |       hsh
1049         ;
1050
1051 refgen_topic:   my_var
1052         |       amper
1053         ;
1054
1055 amper   :       '&' indirob
1056                         { $$ = newCVREF($1,$2); }
1057         ;
1058
1059 scalar  :       '$' indirob
1060                         { $$ = newSVREF($2); }
1061         ;
1062
1063 ary     :       '@' indirob
1064                         { $$ = newAVREF($2);
1065                           if ($$) $$->op_private |= $1;
1066                         }
1067         ;
1068
1069 hsh     :       '%' indirob
1070                         { $$ = newHVREF($2);
1071                           if ($$) $$->op_private |= $1;
1072                         }
1073         ;
1074
1075 arylen  :       DOLSHARP indirob
1076                         { $$ = newAVREF($2); }
1077         |       term ARROW DOLSHARP '*'
1078                         { $$ = newAVREF($1); }
1079         ;
1080
1081 star    :       '*' indirob
1082                         { $$ = newGVREF(0,$2); }
1083         ;
1084
1085 sliceme :       ary
1086         |       term ARROW '@'
1087                         { $$ = newAVREF($1); }
1088         ;
1089
1090 kvslice :       hsh
1091         |       term ARROW '%'
1092                         { $$ = newHVREF($1); }
1093         ;
1094
1095 gelem   :       star
1096         |       term ARROW '*'
1097                         { $$ = newGVREF(0,$1); }
1098         ;
1099
1100 /* Indirect objects */
1101 indirob :       WORD
1102                         { $$ = scalar($1); }
1103         |       scalar %prec PREC_LOW
1104                         { $$ = scalar($1); }
1105         |       block
1106                         { $$ = op_scope($1); }
1107
1108         |       PRIVATEREF
1109                         { $$ = $1; }
1110         ;