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