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