This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make ‘sub foo{}’ respect ‘our foo’
[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         |       PRIVATEREF
688         ;
689
690 /* Subroutine prototype */
691 proto   :       /* NULL */
692                         { $$ = (OP*)NULL; }
693         |       THING
694         ;
695
696 /* Optional list of subroutine attributes */
697 subattrlist:    /* NULL */
698                         { $$ = (OP*)NULL; }
699         |       COLONATTR THING
700                         { $$ = $2;
701                           TOKEN_GETMAD($1,$$,':');
702                         }
703         |       COLONATTR
704                         { $$ = IF_MAD(
705                                     newOP(OP_NULL, 0),
706                                     (OP*)NULL
707                                 );
708                           TOKEN_GETMAD($1,$$,':');
709                         }
710         ;
711
712 /* List of attributes for a "my" variable declaration */
713 myattrlist:     COLONATTR THING
714                         { $$ = $2;
715                           TOKEN_GETMAD($1,$$,':');
716                         }
717         |       COLONATTR
718                         { $$ = IF_MAD(
719                                     newOP(OP_NULL, 0),
720                                     (OP*)NULL
721                                 );
722                           TOKEN_GETMAD($1,$$,':');
723                         }
724         ;
725
726 /* Subroutine body - either null or a block */
727 subbody :       block   { $$ = $1; }
728         |       ';'     { $$ = IF_MAD(
729                                     newOP(OP_NULL,0),
730                                     (OP*)NULL
731                                 );
732                           PL_parser->expect = XSTATE;
733                           TOKEN_GETMAD($1,$$,';');
734                         }
735         ;
736
737 /* Ordinary expressions; logical combinations */
738 expr    :       expr ANDOP expr
739                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
740                           TOKEN_GETMAD($2,$$,'o');
741                         }
742         |       expr OROP expr
743                         { $$ = newLOGOP(IVAL($2), 0, $1, $3);
744                           TOKEN_GETMAD($2,$$,'o');
745                         }
746         |       expr DOROP expr
747                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
748                           TOKEN_GETMAD($2,$$,'o');
749                         }
750         |       listexpr %prec PREC_LOW
751         ;
752
753 /* Expressions are a list of terms joined by commas */
754 listexpr:       listexpr ','
755                         {
756 #ifdef MAD
757                           OP* op = newNULLLIST();
758                           token_getmad($2,op,',');
759                           $$ = op_append_elem(OP_LIST, $1, op);
760 #else
761                           $$ = $1;
762 #endif
763                         }
764         |       listexpr ',' term
765                         { 
766                           OP* term = $3;
767                           DO_MAD(
768                               term = newUNOP(OP_NULL, 0, term);
769                               token_getmad($2,term,',');
770                           )
771                           $$ = op_append_elem(OP_LIST, $1, term);
772                         }
773         |       term %prec PREC_LOW
774         ;
775
776 /* List operators */
777 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
778                         { $$ = convert(IVAL($1), OPf_STACKED,
779                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
780                           TOKEN_GETMAD($1,$$,'o');
781                         }
782         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
783                         { $$ = convert(IVAL($1), OPf_STACKED,
784                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
785                           TOKEN_GETMAD($1,$$,'o');
786                           TOKEN_GETMAD($2,$$,'(');
787                           TOKEN_GETMAD($5,$$,')');
788                         }
789         |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
790                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
791                                 op_append_elem(OP_LIST,
792                                     op_prepend_elem(OP_LIST, scalar($1), $5),
793                                     newUNOP(OP_METHOD, 0, $3)));
794                           TOKEN_GETMAD($2,$$,'A');
795                           TOKEN_GETMAD($4,$$,'(');
796                           TOKEN_GETMAD($6,$$,')');
797                         }
798         |       term ARROW method                     /* $foo->bar */
799                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
800                                 op_append_elem(OP_LIST, scalar($1),
801                                     newUNOP(OP_METHOD, 0, $3)));
802                           TOKEN_GETMAD($2,$$,'A');
803                         }
804         |       METHOD indirob optlistexpr           /* new Class @args */
805                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
806                                 op_append_elem(OP_LIST,
807                                     op_prepend_elem(OP_LIST, $2, $3),
808                                     newUNOP(OP_METHOD, 0, $1)));
809                         }
810         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
811                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
812                                 op_append_elem(OP_LIST,
813                                     op_prepend_elem(OP_LIST, $2, $4),
814                                     newUNOP(OP_METHOD, 0, $1)));
815                           TOKEN_GETMAD($3,$$,'(');
816                           TOKEN_GETMAD($5,$$,')');
817                         }
818         |       LSTOP optlistexpr                    /* print @args */
819                         { $$ = convert(IVAL($1), 0, $2);
820                           TOKEN_GETMAD($1,$$,'o');
821                         }
822         |       FUNC '(' optexpr ')'                 /* print (@args) */
823                         { $$ = convert(IVAL($1), 0, $3);
824                           TOKEN_GETMAD($1,$$,'o');
825                           TOKEN_GETMAD($2,$$,'(');
826                           TOKEN_GETMAD($4,$$,')');
827                         }
828         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
829                         { SvREFCNT_inc_simple_void(PL_compcv);
830                           $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
831                     optlistexpr         %prec LSTOP  /* ... @bar */
832                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
833                                  op_append_elem(OP_LIST,
834                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
835                         }
836         ;
837
838 /* Names of methods. May use $object->$methodname */
839 method  :       METHOD
840         |       scalar
841         ;
842
843 /* Some kind of subscripted expression */
844 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
845                         /* In this and all the hash accessors, ';' is
846                          * provided by the tokeniser */
847                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
848                             PL_parser->expect = XOPERATOR;
849                           TOKEN_GETMAD($2,$$,'{');
850                           TOKEN_GETMAD($4,$$,';');
851                           TOKEN_GETMAD($5,$$,'}');
852                         }
853         |       scalar '[' expr ']'          /* $array[$element] */
854                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
855                           TOKEN_GETMAD($2,$$,'[');
856                           TOKEN_GETMAD($4,$$,']');
857                         }
858         |       term ARROW '[' expr ']'      /* somearef->[$element] */
859                         { $$ = newBINOP(OP_AELEM, 0,
860                                         ref(newAVREF($1),OP_RV2AV),
861                                         scalar($4));
862                           TOKEN_GETMAD($2,$$,'a');
863                           TOKEN_GETMAD($3,$$,'[');
864                           TOKEN_GETMAD($5,$$,']');
865                         }
866         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
867                         { $$ = newBINOP(OP_AELEM, 0,
868                                         ref(newAVREF($1),OP_RV2AV),
869                                         scalar($3));
870                           TOKEN_GETMAD($2,$$,'[');
871                           TOKEN_GETMAD($4,$$,']');
872                         }
873         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
874                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
875                             PL_parser->expect = XOPERATOR;
876                           TOKEN_GETMAD($2,$$,'{');
877                           TOKEN_GETMAD($4,$$,';');
878                           TOKEN_GETMAD($5,$$,'}');
879                         }
880         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
881                         { $$ = newBINOP(OP_HELEM, 0,
882                                         ref(newHVREF($1),OP_RV2HV),
883                                         jmaybe($4));
884                             PL_parser->expect = XOPERATOR;
885                           TOKEN_GETMAD($2,$$,'a');
886                           TOKEN_GETMAD($3,$$,'{');
887                           TOKEN_GETMAD($5,$$,';');
888                           TOKEN_GETMAD($6,$$,'}');
889                         }
890         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
891                         { $$ = newBINOP(OP_HELEM, 0,
892                                         ref(newHVREF($1),OP_RV2HV),
893                                         jmaybe($3));
894                             PL_parser->expect = XOPERATOR;
895                           TOKEN_GETMAD($2,$$,'{');
896                           TOKEN_GETMAD($4,$$,';');
897                           TOKEN_GETMAD($5,$$,'}');
898                         }
899         |       term ARROW '(' ')'          /* $subref->() */
900                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
901                                    newCVREF(0, scalar($1)));
902                           TOKEN_GETMAD($2,$$,'a');
903                           TOKEN_GETMAD($3,$$,'(');
904                           TOKEN_GETMAD($4,$$,')');
905                         }
906         |       term ARROW '(' expr ')'     /* $subref->(@args) */
907                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
908                                    op_append_elem(OP_LIST, $4,
909                                        newCVREF(0, scalar($1))));
910                           TOKEN_GETMAD($2,$$,'a');
911                           TOKEN_GETMAD($3,$$,'(');
912                           TOKEN_GETMAD($5,$$,')');
913                         }
914
915         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
916                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
917                                    op_append_elem(OP_LIST, $3,
918                                                newCVREF(0, scalar($1))));
919                           TOKEN_GETMAD($2,$$,'(');
920                           TOKEN_GETMAD($4,$$,')');
921                         }
922         |       subscripted '(' ')'        /* $foo->{bar}->() */
923                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
924                                    newCVREF(0, scalar($1)));
925                           TOKEN_GETMAD($2,$$,'(');
926                           TOKEN_GETMAD($3,$$,')');
927                         }
928         |       '(' expr ')' '[' expr ']'            /* list slice */
929                         { $$ = newSLICEOP(0, $5, $2);
930                           TOKEN_GETMAD($1,$$,'(');
931                           TOKEN_GETMAD($3,$$,')');
932                           TOKEN_GETMAD($4,$$,'[');
933                           TOKEN_GETMAD($6,$$,']');
934                         }
935         |       QWLIST '[' expr ']'            /* list literal slice */
936                         { $$ = newSLICEOP(0, $3, $1);
937                           TOKEN_GETMAD($2,$$,'[');
938                           TOKEN_GETMAD($4,$$,']');
939                         }
940         |       '(' ')' '[' expr ']'                 /* empty list slice! */
941                         { $$ = newSLICEOP(0, $4, (OP*)NULL);
942                           TOKEN_GETMAD($1,$$,'(');
943                           TOKEN_GETMAD($2,$$,')');
944                           TOKEN_GETMAD($3,$$,'[');
945                           TOKEN_GETMAD($5,$$,']');
946                         }
947     ;
948
949 /* Binary operators between terms */
950 termbinop:      term ASSIGNOP term                     /* $x = $y */
951                         { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
952                           TOKEN_GETMAD($2,$$,'o');
953                         }
954         |       term POWOP term                        /* $x ** $y */
955                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
956                           TOKEN_GETMAD($2,$$,'o');
957                         }
958         |       term MULOP term                        /* $x * $y, $x x $y */
959                         {   if (IVAL($2) != OP_REPEAT)
960                                 scalar($1);
961                             $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
962                           TOKEN_GETMAD($2,$$,'o');
963                         }
964         |       term ADDOP term                        /* $x + $y */
965                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
966                           TOKEN_GETMAD($2,$$,'o');
967                         }
968         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
969                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
970                           TOKEN_GETMAD($2,$$,'o');
971                         }
972         |       term RELOP term                        /* $x > $y, etc. */
973                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
974                           TOKEN_GETMAD($2,$$,'o');
975                         }
976         |       term EQOP term                         /* $x == $y, $x eq $y */
977                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
978                           TOKEN_GETMAD($2,$$,'o');
979                         }
980         |       term BITANDOP term                     /* $x & $y */
981                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
982                           TOKEN_GETMAD($2,$$,'o');
983                         }
984         |       term BITOROP term                      /* $x | $y */
985                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
986                           TOKEN_GETMAD($2,$$,'o');
987                         }
988         |       term DOTDOT term                       /* $x..$y, $x...$y */
989                         {
990                           $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
991                           DO_MAD({
992                               UNOP *op;
993                               op = (UNOP*)$$;
994                               op = (UNOP*)op->op_first; /* get to flop */
995                               op = (UNOP*)op->op_first; /* get to flip */
996                               op = (UNOP*)op->op_first; /* get to range */
997                               token_getmad($2,(OP*)op,'o');
998                             })
999                         }
1000         |       term ANDAND term                       /* $x && $y */
1001                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
1002                           TOKEN_GETMAD($2,$$,'o');
1003                         }
1004         |       term OROR term                         /* $x || $y */
1005                         { $$ = newLOGOP(OP_OR, 0, $1, $3);
1006                           TOKEN_GETMAD($2,$$,'o');
1007                         }
1008         |       term DORDOR term                       /* $x // $y */
1009                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1010                           TOKEN_GETMAD($2,$$,'o');
1011                         }
1012         |       term MATCHOP term                      /* $x =~ /$y/ */
1013                         { $$ = bind_match(IVAL($2), $1, $3);
1014                           TOKEN_GETMAD($2,
1015                                 ($$->op_type == OP_NOT
1016                                     ? ((UNOP*)$$)->op_first : $$),
1017                                 '~');
1018                         }
1019     ;
1020
1021 /* Unary operators and terms */
1022 termunop : '-' term %prec UMINUS                       /* -$x */
1023                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1024                           TOKEN_GETMAD($1,$$,'o');
1025                         }
1026         |       '+' term %prec UMINUS                  /* +$x */
1027                         { $$ = IF_MAD(
1028                                     newUNOP(OP_NULL, 0, $2),
1029                                     $2
1030                                 );
1031                           TOKEN_GETMAD($1,$$,'+');
1032                         }
1033         |       '!' term                               /* !$x */
1034                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1035                           TOKEN_GETMAD($1,$$,'o');
1036                         }
1037         |       '~' term                               /* ~$x */
1038                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1039                           TOKEN_GETMAD($1,$$,'o');
1040                         }
1041         |       term POSTINC                           /* $x++ */
1042                         { $$ = newUNOP(OP_POSTINC, 0,
1043                                         op_lvalue(scalar($1), OP_POSTINC));
1044                           TOKEN_GETMAD($2,$$,'o');
1045                         }
1046         |       term POSTDEC                           /* $x-- */
1047                         { $$ = newUNOP(OP_POSTDEC, 0,
1048                                         op_lvalue(scalar($1), OP_POSTDEC));
1049                           TOKEN_GETMAD($2,$$,'o');
1050                         }
1051         |       PREINC term                            /* ++$x */
1052                         { $$ = newUNOP(OP_PREINC, 0,
1053                                         op_lvalue(scalar($2), OP_PREINC));
1054                           TOKEN_GETMAD($1,$$,'o');
1055                         }
1056         |       PREDEC term                            /* --$x */
1057                         { $$ = newUNOP(OP_PREDEC, 0,
1058                                         op_lvalue(scalar($2), OP_PREDEC));
1059                           TOKEN_GETMAD($1,$$,'o');
1060                         }
1061
1062     ;
1063
1064 /* Constructors for anonymous data */
1065 anonymous:      '[' expr ']'
1066                         { $$ = newANONLIST($2);
1067                           TOKEN_GETMAD($1,$$,'[');
1068                           TOKEN_GETMAD($3,$$,']');
1069                         }
1070         |       '[' ']'
1071                         { $$ = newANONLIST((OP*)NULL);
1072                           TOKEN_GETMAD($1,$$,'[');
1073                           TOKEN_GETMAD($2,$$,']');
1074                         }
1075         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
1076                         { $$ = newANONHASH($2);
1077                           TOKEN_GETMAD($1,$$,'{');
1078                           TOKEN_GETMAD($3,$$,';');
1079                           TOKEN_GETMAD($4,$$,'}');
1080                         }
1081         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
1082                         { $$ = newANONHASH((OP*)NULL);
1083                           TOKEN_GETMAD($1,$$,'{');
1084                           TOKEN_GETMAD($2,$$,';');
1085                           TOKEN_GETMAD($3,$$,'}');
1086                         }
1087         |       ANONSUB startanonsub proto subattrlist block    %prec '('
1088                         { SvREFCNT_inc_simple_void(PL_compcv);
1089                           $$ = newANONATTRSUB($2, $3, $4, $5);
1090                           TOKEN_GETMAD($1,$$,'o');
1091                           OP_GETMAD($3,$$,'s');
1092                           OP_GETMAD($4,$$,'a');
1093                         }
1094
1095     ;
1096
1097 /* Things called with "do" */
1098 termdo  :       DO term %prec UNIOP                     /* do $filename */
1099                         { $$ = dofile($2, IVAL($1));
1100                           TOKEN_GETMAD($1,$$,'o');
1101                         }
1102         |       DO block        %prec '('               /* do { code */
1103                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
1104                           TOKEN_GETMAD($1,$$,'D');
1105                         }
1106         |       DO WORD '(' ')'                  /* do somesub() */
1107                         { $$ = newUNOP(OP_ENTERSUB,
1108                             OPf_SPECIAL|OPf_STACKED,
1109                             op_prepend_elem(OP_LIST,
1110                                 scalar(newCVREF(
1111                                     (OPpENTERSUB_AMPER<<8),
1112                                     scalar($2)
1113                                 )),(OP*)NULL)); dep();
1114                           TOKEN_GETMAD($1,$$,'o');
1115                           TOKEN_GETMAD($3,$$,'(');
1116                           TOKEN_GETMAD($4,$$,')');
1117                         }
1118         |       DO WORD '(' expr ')'             /* do somesub(@args) */
1119                         { $$ = newUNOP(OP_ENTERSUB,
1120                             OPf_SPECIAL|OPf_STACKED,
1121                             op_append_elem(OP_LIST,
1122                                 $4,
1123                                 scalar(newCVREF(
1124                                     (OPpENTERSUB_AMPER<<8),
1125                                     scalar($2)
1126                                 )))); dep();
1127                           TOKEN_GETMAD($1,$$,'o');
1128                           TOKEN_GETMAD($3,$$,'(');
1129                           TOKEN_GETMAD($5,$$,')');
1130                         }
1131         |       DO scalar '(' ')'                /* do $subref () */
1132                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1133                             op_prepend_elem(OP_LIST,
1134                                 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1135                           TOKEN_GETMAD($1,$$,'o');
1136                           TOKEN_GETMAD($3,$$,'(');
1137                           TOKEN_GETMAD($4,$$,')');
1138                         }
1139         |       DO scalar '(' expr ')'           /* do $subref (@args) */
1140                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1141                             op_prepend_elem(OP_LIST,
1142                                 $4,
1143                                 scalar(newCVREF(0,scalar($2))))); dep();
1144                           TOKEN_GETMAD($1,$$,'o');
1145                           TOKEN_GETMAD($3,$$,'(');
1146                           TOKEN_GETMAD($5,$$,')');
1147                         }
1148
1149         ;
1150
1151 term    :       termbinop
1152         |       termunop
1153         |       anonymous
1154         |       termdo
1155         |       term '?' term ':' term
1156                         { $$ = newCONDOP(0, $1, $3, $5);
1157                           TOKEN_GETMAD($2,$$,'?');
1158                           TOKEN_GETMAD($4,$$,':');
1159                         }
1160         |       REFGEN term                          /* \$x, \@y, \%z */
1161                         { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
1162                           TOKEN_GETMAD($1,$$,'o');
1163                         }
1164         |       myattrterm      %prec UNIOP
1165                         { $$ = $1; }
1166         |       LOCAL term      %prec UNIOP
1167                         { $$ = localize($2,IVAL($1));
1168                           TOKEN_GETMAD($1,$$,'k');
1169                         }
1170         |       '(' expr ')'
1171                         { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1172                           TOKEN_GETMAD($1,$$,'(');
1173                           TOKEN_GETMAD($3,$$,')');
1174                         }
1175         |       QWLIST
1176                         { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
1177         |       '(' ')'
1178                         { $$ = sawparens(newNULLLIST());
1179                           TOKEN_GETMAD($1,$$,'(');
1180                           TOKEN_GETMAD($2,$$,')');
1181                         }
1182         |       scalar  %prec '('
1183                         { $$ = $1; }
1184         |       star    %prec '('
1185                         { $$ = $1; }
1186         |       hsh     %prec '('
1187                         { $$ = $1; }
1188         |       ary     %prec '('
1189                         { $$ = $1; }
1190         |       arylen  %prec '('                    /* $#x, $#{ something } */
1191                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1192         |       subscripted
1193                         { $$ = $1; }
1194         |       ary '[' expr ']'                     /* array slice */
1195                         { $$ = op_prepend_elem(OP_ASLICE,
1196                                 newOP(OP_PUSHMARK, 0),
1197                                     newLISTOP(OP_ASLICE, 0,
1198                                         list($3),
1199                                         ref($1, OP_ASLICE)));
1200                           TOKEN_GETMAD($2,$$,'[');
1201                           TOKEN_GETMAD($4,$$,']');
1202                         }
1203         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
1204                         { $$ = op_prepend_elem(OP_HSLICE,
1205                                 newOP(OP_PUSHMARK, 0),
1206                                     newLISTOP(OP_HSLICE, 0,
1207                                         list($3),
1208                                         ref(oopsHV($1), OP_HSLICE)));
1209                             PL_parser->expect = XOPERATOR;
1210                           TOKEN_GETMAD($2,$$,'{');
1211                           TOKEN_GETMAD($4,$$,';');
1212                           TOKEN_GETMAD($5,$$,'}');
1213                         }
1214         |       THING   %prec '('
1215                         { $$ = $1; }
1216         |       amper                                /* &foo; */
1217                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1218         |       amper '(' ')'                 /* &foo() */
1219                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1220                           TOKEN_GETMAD($2,$$,'(');
1221                           TOKEN_GETMAD($3,$$,')');
1222                         }
1223         |       amper '(' expr ')'            /* &foo(@args) */
1224                         {
1225                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1226                                 op_append_elem(OP_LIST, $3, scalar($1)));
1227                           DO_MAD({
1228                               OP* op = $$;
1229                               if (op->op_type == OP_CONST) { /* defeat const fold */
1230                                 op = (OP*)op->op_madprop->mad_val;
1231                               }
1232                               token_getmad($2,op,'(');
1233                               token_getmad($4,op,')');
1234                           })
1235                         }
1236         |       NOAMP WORD optlistexpr               /* foo(@args) */
1237                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1238                             op_append_elem(OP_LIST, $3, scalar($2)));
1239                           TOKEN_GETMAD($1,$$,'o');
1240                         }
1241         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1242                         { $$ = newOP(IVAL($1), OPf_SPECIAL);
1243                             PL_hints |= HINT_BLOCK_SCOPE;
1244                           TOKEN_GETMAD($1,$$,'o');
1245                         }
1246         |       LOOPEX term
1247                         { $$ = newLOOPEX(IVAL($1),$2);
1248                           TOKEN_GETMAD($1,$$,'o');
1249                         }
1250         |       NOTOP listexpr                       /* not $foo */
1251                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1252                           TOKEN_GETMAD($1,$$,'o');
1253                         }
1254         |       UNIOP                                /* Unary op, $_ implied */
1255                         { $$ = newOP(IVAL($1), 0);
1256                           TOKEN_GETMAD($1,$$,'o');
1257                         }
1258         |       UNIOP block                          /* eval { foo }* */
1259                         { $$ = newUNOP(IVAL($1), 0, $2);
1260                           TOKEN_GETMAD($1,$$,'o');
1261                         }
1262         |       UNIOP term                           /* Unary op */
1263                         { $$ = newUNOP(IVAL($1), 0, $2);
1264                           TOKEN_GETMAD($1,$$,'o');
1265                         }
1266         |       REQUIRE                              /* require, $_ implied */
1267                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1268                           TOKEN_GETMAD($1,$$,'o');
1269                         }
1270         |       REQUIRE term                         /* require Foo */
1271                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1272                           TOKEN_GETMAD($1,$$,'o');
1273                         }
1274         |       UNIOPSUB
1275                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1276         |       UNIOPSUB term                        /* Sub treated as unop */
1277                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1278                             op_append_elem(OP_LIST, $2, scalar($1))); }
1279         |       FUNC0                                /* Nullary operator */
1280                         { $$ = newOP(IVAL($1), 0);
1281                           TOKEN_GETMAD($1,$$,'o');
1282                         }
1283         |       FUNC0 '(' ')'
1284                         { $$ = newOP(IVAL($1), 0);
1285                           TOKEN_GETMAD($1,$$,'o');
1286                           TOKEN_GETMAD($2,$$,'(');
1287                           TOKEN_GETMAD($3,$$,')');
1288                         }
1289         |       FUNC0OP       /* Same as above, but op created in toke.c */
1290                         { $$ = $1; }
1291         |       FUNC0OP '(' ')'
1292                         { $$ = $1;
1293                           TOKEN_GETMAD($2,$$,'(');
1294                           TOKEN_GETMAD($3,$$,')');
1295                         }
1296         |       FUNC0SUB                             /* Sub treated as nullop */
1297                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1298                                 scalar($1)); }
1299         |       FUNC1 '(' ')'                        /* not () */
1300                         { $$ = (IVAL($1) == OP_NOT)
1301                             ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1302                             : newOP(IVAL($1), OPf_SPECIAL);
1303
1304                           TOKEN_GETMAD($1,$$,'o');
1305                           TOKEN_GETMAD($2,$$,'(');
1306                           TOKEN_GETMAD($3,$$,')');
1307                         }
1308         |       FUNC1 '(' expr ')'                   /* not($foo) */
1309                         { $$ = newUNOP(IVAL($1), 0, $3);
1310                           TOKEN_GETMAD($1,$$,'o');
1311                           TOKEN_GETMAD($2,$$,'(');
1312                           TOKEN_GETMAD($4,$$,')');
1313                         }
1314         |       PMFUNC /* m//, s///, qr//, tr/// */
1315                         {
1316                             if (   $1->op_type != OP_TRANS
1317                                 && $1->op_type != OP_TRANSR
1318                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1319                             {
1320                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1321                                 SAVEFREESV(PL_compcv);
1322                             } else
1323                                 $<ival>$ = 0;
1324                         }
1325                     '(' listexpr ')'
1326                         { $$ = pmruntime($1, $4, 1, $<ival>2);
1327                           TOKEN_GETMAD($3,$$,'(');
1328                           TOKEN_GETMAD($5,$$,')');
1329                         }
1330         |       WORD
1331         |       listop
1332         |       YADAYADA
1333                         {
1334                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1335                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1336                           TOKEN_GETMAD($1,$$,'X');
1337                         }
1338         |       PLUGEXPR
1339         ;
1340
1341 /* "my" declarations, with optional attributes */
1342 myattrterm:     MY myterm myattrlist
1343                         { $$ = my_attrs($2,$3);
1344                           DO_MAD(
1345                               token_getmad($1,$$,'d');
1346                               append_madprops($3->op_madprop, $$, 'a');
1347                               $3->op_madprop = 0;
1348                           )
1349                         }
1350         |       MY myterm
1351                         { $$ = localize($2,IVAL($1));
1352                           TOKEN_GETMAD($1,$$,'d');
1353                         }
1354         ;
1355
1356 /* Things that can be "my"'d */
1357 myterm  :       '(' expr ')'
1358                         { $$ = sawparens($2);
1359                           TOKEN_GETMAD($1,$$,'(');
1360                           TOKEN_GETMAD($3,$$,')');
1361                         }
1362         |       '(' ')'
1363                         { $$ = sawparens(newNULLLIST());
1364                           TOKEN_GETMAD($1,$$,'(');
1365                           TOKEN_GETMAD($2,$$,')');
1366                         }
1367         |       scalar  %prec '('
1368                         { $$ = $1; }
1369         |       hsh     %prec '('
1370                         { $$ = $1; }
1371         |       ary     %prec '('
1372                         { $$ = $1; }
1373         ;
1374
1375 /* Basic list expressions */
1376 optlistexpr:    /* NULL */ %prec PREC_LOW
1377                         { $$ = (OP*)NULL; }
1378         |       listexpr    %prec PREC_LOW
1379                         { $$ = $1; }
1380         ;
1381
1382 optexpr:        /* NULL */
1383                         { $$ = (OP*)NULL; }
1384         |       expr
1385                         { $$ = $1; }
1386         ;
1387
1388 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1389    lexical */
1390 my_scalar:      scalar
1391                         { PL_parser->in_my = 0; $$ = my($1); }
1392         ;
1393
1394 amper   :       '&' indirob
1395                         { $$ = newCVREF(IVAL($1),$2);
1396                           TOKEN_GETMAD($1,$$,'&');
1397                         }
1398         ;
1399
1400 scalar  :       '$' indirob
1401                         { $$ = newSVREF($2);
1402                           TOKEN_GETMAD($1,$$,'$');
1403                         }
1404         ;
1405
1406 ary     :       '@' indirob
1407                         { $$ = newAVREF($2);
1408                           TOKEN_GETMAD($1,$$,'@');
1409                         }
1410         ;
1411
1412 hsh     :       '%' indirob
1413                         { $$ = newHVREF($2);
1414                           TOKEN_GETMAD($1,$$,'%');
1415                         }
1416         ;
1417
1418 arylen  :       DOLSHARP indirob
1419                         { $$ = newAVREF($2);
1420                           TOKEN_GETMAD($1,$$,'l');
1421                         }
1422         ;
1423
1424 star    :       '*' indirob
1425                         { $$ = newGVREF(0,$2);
1426                           TOKEN_GETMAD($1,$$,'*');
1427                         }
1428         ;
1429
1430 /* Indirect objects */
1431 indirob :       WORD
1432                         { $$ = scalar($1); }
1433         |       scalar %prec PREC_LOW
1434                         { $$ = scalar($1); }
1435         |       block
1436                         { $$ = op_scope($1); }
1437
1438         |       PRIVATEREF
1439                         { $$ = $1; }
1440         ;