3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
4 * Copyright (c) 2007, 2008 by Larry Wall and others
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.
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.'
15 * [p.171 of _The Lord of the Rings_, I/x: "Strider"]
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.
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:
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)
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.
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
46 /* Make the parser re-entrant. */
50 /* FIXME for MAD - is the new mintro on while and until important? */
55 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56 must always be 1st union member) */
60 #ifdef PERL_IN_MADLY_C
72 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
74 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
75 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
76 %token <opval> PLUGEXPR PLUGSTMT
77 %token <p_tkval> LABEL
78 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
79 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
80 %token <i_tkval> GIVEN WHEN DEFAULT
81 %token <i_tkval> LOOPEX DOTDOT YADAYADA
82 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
83 %token <i_tkval> RELOP EQOP MULOP ADDOP
84 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
85 %token <i_tkval> LOCAL MY MYSUB REQUIRE
86 %token <i_tkval> COLONATTR
88 %type <ival> prog progstart remember mremember
89 %type <ival> startsub startanonsub startformsub
90 /* FIXME for MAD - are these two ival? */
91 %type <ival> mydefsv mintro
93 %type <opval> decl format subrout mysubrout package use peg
95 %type <opval> block mblock lineseq line loop cond else
96 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
97 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
98 %type <opval> listexpr listexprcom indirob listop method
99 %type <opval> formname subname proto subbody cont my_scalar
100 %type <opval> subattrlist myattrlist myattrterm myterm
101 %type <opval> termbinop termunop anonymous termdo
102 %type <opval> switch case
103 %type <p_tkval> label
105 %nonassoc <i_tkval> PREC_LOW
108 %left <i_tkval> OROP DOROP
109 %left <i_tkval> ANDOP
110 %right <i_tkval> NOTOP
111 %nonassoc LSTOP LSTOPSUB
113 %right <i_tkval> ASSIGNOP
114 %right <i_tkval> '?' ':'
115 %nonassoc DOTDOT YADAYADA
116 %left <i_tkval> OROR DORDOR
117 %left <i_tkval> ANDAND
118 %left <i_tkval> BITOROP
119 %left <i_tkval> BITANDOP
122 %nonassoc UNIOP UNIOPSUB
124 %left <i_tkval> SHIFTOP
127 %left <i_tkval> MATCHOP
128 %right <i_tkval> '!' '~' UMINUS REFGEN
129 %right <i_tkval> POWOP
130 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
131 %left <i_tkval> ARROW
132 %nonassoc <i_tkval> ')'
140 /* The whole program */
142 /*CONTINUED*/ lineseq
143 { $$ = $1; newPROG(block_end($1,$2)); }
146 /* An ordinary block */
147 block : '{' remember lineseq '}'
148 { if (PL_parser->copline > (line_t)IVAL($1))
149 PL_parser->copline = (line_t)IVAL($1);
150 $$ = block_end($2, $3);
151 TOKEN_GETMAD($1,$$,'{');
152 TOKEN_GETMAD($4,$$,'}');
156 remember: /* NULL */ /* start a full lexical scope */
157 { $$ = block_start(TRUE); }
160 mydefsv: /* NULL */ /* lexicalize $_ */
161 { $$ = (I32) allocmy("$_"); }
166 PL_parser->expect = XSTATE; $$ = block_start(TRUE);
171 mblock : '{' mremember lineseq '}'
172 { if (PL_parser->copline > (line_t)IVAL($1))
173 PL_parser->copline = (line_t)IVAL($1);
174 $$ = block_end($2, $3);
175 TOKEN_GETMAD($1,$$,'{');
176 TOKEN_GETMAD($4,$$,'}');
180 mremember: /* NULL */ /* start a partial lexical scope */
181 { $$ = block_start(FALSE); }
184 /* A collection of "lines" in the program */
190 append_list(OP_LINESEQ,
191 (LISTOP*)$1, (LISTOP*)$2),
195 { $$ = append_list(OP_LINESEQ,
196 (LISTOP*)$1, (LISTOP*)$2);
197 PL_pad_reset_pending = TRUE;
199 PL_hints |= HINT_BLOCK_SCOPE;
203 /* A "line" in the program */
205 { $$ = newSTATEOP(0, PVAL($1), $2);
206 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
207 | loop /* loops add their own labels */
208 | switch /* ... and so do switches */
211 { $$ = newSTATEOP(0, PVAL($1), $2); }
215 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
216 TOKEN_GETMAD($1,$$,'L');
217 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
223 PL_parser->copline = NOLINE;
225 TOKEN_GETMAD($2,$$,';');
227 PL_parser->expect = XSTATE;
231 $$ = newSTATEOP(0, PVAL($1), $2);
232 PL_parser->expect = XSTATE;
234 /* sideff might already have a nexstate */
235 OP* op = ((LISTOP*)$$)->op_first;
237 while (op->op_sibling &&
238 op->op_sibling->op_type == OP_NEXTSTATE)
240 token_getmad($1,op,'L');
241 token_getmad($3,op,';');
246 { $$ = newSTATEOP(0, PVAL($1), $2); }
249 /* An expression which may have a side-effect */
255 { $$ = newLOGOP(OP_AND, 0, $3, $1);
256 TOKEN_GETMAD($2,$$,'i');
259 { $$ = newLOGOP(OP_OR, 0, $3, $1);
260 TOKEN_GETMAD($2,$$,'i');
263 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
264 TOKEN_GETMAD($2,$$,'w');
267 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
268 TOKEN_GETMAD($2,$$,'w');
271 { $$ = newFOROP(0, NULL, (line_t)IVAL($2),
272 (OP*)NULL, $3, $1, (OP*)NULL);
273 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
276 { $$ = newWHENOP($3, scope($1)); }
279 /* else and elsif blocks */
283 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
284 TOKEN_GETMAD($1,$$,'o');
286 | ELSIF '(' mexpr ')' mblock else
287 { PL_parser->copline = (line_t)IVAL($1);
288 $$ = newCONDOP(0, newSTATEOP(OPf_SPECIAL,NULL,$3), scope($5), $6);
289 PL_hints |= HINT_BLOCK_SCOPE;
290 TOKEN_GETMAD($1,$$,'I');
291 TOKEN_GETMAD($2,$$,'(');
292 TOKEN_GETMAD($4,$$,')');
296 /* Real conditional expressions */
297 cond : IF '(' remember mexpr ')' mblock else
298 { PL_parser->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,$$,')');
305 | UNLESS '(' remember miexpr ')' mblock else
306 { PL_parser->copline = (line_t)IVAL($1);
308 newCONDOP(0, $4, scope($6), $7));
309 TOKEN_GETMAD($1,$$,'I');
310 TOKEN_GETMAD($2,$$,'(');
311 TOKEN_GETMAD($5,$$,')');
315 /* Cases for a switch statement */
316 case : WHEN '(' remember mexpr ')' mblock
318 newWHENOP($4, scope($6))); }
320 { $$ = newWHENOP(0, scope($2)); }
323 /* Continue blocks */
328 TOKEN_GETMAD($1,$$,'o');
332 /* Loops: while, until, for, and a bare block */
333 loop : label WHILE '(' remember texpr ')' mintro mblock cont
335 PL_parser->copline = (line_t)$2;
337 newSTATEOP(0, PVAL($1),
338 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
339 IVAL($2), $5, $8, $9, $7)));
340 TOKEN_GETMAD($1,innerop,'L');
341 TOKEN_GETMAD($2,innerop,'W');
342 TOKEN_GETMAD($3,innerop,'(');
343 TOKEN_GETMAD($6,innerop,')');
346 | label UNTIL '(' remember iexpr ')' mintro mblock cont
348 PL_parser->copline = (line_t)$2;
350 newSTATEOP(0, PVAL($1),
351 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
352 IVAL($2), $5, $8, $9, $7)));
353 TOKEN_GETMAD($1,innerop,'L');
354 TOKEN_GETMAD($2,innerop,'W');
355 TOKEN_GETMAD($3,innerop,'(');
356 TOKEN_GETMAD($6,innerop,')');
358 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
361 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
363 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
364 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
365 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
366 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
367 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
369 | label FOR scalar '(' remember mexpr ')' mblock cont
372 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
373 mod($3, OP_ENTERLOOP), $6, $8, $9));
374 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
375 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
376 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
377 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
379 | label FOR '(' remember mexpr ')' mblock cont
382 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
383 (OP*)NULL, $5, $7, $8));
384 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
385 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
386 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
387 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
389 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
391 /* basically fake up an initialize-while lineseq */
393 PL_parser->copline = (line_t)IVAL($2);
394 forop = newSTATEOP(0, PVAL($1),
395 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
396 IVAL($2), scalar($7),
399 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
401 CopLABEL_alloc(($1)->tk_lval.pval),
402 ($5 ? $5 : newOP(OP_NULL, 0)) ),
405 token_getmad($2,forop,'3');
406 token_getmad($3,forop,'(');
407 token_getmad($6,forop,'1');
408 token_getmad($8,forop,'2');
409 token_getmad($11,forop,')');
410 token_getmad($1,forop,'L');
413 forop = append_elem(OP_LINESEQ,
414 newSTATEOP(0, CopLABEL_alloc($1), $5),
420 $$ = block_end($4, forop); }
421 | label block cont /* a block is a loop that happens once */
422 { $$ = newSTATEOP(0, PVAL($1),
423 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
424 NOLINE, (OP*)NULL, $2, $3, 0));
425 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
429 switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
430 { PL_parser->copline = (line_t) $2;
432 newSTATEOP(0, PVAL($1),
433 newGIVENOP($6, scope($8),
434 (PADOFFSET) $5) )); }
437 /* determine whether there are any new my declarations */
439 { $$ = (PL_min_intro_pending &&
440 PL_max_intro_pending >= PL_min_intro_pending);
443 /* Normal expression */
449 /* Boolean expression */
450 texpr : /* NULL means true */
452 (void)scan_num("1", &tmplval);
453 $$ = tmplval.opval; }
457 /* Inverted boolean expression */
459 { $$ = invert(scalar($1)); }
462 /* Expression with its own lexical scope */
464 { $$ = $1; intro_my(); }
468 { $$ = $1; intro_my(); }
472 { $$ = $1; intro_my(); }
475 /* Optional "MAIN:"-style loop labels */
481 $$ = newTOKEN(OP_NULL, tmplval, 0);
489 /* Some kind of declaration - just hang on peg in the parse tree */
501 /* these two are only used by MAD */
508 { $$ = newOP(OP_NULL,0);
509 TOKEN_GETMAD($1,$$,'p');
513 format : FORMAT startformsub formname block
514 { SvREFCNT_inc_simple_void(PL_compcv);
516 $$ = newFORM($2, $3, $4);
517 prepend_madprops($1->tk_mad, $$, 'F');
527 formname: WORD { $$ = $1; }
528 | /* NULL */ { $$ = (OP*)NULL; }
531 /* Unimplemented "my sub foo { }" */
532 mysubrout: MYSUB startsub subname proto subattrlist subbody
533 { SvREFCNT_inc_simple_void(PL_compcv);
535 $$ = newMYSUB($2, $3, $4, $5, $6);
536 token_getmad($1,$$,'d');
538 newMYSUB($2, $3, $4, $5, $6);
544 /* Subroutine definition */
545 subrout : SUB startsub subname proto subattrlist subbody
546 { SvREFCNT_inc_simple_void(PL_compcv);
549 OP* o = newSVOP(OP_ANONCODE, 0,
550 (SV*)newATTRSUB($2, $3, $4, $5, $6));
551 $$ = newOP(OP_NULL,0);
553 op_getmad($3,$$,'n');
554 op_getmad($4,$$,'s');
555 op_getmad($5,$$,'a');
556 token_getmad($1,$$,'d');
557 append_madprops($6->op_madprop, $$, 0);
561 newATTRSUB($2, $3, $4, $5, $6);
567 startsub: /* NULL */ /* start a regular subroutine scope */
568 { $$ = start_subparse(FALSE, 0);
569 SAVEFREESV(PL_compcv); }
573 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
574 { $$ = start_subparse(FALSE, CVf_ANON);
575 SAVEFREESV(PL_compcv); }
578 startformsub: /* NULL */ /* start a format subroutine scope */
579 { $$ = start_subparse(TRUE, 0);
580 SAVEFREESV(PL_compcv); }
583 /* Name of a subroutine - must be a bareword, could be special */
584 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
585 if (strEQ(name, "BEGIN") || strEQ(name, "END")
586 || strEQ(name, "INIT") || strEQ(name, "CHECK")
587 || strEQ(name, "UNITCHECK"))
588 CvSPECIAL_on(PL_compcv);
592 /* Subroutine prototype */
598 /* Optional list of subroutine attributes */
599 subattrlist: /* NULL */
603 TOKEN_GETMAD($1,$$,':');
610 TOKEN_GETMAD($1,$$,':');
614 /* List of attributes for a "my" variable declaration */
615 myattrlist: COLONATTR THING
617 TOKEN_GETMAD($1,$$,':');
624 TOKEN_GETMAD($1,$$,':');
628 /* Subroutine body - either null or a block */
629 subbody : block { $$ = $1; }
634 PL_parser->expect = XSTATE;
635 TOKEN_GETMAD($1,$$,';');
639 package : PACKAGE WORD WORD ';'
643 token_getmad($1,$$,'o');
646 token_getmad($4,$$,';');
657 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
658 WORD WORD listexpr ';'
659 { SvREFCNT_inc_simple_void(PL_compcv);
661 $$ = utilize(IVAL($1), $2, $4, $5, $6);
662 token_getmad($1,$$,'o');
663 token_getmad($7,$$,';');
664 if (PL_parser->rsfp_filters &&
665 AvFILLp(PL_parser->rsfp_filters) >= 0)
666 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
668 utilize(IVAL($1), $2, $4, $5, $6);
674 /* Ordinary expressions; logical combinations */
675 expr : expr ANDOP expr
676 { $$ = newLOGOP(OP_AND, 0, $1, $3);
677 TOKEN_GETMAD($2,$$,'o');
680 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
681 TOKEN_GETMAD($2,$$,'o');
684 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
685 TOKEN_GETMAD($2,$$,'o');
687 | argexpr %prec PREC_LOW
690 /* Expressions are a list of terms joined by commas */
691 argexpr : argexpr ','
694 OP* op = newNULLLIST();
695 token_getmad($2,op,',');
696 $$ = append_elem(OP_LIST, $1, op);
705 term = newUNOP(OP_NULL, 0, term);
706 token_getmad($2,term,',');
708 $$ = append_elem(OP_LIST, $1, term);
710 | term %prec PREC_LOW
714 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
715 { $$ = convert(IVAL($1), OPf_STACKED,
716 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
717 TOKEN_GETMAD($1,$$,'o');
719 | FUNC '(' indirob expr ')' /* print ($fh @args */
720 { $$ = convert(IVAL($1), OPf_STACKED,
721 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
722 TOKEN_GETMAD($1,$$,'o');
723 TOKEN_GETMAD($2,$$,'(');
724 TOKEN_GETMAD($5,$$,')');
726 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
727 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
729 prepend_elem(OP_LIST, scalar($1), $5),
730 newUNOP(OP_METHOD, 0, $3)));
731 TOKEN_GETMAD($2,$$,'A');
732 TOKEN_GETMAD($4,$$,'(');
733 TOKEN_GETMAD($6,$$,')');
735 | term ARROW method /* $foo->bar */
736 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
737 append_elem(OP_LIST, scalar($1),
738 newUNOP(OP_METHOD, 0, $3)));
739 TOKEN_GETMAD($2,$$,'A');
741 | METHOD indirob listexpr /* new Class @args */
742 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
744 prepend_elem(OP_LIST, $2, $3),
745 newUNOP(OP_METHOD, 0, $1)));
747 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
748 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
750 prepend_elem(OP_LIST, $2, $4),
751 newUNOP(OP_METHOD, 0, $1)));
752 TOKEN_GETMAD($3,$$,'(');
753 TOKEN_GETMAD($5,$$,')');
755 | LSTOP listexpr /* print @args */
756 { $$ = convert(IVAL($1), 0, $2);
757 TOKEN_GETMAD($1,$$,'o');
759 | FUNC '(' listexprcom ')' /* print (@args) */
760 { $$ = convert(IVAL($1), 0, $3);
761 TOKEN_GETMAD($1,$$,'o');
762 TOKEN_GETMAD($2,$$,'(');
763 TOKEN_GETMAD($4,$$,')');
765 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
766 { SvREFCNT_inc_simple_void(PL_compcv);
767 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
768 listexpr %prec LSTOP /* ... @bar */
769 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
771 prepend_elem(OP_LIST, $<opval>4, $5), $1));
775 /* Names of methods. May use $object->$methodname */
780 /* Some kind of subscripted expression */
781 subscripted: star '{' expr ';' '}' /* *main::{something} */
782 /* In this and all the hash accessors, ';' is
783 * provided by the tokeniser */
784 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
785 PL_parser->expect = XOPERATOR;
786 TOKEN_GETMAD($2,$$,'{');
787 TOKEN_GETMAD($4,$$,';');
788 TOKEN_GETMAD($5,$$,'}');
790 | scalar '[' expr ']' /* $array[$element] */
791 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
792 TOKEN_GETMAD($2,$$,'[');
793 TOKEN_GETMAD($4,$$,']');
795 | term ARROW '[' expr ']' /* somearef->[$element] */
796 { $$ = newBINOP(OP_AELEM, 0,
797 ref(newAVREF($1),OP_RV2AV),
799 TOKEN_GETMAD($2,$$,'a');
800 TOKEN_GETMAD($3,$$,'[');
801 TOKEN_GETMAD($5,$$,']');
803 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
804 { $$ = newBINOP(OP_AELEM, 0,
805 ref(newAVREF($1),OP_RV2AV),
807 TOKEN_GETMAD($2,$$,'[');
808 TOKEN_GETMAD($4,$$,']');
810 | scalar '{' expr ';' '}' /* $foo->{bar();} */
811 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
812 PL_parser->expect = XOPERATOR;
813 TOKEN_GETMAD($2,$$,'{');
814 TOKEN_GETMAD($4,$$,';');
815 TOKEN_GETMAD($5,$$,'}');
817 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
818 { $$ = newBINOP(OP_HELEM, 0,
819 ref(newHVREF($1),OP_RV2HV),
821 PL_parser->expect = XOPERATOR;
822 TOKEN_GETMAD($2,$$,'a');
823 TOKEN_GETMAD($3,$$,'{');
824 TOKEN_GETMAD($5,$$,';');
825 TOKEN_GETMAD($6,$$,'}');
827 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
828 { $$ = newBINOP(OP_HELEM, 0,
829 ref(newHVREF($1),OP_RV2HV),
831 PL_parser->expect = XOPERATOR;
832 TOKEN_GETMAD($2,$$,'{');
833 TOKEN_GETMAD($4,$$,';');
834 TOKEN_GETMAD($5,$$,'}');
836 | term ARROW '(' ')' /* $subref->() */
837 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
838 newCVREF(0, scalar($1)));
839 TOKEN_GETMAD($2,$$,'a');
840 TOKEN_GETMAD($3,$$,'(');
841 TOKEN_GETMAD($4,$$,')');
843 | term ARROW '(' expr ')' /* $subref->(@args) */
844 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
845 append_elem(OP_LIST, $4,
846 newCVREF(0, scalar($1))));
847 TOKEN_GETMAD($2,$$,'a');
848 TOKEN_GETMAD($3,$$,'(');
849 TOKEN_GETMAD($5,$$,')');
852 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
853 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
854 append_elem(OP_LIST, $3,
855 newCVREF(0, scalar($1))));
856 TOKEN_GETMAD($2,$$,'(');
857 TOKEN_GETMAD($4,$$,')');
859 | subscripted '(' ')' /* $foo->{bar}->() */
860 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
861 newCVREF(0, scalar($1)));
862 TOKEN_GETMAD($2,$$,'(');
863 TOKEN_GETMAD($3,$$,')');
865 | '(' expr ')' '[' expr ']' /* list slice */
866 { $$ = newSLICEOP(0, $5, $2);
867 TOKEN_GETMAD($1,$$,'(');
868 TOKEN_GETMAD($3,$$,')');
869 TOKEN_GETMAD($4,$$,'[');
870 TOKEN_GETMAD($6,$$,']');
872 | '(' ')' '[' expr ']' /* empty list slice! */
873 { $$ = newSLICEOP(0, $4, (OP*)NULL);
874 TOKEN_GETMAD($1,$$,'(');
875 TOKEN_GETMAD($2,$$,')');
876 TOKEN_GETMAD($3,$$,'[');
877 TOKEN_GETMAD($5,$$,']');
881 /* Binary operators between terms */
882 termbinop: term ASSIGNOP term /* $x = $y */
883 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
884 TOKEN_GETMAD($2,$$,'o');
886 | term POWOP term /* $x ** $y */
887 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
888 TOKEN_GETMAD($2,$$,'o');
890 | term MULOP term /* $x * $y, $x x $y */
891 { if (IVAL($2) != OP_REPEAT)
893 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
894 TOKEN_GETMAD($2,$$,'o');
896 | term ADDOP term /* $x + $y */
897 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
898 TOKEN_GETMAD($2,$$,'o');
900 | term SHIFTOP term /* $x >> $y, $x << $y */
901 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
902 TOKEN_GETMAD($2,$$,'o');
904 | term RELOP term /* $x > $y, etc. */
905 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
906 TOKEN_GETMAD($2,$$,'o');
908 | term EQOP term /* $x == $y, $x eq $y */
909 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
910 TOKEN_GETMAD($2,$$,'o');
912 | term BITANDOP term /* $x & $y */
913 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
914 TOKEN_GETMAD($2,$$,'o');
916 | term BITOROP term /* $x | $y */
917 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
918 TOKEN_GETMAD($2,$$,'o');
920 | term DOTDOT term /* $x..$y, $x...$y */
922 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
926 op = (UNOP*)op->op_first; /* get to flop */
927 op = (UNOP*)op->op_first; /* get to flip */
928 op = (UNOP*)op->op_first; /* get to range */
929 token_getmad($2,(OP*)op,'o');
932 | term ANDAND term /* $x && $y */
933 { $$ = newLOGOP(OP_AND, 0, $1, $3);
934 TOKEN_GETMAD($2,$$,'o');
936 | term OROR term /* $x || $y */
937 { $$ = newLOGOP(OP_OR, 0, $1, $3);
938 TOKEN_GETMAD($2,$$,'o');
940 | term DORDOR term /* $x // $y */
941 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
942 TOKEN_GETMAD($2,$$,'o');
944 | term MATCHOP term /* $x =~ /$y/ */
945 { $$ = bind_match(IVAL($2), $1, $3);
947 ($$->op_type == OP_NOT
948 ? ((UNOP*)$$)->op_first : $$),
953 /* Unary operators and terms */
954 termunop : '-' term %prec UMINUS /* -$x */
955 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
956 TOKEN_GETMAD($1,$$,'o');
958 | '+' term %prec UMINUS /* +$x */
960 newUNOP(OP_NULL, 0, $2),
963 TOKEN_GETMAD($1,$$,'+');
966 { $$ = newUNOP(OP_NOT, 0, scalar($2));
967 TOKEN_GETMAD($1,$$,'o');
970 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
971 TOKEN_GETMAD($1,$$,'o');
973 | term POSTINC /* $x++ */
974 { $$ = newUNOP(OP_POSTINC, 0,
975 mod(scalar($1), OP_POSTINC));
976 TOKEN_GETMAD($2,$$,'o');
978 | term POSTDEC /* $x-- */
979 { $$ = newUNOP(OP_POSTDEC, 0,
980 mod(scalar($1), OP_POSTDEC));
981 TOKEN_GETMAD($2,$$,'o');
983 | PREINC term /* ++$x */
984 { $$ = newUNOP(OP_PREINC, 0,
985 mod(scalar($2), OP_PREINC));
986 TOKEN_GETMAD($1,$$,'o');
988 | PREDEC term /* --$x */
989 { $$ = newUNOP(OP_PREDEC, 0,
990 mod(scalar($2), OP_PREDEC));
991 TOKEN_GETMAD($1,$$,'o');
996 /* Constructors for anonymous data */
997 anonymous: '[' expr ']'
998 { $$ = newANONLIST($2);
999 TOKEN_GETMAD($1,$$,'[');
1000 TOKEN_GETMAD($3,$$,']');
1003 { $$ = newANONLIST((OP*)NULL);
1004 TOKEN_GETMAD($1,$$,'[');
1005 TOKEN_GETMAD($2,$$,']');
1007 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1008 { $$ = newANONHASH($2);
1009 TOKEN_GETMAD($1,$$,'{');
1010 TOKEN_GETMAD($3,$$,';');
1011 TOKEN_GETMAD($4,$$,'}');
1013 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1014 { $$ = newANONHASH((OP*)NULL);
1015 TOKEN_GETMAD($1,$$,'{');
1016 TOKEN_GETMAD($2,$$,';');
1017 TOKEN_GETMAD($3,$$,'}');
1019 | ANONSUB startanonsub proto subattrlist block %prec '('
1020 { SvREFCNT_inc_simple_void(PL_compcv);
1021 $$ = newANONATTRSUB($2, $3, $4, $5);
1022 TOKEN_GETMAD($1,$$,'o');
1023 OP_GETMAD($3,$$,'s');
1024 OP_GETMAD($4,$$,'a');
1029 /* Things called with "do" */
1030 termdo : DO term %prec UNIOP /* do $filename */
1031 { $$ = dofile($2, IVAL($1));
1032 TOKEN_GETMAD($1,$$,'o');
1034 | DO block %prec '(' /* do { code */
1035 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1036 TOKEN_GETMAD($1,$$,'D');
1038 | DO WORD '(' ')' /* do somesub() */
1039 { $$ = newUNOP(OP_ENTERSUB,
1040 OPf_SPECIAL|OPf_STACKED,
1041 prepend_elem(OP_LIST,
1043 (OPpENTERSUB_AMPER<<8),
1045 )),(OP*)NULL)); dep();
1046 TOKEN_GETMAD($1,$$,'o');
1047 TOKEN_GETMAD($3,$$,'(');
1048 TOKEN_GETMAD($4,$$,')');
1050 | DO WORD '(' expr ')' /* do somesub(@args) */
1051 { $$ = newUNOP(OP_ENTERSUB,
1052 OPf_SPECIAL|OPf_STACKED,
1053 append_elem(OP_LIST,
1056 (OPpENTERSUB_AMPER<<8),
1059 TOKEN_GETMAD($1,$$,'o');
1060 TOKEN_GETMAD($3,$$,'(');
1061 TOKEN_GETMAD($5,$$,')');
1063 | DO scalar '(' ')' /* do $subref () */
1064 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1065 prepend_elem(OP_LIST,
1066 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1067 TOKEN_GETMAD($1,$$,'o');
1068 TOKEN_GETMAD($3,$$,'(');
1069 TOKEN_GETMAD($4,$$,')');
1071 | DO scalar '(' expr ')' /* do $subref (@args) */
1072 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1073 prepend_elem(OP_LIST,
1075 scalar(newCVREF(0,scalar($2))))); dep();
1076 TOKEN_GETMAD($1,$$,'o');
1077 TOKEN_GETMAD($3,$$,'(');
1078 TOKEN_GETMAD($5,$$,')');
1087 | term '?' term ':' term
1088 { $$ = newCONDOP(0, $1, $3, $5);
1089 TOKEN_GETMAD($2,$$,'?');
1090 TOKEN_GETMAD($4,$$,':');
1092 | REFGEN term /* \$x, \@y, \%z */
1093 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1094 TOKEN_GETMAD($1,$$,'o');
1096 | myattrterm %prec UNIOP
1098 | LOCAL term %prec UNIOP
1099 { $$ = localize($2,IVAL($1));
1100 TOKEN_GETMAD($1,$$,'k');
1103 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1104 TOKEN_GETMAD($1,$$,'(');
1105 TOKEN_GETMAD($3,$$,')');
1108 { $$ = sawparens(newNULLLIST());
1109 TOKEN_GETMAD($1,$$,'(');
1110 TOKEN_GETMAD($2,$$,')');
1120 | arylen %prec '(' /* $#x, $#{ something } */
1121 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1124 | ary '[' expr ']' /* array slice */
1125 { $$ = prepend_elem(OP_ASLICE,
1126 newOP(OP_PUSHMARK, 0),
1127 newLISTOP(OP_ASLICE, 0,
1129 ref($1, OP_ASLICE)));
1130 TOKEN_GETMAD($2,$$,'[');
1131 TOKEN_GETMAD($4,$$,']');
1133 | ary '{' expr ';' '}' /* @hash{@keys} */
1134 { $$ = prepend_elem(OP_HSLICE,
1135 newOP(OP_PUSHMARK, 0),
1136 newLISTOP(OP_HSLICE, 0,
1138 ref(oopsHV($1), OP_HSLICE)));
1139 PL_parser->expect = XOPERATOR;
1140 TOKEN_GETMAD($2,$$,'{');
1141 TOKEN_GETMAD($4,$$,';');
1142 TOKEN_GETMAD($5,$$,'}');
1147 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1148 | amper '(' ')' /* &foo() */
1149 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1150 TOKEN_GETMAD($2,$$,'(');
1151 TOKEN_GETMAD($3,$$,')');
1153 | amper '(' expr ')' /* &foo(@args) */
1155 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1156 append_elem(OP_LIST, $3, scalar($1)));
1159 if (op->op_type == OP_CONST) { /* defeat const fold */
1160 op = (OP*)op->op_madprop->mad_val;
1162 token_getmad($2,op,'(');
1163 token_getmad($4,op,')');
1166 | NOAMP WORD listexpr /* foo(@args) */
1167 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1168 append_elem(OP_LIST, $3, scalar($2)));
1169 TOKEN_GETMAD($1,$$,'o');
1171 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1172 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1173 PL_hints |= HINT_BLOCK_SCOPE;
1174 TOKEN_GETMAD($1,$$,'o');
1177 { $$ = newLOOPEX(IVAL($1),$2);
1178 TOKEN_GETMAD($1,$$,'o');
1180 | NOTOP argexpr /* not $foo */
1181 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1182 TOKEN_GETMAD($1,$$,'o');
1184 | UNIOP /* Unary op, $_ implied */
1185 { $$ = newOP(IVAL($1), 0);
1186 TOKEN_GETMAD($1,$$,'o');
1188 | UNIOP block /* eval { foo }* */
1189 { $$ = newUNOP(IVAL($1), 0, $2);
1190 TOKEN_GETMAD($1,$$,'o');
1192 | UNIOP term /* Unary op */
1193 { $$ = newUNOP(IVAL($1), 0, $2);
1194 TOKEN_GETMAD($1,$$,'o');
1196 | REQUIRE /* require, $_ implied */
1197 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1198 TOKEN_GETMAD($1,$$,'o');
1200 | REQUIRE term /* require Foo */
1201 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1202 TOKEN_GETMAD($1,$$,'o');
1205 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1206 | UNIOPSUB term /* Sub treated as unop */
1207 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1208 append_elem(OP_LIST, $2, scalar($1))); }
1209 | FUNC0 /* Nullary operator */
1210 { $$ = newOP(IVAL($1), 0);
1211 TOKEN_GETMAD($1,$$,'o');
1214 { $$ = newOP(IVAL($1), 0);
1215 TOKEN_GETMAD($1,$$,'o');
1216 TOKEN_GETMAD($2,$$,'(');
1217 TOKEN_GETMAD($3,$$,')');
1219 | FUNC0SUB /* Sub treated as nullop */
1220 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1222 | FUNC1 '(' ')' /* not () */
1223 { $$ = (IVAL($1) == OP_NOT)
1224 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1225 : newOP(IVAL($1), OPf_SPECIAL);
1227 TOKEN_GETMAD($1,$$,'o');
1228 TOKEN_GETMAD($2,$$,'(');
1229 TOKEN_GETMAD($3,$$,')');
1231 | FUNC1 '(' expr ')' /* not($foo) */
1232 { $$ = newUNOP(IVAL($1), 0, $3);
1233 TOKEN_GETMAD($1,$$,'o');
1234 TOKEN_GETMAD($2,$$,'(');
1235 TOKEN_GETMAD($4,$$,')');
1237 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1238 { $$ = pmruntime($1, $3, 1);
1239 TOKEN_GETMAD($2,$$,'(');
1240 TOKEN_GETMAD($4,$$,')');
1246 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1247 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1248 TOKEN_GETMAD($1,$$,'X');
1253 /* "my" declarations, with optional attributes */
1254 myattrterm: MY myterm myattrlist
1255 { $$ = my_attrs($2,$3);
1257 token_getmad($1,$$,'d');
1258 append_madprops($3->op_madprop, $$, 'a');
1263 { $$ = localize($2,IVAL($1));
1264 TOKEN_GETMAD($1,$$,'d');
1268 /* Things that can be "my"'d */
1269 myterm : '(' expr ')'
1270 { $$ = sawparens($2);
1271 TOKEN_GETMAD($1,$$,'(');
1272 TOKEN_GETMAD($3,$$,')');
1275 { $$ = sawparens(newNULLLIST());
1276 TOKEN_GETMAD($1,$$,'(');
1277 TOKEN_GETMAD($2,$$,')');
1287 /* Basic list expressions */
1288 listexpr: /* NULL */ %prec PREC_LOW
1290 | argexpr %prec PREC_LOW
1294 listexprcom: /* NULL */
1301 OP* op = newNULLLIST();
1302 token_getmad($2,op,',');
1303 $$ = append_elem(OP_LIST, $1, op);
1311 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1314 { PL_parser->in_my = 0; $$ = my($1); }
1318 { $$ = newCVREF(IVAL($1),$2);
1319 TOKEN_GETMAD($1,$$,'&');
1323 scalar : '$' indirob
1324 { $$ = newSVREF($2);
1325 TOKEN_GETMAD($1,$$,'$');
1330 { $$ = newAVREF($2);
1331 TOKEN_GETMAD($1,$$,'@');
1336 { $$ = newHVREF($2);
1337 TOKEN_GETMAD($1,$$,'%');
1341 arylen : DOLSHARP indirob
1342 { $$ = newAVREF($2);
1343 TOKEN_GETMAD($1,$$,'l');
1348 { $$ = newGVREF(0,$2);
1349 TOKEN_GETMAD($1,$$,'*');
1353 /* Indirect objects */
1355 { $$ = scalar($1); }
1356 | scalar %prec PREC_LOW
1357 { $$ = scalar($1); }