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