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