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