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