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