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