Refactor t/op/lex_assign.t to use test.pl, and not make TAP by hand.
[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> 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         |       PACKAGE WORD WORD ';'
368                         {
369 #ifdef MAD
370                           $$ = package($3);
371                           token_getmad($1,$$,'o');
372                           if ($2)
373                               package_version($2);
374                           token_getmad($4,$$,';');
375 #else
376                           package($3);
377                           if ($2)
378                               package_version($2);
379                           $$ = (OP*)NULL;
380 #endif
381                         }
382         |       USE startsub
383                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
384                 WORD WORD optlistexpr ';'
385                         {
386                           SvREFCNT_inc_simple_void(PL_compcv);
387 #ifdef MAD
388                           $$ = utilize(IVAL($1), $2, $4, $5, $6);
389                           token_getmad($1,$$,'o');
390                           token_getmad($7,$$,';');
391                           if (PL_parser->rsfp_filters &&
392                                       AvFILLp(PL_parser->rsfp_filters) >= 0)
393                               append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
394 #else
395                           utilize(IVAL($1), $2, $4, $5, $6);
396                           $$ = (OP*)NULL;
397 #endif
398                         }
399         |       IF '(' remember mexpr ')' mblock else
400                         {
401                           $$ = block_end($3,
402                               newCONDOP(0, $4, op_scope($6), $7));
403                           TOKEN_GETMAD($1,$$,'I');
404                           TOKEN_GETMAD($2,$$,'(');
405                           TOKEN_GETMAD($5,$$,')');
406                           PL_parser->copline = (line_t)IVAL($1);
407                         }
408         |       UNLESS '(' remember miexpr ')' mblock else
409                         {
410                           $$ = block_end($3,
411                               newCONDOP(0, $4, op_scope($6), $7));
412                           TOKEN_GETMAD($1,$$,'I');
413                           TOKEN_GETMAD($2,$$,'(');
414                           TOKEN_GETMAD($5,$$,')');
415                           PL_parser->copline = (line_t)IVAL($1);
416                         }
417         |       GIVEN '(' remember mexpr ')' mblock
418                         {
419                           const PADOFFSET offset = pad_findmy_pvs("$_", 0);
420                           $$ = block_end($3,
421                                   newGIVENOP($4, op_scope($6),
422                                     offset == NOT_IN_PAD
423                                     || PAD_COMPNAME_FLAGS_isOUR(offset)
424                                       ? 0
425                                       : offset));
426                           PL_parser->copline = (line_t)IVAL($1);
427                         }
428         |       WHEN '(' remember mexpr ')' mblock
429                         { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
430         |       DEFAULT block
431                         { $$ = newWHENOP(0, op_scope($2)); }
432         |       WHILE '(' remember texpr ')' mintro mblock cont
433                         {
434                           $$ = block_end($3,
435                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
436                                       $4, $7, $8, $6));
437                           TOKEN_GETMAD($1,$$,'W');
438                           TOKEN_GETMAD($2,$$,'(');
439                           TOKEN_GETMAD($5,$$,')');
440                           PL_parser->copline = (line_t)IVAL($1);
441                         }
442         |       UNTIL '(' remember iexpr ')' mintro mblock cont
443                         {
444                           $$ = block_end($3,
445                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
446                                       $4, $7, $8, $6));
447                           TOKEN_GETMAD($1,$$,'W');
448                           TOKEN_GETMAD($2,$$,'(');
449                           TOKEN_GETMAD($5,$$,')');
450                           PL_parser->copline = (line_t)IVAL($1);
451                         }
452         |       FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
453                 mblock
454                         {
455                           OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
456                           OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
457                                       scalar($6), $11, $9, $8);
458                           if (initop) {
459                               forop = op_prepend_elem(OP_LINESEQ, initop,
460                                   op_append_elem(OP_LINESEQ,
461                                       newOP(OP_UNSTACK, OPf_SPECIAL),
462                                       forop));
463                           }
464                           DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
465                           $$ = block_end($3, forop);
466                           TOKEN_GETMAD($1,$$,'3');
467                           TOKEN_GETMAD($2,$$,'(');
468                           TOKEN_GETMAD($5,$$,'1');
469                           TOKEN_GETMAD($7,$$,'2');
470                           TOKEN_GETMAD($10,$$,')');
471                           PL_parser->copline = (line_t)IVAL($1);
472                         }
473         |       FOR MY remember my_scalar '(' mexpr ')' mblock cont
474                         {
475                           $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
476                           TOKEN_GETMAD($1,$$,'W');
477                           TOKEN_GETMAD($2,$$,'d');
478                           TOKEN_GETMAD($5,$$,'(');
479                           TOKEN_GETMAD($7,$$,')');
480                           PL_parser->copline = (line_t)IVAL($1);
481                         }
482         |       FOR scalar '(' remember mexpr ')' mblock cont
483                         {
484                           $$ = block_end($4, newFOROP(0,
485                                       op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
486                           TOKEN_GETMAD($1,$$,'W');
487                           TOKEN_GETMAD($3,$$,'(');
488                           TOKEN_GETMAD($6,$$,')');
489                           PL_parser->copline = (line_t)IVAL($1);
490                         }
491         |       FOR '(' remember mexpr ')' mblock cont
492                         {
493                           $$ = block_end($3,
494                                   newFOROP(0, (OP*)NULL, $4, $6, $7));
495                           TOKEN_GETMAD($1,$$,'W');
496                           TOKEN_GETMAD($2,$$,'(');
497                           TOKEN_GETMAD($5,$$,')');
498                           PL_parser->copline = (line_t)IVAL($1);
499                         }
500         |       block cont
501                         {
502                           /* a block is a loop that happens once */
503                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
504                                   (OP*)NULL, $1, $2, 0);
505                         }
506         |       PACKAGE WORD WORD '{' remember
507                         {
508                           package($3);
509                           if ($2) {
510                               package_version($2);
511                           }
512                         }
513                 stmtseq '}'
514                         {
515                           /* a block is a loop that happens once */
516                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
517                                   (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
518                           TOKEN_GETMAD($4,$$,'{');
519                           TOKEN_GETMAD($8,$$,'}');
520                           if (PL_parser->copline > (line_t)IVAL($4))
521                               PL_parser->copline = (line_t)IVAL($4);
522                         }
523         |       sideff ';'
524                         {
525                           PL_parser->expect = XSTATE;
526                           $$ = $1;
527                           TOKEN_GETMAD($2,$$,';');
528                         }
529         |       ';'
530                         {
531                           PL_parser->expect = XSTATE;
532                           $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
533                           TOKEN_GETMAD($1,$$,';');
534                           PL_parser->copline = NOLINE;
535                         }
536         ;
537
538 /* Format line */
539 formline:       THING formarg
540                         { OP *list;
541                           if ($2) {
542                               OP *term = $2;
543                               DO_MAD(term = newUNOP(OP_NULL, 0, term));
544                               list = op_append_elem(OP_LIST, $1, term);
545                           }
546                           else {
547 #ifdef MAD
548                               OP *op = newNULLLIST();
549                               list = op_append_elem(OP_LIST, $1, op);
550 #else
551                               list = $1;
552 #endif
553                           }
554                           if (PL_parser->copline == NOLINE)
555                                PL_parser->copline = CopLINE(PL_curcop)-1;
556                           else PL_parser->copline--;
557                           $$ = newSTATEOP(0, NULL,
558                                           convert(OP_FORMLINE, 0, list));
559                         }
560         ;
561
562 formarg :       /* NULL */
563                         { $$ = NULL; }
564         |       FORMLBRACK stmtseq FORMRBRACK
565                         { $$ = op_unscope($2); }
566         ;
567
568 /* An expression which may have a side-effect */
569 sideff  :       error
570                         { $$ = (OP*)NULL; }
571         |       expr
572                         { $$ = $1; }
573         |       expr IF expr
574                         { $$ = newLOGOP(OP_AND, 0, $3, $1);
575                           TOKEN_GETMAD($2,$$,'i');
576                         }
577         |       expr UNLESS expr
578                         { $$ = newLOGOP(OP_OR, 0, $3, $1);
579                           TOKEN_GETMAD($2,$$,'i');
580                         }
581         |       expr WHILE expr
582                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
583                           TOKEN_GETMAD($2,$$,'w');
584                         }
585         |       expr UNTIL iexpr
586                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
587                           TOKEN_GETMAD($2,$$,'w');
588                         }
589         |       expr FOR expr
590                         { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
591                           TOKEN_GETMAD($2,$$,'w');
592                           PL_parser->copline = (line_t)IVAL($2);
593                         }
594         |       expr WHEN expr
595                         { $$ = newWHENOP($3, op_scope($1)); }
596         ;
597
598 /* else and elsif blocks */
599 else    :       /* NULL */
600                         { $$ = (OP*)NULL; }
601         |       ELSE mblock
602                         {
603                           ($2)->op_flags |= OPf_PARENS;
604                           $$ = op_scope($2);
605                           TOKEN_GETMAD($1,$$,'o');
606                         }
607         |       ELSIF '(' mexpr ')' mblock else
608                         { PL_parser->copline = (line_t)IVAL($1);
609                             $$ = newCONDOP(0,
610                                 newSTATEOP(OPf_SPECIAL,NULL,$3),
611                                 op_scope($5), $6);
612                           PL_hints |= HINT_BLOCK_SCOPE;
613                           TOKEN_GETMAD($1,$$,'I');
614                           TOKEN_GETMAD($2,$$,'(');
615                           TOKEN_GETMAD($4,$$,')');
616                         }
617         ;
618
619 /* Continue blocks */
620 cont    :       /* NULL */
621                         { $$ = (OP*)NULL; }
622         |       CONTINUE block
623                         {
624                           $$ = op_scope($2);
625                           TOKEN_GETMAD($1,$$,'o');
626                         }
627         ;
628
629 /* determine whether there are any new my declarations */
630 mintro  :       /* NULL */
631                         { $$ = (PL_min_intro_pending &&
632                             PL_max_intro_pending >=  PL_min_intro_pending);
633                           intro_my(); }
634
635 /* Normal expression */
636 nexpr   :       /* NULL */
637                         { $$ = (OP*)NULL; }
638         |       sideff
639         ;
640
641 /* Boolean expression */
642 texpr   :       /* NULL means true */
643                         { YYSTYPE tmplval;
644                           (void)scan_num("1", &tmplval);
645                           $$ = tmplval.opval; }
646         |       expr
647         ;
648
649 /* Inverted boolean expression */
650 iexpr   :       expr
651                         { $$ = invert(scalar($1)); }
652         ;
653
654 /* Expression with its own lexical scope */
655 mexpr   :       expr
656                         { $$ = $1; intro_my(); }
657         ;
658
659 mnexpr  :       nexpr
660                         { $$ = $1; intro_my(); }
661         ;
662
663 miexpr  :       iexpr
664                         { $$ = $1; intro_my(); }
665         ;
666
667 formname:       WORD            { $$ = $1; }
668         |       /* NULL */      { $$ = (OP*)NULL; }
669         ;
670
671 startsub:       /* NULL */      /* start a regular subroutine scope */
672                         { $$ = start_subparse(FALSE, 0);
673                             SAVEFREESV(PL_compcv); }
674
675         ;
676
677 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
678                         { $$ = start_subparse(FALSE, CVf_ANON);
679                             SAVEFREESV(PL_compcv); }
680         ;
681
682 startformsub:   /* NULL */      /* start a format subroutine scope */
683                         { $$ = start_subparse(TRUE, 0);
684                             SAVEFREESV(PL_compcv); }
685         ;
686
687 /* Name of a subroutine - must be a bareword, could be special */
688 subname :       WORD
689         |       PRIVATEREF
690         ;
691
692 /* Subroutine prototype */
693 proto   :       /* NULL */
694                         { $$ = (OP*)NULL; }
695         |       THING
696         ;
697
698 /* Optional list of subroutine attributes */
699 subattrlist:    /* NULL */
700                         { $$ = (OP*)NULL; }
701         |       COLONATTR THING
702                         { $$ = $2;
703                           TOKEN_GETMAD($1,$$,':');
704                         }
705         |       COLONATTR
706                         { $$ = IF_MAD(
707                                     newOP(OP_NULL, 0),
708                                     (OP*)NULL
709                                 );
710                           TOKEN_GETMAD($1,$$,':');
711                         }
712         ;
713
714 /* List of attributes for a "my" variable declaration */
715 myattrlist:     COLONATTR THING
716                         { $$ = $2;
717                           TOKEN_GETMAD($1,$$,':');
718                         }
719         |       COLONATTR
720                         { $$ = IF_MAD(
721                                     newOP(OP_NULL, 0),
722                                     (OP*)NULL
723                                 );
724                           TOKEN_GETMAD($1,$$,':');
725                         }
726         ;
727
728 /* Subroutine body - either null or a block */
729 subbody :       block   { $$ = $1; }
730         |       ';'     { $$ = IF_MAD(
731                                     newOP(OP_NULL,0),
732                                     (OP*)NULL
733                                 );
734                           PL_parser->expect = XSTATE;
735                           TOKEN_GETMAD($1,$$,';');
736                         }
737         ;
738
739 /* Ordinary expressions; logical combinations */
740 expr    :       expr ANDOP expr
741                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
742                           TOKEN_GETMAD($2,$$,'o');
743                         }
744         |       expr OROP expr
745                         { $$ = newLOGOP(IVAL($2), 0, $1, $3);
746                           TOKEN_GETMAD($2,$$,'o');
747                         }
748         |       expr DOROP expr
749                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
750                           TOKEN_GETMAD($2,$$,'o');
751                         }
752         |       listexpr %prec PREC_LOW
753         ;
754
755 /* Expressions are a list of terms joined by commas */
756 listexpr:       listexpr ','
757                         {
758 #ifdef MAD
759                           OP* op = newNULLLIST();
760                           token_getmad($2,op,',');
761                           $$ = op_append_elem(OP_LIST, $1, op);
762 #else
763                           $$ = $1;
764 #endif
765                         }
766         |       listexpr ',' term
767                         { 
768                           OP* term = $3;
769                           DO_MAD(
770                               term = newUNOP(OP_NULL, 0, term);
771                               token_getmad($2,term,',');
772                           )
773                           $$ = op_append_elem(OP_LIST, $1, term);
774                         }
775         |       term %prec PREC_LOW
776         ;
777
778 /* List operators */
779 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
780                         { $$ = convert(IVAL($1), OPf_STACKED,
781                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
782                           TOKEN_GETMAD($1,$$,'o');
783                         }
784         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
785                         { $$ = convert(IVAL($1), OPf_STACKED,
786                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
787                           TOKEN_GETMAD($1,$$,'o');
788                           TOKEN_GETMAD($2,$$,'(');
789                           TOKEN_GETMAD($5,$$,')');
790                         }
791         |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
792                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
793                                 op_append_elem(OP_LIST,
794                                     op_prepend_elem(OP_LIST, scalar($1), $5),
795                                     newUNOP(OP_METHOD, 0, $3)));
796                           TOKEN_GETMAD($2,$$,'A');
797                           TOKEN_GETMAD($4,$$,'(');
798                           TOKEN_GETMAD($6,$$,')');
799                         }
800         |       term ARROW method                     /* $foo->bar */
801                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
802                                 op_append_elem(OP_LIST, scalar($1),
803                                     newUNOP(OP_METHOD, 0, $3)));
804                           TOKEN_GETMAD($2,$$,'A');
805                         }
806         |       METHOD indirob optlistexpr           /* new Class @args */
807                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
808                                 op_append_elem(OP_LIST,
809                                     op_prepend_elem(OP_LIST, $2, $3),
810                                     newUNOP(OP_METHOD, 0, $1)));
811                         }
812         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
813                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
814                                 op_append_elem(OP_LIST,
815                                     op_prepend_elem(OP_LIST, $2, $4),
816                                     newUNOP(OP_METHOD, 0, $1)));
817                           TOKEN_GETMAD($3,$$,'(');
818                           TOKEN_GETMAD($5,$$,')');
819                         }
820         |       LSTOP optlistexpr                    /* print @args */
821                         { $$ = convert(IVAL($1), 0, $2);
822                           TOKEN_GETMAD($1,$$,'o');
823                         }
824         |       FUNC '(' optexpr ')'                 /* print (@args) */
825                         { $$ = convert(IVAL($1), 0, $3);
826                           TOKEN_GETMAD($1,$$,'o');
827                           TOKEN_GETMAD($2,$$,'(');
828                           TOKEN_GETMAD($4,$$,')');
829                         }
830         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
831                         { SvREFCNT_inc_simple_void(PL_compcv);
832                           $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
833                     optlistexpr         %prec LSTOP  /* ... @bar */
834                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
835                                  op_append_elem(OP_LIST,
836                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
837                         }
838         ;
839
840 /* Names of methods. May use $object->$methodname */
841 method  :       METHOD
842         |       scalar
843         ;
844
845 /* Some kind of subscripted expression */
846 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
847                         /* In this and all the hash accessors, ';' is
848                          * provided by the tokeniser */
849                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
850                             PL_parser->expect = XOPERATOR;
851                           TOKEN_GETMAD($2,$$,'{');
852                           TOKEN_GETMAD($4,$$,';');
853                           TOKEN_GETMAD($5,$$,'}');
854                         }
855         |       scalar '[' expr ']'          /* $array[$element] */
856                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
857                           TOKEN_GETMAD($2,$$,'[');
858                           TOKEN_GETMAD($4,$$,']');
859                         }
860         |       term ARROW '[' expr ']'      /* somearef->[$element] */
861                         { $$ = newBINOP(OP_AELEM, 0,
862                                         ref(newAVREF($1),OP_RV2AV),
863                                         scalar($4));
864                           TOKEN_GETMAD($2,$$,'a');
865                           TOKEN_GETMAD($3,$$,'[');
866                           TOKEN_GETMAD($5,$$,']');
867                         }
868         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
869                         { $$ = newBINOP(OP_AELEM, 0,
870                                         ref(newAVREF($1),OP_RV2AV),
871                                         scalar($3));
872                           TOKEN_GETMAD($2,$$,'[');
873                           TOKEN_GETMAD($4,$$,']');
874                         }
875         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
876                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
877                             PL_parser->expect = XOPERATOR;
878                           TOKEN_GETMAD($2,$$,'{');
879                           TOKEN_GETMAD($4,$$,';');
880                           TOKEN_GETMAD($5,$$,'}');
881                         }
882         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
883                         { $$ = newBINOP(OP_HELEM, 0,
884                                         ref(newHVREF($1),OP_RV2HV),
885                                         jmaybe($4));
886                             PL_parser->expect = XOPERATOR;
887                           TOKEN_GETMAD($2,$$,'a');
888                           TOKEN_GETMAD($3,$$,'{');
889                           TOKEN_GETMAD($5,$$,';');
890                           TOKEN_GETMAD($6,$$,'}');
891                         }
892         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
893                         { $$ = newBINOP(OP_HELEM, 0,
894                                         ref(newHVREF($1),OP_RV2HV),
895                                         jmaybe($3));
896                             PL_parser->expect = XOPERATOR;
897                           TOKEN_GETMAD($2,$$,'{');
898                           TOKEN_GETMAD($4,$$,';');
899                           TOKEN_GETMAD($5,$$,'}');
900                         }
901         |       term ARROW '(' ')'          /* $subref->() */
902                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
903                                    newCVREF(0, scalar($1)));
904                           TOKEN_GETMAD($2,$$,'a');
905                           TOKEN_GETMAD($3,$$,'(');
906                           TOKEN_GETMAD($4,$$,')');
907                         }
908         |       term ARROW '(' expr ')'     /* $subref->(@args) */
909                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
910                                    op_append_elem(OP_LIST, $4,
911                                        newCVREF(0, scalar($1))));
912                           TOKEN_GETMAD($2,$$,'a');
913                           TOKEN_GETMAD($3,$$,'(');
914                           TOKEN_GETMAD($5,$$,')');
915                         }
916
917         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
918                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
919                                    op_append_elem(OP_LIST, $3,
920                                                newCVREF(0, scalar($1))));
921                           TOKEN_GETMAD($2,$$,'(');
922                           TOKEN_GETMAD($4,$$,')');
923                         }
924         |       subscripted '(' ')'        /* $foo->{bar}->() */
925                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
926                                    newCVREF(0, scalar($1)));
927                           TOKEN_GETMAD($2,$$,'(');
928                           TOKEN_GETMAD($3,$$,')');
929                         }
930         |       '(' expr ')' '[' expr ']'            /* list slice */
931                         { $$ = newSLICEOP(0, $5, $2);
932                           TOKEN_GETMAD($1,$$,'(');
933                           TOKEN_GETMAD($3,$$,')');
934                           TOKEN_GETMAD($4,$$,'[');
935                           TOKEN_GETMAD($6,$$,']');
936                         }
937         |       QWLIST '[' expr ']'            /* list literal slice */
938                         { $$ = newSLICEOP(0, $3, $1);
939                           TOKEN_GETMAD($2,$$,'[');
940                           TOKEN_GETMAD($4,$$,']');
941                         }
942         |       '(' ')' '[' expr ']'                 /* empty list slice! */
943                         { $$ = newSLICEOP(0, $4, (OP*)NULL);
944                           TOKEN_GETMAD($1,$$,'(');
945                           TOKEN_GETMAD($2,$$,')');
946                           TOKEN_GETMAD($3,$$,'[');
947                           TOKEN_GETMAD($5,$$,']');
948                         }
949     ;
950
951 /* Binary operators between terms */
952 termbinop:      term ASSIGNOP term                     /* $x = $y */
953                         { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
954                           TOKEN_GETMAD($2,$$,'o');
955                         }
956         |       term POWOP term                        /* $x ** $y */
957                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
958                           TOKEN_GETMAD($2,$$,'o');
959                         }
960         |       term MULOP term                        /* $x * $y, $x x $y */
961                         {   if (IVAL($2) != OP_REPEAT)
962                                 scalar($1);
963                             $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
964                           TOKEN_GETMAD($2,$$,'o');
965                         }
966         |       term ADDOP term                        /* $x + $y */
967                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
968                           TOKEN_GETMAD($2,$$,'o');
969                         }
970         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
971                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
972                           TOKEN_GETMAD($2,$$,'o');
973                         }
974         |       term RELOP term                        /* $x > $y, etc. */
975                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
976                           TOKEN_GETMAD($2,$$,'o');
977                         }
978         |       term EQOP term                         /* $x == $y, $x eq $y */
979                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
980                           TOKEN_GETMAD($2,$$,'o');
981                         }
982         |       term BITANDOP term                     /* $x & $y */
983                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
984                           TOKEN_GETMAD($2,$$,'o');
985                         }
986         |       term BITOROP term                      /* $x | $y */
987                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
988                           TOKEN_GETMAD($2,$$,'o');
989                         }
990         |       term DOTDOT term                       /* $x..$y, $x...$y */
991                         {
992                           $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
993                           DO_MAD({
994                               UNOP *op;
995                               op = (UNOP*)$$;
996                               op = (UNOP*)op->op_first; /* get to flop */
997                               op = (UNOP*)op->op_first; /* get to flip */
998                               op = (UNOP*)op->op_first; /* get to range */
999                               token_getmad($2,(OP*)op,'o');
1000                             });
1001                         }
1002         |       term ANDAND term                       /* $x && $y */
1003                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
1004                           TOKEN_GETMAD($2,$$,'o');
1005                         }
1006         |       term OROR term                         /* $x || $y */
1007                         { $$ = newLOGOP(OP_OR, 0, $1, $3);
1008                           TOKEN_GETMAD($2,$$,'o');
1009                         }
1010         |       term DORDOR term                       /* $x // $y */
1011                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1012                           TOKEN_GETMAD($2,$$,'o');
1013                         }
1014         |       term MATCHOP term                      /* $x =~ /$y/ */
1015                         { $$ = bind_match(IVAL($2), $1, $3);
1016                           TOKEN_GETMAD($2,
1017                                 ($$->op_type == OP_NOT
1018                                     ? ((UNOP*)$$)->op_first : $$),
1019                                 '~');
1020                         }
1021     ;
1022
1023 /* Unary operators and terms */
1024 termunop : '-' term %prec UMINUS                       /* -$x */
1025                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1026                           TOKEN_GETMAD($1,$$,'o');
1027                         }
1028         |       '+' term %prec UMINUS                  /* +$x */
1029                         { $$ = IF_MAD(
1030                                     newUNOP(OP_NULL, 0, $2),
1031                                     $2
1032                                 );
1033                           TOKEN_GETMAD($1,$$,'+');
1034                         }
1035         |       '!' term                               /* !$x */
1036                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1037                           TOKEN_GETMAD($1,$$,'o');
1038                         }
1039         |       '~' term                               /* ~$x */
1040                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1041                           TOKEN_GETMAD($1,$$,'o');
1042                         }
1043         |       term POSTINC                           /* $x++ */
1044                         { $$ = newUNOP(OP_POSTINC, 0,
1045                                         op_lvalue(scalar($1), OP_POSTINC));
1046                           TOKEN_GETMAD($2,$$,'o');
1047                         }
1048         |       term POSTDEC                           /* $x-- */
1049                         { $$ = newUNOP(OP_POSTDEC, 0,
1050                                         op_lvalue(scalar($1), OP_POSTDEC));
1051                           TOKEN_GETMAD($2,$$,'o');
1052                         }
1053         |       PREINC term                            /* ++$x */
1054                         { $$ = newUNOP(OP_PREINC, 0,
1055                                         op_lvalue(scalar($2), OP_PREINC));
1056                           TOKEN_GETMAD($1,$$,'o');
1057                         }
1058         |       PREDEC term                            /* --$x */
1059                         { $$ = newUNOP(OP_PREDEC, 0,
1060                                         op_lvalue(scalar($2), OP_PREDEC));
1061                           TOKEN_GETMAD($1,$$,'o');
1062                         }
1063
1064     ;
1065
1066 /* Constructors for anonymous data */
1067 anonymous:      '[' expr ']'
1068                         { $$ = newANONLIST($2);
1069                           TOKEN_GETMAD($1,$$,'[');
1070                           TOKEN_GETMAD($3,$$,']');
1071                         }
1072         |       '[' ']'
1073                         { $$ = newANONLIST((OP*)NULL);
1074                           TOKEN_GETMAD($1,$$,'[');
1075                           TOKEN_GETMAD($2,$$,']');
1076                         }
1077         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
1078                         { $$ = newANONHASH($2);
1079                           TOKEN_GETMAD($1,$$,'{');
1080                           TOKEN_GETMAD($3,$$,';');
1081                           TOKEN_GETMAD($4,$$,'}');
1082                         }
1083         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
1084                         { $$ = newANONHASH((OP*)NULL);
1085                           TOKEN_GETMAD($1,$$,'{');
1086                           TOKEN_GETMAD($2,$$,';');
1087                           TOKEN_GETMAD($3,$$,'}');
1088                         }
1089         |       ANONSUB startanonsub proto subattrlist block    %prec '('
1090                         { SvREFCNT_inc_simple_void(PL_compcv);
1091                           $$ = newANONATTRSUB($2, $3, $4, $5);
1092                           TOKEN_GETMAD($1,$$,'o');
1093                           OP_GETMAD($3,$$,'s');
1094                           OP_GETMAD($4,$$,'a');
1095                         }
1096
1097     ;
1098
1099 /* Things called with "do" */
1100 termdo  :       DO term %prec UNIOP                     /* do $filename */
1101                         { $$ = dofile($2, IVAL($1));
1102                           TOKEN_GETMAD($1,$$,'o');
1103                         }
1104         |       DO block        %prec '('               /* do { code */
1105                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
1106                           TOKEN_GETMAD($1,$$,'D');
1107                         }
1108         |       DO subname '(' ')'                  /* do somesub() */
1109                         { $$ = newUNOP(OP_ENTERSUB,
1110                             OPf_SPECIAL|OPf_STACKED,
1111                             op_prepend_elem(OP_LIST,
1112                                 scalar(newCVREF(
1113                                     (OPpENTERSUB_AMPER<<8),
1114                                     scalar($2)
1115                                 )),(OP*)NULL)); dep();
1116                           TOKEN_GETMAD($1,$$,'o');
1117                           TOKEN_GETMAD($3,$$,'(');
1118                           TOKEN_GETMAD($4,$$,')');
1119                         }
1120         |       DO subname '(' expr ')'             /* do somesub(@args) */
1121                         { $$ = newUNOP(OP_ENTERSUB,
1122                             OPf_SPECIAL|OPf_STACKED,
1123                             op_append_elem(OP_LIST,
1124                                 $4,
1125                                 scalar(newCVREF(
1126                                     (OPpENTERSUB_AMPER<<8),
1127                                     scalar($2)
1128                                 )))); dep();
1129                           TOKEN_GETMAD($1,$$,'o');
1130                           TOKEN_GETMAD($3,$$,'(');
1131                           TOKEN_GETMAD($5,$$,')');
1132                         }
1133         |       DO scalar '(' ')'                /* do $subref () */
1134                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1135                             op_prepend_elem(OP_LIST,
1136                                 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1137                           TOKEN_GETMAD($1,$$,'o');
1138                           TOKEN_GETMAD($3,$$,'(');
1139                           TOKEN_GETMAD($4,$$,')');
1140                         }
1141         |       DO scalar '(' expr ')'           /* do $subref (@args) */
1142                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1143                             op_prepend_elem(OP_LIST,
1144                                 $4,
1145                                 scalar(newCVREF(0,scalar($2))))); dep();
1146                           TOKEN_GETMAD($1,$$,'o');
1147                           TOKEN_GETMAD($3,$$,'(');
1148                           TOKEN_GETMAD($5,$$,')');
1149                         }
1150
1151         ;
1152
1153 term    :       termbinop
1154         |       termunop
1155         |       anonymous
1156         |       termdo
1157         |       term '?' term ':' term
1158                         { $$ = newCONDOP(0, $1, $3, $5);
1159                           TOKEN_GETMAD($2,$$,'?');
1160                           TOKEN_GETMAD($4,$$,':');
1161                         }
1162         |       REFGEN term                          /* \$x, \@y, \%z */
1163                         { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
1164                           TOKEN_GETMAD($1,$$,'o');
1165                         }
1166         |       myattrterm      %prec UNIOP
1167                         { $$ = $1; }
1168         |       LOCAL term      %prec UNIOP
1169                         { $$ = localize($2,IVAL($1));
1170                           TOKEN_GETMAD($1,$$,'k');
1171                         }
1172         |       '(' expr ')'
1173                         { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1174                           TOKEN_GETMAD($1,$$,'(');
1175                           TOKEN_GETMAD($3,$$,')');
1176                         }
1177         |       QWLIST
1178                         { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
1179         |       '(' ')'
1180                         { $$ = sawparens(newNULLLIST());
1181                           TOKEN_GETMAD($1,$$,'(');
1182                           TOKEN_GETMAD($2,$$,')');
1183                         }
1184         |       scalar  %prec '('
1185                         { $$ = $1; }
1186         |       star    %prec '('
1187                         { $$ = $1; }
1188         |       hsh     %prec '('
1189                         { $$ = $1; }
1190         |       ary     %prec '('
1191                         { $$ = $1; }
1192         |       arylen  %prec '('                    /* $#x, $#{ something } */
1193                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1194         |       subscripted
1195                         { $$ = $1; }
1196         |       ary '[' expr ']'                     /* array slice */
1197                         { $$ = op_prepend_elem(OP_ASLICE,
1198                                 newOP(OP_PUSHMARK, 0),
1199                                     newLISTOP(OP_ASLICE, 0,
1200                                         list($3),
1201                                         ref($1, OP_ASLICE)));
1202                           TOKEN_GETMAD($2,$$,'[');
1203                           TOKEN_GETMAD($4,$$,']');
1204                         }
1205         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
1206                         { $$ = op_prepend_elem(OP_HSLICE,
1207                                 newOP(OP_PUSHMARK, 0),
1208                                     newLISTOP(OP_HSLICE, 0,
1209                                         list($3),
1210                                         ref(oopsHV($1), OP_HSLICE)));
1211                             PL_parser->expect = XOPERATOR;
1212                           TOKEN_GETMAD($2,$$,'{');
1213                           TOKEN_GETMAD($4,$$,';');
1214                           TOKEN_GETMAD($5,$$,'}');
1215                         }
1216         |       THING   %prec '('
1217                         { $$ = $1; }
1218         |       amper                                /* &foo; */
1219                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1220         |       amper '(' ')'                 /* &foo() */
1221                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1222                           TOKEN_GETMAD($2,$$,'(');
1223                           TOKEN_GETMAD($3,$$,')');
1224                         }
1225         |       amper '(' expr ')'            /* &foo(@args) */
1226                         {
1227                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1228                                 op_append_elem(OP_LIST, $3, scalar($1)));
1229                           DO_MAD({
1230                               OP* op = $$;
1231                               if (op->op_type == OP_CONST) { /* defeat const fold */
1232                                 op = (OP*)op->op_madprop->mad_val;
1233                               }
1234                               token_getmad($2,op,'(');
1235                               token_getmad($4,op,')');
1236                           });
1237                         }
1238         |       NOAMP subname optlistexpr               /* foo(@args) */
1239                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1240                             op_append_elem(OP_LIST, $3, scalar($2)));
1241                           TOKEN_GETMAD($1,$$,'o');
1242                         }
1243         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1244                         { $$ = newOP(IVAL($1), OPf_SPECIAL);
1245                             PL_hints |= HINT_BLOCK_SCOPE;
1246                           TOKEN_GETMAD($1,$$,'o');
1247                         }
1248         |       LOOPEX term
1249                         { $$ = newLOOPEX(IVAL($1),$2);
1250                           TOKEN_GETMAD($1,$$,'o');
1251                         }
1252         |       NOTOP listexpr                       /* not $foo */
1253                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1254                           TOKEN_GETMAD($1,$$,'o');
1255                         }
1256         |       UNIOP                                /* Unary op, $_ implied */
1257                         { $$ = newOP(IVAL($1), 0);
1258                           TOKEN_GETMAD($1,$$,'o');
1259                         }
1260         |       UNIOP block                          /* eval { foo }* */
1261                         { $$ = newUNOP(IVAL($1), 0, $2);
1262                           TOKEN_GETMAD($1,$$,'o');
1263                         }
1264         |       UNIOP term                           /* Unary op */
1265                         { $$ = newUNOP(IVAL($1), 0, $2);
1266                           TOKEN_GETMAD($1,$$,'o');
1267                         }
1268         |       REQUIRE                              /* require, $_ implied */
1269                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1270                           TOKEN_GETMAD($1,$$,'o');
1271                         }
1272         |       REQUIRE term                         /* require Foo */
1273                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1274                           TOKEN_GETMAD($1,$$,'o');
1275                         }
1276         |       UNIOPSUB
1277                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1278         |       UNIOPSUB term                        /* Sub treated as unop */
1279                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1280                             op_append_elem(OP_LIST, $2, scalar($1))); }
1281         |       FUNC0                                /* Nullary operator */
1282                         { $$ = newOP(IVAL($1), 0);
1283                           TOKEN_GETMAD($1,$$,'o');
1284                         }
1285         |       FUNC0 '(' ')'
1286                         { $$ = newOP(IVAL($1), 0);
1287                           TOKEN_GETMAD($1,$$,'o');
1288                           TOKEN_GETMAD($2,$$,'(');
1289                           TOKEN_GETMAD($3,$$,')');
1290                         }
1291         |       FUNC0OP       /* Same as above, but op created in toke.c */
1292                         { $$ = $1; }
1293         |       FUNC0OP '(' ')'
1294                         { $$ = $1;
1295                           TOKEN_GETMAD($2,$$,'(');
1296                           TOKEN_GETMAD($3,$$,')');
1297                         }
1298         |       FUNC0SUB                             /* Sub treated as nullop */
1299                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1300                                 scalar($1)); }
1301         |       FUNC1 '(' ')'                        /* not () */
1302                         { $$ = (IVAL($1) == OP_NOT)
1303                             ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1304                             : newOP(IVAL($1), OPf_SPECIAL);
1305
1306                           TOKEN_GETMAD($1,$$,'o');
1307                           TOKEN_GETMAD($2,$$,'(');
1308                           TOKEN_GETMAD($3,$$,')');
1309                         }
1310         |       FUNC1 '(' expr ')'                   /* not($foo) */
1311                         { $$ = newUNOP(IVAL($1), 0, $3);
1312                           TOKEN_GETMAD($1,$$,'o');
1313                           TOKEN_GETMAD($2,$$,'(');
1314                           TOKEN_GETMAD($4,$$,')');
1315                         }
1316         |       PMFUNC /* m//, s///, qr//, tr/// */
1317                         {
1318                             if (   $1->op_type != OP_TRANS
1319                                 && $1->op_type != OP_TRANSR
1320                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1321                             {
1322                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1323                                 SAVEFREESV(PL_compcv);
1324                             } else
1325                                 $<ival>$ = 0;
1326                         }
1327                     '(' listexpr ')'
1328                         { $$ = pmruntime($1, $4, 1, $<ival>2);
1329                           TOKEN_GETMAD($3,$$,'(');
1330                           TOKEN_GETMAD($5,$$,')');
1331                         }
1332         |       WORD
1333         |       listop
1334         |       YADAYADA
1335                         {
1336                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1337                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1338                           TOKEN_GETMAD($1,$$,'X');
1339                         }
1340         |       PLUGEXPR
1341         ;
1342
1343 /* "my" declarations, with optional attributes */
1344 myattrterm:     MY myterm myattrlist
1345                         { $$ = my_attrs($2,$3);
1346                           DO_MAD(
1347                               token_getmad($1,$$,'d');
1348                               append_madprops($3->op_madprop, $$, 'a');
1349                               $3->op_madprop = 0;
1350                           );
1351                         }
1352         |       MY myterm
1353                         { $$ = localize($2,IVAL($1));
1354                           TOKEN_GETMAD($1,$$,'d');
1355                         }
1356         ;
1357
1358 /* Things that can be "my"'d */
1359 myterm  :       '(' expr ')'
1360                         { $$ = sawparens($2);
1361                           TOKEN_GETMAD($1,$$,'(');
1362                           TOKEN_GETMAD($3,$$,')');
1363                         }
1364         |       '(' ')'
1365                         { $$ = sawparens(newNULLLIST());
1366                           TOKEN_GETMAD($1,$$,'(');
1367                           TOKEN_GETMAD($2,$$,')');
1368                         }
1369         |       scalar  %prec '('
1370                         { $$ = $1; }
1371         |       hsh     %prec '('
1372                         { $$ = $1; }
1373         |       ary     %prec '('
1374                         { $$ = $1; }
1375         ;
1376
1377 /* Basic list expressions */
1378 optlistexpr:    /* NULL */ %prec PREC_LOW
1379                         { $$ = (OP*)NULL; }
1380         |       listexpr    %prec PREC_LOW
1381                         { $$ = $1; }
1382         ;
1383
1384 optexpr:        /* NULL */
1385                         { $$ = (OP*)NULL; }
1386         |       expr
1387                         { $$ = $1; }
1388         ;
1389
1390 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1391    lexical */
1392 my_scalar:      scalar
1393                         { PL_parser->in_my = 0; $$ = my($1); }
1394         ;
1395
1396 amper   :       '&' indirob
1397                         { $$ = newCVREF(IVAL($1),$2);
1398                           TOKEN_GETMAD($1,$$,'&');
1399                         }
1400         ;
1401
1402 scalar  :       '$' indirob
1403                         { $$ = newSVREF($2);
1404                           TOKEN_GETMAD($1,$$,'$');
1405                         }
1406         ;
1407
1408 ary     :       '@' indirob
1409                         { $$ = newAVREF($2);
1410                           TOKEN_GETMAD($1,$$,'@');
1411                         }
1412         ;
1413
1414 hsh     :       '%' indirob
1415                         { $$ = newHVREF($2);
1416                           TOKEN_GETMAD($1,$$,'%');
1417                         }
1418         ;
1419
1420 arylen  :       DOLSHARP indirob
1421                         { $$ = newAVREF($2);
1422                           TOKEN_GETMAD($1,$$,'l');
1423                         }
1424         ;
1425
1426 star    :       '*' indirob
1427                         { $$ = newGVREF(0,$2);
1428                           TOKEN_GETMAD($1,$$,'*');
1429                         }
1430         ;
1431
1432 /* Indirect objects */
1433 indirob :       WORD
1434                         { $$ = scalar($1); }
1435         |       scalar %prec PREC_LOW
1436                         { $$ = scalar($1); }
1437         |       block
1438                         { $$ = op_scope($1); }
1439
1440         |       PRIVATEREF
1441                         { $$ = $1; }
1442         ;