This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Allocate ‘our sub’ in the pad
[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 WORD startsub
318                         { const char *const name = SvPV_nolen_const(((SVOP*)$2)->op_sv);
319                           if (strEQ(name, "BEGIN") || strEQ(name, "END")
320                               || strEQ(name, "INIT") || strEQ(name, "CHECK")
321                               || strEQ(name, "UNITCHECK"))
322                               CvSPECIAL_on(PL_compcv);
323                           PL_parser->in_my = 0;
324                           PL_parser->in_my_stash = NULL;
325                         }
326                 proto subattrlist subbody
327                         {
328                           SvREFCNT_inc_simple_void(PL_compcv);
329 #ifdef MAD
330                           {
331                               OP* o = newSVOP(OP_ANONCODE, 0,
332                                 (SV*)newATTRSUB($3, $2, $5, $6, $7));
333                               $$ = newOP(OP_NULL,0);
334                               op_getmad(o,$$,'&');
335                               op_getmad($2,$$,'n');
336                               op_getmad($5,$$,'s');
337                               op_getmad($6,$$,'a');
338                               token_getmad($1,$$,'d');
339                               append_madprops($7->op_madprop, $$, 0);
340                               $7->op_madprop = 0;
341                           }
342 #else
343                           newATTRSUB($3, $2, $5, $6, $7);
344                           $$ = (OP*)NULL;
345 #endif
346                           intro_my();
347                         }
348         |       MYSUB startsub subname proto subattrlist subbody
349                         {
350                           /* Unimplemented "my sub foo { }" */
351                           SvREFCNT_inc_simple_void(PL_compcv);
352 #ifdef MAD
353                           $$ = newMYSUB($2, $3, $4, $5, $6);
354                           token_getmad($1,$$,'d');
355 #else
356                           newMYSUB($2, $3, $4, $5, $6);
357                           $$ = (OP*)NULL;
358 #endif
359                         }
360         |       PACKAGE WORD WORD ';'
361                         {
362 #ifdef MAD
363                           $$ = package($3);
364                           token_getmad($1,$$,'o');
365                           if ($2)
366                               package_version($2);
367                           token_getmad($4,$$,';');
368 #else
369                           package($3);
370                           if ($2)
371                               package_version($2);
372                           $$ = (OP*)NULL;
373 #endif
374                         }
375         |       USE startsub
376                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
377                 WORD WORD optlistexpr ';'
378                         {
379                           SvREFCNT_inc_simple_void(PL_compcv);
380 #ifdef MAD
381                           $$ = utilize(IVAL($1), $2, $4, $5, $6);
382                           token_getmad($1,$$,'o');
383                           token_getmad($7,$$,';');
384                           if (PL_parser->rsfp_filters &&
385                                       AvFILLp(PL_parser->rsfp_filters) >= 0)
386                               append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
387 #else
388                           utilize(IVAL($1), $2, $4, $5, $6);
389                           $$ = (OP*)NULL;
390 #endif
391                         }
392         |       IF '(' remember mexpr ')' mblock else
393                         {
394                           $$ = block_end($3,
395                               newCONDOP(0, $4, op_scope($6), $7));
396                           TOKEN_GETMAD($1,$$,'I');
397                           TOKEN_GETMAD($2,$$,'(');
398                           TOKEN_GETMAD($5,$$,')');
399                           PL_parser->copline = (line_t)IVAL($1);
400                         }
401         |       UNLESS '(' remember miexpr ')' mblock else
402                         {
403                           $$ = block_end($3,
404                               newCONDOP(0, $4, op_scope($6), $7));
405                           TOKEN_GETMAD($1,$$,'I');
406                           TOKEN_GETMAD($2,$$,'(');
407                           TOKEN_GETMAD($5,$$,')');
408                           PL_parser->copline = (line_t)IVAL($1);
409                         }
410         |       GIVEN '(' remember mexpr ')' mblock
411                         {
412                           const PADOFFSET offset = pad_findmy_pvs("$_", 0);
413                           $$ = block_end($3,
414                                   newGIVENOP($4, op_scope($6),
415                                     offset == NOT_IN_PAD
416                                     || PAD_COMPNAME_FLAGS_isOUR(offset)
417                                       ? 0
418                                       : offset));
419                           PL_parser->copline = (line_t)IVAL($1);
420                         }
421         |       WHEN '(' remember mexpr ')' mblock
422                         { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
423         |       DEFAULT block
424                         { $$ = newWHENOP(0, op_scope($2)); }
425         |       WHILE '(' remember texpr ')' mintro mblock cont
426                         {
427                           $$ = block_end($3,
428                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
429                                       $4, $7, $8, $6));
430                           TOKEN_GETMAD($1,$$,'W');
431                           TOKEN_GETMAD($2,$$,'(');
432                           TOKEN_GETMAD($5,$$,')');
433                           PL_parser->copline = (line_t)IVAL($1);
434                         }
435         |       UNTIL '(' remember iexpr ')' mintro mblock cont
436                         {
437                           $$ = block_end($3,
438                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
439                                       $4, $7, $8, $6));
440                           TOKEN_GETMAD($1,$$,'W');
441                           TOKEN_GETMAD($2,$$,'(');
442                           TOKEN_GETMAD($5,$$,')');
443                           PL_parser->copline = (line_t)IVAL($1);
444                         }
445         |       FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
446                 mblock
447                         {
448                           OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
449                           OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
450                                       scalar($6), $11, $9, $8);
451                           if (initop) {
452                               forop = op_prepend_elem(OP_LINESEQ, initop,
453                                   op_append_elem(OP_LINESEQ,
454                                       newOP(OP_UNSTACK, OPf_SPECIAL),
455                                       forop));
456                           }
457                           DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
458                           $$ = block_end($3, forop);
459                           TOKEN_GETMAD($1,$$,'3');
460                           TOKEN_GETMAD($2,$$,'(');
461                           TOKEN_GETMAD($5,$$,'1');
462                           TOKEN_GETMAD($7,$$,'2');
463                           TOKEN_GETMAD($10,$$,')');
464                           PL_parser->copline = (line_t)IVAL($1);
465                         }
466         |       FOR MY remember my_scalar '(' mexpr ')' mblock cont
467                         {
468                           $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
469                           TOKEN_GETMAD($1,$$,'W');
470                           TOKEN_GETMAD($2,$$,'d');
471                           TOKEN_GETMAD($5,$$,'(');
472                           TOKEN_GETMAD($7,$$,')');
473                           PL_parser->copline = (line_t)IVAL($1);
474                         }
475         |       FOR scalar '(' remember mexpr ')' mblock cont
476                         {
477                           $$ = block_end($4, newFOROP(0,
478                                       op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
479                           TOKEN_GETMAD($1,$$,'W');
480                           TOKEN_GETMAD($3,$$,'(');
481                           TOKEN_GETMAD($6,$$,')');
482                           PL_parser->copline = (line_t)IVAL($1);
483                         }
484         |       FOR '(' remember mexpr ')' mblock cont
485                         {
486                           $$ = block_end($3,
487                                   newFOROP(0, (OP*)NULL, $4, $6, $7));
488                           TOKEN_GETMAD($1,$$,'W');
489                           TOKEN_GETMAD($2,$$,'(');
490                           TOKEN_GETMAD($5,$$,')');
491                           PL_parser->copline = (line_t)IVAL($1);
492                         }
493         |       block cont
494                         {
495                           /* a block is a loop that happens once */
496                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
497                                   (OP*)NULL, $1, $2, 0);
498                         }
499         |       PACKAGE WORD WORD '{' remember
500                         {
501                           package($3);
502                           if ($2) {
503                               package_version($2);
504                           }
505                         }
506                 stmtseq '}'
507                         {
508                           /* a block is a loop that happens once */
509                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
510                                   (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
511                           TOKEN_GETMAD($4,$$,'{');
512                           TOKEN_GETMAD($8,$$,'}');
513                           if (PL_parser->copline > (line_t)IVAL($4))
514                               PL_parser->copline = (line_t)IVAL($4);
515                         }
516         |       sideff ';'
517                         {
518                           PL_parser->expect = XSTATE;
519                           $$ = $1;
520                           TOKEN_GETMAD($2,$$,';');
521                         }
522         |       ';'
523                         {
524                           PL_parser->expect = XSTATE;
525                           $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
526                           TOKEN_GETMAD($1,$$,';');
527                           PL_parser->copline = NOLINE;
528                         }
529         ;
530
531 /* Format line */
532 formline:       THING formarg
533                         { OP *list;
534                           if ($2) {
535                               OP *term = $2;
536                               DO_MAD(term = newUNOP(OP_NULL, 0, term));
537                               list = op_append_elem(OP_LIST, $1, term);
538                           }
539                           else {
540 #ifdef MAD
541                               OP *op = newNULLLIST();
542                               list = op_append_elem(OP_LIST, $1, op);
543 #else
544                               list = $1;
545 #endif
546                           }
547                           if (PL_parser->copline == NOLINE)
548                                PL_parser->copline = CopLINE(PL_curcop)-1;
549                           else PL_parser->copline--;
550                           $$ = newSTATEOP(0, NULL,
551                                           convert(OP_FORMLINE, 0, list));
552                         }
553         ;
554
555 formarg :       /* NULL */
556                         { $$ = NULL; }
557         |       FORMLBRACK stmtseq FORMRBRACK
558                         { $$ = op_unscope($2); }
559         ;
560
561 /* An expression which may have a side-effect */
562 sideff  :       error
563                         { $$ = (OP*)NULL; }
564         |       expr
565                         { $$ = $1; }
566         |       expr IF expr
567                         { $$ = newLOGOP(OP_AND, 0, $3, $1);
568                           TOKEN_GETMAD($2,$$,'i');
569                         }
570         |       expr UNLESS expr
571                         { $$ = newLOGOP(OP_OR, 0, $3, $1);
572                           TOKEN_GETMAD($2,$$,'i');
573                         }
574         |       expr WHILE expr
575                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
576                           TOKEN_GETMAD($2,$$,'w');
577                         }
578         |       expr UNTIL iexpr
579                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
580                           TOKEN_GETMAD($2,$$,'w');
581                         }
582         |       expr FOR expr
583                         { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
584                           TOKEN_GETMAD($2,$$,'w');
585                           PL_parser->copline = (line_t)IVAL($2);
586                         }
587         |       expr WHEN expr
588                         { $$ = newWHENOP($3, op_scope($1)); }
589         ;
590
591 /* else and elsif blocks */
592 else    :       /* NULL */
593                         { $$ = (OP*)NULL; }
594         |       ELSE mblock
595                         {
596                           ($2)->op_flags |= OPf_PARENS;
597                           $$ = op_scope($2);
598                           TOKEN_GETMAD($1,$$,'o');
599                         }
600         |       ELSIF '(' mexpr ')' mblock else
601                         { PL_parser->copline = (line_t)IVAL($1);
602                             $$ = newCONDOP(0,
603                                 newSTATEOP(OPf_SPECIAL,NULL,$3),
604                                 op_scope($5), $6);
605                           PL_hints |= HINT_BLOCK_SCOPE;
606                           TOKEN_GETMAD($1,$$,'I');
607                           TOKEN_GETMAD($2,$$,'(');
608                           TOKEN_GETMAD($4,$$,')');
609                         }
610         ;
611
612 /* Continue blocks */
613 cont    :       /* NULL */
614                         { $$ = (OP*)NULL; }
615         |       CONTINUE block
616                         {
617                           $$ = op_scope($2);
618                           TOKEN_GETMAD($1,$$,'o');
619                         }
620         ;
621
622 /* determine whether there are any new my declarations */
623 mintro  :       /* NULL */
624                         { $$ = (PL_min_intro_pending &&
625                             PL_max_intro_pending >=  PL_min_intro_pending);
626                           intro_my(); }
627
628 /* Normal expression */
629 nexpr   :       /* NULL */
630                         { $$ = (OP*)NULL; }
631         |       sideff
632         ;
633
634 /* Boolean expression */
635 texpr   :       /* NULL means true */
636                         { YYSTYPE tmplval;
637                           (void)scan_num("1", &tmplval);
638                           $$ = tmplval.opval; }
639         |       expr
640         ;
641
642 /* Inverted boolean expression */
643 iexpr   :       expr
644                         { $$ = invert(scalar($1)); }
645         ;
646
647 /* Expression with its own lexical scope */
648 mexpr   :       expr
649                         { $$ = $1; intro_my(); }
650         ;
651
652 mnexpr  :       nexpr
653                         { $$ = $1; intro_my(); }
654         ;
655
656 miexpr  :       iexpr
657                         { $$ = $1; intro_my(); }
658         ;
659
660 formname:       WORD            { $$ = $1; }
661         |       /* NULL */      { $$ = (OP*)NULL; }
662         ;
663
664 startsub:       /* NULL */      /* start a regular subroutine scope */
665                         { $$ = start_subparse(FALSE, 0);
666                             SAVEFREESV(PL_compcv); }
667
668         ;
669
670 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
671                         { $$ = start_subparse(FALSE, CVf_ANON);
672                             SAVEFREESV(PL_compcv); }
673         ;
674
675 startformsub:   /* NULL */      /* start a format subroutine scope */
676                         { $$ = start_subparse(TRUE, 0);
677                             SAVEFREESV(PL_compcv); }
678         ;
679
680 /* Name of a subroutine - must be a bareword, could be special */
681 subname :       WORD    { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
682                           if (strEQ(name, "BEGIN") || strEQ(name, "END")
683                               || strEQ(name, "INIT") || strEQ(name, "CHECK")
684                               || strEQ(name, "UNITCHECK"))
685                               CvSPECIAL_on(PL_compcv);
686                           $$ = $1; }
687         ;
688
689 /* Subroutine prototype */
690 proto   :       /* NULL */
691                         { $$ = (OP*)NULL; }
692         |       THING
693         ;
694
695 /* Optional list of subroutine attributes */
696 subattrlist:    /* NULL */
697                         { $$ = (OP*)NULL; }
698         |       COLONATTR THING
699                         { $$ = $2;
700                           TOKEN_GETMAD($1,$$,':');
701                         }
702         |       COLONATTR
703                         { $$ = IF_MAD(
704                                     newOP(OP_NULL, 0),
705                                     (OP*)NULL
706                                 );
707                           TOKEN_GETMAD($1,$$,':');
708                         }
709         ;
710
711 /* List of attributes for a "my" variable declaration */
712 myattrlist:     COLONATTR THING
713                         { $$ = $2;
714                           TOKEN_GETMAD($1,$$,':');
715                         }
716         |       COLONATTR
717                         { $$ = IF_MAD(
718                                     newOP(OP_NULL, 0),
719                                     (OP*)NULL
720                                 );
721                           TOKEN_GETMAD($1,$$,':');
722                         }
723         ;
724
725 /* Subroutine body - either null or a block */
726 subbody :       block   { $$ = $1; }
727         |       ';'     { $$ = IF_MAD(
728                                     newOP(OP_NULL,0),
729                                     (OP*)NULL
730                                 );
731                           PL_parser->expect = XSTATE;
732                           TOKEN_GETMAD($1,$$,';');
733                         }
734         ;
735
736 /* Ordinary expressions; logical combinations */
737 expr    :       expr ANDOP expr
738                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
739                           TOKEN_GETMAD($2,$$,'o');
740                         }
741         |       expr OROP expr
742                         { $$ = newLOGOP(IVAL($2), 0, $1, $3);
743                           TOKEN_GETMAD($2,$$,'o');
744                         }
745         |       expr DOROP expr
746                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
747                           TOKEN_GETMAD($2,$$,'o');
748                         }
749         |       listexpr %prec PREC_LOW
750         ;
751
752 /* Expressions are a list of terms joined by commas */
753 listexpr:       listexpr ','
754                         {
755 #ifdef MAD
756                           OP* op = newNULLLIST();
757                           token_getmad($2,op,',');
758                           $$ = op_append_elem(OP_LIST, $1, op);
759 #else
760                           $$ = $1;
761 #endif
762                         }
763         |       listexpr ',' term
764                         { 
765                           OP* term = $3;
766                           DO_MAD(
767                               term = newUNOP(OP_NULL, 0, term);
768                               token_getmad($2,term,',');
769                           )
770                           $$ = op_append_elem(OP_LIST, $1, term);
771                         }
772         |       term %prec PREC_LOW
773         ;
774
775 /* List operators */
776 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
777                         { $$ = convert(IVAL($1), OPf_STACKED,
778                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
779                           TOKEN_GETMAD($1,$$,'o');
780                         }
781         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
782                         { $$ = convert(IVAL($1), OPf_STACKED,
783                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
784                           TOKEN_GETMAD($1,$$,'o');
785                           TOKEN_GETMAD($2,$$,'(');
786                           TOKEN_GETMAD($5,$$,')');
787                         }
788         |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
789                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
790                                 op_append_elem(OP_LIST,
791                                     op_prepend_elem(OP_LIST, scalar($1), $5),
792                                     newUNOP(OP_METHOD, 0, $3)));
793                           TOKEN_GETMAD($2,$$,'A');
794                           TOKEN_GETMAD($4,$$,'(');
795                           TOKEN_GETMAD($6,$$,')');
796                         }
797         |       term ARROW method                     /* $foo->bar */
798                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
799                                 op_append_elem(OP_LIST, scalar($1),
800                                     newUNOP(OP_METHOD, 0, $3)));
801                           TOKEN_GETMAD($2,$$,'A');
802                         }
803         |       METHOD indirob optlistexpr           /* new Class @args */
804                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
805                                 op_append_elem(OP_LIST,
806                                     op_prepend_elem(OP_LIST, $2, $3),
807                                     newUNOP(OP_METHOD, 0, $1)));
808                         }
809         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
810                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
811                                 op_append_elem(OP_LIST,
812                                     op_prepend_elem(OP_LIST, $2, $4),
813                                     newUNOP(OP_METHOD, 0, $1)));
814                           TOKEN_GETMAD($3,$$,'(');
815                           TOKEN_GETMAD($5,$$,')');
816                         }
817         |       LSTOP optlistexpr                    /* print @args */
818                         { $$ = convert(IVAL($1), 0, $2);
819                           TOKEN_GETMAD($1,$$,'o');
820                         }
821         |       FUNC '(' optexpr ')'                 /* print (@args) */
822                         { $$ = convert(IVAL($1), 0, $3);
823                           TOKEN_GETMAD($1,$$,'o');
824                           TOKEN_GETMAD($2,$$,'(');
825                           TOKEN_GETMAD($4,$$,')');
826                         }
827         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
828                         { SvREFCNT_inc_simple_void(PL_compcv);
829                           $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
830                     optlistexpr         %prec LSTOP  /* ... @bar */
831                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
832                                  op_append_elem(OP_LIST,
833                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
834                         }
835         ;
836
837 /* Names of methods. May use $object->$methodname */
838 method  :       METHOD
839         |       scalar
840         ;
841
842 /* Some kind of subscripted expression */
843 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
844                         /* In this and all the hash accessors, ';' is
845                          * provided by the tokeniser */
846                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
847                             PL_parser->expect = XOPERATOR;
848                           TOKEN_GETMAD($2,$$,'{');
849                           TOKEN_GETMAD($4,$$,';');
850                           TOKEN_GETMAD($5,$$,'}');
851                         }
852         |       scalar '[' expr ']'          /* $array[$element] */
853                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
854                           TOKEN_GETMAD($2,$$,'[');
855                           TOKEN_GETMAD($4,$$,']');
856                         }
857         |       term ARROW '[' expr ']'      /* somearef->[$element] */
858                         { $$ = newBINOP(OP_AELEM, 0,
859                                         ref(newAVREF($1),OP_RV2AV),
860                                         scalar($4));
861                           TOKEN_GETMAD($2,$$,'a');
862                           TOKEN_GETMAD($3,$$,'[');
863                           TOKEN_GETMAD($5,$$,']');
864                         }
865         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
866                         { $$ = newBINOP(OP_AELEM, 0,
867                                         ref(newAVREF($1),OP_RV2AV),
868                                         scalar($3));
869                           TOKEN_GETMAD($2,$$,'[');
870                           TOKEN_GETMAD($4,$$,']');
871                         }
872         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
873                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
874                             PL_parser->expect = XOPERATOR;
875                           TOKEN_GETMAD($2,$$,'{');
876                           TOKEN_GETMAD($4,$$,';');
877                           TOKEN_GETMAD($5,$$,'}');
878                         }
879         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
880                         { $$ = newBINOP(OP_HELEM, 0,
881                                         ref(newHVREF($1),OP_RV2HV),
882                                         jmaybe($4));
883                             PL_parser->expect = XOPERATOR;
884                           TOKEN_GETMAD($2,$$,'a');
885                           TOKEN_GETMAD($3,$$,'{');
886                           TOKEN_GETMAD($5,$$,';');
887                           TOKEN_GETMAD($6,$$,'}');
888                         }
889         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
890                         { $$ = newBINOP(OP_HELEM, 0,
891                                         ref(newHVREF($1),OP_RV2HV),
892                                         jmaybe($3));
893                             PL_parser->expect = XOPERATOR;
894                           TOKEN_GETMAD($2,$$,'{');
895                           TOKEN_GETMAD($4,$$,';');
896                           TOKEN_GETMAD($5,$$,'}');
897                         }
898         |       term ARROW '(' ')'          /* $subref->() */
899                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
900                                    newCVREF(0, scalar($1)));
901                           TOKEN_GETMAD($2,$$,'a');
902                           TOKEN_GETMAD($3,$$,'(');
903                           TOKEN_GETMAD($4,$$,')');
904                         }
905         |       term ARROW '(' expr ')'     /* $subref->(@args) */
906                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
907                                    op_append_elem(OP_LIST, $4,
908                                        newCVREF(0, scalar($1))));
909                           TOKEN_GETMAD($2,$$,'a');
910                           TOKEN_GETMAD($3,$$,'(');
911                           TOKEN_GETMAD($5,$$,')');
912                         }
913
914         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
915                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
916                                    op_append_elem(OP_LIST, $3,
917                                                newCVREF(0, scalar($1))));
918                           TOKEN_GETMAD($2,$$,'(');
919                           TOKEN_GETMAD($4,$$,')');
920                         }
921         |       subscripted '(' ')'        /* $foo->{bar}->() */
922                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
923                                    newCVREF(0, scalar($1)));
924                           TOKEN_GETMAD($2,$$,'(');
925                           TOKEN_GETMAD($3,$$,')');
926                         }
927         |       '(' expr ')' '[' expr ']'            /* list slice */
928                         { $$ = newSLICEOP(0, $5, $2);
929                           TOKEN_GETMAD($1,$$,'(');
930                           TOKEN_GETMAD($3,$$,')');
931                           TOKEN_GETMAD($4,$$,'[');
932                           TOKEN_GETMAD($6,$$,']');
933                         }
934         |       QWLIST '[' expr ']'            /* list literal slice */
935                         { $$ = newSLICEOP(0, $3, $1);
936                           TOKEN_GETMAD($2,$$,'[');
937                           TOKEN_GETMAD($4,$$,']');
938                         }
939         |       '(' ')' '[' expr ']'                 /* empty list slice! */
940                         { $$ = newSLICEOP(0, $4, (OP*)NULL);
941                           TOKEN_GETMAD($1,$$,'(');
942                           TOKEN_GETMAD($2,$$,')');
943                           TOKEN_GETMAD($3,$$,'[');
944                           TOKEN_GETMAD($5,$$,']');
945                         }
946     ;
947
948 /* Binary operators between terms */
949 termbinop:      term ASSIGNOP term                     /* $x = $y */
950                         { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
951                           TOKEN_GETMAD($2,$$,'o');
952                         }
953         |       term POWOP term                        /* $x ** $y */
954                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
955                           TOKEN_GETMAD($2,$$,'o');
956                         }
957         |       term MULOP term                        /* $x * $y, $x x $y */
958                         {   if (IVAL($2) != OP_REPEAT)
959                                 scalar($1);
960                             $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
961                           TOKEN_GETMAD($2,$$,'o');
962                         }
963         |       term ADDOP term                        /* $x + $y */
964                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
965                           TOKEN_GETMAD($2,$$,'o');
966                         }
967         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
968                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
969                           TOKEN_GETMAD($2,$$,'o');
970                         }
971         |       term RELOP term                        /* $x > $y, etc. */
972                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
973                           TOKEN_GETMAD($2,$$,'o');
974                         }
975         |       term EQOP term                         /* $x == $y, $x eq $y */
976                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
977                           TOKEN_GETMAD($2,$$,'o');
978                         }
979         |       term BITANDOP term                     /* $x & $y */
980                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
981                           TOKEN_GETMAD($2,$$,'o');
982                         }
983         |       term BITOROP term                      /* $x | $y */
984                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
985                           TOKEN_GETMAD($2,$$,'o');
986                         }
987         |       term DOTDOT term                       /* $x..$y, $x...$y */
988                         {
989                           $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
990                           DO_MAD({
991                               UNOP *op;
992                               op = (UNOP*)$$;
993                               op = (UNOP*)op->op_first; /* get to flop */
994                               op = (UNOP*)op->op_first; /* get to flip */
995                               op = (UNOP*)op->op_first; /* get to range */
996                               token_getmad($2,(OP*)op,'o');
997                             })
998                         }
999         |       term ANDAND term                       /* $x && $y */
1000                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
1001                           TOKEN_GETMAD($2,$$,'o');
1002                         }
1003         |       term OROR term                         /* $x || $y */
1004                         { $$ = newLOGOP(OP_OR, 0, $1, $3);
1005                           TOKEN_GETMAD($2,$$,'o');
1006                         }
1007         |       term DORDOR term                       /* $x // $y */
1008                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1009                           TOKEN_GETMAD($2,$$,'o');
1010                         }
1011         |       term MATCHOP term                      /* $x =~ /$y/ */
1012                         { $$ = bind_match(IVAL($2), $1, $3);
1013                           TOKEN_GETMAD($2,
1014                                 ($$->op_type == OP_NOT
1015                                     ? ((UNOP*)$$)->op_first : $$),
1016                                 '~');
1017                         }
1018     ;
1019
1020 /* Unary operators and terms */
1021 termunop : '-' term %prec UMINUS                       /* -$x */
1022                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1023                           TOKEN_GETMAD($1,$$,'o');
1024                         }
1025         |       '+' term %prec UMINUS                  /* +$x */
1026                         { $$ = IF_MAD(
1027                                     newUNOP(OP_NULL, 0, $2),
1028                                     $2
1029                                 );
1030                           TOKEN_GETMAD($1,$$,'+');
1031                         }
1032         |       '!' term                               /* !$x */
1033                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1034                           TOKEN_GETMAD($1,$$,'o');
1035                         }
1036         |       '~' term                               /* ~$x */
1037                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1038                           TOKEN_GETMAD($1,$$,'o');
1039                         }
1040         |       term POSTINC                           /* $x++ */
1041                         { $$ = newUNOP(OP_POSTINC, 0,
1042                                         op_lvalue(scalar($1), OP_POSTINC));
1043                           TOKEN_GETMAD($2,$$,'o');
1044                         }
1045         |       term POSTDEC                           /* $x-- */
1046                         { $$ = newUNOP(OP_POSTDEC, 0,
1047                                         op_lvalue(scalar($1), OP_POSTDEC));
1048                           TOKEN_GETMAD($2,$$,'o');
1049                         }
1050         |       PREINC term                            /* ++$x */
1051                         { $$ = newUNOP(OP_PREINC, 0,
1052                                         op_lvalue(scalar($2), OP_PREINC));
1053                           TOKEN_GETMAD($1,$$,'o');
1054                         }
1055         |       PREDEC term                            /* --$x */
1056                         { $$ = newUNOP(OP_PREDEC, 0,
1057                                         op_lvalue(scalar($2), OP_PREDEC));
1058                           TOKEN_GETMAD($1,$$,'o');
1059                         }
1060
1061     ;
1062
1063 /* Constructors for anonymous data */
1064 anonymous:      '[' expr ']'
1065                         { $$ = newANONLIST($2);
1066                           TOKEN_GETMAD($1,$$,'[');
1067                           TOKEN_GETMAD($3,$$,']');
1068                         }
1069         |       '[' ']'
1070                         { $$ = newANONLIST((OP*)NULL);
1071                           TOKEN_GETMAD($1,$$,'[');
1072                           TOKEN_GETMAD($2,$$,']');
1073                         }
1074         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
1075                         { $$ = newANONHASH($2);
1076                           TOKEN_GETMAD($1,$$,'{');
1077                           TOKEN_GETMAD($3,$$,';');
1078                           TOKEN_GETMAD($4,$$,'}');
1079                         }
1080         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
1081                         { $$ = newANONHASH((OP*)NULL);
1082                           TOKEN_GETMAD($1,$$,'{');
1083                           TOKEN_GETMAD($2,$$,';');
1084                           TOKEN_GETMAD($3,$$,'}');
1085                         }
1086         |       ANONSUB startanonsub proto subattrlist block    %prec '('
1087                         { SvREFCNT_inc_simple_void(PL_compcv);
1088                           $$ = newANONATTRSUB($2, $3, $4, $5);
1089                           TOKEN_GETMAD($1,$$,'o');
1090                           OP_GETMAD($3,$$,'s');
1091                           OP_GETMAD($4,$$,'a');
1092                         }
1093
1094     ;
1095
1096 /* Things called with "do" */
1097 termdo  :       DO term %prec UNIOP                     /* do $filename */
1098                         { $$ = dofile($2, IVAL($1));
1099                           TOKEN_GETMAD($1,$$,'o');
1100                         }
1101         |       DO block        %prec '('               /* do { code */
1102                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
1103                           TOKEN_GETMAD($1,$$,'D');
1104                         }
1105         |       DO WORD '(' ')'                  /* do somesub() */
1106                         { $$ = newUNOP(OP_ENTERSUB,
1107                             OPf_SPECIAL|OPf_STACKED,
1108                             op_prepend_elem(OP_LIST,
1109                                 scalar(newCVREF(
1110                                     (OPpENTERSUB_AMPER<<8),
1111                                     scalar($2)
1112                                 )),(OP*)NULL)); dep();
1113                           TOKEN_GETMAD($1,$$,'o');
1114                           TOKEN_GETMAD($3,$$,'(');
1115                           TOKEN_GETMAD($4,$$,')');
1116                         }
1117         |       DO WORD '(' expr ')'             /* do somesub(@args) */
1118                         { $$ = newUNOP(OP_ENTERSUB,
1119                             OPf_SPECIAL|OPf_STACKED,
1120                             op_append_elem(OP_LIST,
1121                                 $4,
1122                                 scalar(newCVREF(
1123                                     (OPpENTERSUB_AMPER<<8),
1124                                     scalar($2)
1125                                 )))); dep();
1126                           TOKEN_GETMAD($1,$$,'o');
1127                           TOKEN_GETMAD($3,$$,'(');
1128                           TOKEN_GETMAD($5,$$,')');
1129                         }
1130         |       DO scalar '(' ')'                /* do $subref () */
1131                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1132                             op_prepend_elem(OP_LIST,
1133                                 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1134                           TOKEN_GETMAD($1,$$,'o');
1135                           TOKEN_GETMAD($3,$$,'(');
1136                           TOKEN_GETMAD($4,$$,')');
1137                         }
1138         |       DO scalar '(' expr ')'           /* do $subref (@args) */
1139                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1140                             op_prepend_elem(OP_LIST,
1141                                 $4,
1142                                 scalar(newCVREF(0,scalar($2))))); dep();
1143                           TOKEN_GETMAD($1,$$,'o');
1144                           TOKEN_GETMAD($3,$$,'(');
1145                           TOKEN_GETMAD($5,$$,')');
1146                         }
1147
1148         ;
1149
1150 term    :       termbinop
1151         |       termunop
1152         |       anonymous
1153         |       termdo
1154         |       term '?' term ':' term
1155                         { $$ = newCONDOP(0, $1, $3, $5);
1156                           TOKEN_GETMAD($2,$$,'?');
1157                           TOKEN_GETMAD($4,$$,':');
1158                         }
1159         |       REFGEN term                          /* \$x, \@y, \%z */
1160                         { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
1161                           TOKEN_GETMAD($1,$$,'o');
1162                         }
1163         |       myattrterm      %prec UNIOP
1164                         { $$ = $1; }
1165         |       LOCAL term      %prec UNIOP
1166                         { $$ = localize($2,IVAL($1));
1167                           TOKEN_GETMAD($1,$$,'k');
1168                         }
1169         |       '(' expr ')'
1170                         { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1171                           TOKEN_GETMAD($1,$$,'(');
1172                           TOKEN_GETMAD($3,$$,')');
1173                         }
1174         |       QWLIST
1175                         { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
1176         |       '(' ')'
1177                         { $$ = sawparens(newNULLLIST());
1178                           TOKEN_GETMAD($1,$$,'(');
1179                           TOKEN_GETMAD($2,$$,')');
1180                         }
1181         |       scalar  %prec '('
1182                         { $$ = $1; }
1183         |       star    %prec '('
1184                         { $$ = $1; }
1185         |       hsh     %prec '('
1186                         { $$ = $1; }
1187         |       ary     %prec '('
1188                         { $$ = $1; }
1189         |       arylen  %prec '('                    /* $#x, $#{ something } */
1190                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1191         |       subscripted
1192                         { $$ = $1; }
1193         |       ary '[' expr ']'                     /* array slice */
1194                         { $$ = op_prepend_elem(OP_ASLICE,
1195                                 newOP(OP_PUSHMARK, 0),
1196                                     newLISTOP(OP_ASLICE, 0,
1197                                         list($3),
1198                                         ref($1, OP_ASLICE)));
1199                           TOKEN_GETMAD($2,$$,'[');
1200                           TOKEN_GETMAD($4,$$,']');
1201                         }
1202         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
1203                         { $$ = op_prepend_elem(OP_HSLICE,
1204                                 newOP(OP_PUSHMARK, 0),
1205                                     newLISTOP(OP_HSLICE, 0,
1206                                         list($3),
1207                                         ref(oopsHV($1), OP_HSLICE)));
1208                             PL_parser->expect = XOPERATOR;
1209                           TOKEN_GETMAD($2,$$,'{');
1210                           TOKEN_GETMAD($4,$$,';');
1211                           TOKEN_GETMAD($5,$$,'}');
1212                         }
1213         |       THING   %prec '('
1214                         { $$ = $1; }
1215         |       amper                                /* &foo; */
1216                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1217         |       amper '(' ')'                 /* &foo() */
1218                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1219                           TOKEN_GETMAD($2,$$,'(');
1220                           TOKEN_GETMAD($3,$$,')');
1221                         }
1222         |       amper '(' expr ')'            /* &foo(@args) */
1223                         {
1224                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1225                                 op_append_elem(OP_LIST, $3, scalar($1)));
1226                           DO_MAD({
1227                               OP* op = $$;
1228                               if (op->op_type == OP_CONST) { /* defeat const fold */
1229                                 op = (OP*)op->op_madprop->mad_val;
1230                               }
1231                               token_getmad($2,op,'(');
1232                               token_getmad($4,op,')');
1233                           })
1234                         }
1235         |       NOAMP WORD optlistexpr               /* foo(@args) */
1236                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1237                             op_append_elem(OP_LIST, $3, scalar($2)));
1238                           TOKEN_GETMAD($1,$$,'o');
1239                         }
1240         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1241                         { $$ = newOP(IVAL($1), OPf_SPECIAL);
1242                             PL_hints |= HINT_BLOCK_SCOPE;
1243                           TOKEN_GETMAD($1,$$,'o');
1244                         }
1245         |       LOOPEX term
1246                         { $$ = newLOOPEX(IVAL($1),$2);
1247                           TOKEN_GETMAD($1,$$,'o');
1248                         }
1249         |       NOTOP listexpr                       /* not $foo */
1250                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1251                           TOKEN_GETMAD($1,$$,'o');
1252                         }
1253         |       UNIOP                                /* Unary op, $_ implied */
1254                         { $$ = newOP(IVAL($1), 0);
1255                           TOKEN_GETMAD($1,$$,'o');
1256                         }
1257         |       UNIOP block                          /* eval { foo }* */
1258                         { $$ = newUNOP(IVAL($1), 0, $2);
1259                           TOKEN_GETMAD($1,$$,'o');
1260                         }
1261         |       UNIOP term                           /* Unary op */
1262                         { $$ = newUNOP(IVAL($1), 0, $2);
1263                           TOKEN_GETMAD($1,$$,'o');
1264                         }
1265         |       REQUIRE                              /* require, $_ implied */
1266                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1267                           TOKEN_GETMAD($1,$$,'o');
1268                         }
1269         |       REQUIRE term                         /* require Foo */
1270                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1271                           TOKEN_GETMAD($1,$$,'o');
1272                         }
1273         |       UNIOPSUB
1274                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1275         |       UNIOPSUB term                        /* Sub treated as unop */
1276                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1277                             op_append_elem(OP_LIST, $2, scalar($1))); }
1278         |       FUNC0                                /* Nullary operator */
1279                         { $$ = newOP(IVAL($1), 0);
1280                           TOKEN_GETMAD($1,$$,'o');
1281                         }
1282         |       FUNC0 '(' ')'
1283                         { $$ = newOP(IVAL($1), 0);
1284                           TOKEN_GETMAD($1,$$,'o');
1285                           TOKEN_GETMAD($2,$$,'(');
1286                           TOKEN_GETMAD($3,$$,')');
1287                         }
1288         |       FUNC0OP       /* Same as above, but op created in toke.c */
1289                         { $$ = $1; }
1290         |       FUNC0OP '(' ')'
1291                         { $$ = $1;
1292                           TOKEN_GETMAD($2,$$,'(');
1293                           TOKEN_GETMAD($3,$$,')');
1294                         }
1295         |       FUNC0SUB                             /* Sub treated as nullop */
1296                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1297                                 scalar($1)); }
1298         |       FUNC1 '(' ')'                        /* not () */
1299                         { $$ = (IVAL($1) == OP_NOT)
1300                             ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1301                             : newOP(IVAL($1), OPf_SPECIAL);
1302
1303                           TOKEN_GETMAD($1,$$,'o');
1304                           TOKEN_GETMAD($2,$$,'(');
1305                           TOKEN_GETMAD($3,$$,')');
1306                         }
1307         |       FUNC1 '(' expr ')'                   /* not($foo) */
1308                         { $$ = newUNOP(IVAL($1), 0, $3);
1309                           TOKEN_GETMAD($1,$$,'o');
1310                           TOKEN_GETMAD($2,$$,'(');
1311                           TOKEN_GETMAD($4,$$,')');
1312                         }
1313         |       PMFUNC /* m//, s///, qr//, tr/// */
1314                         {
1315                             if (   $1->op_type != OP_TRANS
1316                                 && $1->op_type != OP_TRANSR
1317                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1318                             {
1319                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1320                                 SAVEFREESV(PL_compcv);
1321                             } else
1322                                 $<ival>$ = 0;
1323                         }
1324                     '(' listexpr ')'
1325                         { $$ = pmruntime($1, $4, 1, $<ival>2);
1326                           TOKEN_GETMAD($3,$$,'(');
1327                           TOKEN_GETMAD($5,$$,')');
1328                         }
1329         |       WORD
1330         |       listop
1331         |       YADAYADA
1332                         {
1333                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1334                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1335                           TOKEN_GETMAD($1,$$,'X');
1336                         }
1337         |       PLUGEXPR
1338         ;
1339
1340 /* "my" declarations, with optional attributes */
1341 myattrterm:     MY myterm myattrlist
1342                         { $$ = my_attrs($2,$3);
1343                           DO_MAD(
1344                               token_getmad($1,$$,'d');
1345                               append_madprops($3->op_madprop, $$, 'a');
1346                               $3->op_madprop = 0;
1347                           )
1348                         }
1349         |       MY myterm
1350                         { $$ = localize($2,IVAL($1));
1351                           TOKEN_GETMAD($1,$$,'d');
1352                         }
1353         ;
1354
1355 /* Things that can be "my"'d */
1356 myterm  :       '(' expr ')'
1357                         { $$ = sawparens($2);
1358                           TOKEN_GETMAD($1,$$,'(');
1359                           TOKEN_GETMAD($3,$$,')');
1360                         }
1361         |       '(' ')'
1362                         { $$ = sawparens(newNULLLIST());
1363                           TOKEN_GETMAD($1,$$,'(');
1364                           TOKEN_GETMAD($2,$$,')');
1365                         }
1366         |       scalar  %prec '('
1367                         { $$ = $1; }
1368         |       hsh     %prec '('
1369                         { $$ = $1; }
1370         |       ary     %prec '('
1371                         { $$ = $1; }
1372         ;
1373
1374 /* Basic list expressions */
1375 optlistexpr:    /* NULL */ %prec PREC_LOW
1376                         { $$ = (OP*)NULL; }
1377         |       listexpr    %prec PREC_LOW
1378                         { $$ = $1; }
1379         ;
1380
1381 optexpr:        /* NULL */
1382                         { $$ = (OP*)NULL; }
1383         |       expr
1384                         { $$ = $1; }
1385         ;
1386
1387 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1388    lexical */
1389 my_scalar:      scalar
1390                         { PL_parser->in_my = 0; $$ = my($1); }
1391         ;
1392
1393 amper   :       '&' indirob
1394                         { $$ = newCVREF(IVAL($1),$2);
1395                           TOKEN_GETMAD($1,$$,'&');
1396                         }
1397         ;
1398
1399 scalar  :       '$' indirob
1400                         { $$ = newSVREF($2);
1401                           TOKEN_GETMAD($1,$$,'$');
1402                         }
1403         ;
1404
1405 ary     :       '@' indirob
1406                         { $$ = newAVREF($2);
1407                           TOKEN_GETMAD($1,$$,'@');
1408                         }
1409         ;
1410
1411 hsh     :       '%' indirob
1412                         { $$ = newHVREF($2);
1413                           TOKEN_GETMAD($1,$$,'%');
1414                         }
1415         ;
1416
1417 arylen  :       DOLSHARP indirob
1418                         { $$ = newAVREF($2);
1419                           TOKEN_GETMAD($1,$$,'l');
1420                         }
1421         ;
1422
1423 star    :       '*' indirob
1424                         { $$ = newGVREF(0,$2);
1425                           TOKEN_GETMAD($1,$$,'*');
1426                         }
1427         ;
1428
1429 /* Indirect objects */
1430 indirob :       WORD
1431                         { $$ = scalar($1); }
1432         |       scalar %prec PREC_LOW
1433                         { $$ = scalar($1); }
1434         |       block
1435                         { $$ = op_scope($1); }
1436
1437         |       PRIVATEREF
1438                         { $$ = $1; }
1439         ;