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