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