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