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