This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Set PL_expect less often when parsing semicolons
[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                           PL_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 (PL_parser->copline > (line_t)$1)
181                               PL_parser->copline = (line_t)$1;
182                           $$ = block_end($2, $3);
183                         }
184         ;
185
186 /* format body */
187 formblock:      '=' remember ';' FORMRBRACK formstmtseq ';' '.'
188                         { if (PL_parser->copline > (line_t)$1)
189                               PL_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 (PL_parser->copline > (line_t)$1)
200                               PL_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                           PL_parser->in_my = 0;
283                           PL_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                           PL_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                           PL_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                           PL_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                           PL_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                           PL_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                           PL_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                           PL_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                           PL_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                           PL_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 (PL_parser->copline > (line_t)$4)
406                               PL_parser->copline = (line_t)$4;
407                         }
408         |       sideff ';'
409                         {
410                           $$ = $1;
411                         }
412         |       ';'
413                         {
414                           $$ = (OP*)NULL;
415                           PL_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 (PL_parser->copline == NOLINE)
430                                PL_parser->copline = CopLINE(PL_curcop)-1;
431                           else PL_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                           PL_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                         { PL_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                           PL_parser->expect = XBLOCK;
589                         }
590         ;
591
592 /* Subroutine body - block with optional signature */
593 realsubbody:    remember subsignature '{' stmtseq '}'
594                         {
595                           if (PL_parser->copline > (line_t)$3)
596                               PL_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                             PL_parser->expect = XOPERATOR;
685                         }
686         |       scalar '[' expr ']'          /* $array[$element] */
687                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
688                         }
689         |       term ARROW '[' expr ']'      /* somearef->[$element] */
690                         { $$ = newBINOP(OP_AELEM, 0,
691                                         ref(newAVREF($1),OP_RV2AV),
692                                         scalar($4));
693                         }
694         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
695                         { $$ = newBINOP(OP_AELEM, 0,
696                                         ref(newAVREF($1),OP_RV2AV),
697                                         scalar($3));
698                         }
699         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
700                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
701                             PL_parser->expect = XOPERATOR;
702                         }
703         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
704                         { $$ = newBINOP(OP_HELEM, 0,
705                                         ref(newHVREF($1),OP_RV2HV),
706                                         jmaybe($4));
707                             PL_parser->expect = XOPERATOR;
708                         }
709         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
710                         { $$ = newBINOP(OP_HELEM, 0,
711                                         ref(newHVREF($1),OP_RV2HV),
712                                         jmaybe($3));
713                             PL_parser->expect = XOPERATOR;
714                         }
715         |       term ARROW '(' ')'          /* $subref->() */
716                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
717                                    newCVREF(0, scalar($1))); }
718         |       term ARROW '(' expr ')'     /* $subref->(@args) */
719                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
720                                    op_append_elem(OP_LIST, $4,
721                                        newCVREF(0, scalar($1)))); }
722
723         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
724                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
725                                    op_append_elem(OP_LIST, $3,
726                                                newCVREF(0, scalar($1)))); }
727         |       subscripted '(' ')'        /* $foo->{bar}->() */
728                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
729                                    newCVREF(0, scalar($1))); }
730         |       '(' expr ')' '[' expr ']'            /* list slice */
731                         { $$ = newSLICEOP(0, $5, $2); }
732         |       QWLIST '[' expr ']'            /* list literal slice */
733                         { $$ = newSLICEOP(0, $3, $1); }
734         |       '(' ')' '[' expr ']'                 /* empty list slice! */
735                         { $$ = newSLICEOP(0, $4, (OP*)NULL); }
736     ;
737
738 /* Binary operators between terms */
739 termbinop:      term ASSIGNOP term                     /* $x = $y */
740                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
741         |       term POWOP term                        /* $x ** $y */
742                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
743         |       term MULOP term                        /* $x * $y, $x x $y */
744                         {   if ($2 != OP_REPEAT)
745                                 scalar($1);
746                             $$ = newBINOP($2, 0, $1, scalar($3));
747                         }
748         |       term ADDOP term                        /* $x + $y */
749                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
750         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
751                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
752         |       term RELOP term                        /* $x > $y, etc. */
753                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
754         |       term EQOP term                         /* $x == $y, $x eq $y */
755                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
756         |       term BITANDOP term                     /* $x & $y */
757                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
758         |       term BITOROP term                      /* $x | $y */
759                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
760         |       term DOTDOT term                       /* $x..$y, $x...$y */
761                         { $$ = newRANGE($2, scalar($1), scalar($3)); }
762         |       term ANDAND term                       /* $x && $y */
763                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
764         |       term OROR term                         /* $x || $y */
765                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
766         |       term DORDOR term                       /* $x // $y */
767                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
768         |       term MATCHOP term                      /* $x =~ /$y/ */
769                         { $$ = bind_match($2, $1, $3); }
770     ;
771
772 /* Unary operators and terms */
773 termunop : '-' term %prec UMINUS                       /* -$x */
774                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
775         |       '+' term %prec UMINUS                  /* +$x */
776                         { $$ = $2; }
777
778         |       '!' term                               /* !$x */
779                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
780         |       '~' term                               /* ~$x */
781                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2)); }
782         |       term POSTINC                           /* $x++ */
783                         { $$ = newUNOP(OP_POSTINC, 0,
784                                         op_lvalue(scalar($1), OP_POSTINC)); }
785         |       term POSTDEC                           /* $x-- */
786                         { $$ = newUNOP(OP_POSTDEC, 0,
787                                         op_lvalue(scalar($1), OP_POSTDEC));}
788         |       term POSTJOIN    /* implicit join after interpolated ->@ */
789                         { $$ = convert(OP_JOIN, 0,
790                                        op_append_elem(
791                                         OP_LIST,
792                                         newSVREF(scalar(
793                                             newSVOP(OP_CONST,0,
794                                                     newSVpvs("\""))
795                                         )),
796                                         $1
797                                        ));
798                         }
799         |       PREINC term                            /* ++$x */
800                         { $$ = newUNOP(OP_PREINC, 0,
801                                         op_lvalue(scalar($2), OP_PREINC)); }
802         |       PREDEC term                            /* --$x */
803                         { $$ = newUNOP(OP_PREDEC, 0,
804                                         op_lvalue(scalar($2), OP_PREDEC)); }
805
806     ;
807
808 /* Constructors for anonymous data */
809 anonymous:      '[' expr ']'
810                         { $$ = newANONLIST($2); }
811         |       '[' ']'
812                         { $$ = newANONLIST((OP*)NULL);}
813         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
814                         { $$ = newANONHASH($2); }
815         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
816                         { $$ = newANONHASH((OP*)NULL); }
817         |       ANONSUB startanonsub proto subattrlist realsubbody      %prec '('
818                         { SvREFCNT_inc_simple_void(PL_compcv);
819                           $$ = newANONATTRSUB($2, $3, $4, $5); }
820
821     ;
822
823 /* Things called with "do" */
824 termdo  :       DO term %prec UNIOP                     /* do $filename */
825                         { $$ = dofile($2, $1);}
826         |       DO block        %prec '('               /* do { code */
827                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
828         ;
829
830 term    :       termbinop
831         |       termunop
832         |       anonymous
833         |       termdo
834         |       term '?' term ':' term
835                         { $$ = newCONDOP(0, $1, $3, $5); }
836         |       REFGEN term                          /* \$x, \@y, \%z */
837                         { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN)); }
838         |       myattrterm      %prec UNIOP
839                         { $$ = $1; }
840         |       LOCAL term      %prec UNIOP
841                         { $$ = localize($2,$1); }
842         |       '(' expr ')'
843                         { $$ = sawparens($2); }
844         |       QWLIST
845                         { $$ = $1; }
846         |       '(' ')'
847                         { $$ = sawparens(newNULLLIST()); }
848         |       scalar  %prec '('
849                         { $$ = $1; }
850         |       star    %prec '('
851                         { $$ = $1; }
852         |       hsh     %prec '('
853                         { $$ = $1; }
854         |       ary     %prec '('
855                         { $$ = $1; }
856         |       arylen  %prec '('                    /* $#x, $#{ something } */
857                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
858         |       subscripted
859                         { $$ = $1; }
860         |       sliceme '[' expr ']'                     /* array slice */
861                         { $$ = op_prepend_elem(OP_ASLICE,
862                                 newOP(OP_PUSHMARK, 0),
863                                     newLISTOP(OP_ASLICE, 0,
864                                         list($3),
865                                         ref($1, OP_ASLICE)));
866                           if ($$ && $1)
867                               $$->op_private |=
868                                   $1->op_private & OPpSLICEWARNING;
869                         }
870         |       kvslice '[' expr ']'                 /* array key/value slice */
871                         { $$ = op_prepend_elem(OP_KVASLICE,
872                                 newOP(OP_PUSHMARK, 0),
873                                     newLISTOP(OP_KVASLICE, 0,
874                                         list($3),
875                                         ref(oopsAV($1), OP_KVASLICE)));
876                           if ($$ && $1)
877                               $$->op_private |=
878                                   $1->op_private & OPpSLICEWARNING;
879                         }
880         |       sliceme '{' expr ';' '}'                 /* @hash{@keys} */
881                         { $$ = op_prepend_elem(OP_HSLICE,
882                                 newOP(OP_PUSHMARK, 0),
883                                     newLISTOP(OP_HSLICE, 0,
884                                         list($3),
885                                         ref(oopsHV($1), OP_HSLICE)));
886                           if ($$ && $1)
887                               $$->op_private |=
888                                   $1->op_private & OPpSLICEWARNING;
889                             PL_parser->expect = XOPERATOR;
890                         }
891         |       kvslice '{' expr ';' '}'                 /* %hash{@keys} */
892                         { $$ = op_prepend_elem(OP_KVHSLICE,
893                                 newOP(OP_PUSHMARK, 0),
894                                     newLISTOP(OP_KVHSLICE, 0,
895                                         list($3),
896                                         ref($1, OP_KVHSLICE)));
897                           if ($$ && $1)
898                               $$->op_private |=
899                                   $1->op_private & OPpSLICEWARNING;
900                             PL_parser->expect = XOPERATOR;
901                         }
902         |       THING   %prec '('
903                         { $$ = $1; }
904         |       amper                                /* &foo; */
905                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
906         |       amper '(' ')'                 /* &foo() or foo() */
907                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
908                         }
909         |       amper '(' expr ')'          /* &foo(@args) or foo(@args) */
910                         {
911                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
912                                 op_append_elem(OP_LIST, $3, scalar($1)));
913                         }
914         |       NOAMP subname optlistexpr       /* foo @args (no parens) */
915                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
916                             op_append_elem(OP_LIST, $3, scalar($2)));
917                         }
918         |       term ARROW '$' '*'
919                         { $$ = newSVREF($1); }
920         |       term ARROW '@' '*'
921                         { $$ = newAVREF($1); }
922         |       term ARROW '%' '*'
923                         { $$ = newHVREF($1); }
924         |       term ARROW '&' '*'
925                         { $$ = newUNOP(OP_ENTERSUB, 0,
926                                        scalar(newCVREF($3,$1))); }
927         |       term ARROW '*' '*'      %prec '('
928                         { $$ = newGVREF(0,$1); }
929         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
930                         { $$ = newOP($1, OPf_SPECIAL);
931                             PL_hints |= HINT_BLOCK_SCOPE; }
932         |       LOOPEX term
933                         { $$ = newLOOPEX($1,$2); }
934         |       NOTOP listexpr                       /* not $foo */
935                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
936         |       UNIOP                                /* Unary op, $_ implied */
937                         { $$ = newOP($1, 0); }
938         |       UNIOP block                          /* eval { foo }* */
939                         { $$ = newUNOP($1, 0, $2); }
940         |       UNIOP term                           /* Unary op */
941                         { $$ = newUNOP($1, 0, $2); }
942         |       REQUIRE                              /* require, $_ implied */
943                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
944         |       REQUIRE term                         /* require Foo */
945                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
946         |       UNIOPSUB
947                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
948         |       UNIOPSUB term                        /* Sub treated as unop */
949                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
950                             op_append_elem(OP_LIST, $2, scalar($1))); }
951         |       FUNC0                                /* Nullary operator */
952                         { $$ = newOP($1, 0); }
953         |       FUNC0 '(' ')'
954                         { $$ = newOP($1, 0);}
955         |       FUNC0OP       /* Same as above, but op created in toke.c */
956                         { $$ = $1; }
957         |       FUNC0OP '(' ')'
958                         { $$ = $1; }
959         |       FUNC0SUB                             /* Sub treated as nullop */
960                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
961         |       FUNC1 '(' ')'                        /* not () */
962                         { $$ = ($1 == OP_NOT)
963                           ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
964                           : newOP($1, OPf_SPECIAL); }
965         |       FUNC1 '(' expr ')'                   /* not($foo) */
966                         { $$ = newUNOP($1, 0, $3); }
967         |       PMFUNC /* m//, s///, qr//, tr/// */
968                         {
969                             if (   $1->op_type != OP_TRANS
970                                 && $1->op_type != OP_TRANSR
971                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
972                             {
973                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
974                                 SAVEFREESV(PL_compcv);
975                             } else
976                                 $<ival>$ = 0;
977                         }
978                     '(' listexpr ')'
979                         { $$ = pmruntime($1, $4, 1, $<ival>2); }
980         |       WORD
981         |       listop
982         |       YADAYADA
983                         {
984                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
985                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
986                         }
987         |       PLUGEXPR
988         ;
989
990 /* "my" declarations, with optional attributes */
991 myattrterm:     MY myterm myattrlist
992                         { $$ = my_attrs($2,$3); }
993         |       MY myterm
994                         { $$ = localize($2,$1); }
995         ;
996
997 /* Things that can be "my"'d */
998 myterm  :       '(' expr ')'
999                         { $$ = sawparens($2); }
1000         |       '(' ')'
1001                         { $$ = sawparens(newNULLLIST()); }
1002
1003         |       scalar  %prec '('
1004                         { $$ = $1; }
1005         |       hsh     %prec '('
1006                         { $$ = $1; }
1007         |       ary     %prec '('
1008                         { $$ = $1; }
1009         ;
1010
1011 /* Basic list expressions */
1012 optlistexpr:    /* NULL */ %prec PREC_LOW
1013                         { $$ = (OP*)NULL; }
1014         |       listexpr    %prec PREC_LOW
1015                         { $$ = $1; }
1016         ;
1017
1018 optexpr:        /* NULL */
1019                         { $$ = (OP*)NULL; }
1020         |       expr
1021                         { $$ = $1; }
1022         ;
1023
1024 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1025    lexical */
1026 my_scalar:      scalar
1027                         { PL_parser->in_my = 0; $$ = my($1); }
1028         ;
1029
1030 amper   :       '&' indirob
1031                         { $$ = newCVREF($1,$2); }
1032         ;
1033
1034 scalar  :       '$' indirob
1035                         { $$ = newSVREF($2); }
1036         ;
1037
1038 ary     :       '@' indirob
1039                         { $$ = newAVREF($2);
1040                           if ($$) $$->op_private |= $1;
1041                         }
1042         ;
1043
1044 hsh     :       '%' indirob
1045                         { $$ = newHVREF($2);
1046                           if ($$) $$->op_private |= $1;
1047                         }
1048         ;
1049
1050 arylen  :       DOLSHARP indirob
1051                         { $$ = newAVREF($2); }
1052         |       term ARROW DOLSHARP '*'
1053                         { $$ = newAVREF($1); }
1054         ;
1055
1056 star    :       '*' indirob
1057                         { $$ = newGVREF(0,$2); }
1058         ;
1059
1060 sliceme :       ary
1061         |       term ARROW '@'
1062                         { $$ = newAVREF($1); }
1063         ;
1064
1065 kvslice :       hsh
1066         |       term ARROW '%'
1067                         { $$ = newHVREF($1); }
1068         ;
1069
1070 gelem   :       star
1071         |       term ARROW '*'
1072                         { $$ = newGVREF(0,$1); }
1073         ;
1074
1075 /* Indirect objects */
1076 indirob :       WORD
1077                         { $$ = scalar($1); }
1078         |       scalar %prec PREC_LOW
1079                         { $$ = scalar($1); }
1080         |       block
1081                         { $$ = op_scope($1); }
1082
1083         |       PRIVATEREF
1084                         { $$ = $1; }
1085         ;