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