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