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