This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Clone state subs in anon subs
[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  * Note that these derived files are included and compiled twice; once
24  * from perly.c, and once from madly.c. The second time, a number of MAD
25  * macros are defined, which compile in extra code that allows the parse
26  * tree to be accurately dumped. In particular:
27  *
28  * MAD            defined if compiling madly.c
29  * DO_MAD(A)      expands to A  under madly.c, to null otherwise
30  * IF_MAD(a,b)    expands to A under madly.c, to B otherwise
31  * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
32  * TOKEN_FREE()   similarly
33  * OP_GETMAD()    similarly
34  * IVAL(i)        expands to (i)->tk_lval.ival or (i)
35  * PVAL(p)        expands to (p)->tk_lval.pval or (p)
36  *
37  * The main job of of this grammar is to call the various newFOO()
38  * functions in op.c to build a syntax tree of OP structs.
39  * It relies on the lexer in toke.c to do the tokenizing.
40  *
41  * Note: due to the way that the cleanup code works WRT to freeing ops on
42  * the parse stack, it is dangerous to assign to the $n variables within
43  * an action.
44  */
45
46 /*  Make the parser re-entrant. */
47
48 %pure_parser
49
50 /* FIXME for MAD - is the new mintro on while and until important?  */
51
52 %start grammar
53
54 %union {
55     I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56                                 must always be 1st union member) */
57     char *pval;
58     OP *opval;
59     GV *gvval;
60 #ifdef PERL_IN_MADLY_C
61     TOKEN* p_tkval;
62     TOKEN* i_tkval;
63 #else
64     char *p_tkval;
65     I32 i_tkval;
66 #endif
67 #ifdef PERL_MAD
68     TOKEN* tkval;
69 #endif
70 }
71
72 %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
73
74 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.'
75
76 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
77 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
78 %token <opval> PLUGEXPR PLUGSTMT
79 %token <p_tkval> LABEL
80 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
81 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
82 %token <i_tkval> GIVEN WHEN DEFAULT
83 %token <i_tkval> LOOPEX DOTDOT YADAYADA
84 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
85 %token <i_tkval> RELOP EQOP MULOP ADDOP
86 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
87 %token <i_tkval> LOCAL MY MYSUB REQUIRE
88 %token <i_tkval> COLONATTR FORMLBRACK FORMRBRACK
89
90 %type <ival> grammar remember mremember
91 %type <ival>  startsub startanonsub startformsub
92 /* FIXME for MAD - are these two ival? */
93 %type <ival> mintro
94
95 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
96 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
97 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
98 %type <opval> optlistexpr optexpr indirob listop method
99 %type <opval> formname subname proto subbody cont my_scalar formblock
100 %type <opval> subattrlist myattrlist myattrterm myterm
101 %type <opval> termbinop termunop anonymous termdo
102 %type <opval> formstmtseq formline formarg
103
104 %nonassoc <i_tkval> PREC_LOW
105 %nonassoc LOOPEX
106
107 %left <i_tkval> OROP DOROP
108 %left <i_tkval> ANDOP
109 %right <i_tkval> NOTOP
110 %nonassoc LSTOP LSTOPSUB
111 %left <i_tkval> ','
112 %right <i_tkval> ASSIGNOP
113 %right <i_tkval> '?' ':'
114 %nonassoc DOTDOT YADAYADA
115 %left <i_tkval> OROR DORDOR
116 %left <i_tkval> ANDAND
117 %left <i_tkval> BITOROP
118 %left <i_tkval> BITANDOP
119 %nonassoc EQOP
120 %nonassoc RELOP
121 %nonassoc UNIOP UNIOPSUB
122 %nonassoc REQUIRE
123 %left <i_tkval> SHIFTOP
124 %left ADDOP
125 %left MULOP
126 %left <i_tkval> MATCHOP
127 %right <i_tkval> '!' '~' UMINUS REFGEN
128 %right <i_tkval> POWOP
129 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
130 %left <i_tkval> ARROW
131 %nonassoc <i_tkval> ')'
132 %left <i_tkval> '('
133 %left '[' '{'
134
135 %token <i_tkval> PEG
136
137 %% /* RULES */
138
139 /* Top-level choice of what kind of thing yyparse was called to parse */
140 grammar :       GRAMPROG
141                         {
142                           PL_parser->expect = XSTATE;
143                         }
144                 remember stmtseq
145                         {
146                           newPROG(block_end($3,$4));
147                           $$ = 0;
148                         }
149         |       GRAMEXPR
150                         {
151                           parser->expect = XTERM;
152                         }
153                 optexpr
154                         {
155                           PL_eval_root = $3;
156                           $$ = 0;
157                         }
158         |       GRAMBLOCK
159                         {
160                           parser->expect = XBLOCK;
161                         }
162                 block
163                         {
164                           PL_pad_reset_pending = TRUE;
165                           PL_eval_root = $3;
166                           $$ = 0;
167                           yyunlex();
168                           parser->yychar = YYEOF;
169                         }
170         |       GRAMBARESTMT
171                         {
172                           parser->expect = XSTATE;
173                         }
174                 barestmt
175                         {
176                           PL_pad_reset_pending = TRUE;
177                           PL_eval_root = $3;
178                           $$ = 0;
179                           yyunlex();
180                           parser->yychar = YYEOF;
181                         }
182         |       GRAMFULLSTMT
183                         {
184                           parser->expect = XSTATE;
185                         }
186                 fullstmt
187                         {
188                           PL_pad_reset_pending = TRUE;
189                           PL_eval_root = $3;
190                           $$ = 0;
191                           yyunlex();
192                           parser->yychar = YYEOF;
193                         }
194         |       GRAMSTMTSEQ
195                         {
196                           parser->expect = XSTATE;
197                         }
198                 stmtseq
199                         {
200                           PL_eval_root = $3;
201                           $$ = 0;
202                         }
203         ;
204
205 /* An ordinary block */
206 block   :       '{' remember stmtseq '}'
207                         { if (PL_parser->copline > (line_t)IVAL($1))
208                               PL_parser->copline = (line_t)IVAL($1);
209                           $$ = block_end($2, $3);
210                           TOKEN_GETMAD($1,$$,'{');
211                           TOKEN_GETMAD($4,$$,'}');
212                         }
213         ;
214
215 /* format body */
216 formblock:      '=' remember ';' FORMRBRACK formstmtseq ';' '.'
217                         { if (PL_parser->copline > (line_t)IVAL($1))
218                               PL_parser->copline = (line_t)IVAL($1);
219                           $$ = block_end($2, $5);
220                           TOKEN_GETMAD($1,$$,'{');
221                           TOKEN_GETMAD($7,$$,'}');
222                         }
223         ;
224
225 remember:       /* NULL */      /* start a full lexical scope */
226                         { $$ = block_start(TRUE); }
227         ;
228
229 mblock  :       '{' mremember stmtseq '}'
230                         { if (PL_parser->copline > (line_t)IVAL($1))
231                               PL_parser->copline = (line_t)IVAL($1);
232                           $$ = block_end($2, $3);
233                           TOKEN_GETMAD($1,$$,'{');
234                           TOKEN_GETMAD($4,$$,'}');
235                         }
236         ;
237
238 mremember:      /* NULL */      /* start a partial lexical scope */
239                         { $$ = block_start(FALSE); }
240         ;
241
242 /* A sequence of statements in the program */
243 stmtseq :       /* NULL */
244                         { $$ = (OP*)NULL; }
245         |       stmtseq fullstmt
246                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
247                             PL_pad_reset_pending = TRUE;
248                             if ($1 && $2)
249                                 PL_hints |= HINT_BLOCK_SCOPE;
250                         }
251         ;
252
253 /* A sequence of format lines */
254 formstmtseq:    /* NULL */
255                         { $$ = (OP*)NULL; }
256         |       formstmtseq formline
257                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
258                             PL_pad_reset_pending = TRUE;
259                             if ($1 && $2)
260                                 PL_hints |= HINT_BLOCK_SCOPE;
261                         }
262         ;
263
264 /* A statement in the program, including optional labels */
265 fullstmt:       barestmt
266                         {
267                           if($1) {
268                               $$ = newSTATEOP(0, NULL, $1);
269                           } else {
270                               $$ = IF_MAD(newOP(OP_NULL, 0), NULL);
271                           }
272                         }
273         |       labfullstmt
274                         { $$ = $1; }
275         ;
276
277 labfullstmt:    LABEL barestmt
278                         {
279                           $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
280                                         savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
281                           TOKEN_GETMAD($1,
282                               $2 ? cLISTOPx($$)->op_first : $$, 'L');
283                         }
284         |       LABEL labfullstmt
285                         {
286                           $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
287                                         savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
288                           TOKEN_GETMAD($1, cLISTOPx($$)->op_first, 'L');
289                         }
290         ;
291
292 /* A bare statement, lacking label and other aspects of state op */
293 barestmt:       PLUGSTMT
294                         { $$ = $1; }
295         |       PEG
296                         {
297                           $$ = newOP(OP_NULL,0);
298                           TOKEN_GETMAD($1,$$,'p');
299                         }
300         |       FORMAT startformsub formname formblock
301                         {
302                           CV *fmtcv = PL_compcv;
303 #ifdef MAD
304                           $$ = newFORM($2, $3, $4);
305                           prepend_madprops($1->tk_mad, $$, 'F');
306                           $1->tk_mad = 0;
307                           token_free($1);
308 #else
309                           newFORM($2, $3, $4);
310                           $$ = (OP*)NULL;
311 #endif
312                           if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
313                               SvREFCNT_inc_simple_void(fmtcv);
314                               pad_add_anon(fmtcv, OP_NULL);
315                           }
316                         }
317         |       SUB subname startsub
318                         {
319                           if ($2->op_type == OP_CONST) {
320                             const char *const name =
321                                 SvPV_nolen_const(((SVOP*)$2)->op_sv);
322                             if (strEQ(name, "BEGIN") || strEQ(name, "END")
323                               || strEQ(name, "INIT") || strEQ(name, "CHECK")
324                               || strEQ(name, "UNITCHECK"))
325                               CvSPECIAL_on(PL_compcv);
326                           }
327                           else
328                           /* State subs inside anonymous subs need to be
329                              clonable themselves. */
330                           /* XXX This will need adjustment for state subs
331                                  inside my subs. */
332                           if (CvANON(CvOUTSIDE(PL_compcv)))
333                               CvCLONE_on(PL_compcv);
334                           PL_parser->in_my = 0;
335                           PL_parser->in_my_stash = NULL;
336                         }
337                 proto subattrlist subbody
338                         {
339                           SvREFCNT_inc_simple_void(PL_compcv);
340 #ifdef MAD
341                           {
342                               OP* o = newSVOP(OP_ANONCODE, 0,
343                                 (SV*)(
344 #endif
345                           $2->op_type == OP_CONST
346                               ? newATTRSUB($3, $2, $5, $6, $7)
347                               : newMYSUB($3, $2, $5, $6, $7)
348 #ifdef MAD
349                                 ));
350                               $$ = newOP(OP_NULL,0);
351                               op_getmad(o,$$,'&');
352                               op_getmad($2,$$,'n');
353                               op_getmad($5,$$,'s');
354                               op_getmad($6,$$,'a');
355                               token_getmad($1,$$,'d');
356                               append_madprops($7->op_madprop, $$, 0);
357                               $7->op_madprop = 0;
358                           }
359 #else
360                           ;
361                           $$ = (OP*)NULL;
362 #endif
363                           intro_my();
364                         }
365         |       MYSUB startsub subname proto subattrlist subbody
366                         {
367                           /* Unimplemented "my sub foo { }" */
368                           SvREFCNT_inc_simple_void(PL_compcv);
369 #ifdef MAD
370                           $$ = newMYSUB($2, $3, $4, $5, $6);
371                           token_getmad($1,$$,'d');
372 #else
373                           newMYSUB($2, $3, $4, $5, $6);
374                           $$ = (OP*)NULL;
375 #endif
376                         }
377         |       PACKAGE WORD WORD ';'
378                         {
379 #ifdef MAD
380                           $$ = package($3);
381                           token_getmad($1,$$,'o');
382                           if ($2)
383                               package_version($2);
384                           token_getmad($4,$$,';');
385 #else
386                           package($3);
387                           if ($2)
388                               package_version($2);
389                           $$ = (OP*)NULL;
390 #endif
391                         }
392         |       USE startsub
393                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
394                 WORD WORD optlistexpr ';'
395                         {
396                           SvREFCNT_inc_simple_void(PL_compcv);
397 #ifdef MAD
398                           $$ = utilize(IVAL($1), $2, $4, $5, $6);
399                           token_getmad($1,$$,'o');
400                           token_getmad($7,$$,';');
401                           if (PL_parser->rsfp_filters &&
402                                       AvFILLp(PL_parser->rsfp_filters) >= 0)
403                               append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
404 #else
405                           utilize(IVAL($1), $2, $4, $5, $6);
406                           $$ = (OP*)NULL;
407 #endif
408                         }
409         |       IF '(' remember mexpr ')' mblock else
410                         {
411                           $$ = block_end($3,
412                               newCONDOP(0, $4, op_scope($6), $7));
413                           TOKEN_GETMAD($1,$$,'I');
414                           TOKEN_GETMAD($2,$$,'(');
415                           TOKEN_GETMAD($5,$$,')');
416                           PL_parser->copline = (line_t)IVAL($1);
417                         }
418         |       UNLESS '(' remember miexpr ')' mblock else
419                         {
420                           $$ = block_end($3,
421                               newCONDOP(0, $4, op_scope($6), $7));
422                           TOKEN_GETMAD($1,$$,'I');
423                           TOKEN_GETMAD($2,$$,'(');
424                           TOKEN_GETMAD($5,$$,')');
425                           PL_parser->copline = (line_t)IVAL($1);
426                         }
427         |       GIVEN '(' remember mexpr ')' mblock
428                         {
429                           const PADOFFSET offset = pad_findmy_pvs("$_", 0);
430                           $$ = block_end($3,
431                                   newGIVENOP($4, op_scope($6),
432                                     offset == NOT_IN_PAD
433                                     || PAD_COMPNAME_FLAGS_isOUR(offset)
434                                       ? 0
435                                       : offset));
436                           PL_parser->copline = (line_t)IVAL($1);
437                         }
438         |       WHEN '(' remember mexpr ')' mblock
439                         { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
440         |       DEFAULT block
441                         { $$ = newWHENOP(0, op_scope($2)); }
442         |       WHILE '(' remember texpr ')' mintro mblock cont
443                         {
444                           $$ = block_end($3,
445                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
446                                       $4, $7, $8, $6));
447                           TOKEN_GETMAD($1,$$,'W');
448                           TOKEN_GETMAD($2,$$,'(');
449                           TOKEN_GETMAD($5,$$,')');
450                           PL_parser->copline = (line_t)IVAL($1);
451                         }
452         |       UNTIL '(' remember iexpr ')' mintro mblock cont
453                         {
454                           $$ = block_end($3,
455                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
456                                       $4, $7, $8, $6));
457                           TOKEN_GETMAD($1,$$,'W');
458                           TOKEN_GETMAD($2,$$,'(');
459                           TOKEN_GETMAD($5,$$,')');
460                           PL_parser->copline = (line_t)IVAL($1);
461                         }
462         |       FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
463                 mblock
464                         {
465                           OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
466                           OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
467                                       scalar($6), $11, $9, $8);
468                           if (initop) {
469                               forop = op_prepend_elem(OP_LINESEQ, initop,
470                                   op_append_elem(OP_LINESEQ,
471                                       newOP(OP_UNSTACK, OPf_SPECIAL),
472                                       forop));
473                           }
474                           DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
475                           $$ = block_end($3, forop);
476                           TOKEN_GETMAD($1,$$,'3');
477                           TOKEN_GETMAD($2,$$,'(');
478                           TOKEN_GETMAD($5,$$,'1');
479                           TOKEN_GETMAD($7,$$,'2');
480                           TOKEN_GETMAD($10,$$,')');
481                           PL_parser->copline = (line_t)IVAL($1);
482                         }
483         |       FOR MY remember my_scalar '(' mexpr ')' mblock cont
484                         {
485                           $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
486                           TOKEN_GETMAD($1,$$,'W');
487                           TOKEN_GETMAD($2,$$,'d');
488                           TOKEN_GETMAD($5,$$,'(');
489                           TOKEN_GETMAD($7,$$,')');
490                           PL_parser->copline = (line_t)IVAL($1);
491                         }
492         |       FOR scalar '(' remember mexpr ')' mblock cont
493                         {
494                           $$ = block_end($4, newFOROP(0,
495                                       op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
496                           TOKEN_GETMAD($1,$$,'W');
497                           TOKEN_GETMAD($3,$$,'(');
498                           TOKEN_GETMAD($6,$$,')');
499                           PL_parser->copline = (line_t)IVAL($1);
500                         }
501         |       FOR '(' remember mexpr ')' mblock cont
502                         {
503                           $$ = block_end($3,
504                                   newFOROP(0, (OP*)NULL, $4, $6, $7));
505                           TOKEN_GETMAD($1,$$,'W');
506                           TOKEN_GETMAD($2,$$,'(');
507                           TOKEN_GETMAD($5,$$,')');
508                           PL_parser->copline = (line_t)IVAL($1);
509                         }
510         |       block cont
511                         {
512                           /* a block is a loop that happens once */
513                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
514                                   (OP*)NULL, $1, $2, 0);
515                         }
516         |       PACKAGE WORD WORD '{' remember
517                         {
518                           package($3);
519                           if ($2) {
520                               package_version($2);
521                           }
522                         }
523                 stmtseq '}'
524                         {
525                           /* a block is a loop that happens once */
526                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
527                                   (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
528                           TOKEN_GETMAD($4,$$,'{');
529                           TOKEN_GETMAD($8,$$,'}');
530                           if (PL_parser->copline > (line_t)IVAL($4))
531                               PL_parser->copline = (line_t)IVAL($4);
532                         }
533         |       sideff ';'
534                         {
535                           PL_parser->expect = XSTATE;
536                           $$ = $1;
537                           TOKEN_GETMAD($2,$$,';');
538                         }
539         |       ';'
540                         {
541                           PL_parser->expect = XSTATE;
542                           $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
543                           TOKEN_GETMAD($1,$$,';');
544                           PL_parser->copline = NOLINE;
545                         }
546         ;
547
548 /* Format line */
549 formline:       THING formarg
550                         { OP *list;
551                           if ($2) {
552                               OP *term = $2;
553                               DO_MAD(term = newUNOP(OP_NULL, 0, term));
554                               list = op_append_elem(OP_LIST, $1, term);
555                           }
556                           else {
557 #ifdef MAD
558                               OP *op = newNULLLIST();
559                               list = op_append_elem(OP_LIST, $1, op);
560 #else
561                               list = $1;
562 #endif
563                           }
564                           if (PL_parser->copline == NOLINE)
565                                PL_parser->copline = CopLINE(PL_curcop)-1;
566                           else PL_parser->copline--;
567                           $$ = newSTATEOP(0, NULL,
568                                           convert(OP_FORMLINE, 0, list));
569                         }
570         ;
571
572 formarg :       /* NULL */
573                         { $$ = NULL; }
574         |       FORMLBRACK stmtseq FORMRBRACK
575                         { $$ = op_unscope($2); }
576         ;
577
578 /* An expression which may have a side-effect */
579 sideff  :       error
580                         { $$ = (OP*)NULL; }
581         |       expr
582                         { $$ = $1; }
583         |       expr IF expr
584                         { $$ = newLOGOP(OP_AND, 0, $3, $1);
585                           TOKEN_GETMAD($2,$$,'i');
586                         }
587         |       expr UNLESS expr
588                         { $$ = newLOGOP(OP_OR, 0, $3, $1);
589                           TOKEN_GETMAD($2,$$,'i');
590                         }
591         |       expr WHILE expr
592                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
593                           TOKEN_GETMAD($2,$$,'w');
594                         }
595         |       expr UNTIL iexpr
596                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
597                           TOKEN_GETMAD($2,$$,'w');
598                         }
599         |       expr FOR expr
600                         { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
601                           TOKEN_GETMAD($2,$$,'w');
602                           PL_parser->copline = (line_t)IVAL($2);
603                         }
604         |       expr WHEN expr
605                         { $$ = newWHENOP($3, op_scope($1)); }
606         ;
607
608 /* else and elsif blocks */
609 else    :       /* NULL */
610                         { $$ = (OP*)NULL; }
611         |       ELSE mblock
612                         {
613                           ($2)->op_flags |= OPf_PARENS;
614                           $$ = op_scope($2);
615                           TOKEN_GETMAD($1,$$,'o');
616                         }
617         |       ELSIF '(' mexpr ')' mblock else
618                         { PL_parser->copline = (line_t)IVAL($1);
619                             $$ = newCONDOP(0,
620                                 newSTATEOP(OPf_SPECIAL,NULL,$3),
621                                 op_scope($5), $6);
622                           PL_hints |= HINT_BLOCK_SCOPE;
623                           TOKEN_GETMAD($1,$$,'I');
624                           TOKEN_GETMAD($2,$$,'(');
625                           TOKEN_GETMAD($4,$$,')');
626                         }
627         ;
628
629 /* Continue blocks */
630 cont    :       /* NULL */
631                         { $$ = (OP*)NULL; }
632         |       CONTINUE block
633                         {
634                           $$ = op_scope($2);
635                           TOKEN_GETMAD($1,$$,'o');
636                         }
637         ;
638
639 /* determine whether there are any new my declarations */
640 mintro  :       /* NULL */
641                         { $$ = (PL_min_intro_pending &&
642                             PL_max_intro_pending >=  PL_min_intro_pending);
643                           intro_my(); }
644
645 /* Normal expression */
646 nexpr   :       /* NULL */
647                         { $$ = (OP*)NULL; }
648         |       sideff
649         ;
650
651 /* Boolean expression */
652 texpr   :       /* NULL means true */
653                         { YYSTYPE tmplval;
654                           (void)scan_num("1", &tmplval);
655                           $$ = tmplval.opval; }
656         |       expr
657         ;
658
659 /* Inverted boolean expression */
660 iexpr   :       expr
661                         { $$ = invert(scalar($1)); }
662         ;
663
664 /* Expression with its own lexical scope */
665 mexpr   :       expr
666                         { $$ = $1; intro_my(); }
667         ;
668
669 mnexpr  :       nexpr
670                         { $$ = $1; intro_my(); }
671         ;
672
673 miexpr  :       iexpr
674                         { $$ = $1; intro_my(); }
675         ;
676
677 formname:       WORD            { $$ = $1; }
678         |       /* NULL */      { $$ = (OP*)NULL; }
679         ;
680
681 startsub:       /* NULL */      /* start a regular subroutine scope */
682                         { $$ = start_subparse(FALSE, 0);
683                             SAVEFREESV(PL_compcv); }
684
685         ;
686
687 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
688                         { $$ = start_subparse(FALSE, CVf_ANON);
689                             SAVEFREESV(PL_compcv); }
690         ;
691
692 startformsub:   /* NULL */      /* start a format subroutine scope */
693                         { $$ = start_subparse(TRUE, 0);
694                             SAVEFREESV(PL_compcv); }
695         ;
696
697 /* Name of a subroutine - must be a bareword, could be special */
698 subname :       WORD
699         |       PRIVATEREF
700         ;
701
702 /* Subroutine prototype */
703 proto   :       /* NULL */
704                         { $$ = (OP*)NULL; }
705         |       THING
706         ;
707
708 /* Optional list of subroutine attributes */
709 subattrlist:    /* NULL */
710                         { $$ = (OP*)NULL; }
711         |       COLONATTR THING
712                         { $$ = $2;
713                           TOKEN_GETMAD($1,$$,':');
714                         }
715         |       COLONATTR
716                         { $$ = IF_MAD(
717                                     newOP(OP_NULL, 0),
718                                     (OP*)NULL
719                                 );
720                           TOKEN_GETMAD($1,$$,':');
721                         }
722         ;
723
724 /* List of attributes for a "my" variable declaration */
725 myattrlist:     COLONATTR THING
726                         { $$ = $2;
727                           TOKEN_GETMAD($1,$$,':');
728                         }
729         |       COLONATTR
730                         { $$ = IF_MAD(
731                                     newOP(OP_NULL, 0),
732                                     (OP*)NULL
733                                 );
734                           TOKEN_GETMAD($1,$$,':');
735                         }
736         ;
737
738 /* Subroutine body - either null or a block */
739 subbody :       block   { $$ = $1; }
740         |       ';'     { $$ = IF_MAD(
741                                     newOP(OP_NULL,0),
742                                     (OP*)NULL
743                                 );
744                           PL_parser->expect = XSTATE;
745                           TOKEN_GETMAD($1,$$,';');
746                         }
747         ;
748
749 /* Ordinary expressions; logical combinations */
750 expr    :       expr ANDOP expr
751                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
752                           TOKEN_GETMAD($2,$$,'o');
753                         }
754         |       expr OROP expr
755                         { $$ = newLOGOP(IVAL($2), 0, $1, $3);
756                           TOKEN_GETMAD($2,$$,'o');
757                         }
758         |       expr DOROP expr
759                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
760                           TOKEN_GETMAD($2,$$,'o');
761                         }
762         |       listexpr %prec PREC_LOW
763         ;
764
765 /* Expressions are a list of terms joined by commas */
766 listexpr:       listexpr ','
767                         {
768 #ifdef MAD
769                           OP* op = newNULLLIST();
770                           token_getmad($2,op,',');
771                           $$ = op_append_elem(OP_LIST, $1, op);
772 #else
773                           $$ = $1;
774 #endif
775                         }
776         |       listexpr ',' term
777                         { 
778                           OP* term = $3;
779                           DO_MAD(
780                               term = newUNOP(OP_NULL, 0, term);
781                               token_getmad($2,term,',');
782                           )
783                           $$ = op_append_elem(OP_LIST, $1, term);
784                         }
785         |       term %prec PREC_LOW
786         ;
787
788 /* List operators */
789 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
790                         { $$ = convert(IVAL($1), OPf_STACKED,
791                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
792                           TOKEN_GETMAD($1,$$,'o');
793                         }
794         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
795                         { $$ = convert(IVAL($1), OPf_STACKED,
796                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
797                           TOKEN_GETMAD($1,$$,'o');
798                           TOKEN_GETMAD($2,$$,'(');
799                           TOKEN_GETMAD($5,$$,')');
800                         }
801         |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
802                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
803                                 op_append_elem(OP_LIST,
804                                     op_prepend_elem(OP_LIST, scalar($1), $5),
805                                     newUNOP(OP_METHOD, 0, $3)));
806                           TOKEN_GETMAD($2,$$,'A');
807                           TOKEN_GETMAD($4,$$,'(');
808                           TOKEN_GETMAD($6,$$,')');
809                         }
810         |       term ARROW method                     /* $foo->bar */
811                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
812                                 op_append_elem(OP_LIST, scalar($1),
813                                     newUNOP(OP_METHOD, 0, $3)));
814                           TOKEN_GETMAD($2,$$,'A');
815                         }
816         |       METHOD indirob optlistexpr           /* new Class @args */
817                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
818                                 op_append_elem(OP_LIST,
819                                     op_prepend_elem(OP_LIST, $2, $3),
820                                     newUNOP(OP_METHOD, 0, $1)));
821                         }
822         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
823                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
824                                 op_append_elem(OP_LIST,
825                                     op_prepend_elem(OP_LIST, $2, $4),
826                                     newUNOP(OP_METHOD, 0, $1)));
827                           TOKEN_GETMAD($3,$$,'(');
828                           TOKEN_GETMAD($5,$$,')');
829                         }
830         |       LSTOP optlistexpr                    /* print @args */
831                         { $$ = convert(IVAL($1), 0, $2);
832                           TOKEN_GETMAD($1,$$,'o');
833                         }
834         |       FUNC '(' optexpr ')'                 /* print (@args) */
835                         { $$ = convert(IVAL($1), 0, $3);
836                           TOKEN_GETMAD($1,$$,'o');
837                           TOKEN_GETMAD($2,$$,'(');
838                           TOKEN_GETMAD($4,$$,')');
839                         }
840         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
841                         { SvREFCNT_inc_simple_void(PL_compcv);
842                           $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
843                     optlistexpr         %prec LSTOP  /* ... @bar */
844                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
845                                  op_append_elem(OP_LIST,
846                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
847                         }
848         ;
849
850 /* Names of methods. May use $object->$methodname */
851 method  :       METHOD
852         |       scalar
853         ;
854
855 /* Some kind of subscripted expression */
856 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
857                         /* In this and all the hash accessors, ';' is
858                          * provided by the tokeniser */
859                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
860                             PL_parser->expect = XOPERATOR;
861                           TOKEN_GETMAD($2,$$,'{');
862                           TOKEN_GETMAD($4,$$,';');
863                           TOKEN_GETMAD($5,$$,'}');
864                         }
865         |       scalar '[' expr ']'          /* $array[$element] */
866                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
867                           TOKEN_GETMAD($2,$$,'[');
868                           TOKEN_GETMAD($4,$$,']');
869                         }
870         |       term ARROW '[' expr ']'      /* somearef->[$element] */
871                         { $$ = newBINOP(OP_AELEM, 0,
872                                         ref(newAVREF($1),OP_RV2AV),
873                                         scalar($4));
874                           TOKEN_GETMAD($2,$$,'a');
875                           TOKEN_GETMAD($3,$$,'[');
876                           TOKEN_GETMAD($5,$$,']');
877                         }
878         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
879                         { $$ = newBINOP(OP_AELEM, 0,
880                                         ref(newAVREF($1),OP_RV2AV),
881                                         scalar($3));
882                           TOKEN_GETMAD($2,$$,'[');
883                           TOKEN_GETMAD($4,$$,']');
884                         }
885         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
886                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
887                             PL_parser->expect = XOPERATOR;
888                           TOKEN_GETMAD($2,$$,'{');
889                           TOKEN_GETMAD($4,$$,';');
890                           TOKEN_GETMAD($5,$$,'}');
891                         }
892         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
893                         { $$ = newBINOP(OP_HELEM, 0,
894                                         ref(newHVREF($1),OP_RV2HV),
895                                         jmaybe($4));
896                             PL_parser->expect = XOPERATOR;
897                           TOKEN_GETMAD($2,$$,'a');
898                           TOKEN_GETMAD($3,$$,'{');
899                           TOKEN_GETMAD($5,$$,';');
900                           TOKEN_GETMAD($6,$$,'}');
901                         }
902         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
903                         { $$ = newBINOP(OP_HELEM, 0,
904                                         ref(newHVREF($1),OP_RV2HV),
905                                         jmaybe($3));
906                             PL_parser->expect = XOPERATOR;
907                           TOKEN_GETMAD($2,$$,'{');
908                           TOKEN_GETMAD($4,$$,';');
909                           TOKEN_GETMAD($5,$$,'}');
910                         }
911         |       term ARROW '(' ')'          /* $subref->() */
912                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
913                                    newCVREF(0, scalar($1)));
914                           TOKEN_GETMAD($2,$$,'a');
915                           TOKEN_GETMAD($3,$$,'(');
916                           TOKEN_GETMAD($4,$$,')');
917                         }
918         |       term ARROW '(' expr ')'     /* $subref->(@args) */
919                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
920                                    op_append_elem(OP_LIST, $4,
921                                        newCVREF(0, scalar($1))));
922                           TOKEN_GETMAD($2,$$,'a');
923                           TOKEN_GETMAD($3,$$,'(');
924                           TOKEN_GETMAD($5,$$,')');
925                         }
926
927         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
928                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
929                                    op_append_elem(OP_LIST, $3,
930                                                newCVREF(0, scalar($1))));
931                           TOKEN_GETMAD($2,$$,'(');
932                           TOKEN_GETMAD($4,$$,')');
933                         }
934         |       subscripted '(' ')'        /* $foo->{bar}->() */
935                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
936                                    newCVREF(0, scalar($1)));
937                           TOKEN_GETMAD($2,$$,'(');
938                           TOKEN_GETMAD($3,$$,')');
939                         }
940         |       '(' expr ')' '[' expr ']'            /* list slice */
941                         { $$ = newSLICEOP(0, $5, $2);
942                           TOKEN_GETMAD($1,$$,'(');
943                           TOKEN_GETMAD($3,$$,')');
944                           TOKEN_GETMAD($4,$$,'[');
945                           TOKEN_GETMAD($6,$$,']');
946                         }
947         |       QWLIST '[' expr ']'            /* list literal slice */
948                         { $$ = newSLICEOP(0, $3, $1);
949                           TOKEN_GETMAD($2,$$,'[');
950                           TOKEN_GETMAD($4,$$,']');
951                         }
952         |       '(' ')' '[' expr ']'                 /* empty list slice! */
953                         { $$ = newSLICEOP(0, $4, (OP*)NULL);
954                           TOKEN_GETMAD($1,$$,'(');
955                           TOKEN_GETMAD($2,$$,')');
956                           TOKEN_GETMAD($3,$$,'[');
957                           TOKEN_GETMAD($5,$$,']');
958                         }
959     ;
960
961 /* Binary operators between terms */
962 termbinop:      term ASSIGNOP term                     /* $x = $y */
963                         { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
964                           TOKEN_GETMAD($2,$$,'o');
965                         }
966         |       term POWOP term                        /* $x ** $y */
967                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
968                           TOKEN_GETMAD($2,$$,'o');
969                         }
970         |       term MULOP term                        /* $x * $y, $x x $y */
971                         {   if (IVAL($2) != OP_REPEAT)
972                                 scalar($1);
973                             $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
974                           TOKEN_GETMAD($2,$$,'o');
975                         }
976         |       term ADDOP term                        /* $x + $y */
977                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
978                           TOKEN_GETMAD($2,$$,'o');
979                         }
980         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
981                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
982                           TOKEN_GETMAD($2,$$,'o');
983                         }
984         |       term RELOP term                        /* $x > $y, etc. */
985                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
986                           TOKEN_GETMAD($2,$$,'o');
987                         }
988         |       term EQOP term                         /* $x == $y, $x eq $y */
989                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
990                           TOKEN_GETMAD($2,$$,'o');
991                         }
992         |       term BITANDOP term                     /* $x & $y */
993                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
994                           TOKEN_GETMAD($2,$$,'o');
995                         }
996         |       term BITOROP term                      /* $x | $y */
997                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
998                           TOKEN_GETMAD($2,$$,'o');
999                         }
1000         |       term DOTDOT term                       /* $x..$y, $x...$y */
1001                         {
1002                           $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
1003                           DO_MAD({
1004                               UNOP *op;
1005                               op = (UNOP*)$$;
1006                               op = (UNOP*)op->op_first; /* get to flop */
1007                               op = (UNOP*)op->op_first; /* get to flip */
1008                               op = (UNOP*)op->op_first; /* get to range */
1009                               token_getmad($2,(OP*)op,'o');
1010                             })
1011                         }
1012         |       term ANDAND term                       /* $x && $y */
1013                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
1014                           TOKEN_GETMAD($2,$$,'o');
1015                         }
1016         |       term OROR term                         /* $x || $y */
1017                         { $$ = newLOGOP(OP_OR, 0, $1, $3);
1018                           TOKEN_GETMAD($2,$$,'o');
1019                         }
1020         |       term DORDOR term                       /* $x // $y */
1021                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1022                           TOKEN_GETMAD($2,$$,'o');
1023                         }
1024         |       term MATCHOP term                      /* $x =~ /$y/ */
1025                         { $$ = bind_match(IVAL($2), $1, $3);
1026                           TOKEN_GETMAD($2,
1027                                 ($$->op_type == OP_NOT
1028                                     ? ((UNOP*)$$)->op_first : $$),
1029                                 '~');
1030                         }
1031     ;
1032
1033 /* Unary operators and terms */
1034 termunop : '-' term %prec UMINUS                       /* -$x */
1035                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1036                           TOKEN_GETMAD($1,$$,'o');
1037                         }
1038         |       '+' term %prec UMINUS                  /* +$x */
1039                         { $$ = IF_MAD(
1040                                     newUNOP(OP_NULL, 0, $2),
1041                                     $2
1042                                 );
1043                           TOKEN_GETMAD($1,$$,'+');
1044                         }
1045         |       '!' term                               /* !$x */
1046                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1047                           TOKEN_GETMAD($1,$$,'o');
1048                         }
1049         |       '~' term                               /* ~$x */
1050                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1051                           TOKEN_GETMAD($1,$$,'o');
1052                         }
1053         |       term POSTINC                           /* $x++ */
1054                         { $$ = newUNOP(OP_POSTINC, 0,
1055                                         op_lvalue(scalar($1), OP_POSTINC));
1056                           TOKEN_GETMAD($2,$$,'o');
1057                         }
1058         |       term POSTDEC                           /* $x-- */
1059                         { $$ = newUNOP(OP_POSTDEC, 0,
1060                                         op_lvalue(scalar($1), OP_POSTDEC));
1061                           TOKEN_GETMAD($2,$$,'o');
1062                         }
1063         |       PREINC term                            /* ++$x */
1064                         { $$ = newUNOP(OP_PREINC, 0,
1065                                         op_lvalue(scalar($2), OP_PREINC));
1066                           TOKEN_GETMAD($1,$$,'o');
1067                         }
1068         |       PREDEC term                            /* --$x */
1069                         { $$ = newUNOP(OP_PREDEC, 0,
1070                                         op_lvalue(scalar($2), OP_PREDEC));
1071                           TOKEN_GETMAD($1,$$,'o');
1072                         }
1073
1074     ;
1075
1076 /* Constructors for anonymous data */
1077 anonymous:      '[' expr ']'
1078                         { $$ = newANONLIST($2);
1079                           TOKEN_GETMAD($1,$$,'[');
1080                           TOKEN_GETMAD($3,$$,']');
1081                         }
1082         |       '[' ']'
1083                         { $$ = newANONLIST((OP*)NULL);
1084                           TOKEN_GETMAD($1,$$,'[');
1085                           TOKEN_GETMAD($2,$$,']');
1086                         }
1087         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
1088                         { $$ = newANONHASH($2);
1089                           TOKEN_GETMAD($1,$$,'{');
1090                           TOKEN_GETMAD($3,$$,';');
1091                           TOKEN_GETMAD($4,$$,'}');
1092                         }
1093         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
1094                         { $$ = newANONHASH((OP*)NULL);
1095                           TOKEN_GETMAD($1,$$,'{');
1096                           TOKEN_GETMAD($2,$$,';');
1097                           TOKEN_GETMAD($3,$$,'}');
1098                         }
1099         |       ANONSUB startanonsub proto subattrlist block    %prec '('
1100                         { SvREFCNT_inc_simple_void(PL_compcv);
1101                           $$ = newANONATTRSUB($2, $3, $4, $5);
1102                           TOKEN_GETMAD($1,$$,'o');
1103                           OP_GETMAD($3,$$,'s');
1104                           OP_GETMAD($4,$$,'a');
1105                         }
1106
1107     ;
1108
1109 /* Things called with "do" */
1110 termdo  :       DO term %prec UNIOP                     /* do $filename */
1111                         { $$ = dofile($2, IVAL($1));
1112                           TOKEN_GETMAD($1,$$,'o');
1113                         }
1114         |       DO block        %prec '('               /* do { code */
1115                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
1116                           TOKEN_GETMAD($1,$$,'D');
1117                         }
1118         |       DO subname '(' ')'                  /* do somesub() */
1119                         { $$ = newUNOP(OP_ENTERSUB,
1120                             OPf_SPECIAL|OPf_STACKED,
1121                             op_prepend_elem(OP_LIST,
1122                                 scalar(newCVREF(
1123                                     (OPpENTERSUB_AMPER<<8),
1124                                     scalar($2)
1125                                 )),(OP*)NULL)); dep();
1126                           TOKEN_GETMAD($1,$$,'o');
1127                           TOKEN_GETMAD($3,$$,'(');
1128                           TOKEN_GETMAD($4,$$,')');
1129                         }
1130         |       DO subname '(' expr ')'             /* do somesub(@args) */
1131                         { $$ = newUNOP(OP_ENTERSUB,
1132                             OPf_SPECIAL|OPf_STACKED,
1133                             op_append_elem(OP_LIST,
1134                                 $4,
1135                                 scalar(newCVREF(
1136                                     (OPpENTERSUB_AMPER<<8),
1137                                     scalar($2)
1138                                 )))); dep();
1139                           TOKEN_GETMAD($1,$$,'o');
1140                           TOKEN_GETMAD($3,$$,'(');
1141                           TOKEN_GETMAD($5,$$,')');
1142                         }
1143         |       DO scalar '(' ')'                /* do $subref () */
1144                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1145                             op_prepend_elem(OP_LIST,
1146                                 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1147                           TOKEN_GETMAD($1,$$,'o');
1148                           TOKEN_GETMAD($3,$$,'(');
1149                           TOKEN_GETMAD($4,$$,')');
1150                         }
1151         |       DO scalar '(' expr ')'           /* do $subref (@args) */
1152                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1153                             op_prepend_elem(OP_LIST,
1154                                 $4,
1155                                 scalar(newCVREF(0,scalar($2))))); dep();
1156                           TOKEN_GETMAD($1,$$,'o');
1157                           TOKEN_GETMAD($3,$$,'(');
1158                           TOKEN_GETMAD($5,$$,')');
1159                         }
1160
1161         ;
1162
1163 term    :       termbinop
1164         |       termunop
1165         |       anonymous
1166         |       termdo
1167         |       term '?' term ':' term
1168                         { $$ = newCONDOP(0, $1, $3, $5);
1169                           TOKEN_GETMAD($2,$$,'?');
1170                           TOKEN_GETMAD($4,$$,':');
1171                         }
1172         |       REFGEN term                          /* \$x, \@y, \%z */
1173                         { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
1174                           TOKEN_GETMAD($1,$$,'o');
1175                         }
1176         |       myattrterm      %prec UNIOP
1177                         { $$ = $1; }
1178         |       LOCAL term      %prec UNIOP
1179                         { $$ = localize($2,IVAL($1));
1180                           TOKEN_GETMAD($1,$$,'k');
1181                         }
1182         |       '(' expr ')'
1183                         { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1184                           TOKEN_GETMAD($1,$$,'(');
1185                           TOKEN_GETMAD($3,$$,')');
1186                         }
1187         |       QWLIST
1188                         { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
1189         |       '(' ')'
1190                         { $$ = sawparens(newNULLLIST());
1191                           TOKEN_GETMAD($1,$$,'(');
1192                           TOKEN_GETMAD($2,$$,')');
1193                         }
1194         |       scalar  %prec '('
1195                         { $$ = $1; }
1196         |       star    %prec '('
1197                         { $$ = $1; }
1198         |       hsh     %prec '('
1199                         { $$ = $1; }
1200         |       ary     %prec '('
1201                         { $$ = $1; }
1202         |       arylen  %prec '('                    /* $#x, $#{ something } */
1203                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1204         |       subscripted
1205                         { $$ = $1; }
1206         |       ary '[' expr ']'                     /* array slice */
1207                         { $$ = op_prepend_elem(OP_ASLICE,
1208                                 newOP(OP_PUSHMARK, 0),
1209                                     newLISTOP(OP_ASLICE, 0,
1210                                         list($3),
1211                                         ref($1, OP_ASLICE)));
1212                           TOKEN_GETMAD($2,$$,'[');
1213                           TOKEN_GETMAD($4,$$,']');
1214                         }
1215         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
1216                         { $$ = op_prepend_elem(OP_HSLICE,
1217                                 newOP(OP_PUSHMARK, 0),
1218                                     newLISTOP(OP_HSLICE, 0,
1219                                         list($3),
1220                                         ref(oopsHV($1), OP_HSLICE)));
1221                             PL_parser->expect = XOPERATOR;
1222                           TOKEN_GETMAD($2,$$,'{');
1223                           TOKEN_GETMAD($4,$$,';');
1224                           TOKEN_GETMAD($5,$$,'}');
1225                         }
1226         |       THING   %prec '('
1227                         { $$ = $1; }
1228         |       amper                                /* &foo; */
1229                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1230         |       amper '(' ')'                 /* &foo() */
1231                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1232                           TOKEN_GETMAD($2,$$,'(');
1233                           TOKEN_GETMAD($3,$$,')');
1234                         }
1235         |       amper '(' expr ')'            /* &foo(@args) */
1236                         {
1237                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1238                                 op_append_elem(OP_LIST, $3, scalar($1)));
1239                           DO_MAD({
1240                               OP* op = $$;
1241                               if (op->op_type == OP_CONST) { /* defeat const fold */
1242                                 op = (OP*)op->op_madprop->mad_val;
1243                               }
1244                               token_getmad($2,op,'(');
1245                               token_getmad($4,op,')');
1246                           })
1247                         }
1248         |       NOAMP subname optlistexpr               /* foo(@args) */
1249                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1250                             op_append_elem(OP_LIST, $3, scalar($2)));
1251                           TOKEN_GETMAD($1,$$,'o');
1252                         }
1253         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1254                         { $$ = newOP(IVAL($1), OPf_SPECIAL);
1255                             PL_hints |= HINT_BLOCK_SCOPE;
1256                           TOKEN_GETMAD($1,$$,'o');
1257                         }
1258         |       LOOPEX term
1259                         { $$ = newLOOPEX(IVAL($1),$2);
1260                           TOKEN_GETMAD($1,$$,'o');
1261                         }
1262         |       NOTOP listexpr                       /* not $foo */
1263                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1264                           TOKEN_GETMAD($1,$$,'o');
1265                         }
1266         |       UNIOP                                /* Unary op, $_ implied */
1267                         { $$ = newOP(IVAL($1), 0);
1268                           TOKEN_GETMAD($1,$$,'o');
1269                         }
1270         |       UNIOP block                          /* eval { foo }* */
1271                         { $$ = newUNOP(IVAL($1), 0, $2);
1272                           TOKEN_GETMAD($1,$$,'o');
1273                         }
1274         |       UNIOP term                           /* Unary op */
1275                         { $$ = newUNOP(IVAL($1), 0, $2);
1276                           TOKEN_GETMAD($1,$$,'o');
1277                         }
1278         |       REQUIRE                              /* require, $_ implied */
1279                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1280                           TOKEN_GETMAD($1,$$,'o');
1281                         }
1282         |       REQUIRE term                         /* require Foo */
1283                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1284                           TOKEN_GETMAD($1,$$,'o');
1285                         }
1286         |       UNIOPSUB
1287                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1288         |       UNIOPSUB term                        /* Sub treated as unop */
1289                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1290                             op_append_elem(OP_LIST, $2, scalar($1))); }
1291         |       FUNC0                                /* Nullary operator */
1292                         { $$ = newOP(IVAL($1), 0);
1293                           TOKEN_GETMAD($1,$$,'o');
1294                         }
1295         |       FUNC0 '(' ')'
1296                         { $$ = newOP(IVAL($1), 0);
1297                           TOKEN_GETMAD($1,$$,'o');
1298                           TOKEN_GETMAD($2,$$,'(');
1299                           TOKEN_GETMAD($3,$$,')');
1300                         }
1301         |       FUNC0OP       /* Same as above, but op created in toke.c */
1302                         { $$ = $1; }
1303         |       FUNC0OP '(' ')'
1304                         { $$ = $1;
1305                           TOKEN_GETMAD($2,$$,'(');
1306                           TOKEN_GETMAD($3,$$,')');
1307                         }
1308         |       FUNC0SUB                             /* Sub treated as nullop */
1309                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1310                                 scalar($1)); }
1311         |       FUNC1 '(' ')'                        /* not () */
1312                         { $$ = (IVAL($1) == OP_NOT)
1313                             ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1314                             : newOP(IVAL($1), OPf_SPECIAL);
1315
1316                           TOKEN_GETMAD($1,$$,'o');
1317                           TOKEN_GETMAD($2,$$,'(');
1318                           TOKEN_GETMAD($3,$$,')');
1319                         }
1320         |       FUNC1 '(' expr ')'                   /* not($foo) */
1321                         { $$ = newUNOP(IVAL($1), 0, $3);
1322                           TOKEN_GETMAD($1,$$,'o');
1323                           TOKEN_GETMAD($2,$$,'(');
1324                           TOKEN_GETMAD($4,$$,')');
1325                         }
1326         |       PMFUNC /* m//, s///, qr//, tr/// */
1327                         {
1328                             if (   $1->op_type != OP_TRANS
1329                                 && $1->op_type != OP_TRANSR
1330                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1331                             {
1332                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1333                                 SAVEFREESV(PL_compcv);
1334                             } else
1335                                 $<ival>$ = 0;
1336                         }
1337                     '(' listexpr ')'
1338                         { $$ = pmruntime($1, $4, 1, $<ival>2);
1339                           TOKEN_GETMAD($3,$$,'(');
1340                           TOKEN_GETMAD($5,$$,')');
1341                         }
1342         |       WORD
1343         |       listop
1344         |       YADAYADA
1345                         {
1346                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1347                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1348                           TOKEN_GETMAD($1,$$,'X');
1349                         }
1350         |       PLUGEXPR
1351         ;
1352
1353 /* "my" declarations, with optional attributes */
1354 myattrterm:     MY myterm myattrlist
1355                         { $$ = my_attrs($2,$3);
1356                           DO_MAD(
1357                               token_getmad($1,$$,'d');
1358                               append_madprops($3->op_madprop, $$, 'a');
1359                               $3->op_madprop = 0;
1360                           )
1361                         }
1362         |       MY myterm
1363                         { $$ = localize($2,IVAL($1));
1364                           TOKEN_GETMAD($1,$$,'d');
1365                         }
1366         ;
1367
1368 /* Things that can be "my"'d */
1369 myterm  :       '(' expr ')'
1370                         { $$ = sawparens($2);
1371                           TOKEN_GETMAD($1,$$,'(');
1372                           TOKEN_GETMAD($3,$$,')');
1373                         }
1374         |       '(' ')'
1375                         { $$ = sawparens(newNULLLIST());
1376                           TOKEN_GETMAD($1,$$,'(');
1377                           TOKEN_GETMAD($2,$$,')');
1378                         }
1379         |       scalar  %prec '('
1380                         { $$ = $1; }
1381         |       hsh     %prec '('
1382                         { $$ = $1; }
1383         |       ary     %prec '('
1384                         { $$ = $1; }
1385         ;
1386
1387 /* Basic list expressions */
1388 optlistexpr:    /* NULL */ %prec PREC_LOW
1389                         { $$ = (OP*)NULL; }
1390         |       listexpr    %prec PREC_LOW
1391                         { $$ = $1; }
1392         ;
1393
1394 optexpr:        /* NULL */
1395                         { $$ = (OP*)NULL; }
1396         |       expr
1397                         { $$ = $1; }
1398         ;
1399
1400 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1401    lexical */
1402 my_scalar:      scalar
1403                         { PL_parser->in_my = 0; $$ = my($1); }
1404         ;
1405
1406 amper   :       '&' indirob
1407                         { $$ = newCVREF(IVAL($1),$2);
1408                           TOKEN_GETMAD($1,$$,'&');
1409                         }
1410         ;
1411
1412 scalar  :       '$' indirob
1413                         { $$ = newSVREF($2);
1414                           TOKEN_GETMAD($1,$$,'$');
1415                         }
1416         ;
1417
1418 ary     :       '@' indirob
1419                         { $$ = newAVREF($2);
1420                           TOKEN_GETMAD($1,$$,'@');
1421                         }
1422         ;
1423
1424 hsh     :       '%' indirob
1425                         { $$ = newHVREF($2);
1426                           TOKEN_GETMAD($1,$$,'%');
1427                         }
1428         ;
1429
1430 arylen  :       DOLSHARP indirob
1431                         { $$ = newAVREF($2);
1432                           TOKEN_GETMAD($1,$$,'l');
1433                         }
1434         ;
1435
1436 star    :       '*' indirob
1437                         { $$ = newGVREF(0,$2);
1438                           TOKEN_GETMAD($1,$$,'*');
1439                         }
1440         ;
1441
1442 /* Indirect objects */
1443 indirob :       WORD
1444                         { $$ = scalar($1); }
1445         |       scalar %prec PREC_LOW
1446                         { $$ = scalar($1); }
1447         |       block
1448                         { $$ = op_scope($1); }
1449
1450         |       PRIVATEREF
1451                         { $$ = $1; }
1452         ;