3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
12 * All that is gold does not glitter, not all those who wander are lost.'
14 * This file holds the grammar for the Perl language. If edited, you need
15 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
16 * and perly.act which are derived from this.
18 * Note that these derived files are included and compiled twice; once
19 * from perly.c, and once from madly.c. The second time, a number of MAD
20 * macros are defined, which compile in extra code that allows the parse
21 * tree to be accurately dumped. In particular:
23 * MAD defined if compiling madly.c
24 * DO_MAD(A) expands to A under madly.c, to null otherwise
25 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
26 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
27 * TOKEN_FREE() similarly
28 * OP_GETMAD() similarly
29 * IVAL(i) expands to (i)->tk_lval.ival or (i)
30 * PVAL(p) expands to (p)->tk_lval.pval or (p)
32 * The main job of of this grammar is to call the various newFOO()
33 * functions in op.c to build a syntax tree of OP structs.
34 * It relies on the lexer in toke.c to do the tokenizing.
36 * Note: due to the way that the cleanup code works WRT to freeing ops on
37 * the parse stack, it is dangerous to assign to the $n variables within
41 /* Make the parser re-entrant. */
45 /* FIXME for MAD - is the new mintro on while and until important? */
50 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
51 must always be 1st union member) */
55 #ifdef PERL_IN_MADLY_C
67 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
69 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
70 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
71 %token <p_tkval> LABEL
72 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
73 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
74 %token <i_tkval> GIVEN WHEN DEFAULT
75 %token <i_tkval> LOOPEX DOTDOT
76 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
77 %token <i_tkval> RELOP EQOP MULOP ADDOP
78 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
79 %token <i_tkval> LOCAL MY MYSUB REQUIRE
80 %token <i_tkval> COLONATTR
82 %type <ival> prog progstart remember mremember savescope
83 %type <ival> startsub startanonsub startformsub
84 /* FIXME for MAD - are these two ival? */
85 %type <ival> mydefsv mintro
87 %type <opval> decl format subrout mysubrout package use peg
89 %type <opval> block mblock lineseq line loop cond else
90 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
91 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
92 %type <opval> listexpr listexprcom indirob listop method
93 %type <opval> formname subname proto subbody cont my_scalar
94 %type <opval> subattrlist myattrlist myattrterm myterm
95 %type <opval> termbinop termunop anonymous termdo
96 %type <opval> switch case
99 %nonassoc <i_tkval> PREC_LOW
102 %left <i_tkval> OROP DOROP
103 %left <i_tkval> ANDOP
104 %right <i_tkval> NOTOP
105 %nonassoc LSTOP LSTOPSUB
107 %right <i_tkval> ASSIGNOP
108 %right <i_tkval> '?' ':'
110 %left <i_tkval> OROR DORDOR
111 %left <i_tkval> ANDAND
112 %left <i_tkval> BITOROP
113 %left <i_tkval> BITANDOP
116 %nonassoc UNIOP UNIOPSUB
118 %left <i_tkval> SHIFTOP
121 %left <i_tkval> MATCHOP
122 %right <i_tkval> '!' '~' UMINUS REFGEN
123 %right <i_tkval> POWOP
124 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
125 %left <i_tkval> ARROW
126 %nonassoc <i_tkval> ')'
134 /* The whole program */
136 /*CONTINUED*/ lineseq
137 { $$ = $1; newPROG(block_end($1,$2)); }
140 /* An ordinary block */
141 block : '{' remember lineseq '}'
142 { if (PL_copline > (line_t)IVAL($1))
143 PL_copline = (line_t)IVAL($1);
144 $$ = block_end($2, $3);
145 TOKEN_GETMAD($1,$$,'{');
146 TOKEN_GETMAD($4,$$,'}');
150 remember: /* NULL */ /* start a full lexical scope */
151 { $$ = block_start(TRUE); }
154 mydefsv: /* NULL */ /* lexicalize $_ */
155 { $$ = (I32) allocmy("$_"); }
160 PL_expect = XSTATE; $$ = block_start(TRUE);
165 mblock : '{' mremember lineseq '}'
166 { if (PL_copline > (line_t)IVAL($1))
167 PL_copline = (line_t)IVAL($1);
168 $$ = block_end($2, $3);
169 TOKEN_GETMAD($1,$$,'{');
170 TOKEN_GETMAD($4,$$,'}');
174 mremember: /* NULL */ /* start a partial lexical scope */
175 { $$ = block_start(FALSE); }
178 savescope: /* NULL */ /* remember stack pos in case of error */
179 { $$ = PL_savestack_ix; }
181 /* A collection of "lines" in the program */
187 append_list(OP_LINESEQ,
188 (LISTOP*)$1, (LISTOP*)$2),
191 | lineseq savescope line
193 $$ = append_list(OP_LINESEQ,
194 (LISTOP*)$1, (LISTOP*)$3);
195 PL_pad_reset_pending = TRUE;
196 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
199 /* A "line" in the program */
201 { $$ = newSTATEOP(0, PVAL($1), $2);
202 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
203 | loop /* loops add their own labels */
204 | switch /* ... and so do switches */
207 { $$ = newSTATEOP(0, PVAL($1), $2); }
211 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
212 TOKEN_GETMAD($1,$$,'L');
213 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
221 TOKEN_GETMAD($2,$$,';');
227 $$ = newSTATEOP(0, PVAL($1), $2);
230 /* sideff might already have a nexstate */
231 OP* op = ((LISTOP*)$$)->op_first;
233 while (op->op_sibling &&
234 op->op_sibling->op_type == OP_NEXTSTATE)
236 token_getmad($1,op,'L');
237 token_getmad($3,op,';');
243 /* An expression which may have a side-effect */
249 { $$ = newLOGOP(OP_AND, 0, $3, $1);
250 TOKEN_GETMAD($2,$$,'i');
253 { $$ = newLOGOP(OP_OR, 0, $3, $1);
254 TOKEN_GETMAD($2,$$,'i');
257 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
258 TOKEN_GETMAD($2,$$,'w');
261 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
262 TOKEN_GETMAD($2,$$,'w');
265 { $$ = newFOROP(0, Nullch, (line_t)IVAL($2),
266 Nullop, $3, $1, Nullop);
267 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
271 /* else and elsif blocks */
275 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
276 TOKEN_GETMAD($1,$$,'o');
278 | ELSIF '(' mexpr ')' mblock else
279 { PL_copline = (line_t)IVAL($1);
280 $$ = newCONDOP(0, $3, scope($5), $6);
281 PL_hints |= HINT_BLOCK_SCOPE;
282 TOKEN_GETMAD($1,$$,'I');
283 TOKEN_GETMAD($2,$$,'(');
284 TOKEN_GETMAD($4,$$,')');
288 /* Real conditional expressions */
289 cond : IF '(' remember mexpr ')' mblock else
290 { PL_copline = (line_t)IVAL($1);
292 newCONDOP(0, $4, scope($6), $7));
293 TOKEN_GETMAD($1,$$,'I');
294 TOKEN_GETMAD($2,$$,'(');
295 TOKEN_GETMAD($5,$$,')');
297 | UNLESS '(' remember miexpr ')' mblock else
298 { PL_copline = (line_t)IVAL($1);
300 newCONDOP(0, $4, scope($6), $7));
301 TOKEN_GETMAD($1,$$,'I');
302 TOKEN_GETMAD($2,$$,'(');
303 TOKEN_GETMAD($5,$$,')');
307 /* Cases for a switch statement */
308 case : WHEN '(' remember mexpr ')' mblock
310 newWHENOP($4, scope($6))); }
312 { $$ = newWHENOP(0, scope($2)); }
315 /* Continue blocks */
320 TOKEN_GETMAD($1,$$,'o');
324 /* Loops: while, until, for, and a bare block */
325 loop : label WHILE '(' remember texpr ')' mintro mblock cont
327 PL_copline = (line_t)$2;
329 newSTATEOP(0, PVAL($1),
330 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
331 IVAL($2), $5, $8, $9, $7)));
332 TOKEN_GETMAD($1,innerop,'L');
333 TOKEN_GETMAD($2,innerop,'W');
334 TOKEN_GETMAD($3,innerop,'(');
335 TOKEN_GETMAD($6,innerop,')');
338 | label UNTIL '(' remember iexpr ')' mintro mblock cont
340 PL_copline = (line_t)$2;
342 newSTATEOP(0, PVAL($1),
343 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
344 IVAL($2), $5, $8, $9, $7)));
345 TOKEN_GETMAD($1,innerop,'L');
346 TOKEN_GETMAD($2,innerop,'W');
347 TOKEN_GETMAD($3,innerop,'(');
348 TOKEN_GETMAD($6,innerop,')');
350 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
353 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
355 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
356 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
357 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
358 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
359 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
361 | label FOR scalar '(' remember mexpr ')' mblock cont
364 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
365 mod($3, OP_ENTERLOOP), $6, $8, $9));
366 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
367 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
368 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
369 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
371 | label FOR '(' remember mexpr ')' mblock cont
374 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
375 Nullop, $5, $7, $8));
376 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
377 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
378 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
379 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
381 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
383 /* basically fake up an initialize-while lineseq */
385 PL_copline = (line_t)IVAL($2);
386 forop = newSTATEOP(0, PVAL($1),
387 newWHILEOP(0, 1, (LOOP*)Nullop,
388 IVAL($2), scalar($7),
391 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
393 CopLABEL_alloc(($1)->tk_lval.pval),
394 ($5 ? newOP(OP_NULL, 0) : $5) ),
397 token_getmad($2,forop,'3');
398 token_getmad($3,forop,'(');
399 token_getmad($6,forop,'1');
400 token_getmad($8,forop,'2');
401 token_getmad($11,forop,')');
402 token_getmad($1,forop,'L');
405 forop = append_elem(OP_LINESEQ,
406 newSTATEOP(0, CopLABEL_alloc($1), $5),
412 $$ = block_end($4, forop); }
413 | label block cont /* a block is a loop that happens once */
414 { $$ = newSTATEOP(0, PVAL($1),
415 newWHILEOP(0, 1, (LOOP*)Nullop,
416 NOLINE, Nullop, $2, $3, 0));
417 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
421 switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
422 { PL_copline = (line_t) $2;
424 newSTATEOP(0, PVAL($1),
425 newGIVENOP($6, scope($8),
426 (PADOFFSET) $5) )); }
429 /* determine whether there are any new my declarations */
431 { $$ = (PL_min_intro_pending &&
432 PL_max_intro_pending >= PL_min_intro_pending);
435 /* Normal expression */
441 /* Boolean expression */
442 texpr : /* NULL means true */
444 (void)scan_num("1", &tmplval);
445 $$ = tmplval.opval; }
449 /* Inverted boolean expression */
451 { $$ = invert(scalar($1)); }
454 /* Expression with its own lexical scope */
456 { $$ = $1; intro_my(); }
460 { $$ = $1; intro_my(); }
464 { $$ = $1; intro_my(); }
467 /* Optional "MAIN:"-style loop labels */
472 tmplval.pval = Nullch;
473 $$ = newTOKEN(OP_NULL, tmplval, 0);
481 /* Some kind of declaration - just hang on peg in the parse tree */
493 /* these two are only used by MAD */
500 { $$ = newOP(OP_NULL,0);
501 TOKEN_GETMAD($1,$$,'p');
505 format : FORMAT startformsub formname block
506 { SvREFCNT_inc(PL_compcv);
508 $$ = newFORM($2, $3, $4);
509 prepend_madprops($1->tk_mad, $$, 'F');
519 formname: WORD { $$ = $1; }
520 | /* NULL */ { $$ = Nullop; }
523 /* Unimplemented "my sub foo { }" */
524 mysubrout: MYSUB startsub subname proto subattrlist subbody
525 { SvREFCNT_inc(PL_compcv);
527 $$ = newMYSUB($2, $3, $4, $5, $6);
528 token_getmad($1,$$,'d');
530 newMYSUB($2, $3, $4, $5, $6);
536 /* Subroutine definition */
537 subrout : SUB startsub subname proto subattrlist subbody
538 { SvREFCNT_inc(PL_compcv);
540 OP* o = newSVOP(OP_ANONCODE, 0,
541 (SV*)newATTRSUB($2, $3, $4, $5, $6));
542 $$ = newOP(OP_NULL,0);
544 op_getmad($3,$$,'n');
545 op_getmad($4,$$,'s');
546 op_getmad($5,$$,'a');
547 token_getmad($1,$$,'d');
548 append_madprops($6->op_madprop, $$, 0);
551 newATTRSUB($2, $3, $4, $5, $6);
557 startsub: /* NULL */ /* start a regular subroutine scope */
558 { $$ = start_subparse(FALSE, 0);
559 SAVEFREESV(PL_compcv); }
563 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
564 { $$ = start_subparse(FALSE, CVf_ANON);
565 SAVEFREESV(PL_compcv); }
568 startformsub: /* NULL */ /* start a format subroutine scope */
569 { $$ = start_subparse(TRUE, 0);
570 SAVEFREESV(PL_compcv); }
573 /* Name of a subroutine - must be a bareword, could be special */
574 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
575 if (strEQ(name, "BEGIN") || strEQ(name, "END")
576 || strEQ(name, "INIT") || strEQ(name, "CHECK")
577 || strEQ(name, "UNITCHECK"))
578 CvSPECIAL_on(PL_compcv);
582 /* Subroutine prototype */
588 /* Optional list of subroutine attributes */
589 subattrlist: /* NULL */
593 TOKEN_GETMAD($1,$$,':');
600 TOKEN_GETMAD($1,$$,':');
604 /* List of attributes for a "my" variable declaration */
605 myattrlist: COLONATTR THING
607 TOKEN_GETMAD($1,$$,':');
614 TOKEN_GETMAD($1,$$,':');
618 /* Subroutine body - either null or a block */
619 subbody : block { $$ = $1; }
625 TOKEN_GETMAD($1,$$,';');
629 package : PACKAGE WORD ';'
633 token_getmad($1,$$,'o');
634 token_getmad($3,$$,';');
643 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
644 WORD WORD listexpr ';'
645 { SvREFCNT_inc(PL_compcv);
647 $$ = utilize(IVAL($1), $2, $4, $5, $6);
648 token_getmad($1,$$,'o');
649 token_getmad($7,$$,';');
650 if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0)
651 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
653 utilize(IVAL($1), $2, $4, $5, $6);
659 /* Ordinary expressions; logical combinations */
660 expr : expr ANDOP expr
661 { $$ = newLOGOP(OP_AND, 0, $1, $3);
662 TOKEN_GETMAD($2,$$,'o');
665 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
666 TOKEN_GETMAD($2,$$,'o');
669 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
670 TOKEN_GETMAD($2,$$,'o');
672 | argexpr %prec PREC_LOW
675 /* Expressions are a list of terms joined by commas */
676 argexpr : argexpr ','
679 OP* op = newNULLLIST();
680 token_getmad($2,op,',');
681 $$ = append_elem(OP_LIST, $1, op);
690 term = newUNOP(OP_NULL, 0, term);
691 token_getmad($2,term,',');
693 $$ = append_elem(OP_LIST, $1, term);
695 | term %prec PREC_LOW
699 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
700 { $$ = convert(IVAL($1), OPf_STACKED,
701 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
702 TOKEN_GETMAD($1,$$,'o');
704 | FUNC '(' indirob expr ')' /* print ($fh @args */
705 { $$ = convert(IVAL($1), OPf_STACKED,
706 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
707 TOKEN_GETMAD($1,$$,'o');
708 TOKEN_GETMAD($2,$$,'(');
709 TOKEN_GETMAD($5,$$,')');
711 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
712 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
714 prepend_elem(OP_LIST, scalar($1), $5),
715 newUNOP(OP_METHOD, 0, $3)));
716 TOKEN_GETMAD($2,$$,'A');
717 TOKEN_GETMAD($4,$$,'(');
718 TOKEN_GETMAD($6,$$,')');
720 | term ARROW method /* $foo->bar */
721 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
722 append_elem(OP_LIST, scalar($1),
723 newUNOP(OP_METHOD, 0, $3)));
724 TOKEN_GETMAD($2,$$,'A');
726 | METHOD indirob listexpr /* new Class @args */
727 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
729 prepend_elem(OP_LIST, $2, $3),
730 newUNOP(OP_METHOD, 0, $1)));
732 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
733 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
735 prepend_elem(OP_LIST, $2, $4),
736 newUNOP(OP_METHOD, 0, $1)));
737 TOKEN_GETMAD($3,$$,'(');
738 TOKEN_GETMAD($5,$$,')');
740 | LSTOP listexpr /* print @args */
741 { $$ = convert(IVAL($1), 0, $2);
742 TOKEN_GETMAD($1,$$,'o');
744 | FUNC '(' listexprcom ')' /* print (@args) */
745 { $$ = convert(IVAL($1), 0, $3);
746 TOKEN_GETMAD($1,$$,'o');
747 TOKEN_GETMAD($2,$$,'(');
748 TOKEN_GETMAD($4,$$,')');
750 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
751 { SvREFCNT_inc(PL_compcv);
752 $<opval>$ = newANONATTRSUB($2, 0, Nullop, $3); }
753 listexpr %prec LSTOP /* ... @bar */
754 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
756 prepend_elem(OP_LIST, $<opval>4, $5), $1));
760 /* Names of methods. May use $object->$methodname */
765 /* Some kind of subscripted expression */
766 subscripted: star '{' expr ';' '}' /* *main::{something} */
767 /* In this and all the hash accessors, ';' is
768 * provided by the tokeniser */
769 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
770 PL_expect = XOPERATOR;
771 TOKEN_GETMAD($2,$$,'{');
772 TOKEN_GETMAD($4,$$,';');
773 TOKEN_GETMAD($5,$$,'}');
775 | scalar '[' expr ']' /* $array[$element] */
776 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
777 TOKEN_GETMAD($2,$$,'[');
778 TOKEN_GETMAD($4,$$,']');
780 | term ARROW '[' expr ']' /* somearef->[$element] */
781 { $$ = newBINOP(OP_AELEM, 0,
782 ref(newAVREF($1),OP_RV2AV),
784 TOKEN_GETMAD($2,$$,'a');
785 TOKEN_GETMAD($3,$$,'[');
786 TOKEN_GETMAD($5,$$,']');
788 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
789 { $$ = newBINOP(OP_AELEM, 0,
790 ref(newAVREF($1),OP_RV2AV),
792 TOKEN_GETMAD($2,$$,'[');
793 TOKEN_GETMAD($4,$$,']');
795 | scalar '{' expr ';' '}' /* $foo->{bar();} */
796 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
797 PL_expect = XOPERATOR;
798 TOKEN_GETMAD($2,$$,'{');
799 TOKEN_GETMAD($4,$$,';');
800 TOKEN_GETMAD($5,$$,'}');
802 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
803 { $$ = newBINOP(OP_HELEM, 0,
804 ref(newHVREF($1),OP_RV2HV),
806 PL_expect = XOPERATOR;
807 TOKEN_GETMAD($2,$$,'a');
808 TOKEN_GETMAD($3,$$,'{');
809 TOKEN_GETMAD($5,$$,';');
810 TOKEN_GETMAD($6,$$,'}');
812 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
813 { $$ = newBINOP(OP_HELEM, 0,
814 ref(newHVREF($1),OP_RV2HV),
816 PL_expect = XOPERATOR;
817 TOKEN_GETMAD($2,$$,'{');
818 TOKEN_GETMAD($4,$$,';');
819 TOKEN_GETMAD($5,$$,'}');
821 | term ARROW '(' ')' /* $subref->() */
822 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
823 newCVREF(0, scalar($1)));
824 TOKEN_GETMAD($2,$$,'a');
825 TOKEN_GETMAD($3,$$,'(');
826 TOKEN_GETMAD($4,$$,')');
828 | term ARROW '(' expr ')' /* $subref->(@args) */
829 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
830 append_elem(OP_LIST, $4,
831 newCVREF(0, scalar($1))));
832 TOKEN_GETMAD($2,$$,'a');
833 TOKEN_GETMAD($3,$$,'(');
834 TOKEN_GETMAD($5,$$,')');
837 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
838 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
839 append_elem(OP_LIST, $3,
840 newCVREF(0, scalar($1))));
841 TOKEN_GETMAD($2,$$,'(');
842 TOKEN_GETMAD($4,$$,')');
844 | subscripted '(' ')' /* $foo->{bar}->() */
845 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
846 newCVREF(0, scalar($1)));
847 TOKEN_GETMAD($2,$$,'(');
848 TOKEN_GETMAD($3,$$,')');
850 | '(' expr ')' '[' expr ']' /* list slice */
851 { $$ = newSLICEOP(0, $5, $2);
852 TOKEN_GETMAD($1,$$,'(');
853 TOKEN_GETMAD($3,$$,')');
854 TOKEN_GETMAD($4,$$,'[');
855 TOKEN_GETMAD($6,$$,']');
857 | '(' ')' '[' expr ']' /* empty list slice! */
858 { $$ = newSLICEOP(0, $4, Nullop);
859 TOKEN_GETMAD($1,$$,'(');
860 TOKEN_GETMAD($2,$$,')');
861 TOKEN_GETMAD($3,$$,'[');
862 TOKEN_GETMAD($5,$$,']');
866 /* Binary operators between terms */
867 termbinop: term ASSIGNOP term /* $x = $y */
868 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
869 TOKEN_GETMAD($2,$$,'o');
871 | term POWOP term /* $x ** $y */
872 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
873 TOKEN_GETMAD($2,$$,'o');
875 | term MULOP term /* $x * $y, $x x $y */
876 { if (IVAL($2) != OP_REPEAT)
878 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
879 TOKEN_GETMAD($2,$$,'o');
881 | term ADDOP term /* $x + $y */
882 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
883 TOKEN_GETMAD($2,$$,'o');
885 | term SHIFTOP term /* $x >> $y, $x << $y */
886 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
887 TOKEN_GETMAD($2,$$,'o');
889 | term RELOP term /* $x > $y, etc. */
890 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
891 TOKEN_GETMAD($2,$$,'o');
893 | term EQOP term /* $x == $y, $x eq $y */
894 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
895 TOKEN_GETMAD($2,$$,'o');
897 | term BITANDOP term /* $x & $y */
898 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
899 TOKEN_GETMAD($2,$$,'o');
901 | term BITOROP term /* $x | $y */
902 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
903 TOKEN_GETMAD($2,$$,'o');
905 | term DOTDOT term /* $x..$y, $x...$y */
907 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
911 op = (UNOP*)op->op_first; /* get to flop */
912 op = (UNOP*)op->op_first; /* get to flip */
913 op = (UNOP*)op->op_first; /* get to range */
914 token_getmad($2,(OP*)op,'o');
917 | term ANDAND term /* $x && $y */
918 { $$ = newLOGOP(OP_AND, 0, $1, $3);
919 TOKEN_GETMAD($2,$$,'o');
921 | term OROR term /* $x || $y */
922 { $$ = newLOGOP(OP_OR, 0, $1, $3);
923 TOKEN_GETMAD($2,$$,'o');
925 | term DORDOR term /* $x // $y */
926 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
927 TOKEN_GETMAD($2,$$,'o');
929 | term MATCHOP term /* $x =~ /$y/ */
930 { $$ = bind_match(IVAL($2), $1, $3);
932 ($$->op_type == OP_NOT
933 ? ((UNOP*)$$)->op_first : $$),
938 /* Unary operators and terms */
939 termunop : '-' term %prec UMINUS /* -$x */
940 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
941 TOKEN_GETMAD($1,$$,'o');
943 | '+' term %prec UMINUS /* +$x */
945 newUNOP(OP_NULL, 0, $2),
948 TOKEN_GETMAD($1,$$,'+');
951 { $$ = newUNOP(OP_NOT, 0, scalar($2));
952 TOKEN_GETMAD($1,$$,'o');
955 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
956 TOKEN_GETMAD($1,$$,'o');
958 | term POSTINC /* $x++ */
959 { $$ = newUNOP(OP_POSTINC, 0,
960 mod(scalar($1), OP_POSTINC));
961 TOKEN_GETMAD($2,$$,'o');
963 | term POSTDEC /* $x-- */
964 { $$ = newUNOP(OP_POSTDEC, 0,
965 mod(scalar($1), OP_POSTDEC));
966 TOKEN_GETMAD($2,$$,'o');
968 | PREINC term /* ++$x */
969 { $$ = newUNOP(OP_PREINC, 0,
970 mod(scalar($2), OP_PREINC));
971 TOKEN_GETMAD($1,$$,'o');
973 | PREDEC term /* --$x */
974 { $$ = newUNOP(OP_PREDEC, 0,
975 mod(scalar($2), OP_PREDEC));
976 TOKEN_GETMAD($1,$$,'o');
981 /* Constructors for anonymous data */
982 anonymous: '[' expr ']'
983 { $$ = newANONLIST($2);
984 TOKEN_GETMAD($1,$$,'[');
985 TOKEN_GETMAD($3,$$,']');
988 { $$ = newANONLIST(Nullop);
989 TOKEN_GETMAD($1,$$,'[');
990 TOKEN_GETMAD($2,$$,']');
992 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
993 { $$ = newANONHASH($2);
994 TOKEN_GETMAD($1,$$,'{');
995 TOKEN_GETMAD($3,$$,';');
996 TOKEN_GETMAD($4,$$,'}');
998 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
999 { $$ = newANONHASH(Nullop);
1000 TOKEN_GETMAD($1,$$,'{');
1001 TOKEN_GETMAD($2,$$,';');
1002 TOKEN_GETMAD($3,$$,'}');
1004 | ANONSUB startanonsub proto subattrlist block %prec '('
1005 { SvREFCNT_inc(PL_compcv);
1006 $$ = newANONATTRSUB($2, $3, $4, $5);
1007 TOKEN_GETMAD($1,$$,'o');
1008 OP_GETMAD($3,$$,'s');
1009 OP_GETMAD($4,$$,'a');
1014 /* Things called with "do" */
1015 termdo : DO term %prec UNIOP /* do $filename */
1016 { $$ = dofile($2, IVAL($1));
1017 TOKEN_GETMAD($1,$$,'o');
1019 | DO block %prec '(' /* do { code */
1020 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1021 TOKEN_GETMAD($1,$$,'D');
1023 | DO WORD '(' ')' /* do somesub() */
1024 { $$ = newUNOP(OP_ENTERSUB,
1025 OPf_SPECIAL|OPf_STACKED,
1026 prepend_elem(OP_LIST,
1028 (OPpENTERSUB_AMPER<<8),
1031 TOKEN_GETMAD($1,$$,'o');
1032 TOKEN_GETMAD($3,$$,'(');
1033 TOKEN_GETMAD($4,$$,')');
1035 | DO WORD '(' expr ')' /* do somesub(@args) */
1036 { $$ = newUNOP(OP_ENTERSUB,
1037 OPf_SPECIAL|OPf_STACKED,
1038 append_elem(OP_LIST,
1041 (OPpENTERSUB_AMPER<<8),
1044 TOKEN_GETMAD($1,$$,'o');
1045 TOKEN_GETMAD($3,$$,'(');
1046 TOKEN_GETMAD($5,$$,')');
1048 | DO scalar '(' ')' /* do $subref () */
1049 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1050 prepend_elem(OP_LIST,
1051 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
1052 TOKEN_GETMAD($1,$$,'o');
1053 TOKEN_GETMAD($3,$$,'(');
1054 TOKEN_GETMAD($4,$$,')');
1056 | DO scalar '(' expr ')' /* do $subref (@args) */
1057 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1058 prepend_elem(OP_LIST,
1060 scalar(newCVREF(0,scalar($2))))); dep();
1061 TOKEN_GETMAD($1,$$,'o');
1062 TOKEN_GETMAD($3,$$,'(');
1063 TOKEN_GETMAD($5,$$,')');
1072 | term '?' term ':' term
1073 { $$ = newCONDOP(0, $1, $3, $5);
1074 TOKEN_GETMAD($2,$$,'?');
1075 TOKEN_GETMAD($4,$$,':');
1077 | REFGEN term /* \$x, \@y, \%z */
1078 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1079 TOKEN_GETMAD($1,$$,'o');
1081 | myattrterm %prec UNIOP
1083 | LOCAL term %prec UNIOP
1084 { $$ = localize($2,IVAL($1));
1085 TOKEN_GETMAD($1,$$,'d');
1088 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1089 TOKEN_GETMAD($1,$$,'(');
1090 TOKEN_GETMAD($3,$$,')');
1093 { $$ = sawparens(newNULLLIST());
1094 TOKEN_GETMAD($1,$$,'(');
1095 TOKEN_GETMAD($2,$$,')');
1105 | arylen %prec '(' /* $#x, $#{ something } */
1106 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1109 | ary '[' expr ']' /* array slice */
1110 { $$ = prepend_elem(OP_ASLICE,
1111 newOP(OP_PUSHMARK, 0),
1112 newLISTOP(OP_ASLICE, 0,
1114 ref($1, OP_ASLICE)));
1115 TOKEN_GETMAD($2,$$,'[');
1116 TOKEN_GETMAD($4,$$,']');
1118 | ary '{' expr ';' '}' /* @hash{@keys} */
1119 { $$ = prepend_elem(OP_HSLICE,
1120 newOP(OP_PUSHMARK, 0),
1121 newLISTOP(OP_HSLICE, 0,
1123 ref(oopsHV($1), OP_HSLICE)));
1124 PL_expect = XOPERATOR;
1125 TOKEN_GETMAD($2,$$,'{');
1126 TOKEN_GETMAD($4,$$,';');
1127 TOKEN_GETMAD($5,$$,'}');
1132 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1133 | amper '(' ')' /* &foo() */
1134 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1135 TOKEN_GETMAD($2,$$,'(');
1136 TOKEN_GETMAD($3,$$,')');
1138 | amper '(' expr ')' /* &foo(@args) */
1140 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1141 append_elem(OP_LIST, $3, scalar($1)));
1144 if (op->op_type == OP_CONST) { /* defeat const fold */
1145 op = (OP*)op->op_madprop->mad_val;
1147 token_getmad($2,op,'(');
1148 token_getmad($4,op,')');
1151 | NOAMP WORD listexpr /* foo(@args) */
1152 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1153 append_elem(OP_LIST, $3, scalar($2)));
1154 TOKEN_GETMAD($1,$$,'o');
1156 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1157 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1158 PL_hints |= HINT_BLOCK_SCOPE;
1159 TOKEN_GETMAD($1,$$,'o');
1162 { $$ = newLOOPEX(IVAL($1),$2);
1163 TOKEN_GETMAD($1,$$,'o');
1165 | NOTOP argexpr /* not $foo */
1166 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1167 TOKEN_GETMAD($1,$$,'o');
1169 | UNIOP /* Unary op, $_ implied */
1170 { $$ = newOP(IVAL($1), 0);
1171 TOKEN_GETMAD($1,$$,'o');
1173 | UNIOP block /* eval { foo }* */
1174 { $$ = newUNOP(IVAL($1), 0, $2);
1175 TOKEN_GETMAD($1,$$,'o');
1177 | UNIOP term /* Unary op */
1178 { $$ = newUNOP(IVAL($1), 0, $2);
1179 TOKEN_GETMAD($1,$$,'o');
1181 | REQUIRE /* require, $_ implied *//* FIMXE for MAD needed? */
1182 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1183 | REQUIRE term /* require Foo *//* FIMXE for MAD needed? */
1184 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1186 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1187 | UNIOPSUB term /* Sub treated as unop */
1188 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1189 append_elem(OP_LIST, $2, scalar($1))); }
1190 | FUNC0 /* Nullary operator */
1191 { $$ = newOP(IVAL($1), 0);
1192 TOKEN_GETMAD($1,$$,'o');
1195 { $$ = newOP(IVAL($1), 0);
1196 TOKEN_GETMAD($1,$$,'o');
1197 TOKEN_GETMAD($2,$$,'(');
1198 TOKEN_GETMAD($3,$$,')');
1200 | FUNC0SUB /* Sub treated as nullop */
1201 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1203 | FUNC1 '(' ')' /* not () */
1204 { $$ = (IVAL($1) == OP_NOT)
1205 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1206 : newOP(IVAL($1), OPf_SPECIAL);
1208 TOKEN_GETMAD($1,$$,'o');
1209 TOKEN_GETMAD($2,$$,'(');
1210 TOKEN_GETMAD($3,$$,')');
1212 | FUNC1 '(' expr ')' /* not($foo) */
1213 { $$ = newUNOP(IVAL($1), 0, $3);
1214 TOKEN_GETMAD($1,$$,'o');
1215 TOKEN_GETMAD($2,$$,'(');
1216 TOKEN_GETMAD($4,$$,')');
1218 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1219 { $$ = pmruntime($1, $3, 1);
1220 TOKEN_GETMAD($2,$$,'(');
1221 TOKEN_GETMAD($4,$$,')');
1227 /* "my" declarations, with optional attributes */
1228 myattrterm: MY myterm myattrlist
1229 { $$ = my_attrs($2,$3);
1231 token_getmad($1,$$,'d');
1232 append_madprops($3->op_madprop, $$, 'a');
1237 { $$ = localize($2,IVAL($1));
1238 TOKEN_GETMAD($1,$$,'d');
1242 /* Things that can be "my"'d */
1243 myterm : '(' expr ')'
1244 { $$ = sawparens($2);
1245 TOKEN_GETMAD($1,$$,'(');
1246 TOKEN_GETMAD($3,$$,')');
1249 { $$ = sawparens(newNULLLIST());
1250 TOKEN_GETMAD($1,$$,'(');
1251 TOKEN_GETMAD($2,$$,')');
1261 /* Basic list expressions */
1262 listexpr: /* NULL */ %prec PREC_LOW
1264 | argexpr %prec PREC_LOW
1268 listexprcom: /* NULL */
1275 OP* op = newNULLLIST();
1276 token_getmad($2,op,',');
1277 $$ = append_elem(OP_LIST, $1, op);
1285 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1288 { PL_in_my = 0; $$ = my($1); }
1292 { $$ = newCVREF(IVAL($1),$2);
1293 TOKEN_GETMAD($1,$$,'&');
1297 scalar : '$' indirob
1298 { $$ = newSVREF($2);
1299 TOKEN_GETMAD($1,$$,'$');
1304 { $$ = newAVREF($2);
1305 TOKEN_GETMAD($1,$$,'@');
1310 { $$ = newHVREF($2);
1311 TOKEN_GETMAD($1,$$,'%');
1315 arylen : DOLSHARP indirob
1316 { $$ = newAVREF($2);
1317 TOKEN_GETMAD($1,$$,'l');
1322 { $$ = newGVREF(0,$2);
1323 TOKEN_GETMAD($1,$$,'*');
1327 /* Indirect objects */
1329 { $$ = scalar($1); }
1330 | scalar %prec PREC_LOW
1331 { $$ = scalar($1); }