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