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