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 <p_tkval> LABEL
77 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
78 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
79 %token <i_tkval> GIVEN WHEN DEFAULT
80 %token <i_tkval> LOOPEX DOTDOT YADAYADA
81 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
82 %token <i_tkval> RELOP EQOP MULOP ADDOP
83 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
84 %token <i_tkval> LOCAL MY MYSUB REQUIRE
85 %token <i_tkval> COLONATTR
87 %type <ival> prog progstart remember mremember
88 %type <ival> startsub startanonsub startformsub
89 /* FIXME for MAD - are these two ival? */
90 %type <ival> mydefsv mintro
92 %type <opval> decl format subrout mysubrout package use peg
94 %type <opval> block mblock lineseq line loop cond else
95 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
96 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
97 %type <opval> listexpr listexprcom indirob listop method
98 %type <opval> formname subname proto subbody cont my_scalar
99 %type <opval> subattrlist myattrlist myattrterm myterm
100 %type <opval> termbinop termunop anonymous termdo
101 %type <opval> switch case
102 %type <p_tkval> label
104 %nonassoc <i_tkval> PREC_LOW
107 %left <i_tkval> OROP DOROP
108 %left <i_tkval> ANDOP
109 %right <i_tkval> NOTOP
110 %nonassoc LSTOP LSTOPSUB
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
121 %nonassoc UNIOP UNIOPSUB
123 %left <i_tkval> SHIFTOP
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> ')'
139 /* The whole program */
141 /*CONTINUED*/ lineseq
142 { $$ = $1; newPROG(block_end($1,$2)); }
145 /* An ordinary block */
146 block : '{' remember lineseq '}'
147 { if (PL_parser->copline > (line_t)IVAL($1))
148 PL_parser->copline = (line_t)IVAL($1);
149 $$ = block_end($2, $3);
150 TOKEN_GETMAD($1,$$,'{');
151 TOKEN_GETMAD($4,$$,'}');
155 remember: /* NULL */ /* start a full lexical scope */
156 { $$ = block_start(TRUE); }
159 mydefsv: /* NULL */ /* lexicalize $_ */
160 { $$ = (I32) allocmy("$_"); }
165 PL_parser->expect = XSTATE; $$ = block_start(TRUE);
170 mblock : '{' mremember lineseq '}'
171 { if (PL_parser->copline > (line_t)IVAL($1))
172 PL_parser->copline = (line_t)IVAL($1);
173 $$ = block_end($2, $3);
174 TOKEN_GETMAD($1,$$,'{');
175 TOKEN_GETMAD($4,$$,'}');
179 mremember: /* NULL */ /* start a partial lexical scope */
180 { $$ = block_start(FALSE); }
183 /* A collection of "lines" in the program */
189 append_list(OP_LINESEQ,
190 (LISTOP*)$1, (LISTOP*)$2),
194 { $$ = append_list(OP_LINESEQ,
195 (LISTOP*)$1, (LISTOP*)$2);
196 PL_pad_reset_pending = TRUE;
198 PL_hints |= HINT_BLOCK_SCOPE;
202 /* A "line" in the program */
204 { $$ = newSTATEOP(0, PVAL($1), $2);
205 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
206 | loop /* loops add their own labels */
207 | switch /* ... and so do switches */
210 { $$ = newSTATEOP(0, PVAL($1), $2); }
214 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
215 TOKEN_GETMAD($1,$$,'L');
216 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
222 PL_parser->copline = NOLINE;
224 TOKEN_GETMAD($2,$$,';');
226 PL_parser->expect = XSTATE;
230 $$ = newSTATEOP(0, PVAL($1), $2);
231 PL_parser->expect = XSTATE;
233 /* sideff might already have a nexstate */
234 OP* op = ((LISTOP*)$$)->op_first;
236 while (op->op_sibling &&
237 op->op_sibling->op_type == OP_NEXTSTATE)
239 token_getmad($1,op,'L');
240 token_getmad($3,op,';');
246 /* An expression which may have a side-effect */
252 { $$ = newLOGOP(OP_AND, 0, $3, $1);
253 TOKEN_GETMAD($2,$$,'i');
256 { $$ = newLOGOP(OP_OR, 0, $3, $1);
257 TOKEN_GETMAD($2,$$,'i');
260 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
261 TOKEN_GETMAD($2,$$,'w');
264 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
265 TOKEN_GETMAD($2,$$,'w');
268 { $$ = newFOROP(0, NULL, (line_t)IVAL($2),
269 (OP*)NULL, $3, $1, (OP*)NULL);
270 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
274 /* else and elsif blocks */
278 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
279 TOKEN_GETMAD($1,$$,'o');
281 | ELSIF '(' mexpr ')' mblock else
282 { PL_parser->copline = (line_t)IVAL($1);
283 $$ = newCONDOP(0, newSTATEOP(OPf_SPECIAL,NULL,$3), scope($5), $6);
284 PL_hints |= HINT_BLOCK_SCOPE;
285 TOKEN_GETMAD($1,$$,'I');
286 TOKEN_GETMAD($2,$$,'(');
287 TOKEN_GETMAD($4,$$,')');
291 /* Real conditional expressions */
292 cond : IF '(' remember mexpr ')' mblock else
293 { PL_parser->copline = (line_t)IVAL($1);
295 newCONDOP(0, $4, scope($6), $7));
296 TOKEN_GETMAD($1,$$,'I');
297 TOKEN_GETMAD($2,$$,'(');
298 TOKEN_GETMAD($5,$$,')');
300 | UNLESS '(' remember miexpr ')' mblock else
301 { PL_parser->copline = (line_t)IVAL($1);
303 newCONDOP(0, $4, scope($6), $7));
304 TOKEN_GETMAD($1,$$,'I');
305 TOKEN_GETMAD($2,$$,'(');
306 TOKEN_GETMAD($5,$$,')');
310 /* Cases for a switch statement */
311 case : WHEN '(' remember mexpr ')' mblock
313 newWHENOP($4, scope($6))); }
315 { $$ = newWHENOP(0, scope($2)); }
318 /* Continue blocks */
323 TOKEN_GETMAD($1,$$,'o');
327 /* Loops: while, until, for, and a bare block */
328 loop : label WHILE '(' remember texpr ')' mintro mblock cont
330 PL_parser->copline = (line_t)$2;
332 newSTATEOP(0, PVAL($1),
333 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
334 IVAL($2), $5, $8, $9, $7)));
335 TOKEN_GETMAD($1,innerop,'L');
336 TOKEN_GETMAD($2,innerop,'W');
337 TOKEN_GETMAD($3,innerop,'(');
338 TOKEN_GETMAD($6,innerop,')');
341 | label UNTIL '(' remember iexpr ')' mintro mblock cont
343 PL_parser->copline = (line_t)$2;
345 newSTATEOP(0, PVAL($1),
346 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
347 IVAL($2), $5, $8, $9, $7)));
348 TOKEN_GETMAD($1,innerop,'L');
349 TOKEN_GETMAD($2,innerop,'W');
350 TOKEN_GETMAD($3,innerop,'(');
351 TOKEN_GETMAD($6,innerop,')');
353 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
356 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
358 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
359 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
360 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
361 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
362 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
364 | label FOR scalar '(' remember mexpr ')' mblock cont
367 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
368 mod($3, OP_ENTERLOOP), $6, $8, $9));
369 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
370 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
371 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
372 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
374 | label FOR '(' remember mexpr ')' mblock cont
377 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
378 (OP*)NULL, $5, $7, $8));
379 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
380 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
381 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
382 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
384 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
386 /* basically fake up an initialize-while lineseq */
388 PL_parser->copline = (line_t)IVAL($2);
389 forop = newSTATEOP(0, PVAL($1),
390 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
391 IVAL($2), scalar($7),
394 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
396 CopLABEL_alloc(($1)->tk_lval.pval),
397 ($5 ? $5 : newOP(OP_NULL, 0)) ),
400 token_getmad($2,forop,'3');
401 token_getmad($3,forop,'(');
402 token_getmad($6,forop,'1');
403 token_getmad($8,forop,'2');
404 token_getmad($11,forop,')');
405 token_getmad($1,forop,'L');
408 forop = append_elem(OP_LINESEQ,
409 newSTATEOP(0, CopLABEL_alloc($1), $5),
415 $$ = block_end($4, forop); }
416 | label block cont /* a block is a loop that happens once */
417 { $$ = newSTATEOP(0, PVAL($1),
418 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
419 NOLINE, (OP*)NULL, $2, $3, 0));
420 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
424 switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
425 { PL_parser->copline = (line_t) $2;
427 newSTATEOP(0, PVAL($1),
428 newGIVENOP($6, scope($8),
429 (PADOFFSET) $5) )); }
432 /* determine whether there are any new my declarations */
434 { $$ = (PL_min_intro_pending &&
435 PL_max_intro_pending >= PL_min_intro_pending);
438 /* Normal expression */
444 /* Boolean expression */
445 texpr : /* NULL means true */
447 (void)scan_num("1", &tmplval);
448 $$ = tmplval.opval; }
452 /* Inverted boolean expression */
454 { $$ = invert(scalar($1)); }
457 /* Expression with its own lexical scope */
459 { $$ = $1; intro_my(); }
463 { $$ = $1; intro_my(); }
467 { $$ = $1; intro_my(); }
470 /* Optional "MAIN:"-style loop labels */
476 $$ = newTOKEN(OP_NULL, tmplval, 0);
484 /* Some kind of declaration - just hang on peg in the parse tree */
496 /* these two are only used by MAD */
503 { $$ = newOP(OP_NULL,0);
504 TOKEN_GETMAD($1,$$,'p');
508 format : FORMAT startformsub formname block
509 { SvREFCNT_inc_simple_void(PL_compcv);
511 $$ = newFORM($2, $3, $4);
512 prepend_madprops($1->tk_mad, $$, 'F');
522 formname: WORD { $$ = $1; }
523 | /* NULL */ { $$ = (OP*)NULL; }
526 /* Unimplemented "my sub foo { }" */
527 mysubrout: MYSUB startsub subname proto subattrlist subbody
528 { SvREFCNT_inc_simple_void(PL_compcv);
530 $$ = newMYSUB($2, $3, $4, $5, $6);
531 token_getmad($1,$$,'d');
533 newMYSUB($2, $3, $4, $5, $6);
539 /* Subroutine definition */
540 subrout : SUB startsub subname proto subattrlist subbody
541 { SvREFCNT_inc_simple_void(PL_compcv);
544 OP* o = newSVOP(OP_ANONCODE, 0,
545 (SV*)newATTRSUB($2, $3, $4, $5, $6));
546 $$ = newOP(OP_NULL,0);
548 op_getmad($3,$$,'n');
549 op_getmad($4,$$,'s');
550 op_getmad($5,$$,'a');
551 token_getmad($1,$$,'d');
552 append_madprops($6->op_madprop, $$, 0);
556 newATTRSUB($2, $3, $4, $5, $6);
562 startsub: /* NULL */ /* start a regular subroutine scope */
563 { $$ = start_subparse(FALSE, 0);
564 SAVEFREESV(PL_compcv); }
568 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
569 { $$ = start_subparse(FALSE, CVf_ANON);
570 SAVEFREESV(PL_compcv); }
573 startformsub: /* NULL */ /* start a format subroutine scope */
574 { $$ = start_subparse(TRUE, 0);
575 SAVEFREESV(PL_compcv); }
578 /* Name of a subroutine - must be a bareword, could be special */
579 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
580 if (strEQ(name, "BEGIN") || strEQ(name, "END")
581 || strEQ(name, "INIT") || strEQ(name, "CHECK")
582 || strEQ(name, "UNITCHECK"))
583 CvSPECIAL_on(PL_compcv);
587 /* Subroutine prototype */
593 /* Optional list of subroutine attributes */
594 subattrlist: /* NULL */
598 TOKEN_GETMAD($1,$$,':');
605 TOKEN_GETMAD($1,$$,':');
609 /* List of attributes for a "my" variable declaration */
610 myattrlist: COLONATTR THING
612 TOKEN_GETMAD($1,$$,':');
619 TOKEN_GETMAD($1,$$,':');
623 /* Subroutine body - either null or a block */
624 subbody : block { $$ = $1; }
629 PL_parser->expect = XSTATE;
630 TOKEN_GETMAD($1,$$,';');
634 package : PACKAGE WORD ';'
638 token_getmad($1,$$,'o');
639 token_getmad($3,$$,';');
648 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
649 WORD WORD listexpr ';'
650 { SvREFCNT_inc_simple_void(PL_compcv);
652 $$ = utilize(IVAL($1), $2, $4, $5, $6);
653 token_getmad($1,$$,'o');
654 token_getmad($7,$$,';');
655 if (PL_parser->rsfp_filters &&
656 AvFILLp(PL_parser->rsfp_filters) >= 0)
657 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
659 utilize(IVAL($1), $2, $4, $5, $6);
665 /* Ordinary expressions; logical combinations */
666 expr : expr ANDOP expr
667 { $$ = newLOGOP(OP_AND, 0, $1, $3);
668 TOKEN_GETMAD($2,$$,'o');
671 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
672 TOKEN_GETMAD($2,$$,'o');
675 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
676 TOKEN_GETMAD($2,$$,'o');
678 | argexpr %prec PREC_LOW
681 /* Expressions are a list of terms joined by commas */
682 argexpr : argexpr ','
685 OP* op = newNULLLIST();
686 token_getmad($2,op,',');
687 $$ = append_elem(OP_LIST, $1, op);
696 term = newUNOP(OP_NULL, 0, term);
697 token_getmad($2,term,',');
699 $$ = append_elem(OP_LIST, $1, term);
701 | term %prec PREC_LOW
705 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
706 { $$ = convert(IVAL($1), OPf_STACKED,
707 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
708 TOKEN_GETMAD($1,$$,'o');
710 | FUNC '(' indirob expr ')' /* print ($fh @args */
711 { $$ = convert(IVAL($1), OPf_STACKED,
712 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
713 TOKEN_GETMAD($1,$$,'o');
714 TOKEN_GETMAD($2,$$,'(');
715 TOKEN_GETMAD($5,$$,')');
717 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
718 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
720 prepend_elem(OP_LIST, scalar($1), $5),
721 newUNOP(OP_METHOD, 0, $3)));
722 TOKEN_GETMAD($2,$$,'A');
723 TOKEN_GETMAD($4,$$,'(');
724 TOKEN_GETMAD($6,$$,')');
726 | term ARROW method /* $foo->bar */
727 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
728 append_elem(OP_LIST, scalar($1),
729 newUNOP(OP_METHOD, 0, $3)));
730 TOKEN_GETMAD($2,$$,'A');
732 | METHOD indirob listexpr /* new Class @args */
733 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
735 prepend_elem(OP_LIST, $2, $3),
736 newUNOP(OP_METHOD, 0, $1)));
738 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
739 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
741 prepend_elem(OP_LIST, $2, $4),
742 newUNOP(OP_METHOD, 0, $1)));
743 TOKEN_GETMAD($3,$$,'(');
744 TOKEN_GETMAD($5,$$,')');
746 | LSTOP listexpr /* print @args */
747 { $$ = convert(IVAL($1), 0, $2);
748 TOKEN_GETMAD($1,$$,'o');
750 | FUNC '(' listexprcom ')' /* print (@args) */
751 { $$ = convert(IVAL($1), 0, $3);
752 TOKEN_GETMAD($1,$$,'o');
753 TOKEN_GETMAD($2,$$,'(');
754 TOKEN_GETMAD($4,$$,')');
756 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
757 { SvREFCNT_inc_simple_void(PL_compcv);
758 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
759 listexpr %prec LSTOP /* ... @bar */
760 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
762 prepend_elem(OP_LIST, $<opval>4, $5), $1));
766 /* Names of methods. May use $object->$methodname */
771 /* Some kind of subscripted expression */
772 subscripted: star '{' expr ';' '}' /* *main::{something} */
773 /* In this and all the hash accessors, ';' is
774 * provided by the tokeniser */
775 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
776 PL_parser->expect = XOPERATOR;
777 TOKEN_GETMAD($2,$$,'{');
778 TOKEN_GETMAD($4,$$,';');
779 TOKEN_GETMAD($5,$$,'}');
781 | scalar '[' expr ']' /* $array[$element] */
782 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
783 TOKEN_GETMAD($2,$$,'[');
784 TOKEN_GETMAD($4,$$,']');
786 | term ARROW '[' expr ']' /* somearef->[$element] */
787 { $$ = newBINOP(OP_AELEM, 0,
788 ref(newAVREF($1),OP_RV2AV),
790 TOKEN_GETMAD($2,$$,'a');
791 TOKEN_GETMAD($3,$$,'[');
792 TOKEN_GETMAD($5,$$,']');
794 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
795 { $$ = newBINOP(OP_AELEM, 0,
796 ref(newAVREF($1),OP_RV2AV),
798 TOKEN_GETMAD($2,$$,'[');
799 TOKEN_GETMAD($4,$$,']');
801 | scalar '{' expr ';' '}' /* $foo->{bar();} */
802 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
803 PL_parser->expect = XOPERATOR;
804 TOKEN_GETMAD($2,$$,'{');
805 TOKEN_GETMAD($4,$$,';');
806 TOKEN_GETMAD($5,$$,'}');
808 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
809 { $$ = newBINOP(OP_HELEM, 0,
810 ref(newHVREF($1),OP_RV2HV),
812 PL_parser->expect = XOPERATOR;
813 TOKEN_GETMAD($2,$$,'a');
814 TOKEN_GETMAD($3,$$,'{');
815 TOKEN_GETMAD($5,$$,';');
816 TOKEN_GETMAD($6,$$,'}');
818 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
819 { $$ = newBINOP(OP_HELEM, 0,
820 ref(newHVREF($1),OP_RV2HV),
822 PL_parser->expect = XOPERATOR;
823 TOKEN_GETMAD($2,$$,'{');
824 TOKEN_GETMAD($4,$$,';');
825 TOKEN_GETMAD($5,$$,'}');
827 | term ARROW '(' ')' /* $subref->() */
828 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
829 newCVREF(0, scalar($1)));
830 TOKEN_GETMAD($2,$$,'a');
831 TOKEN_GETMAD($3,$$,'(');
832 TOKEN_GETMAD($4,$$,')');
834 | term ARROW '(' expr ')' /* $subref->(@args) */
835 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
836 append_elem(OP_LIST, $4,
837 newCVREF(0, scalar($1))));
838 TOKEN_GETMAD($2,$$,'a');
839 TOKEN_GETMAD($3,$$,'(');
840 TOKEN_GETMAD($5,$$,')');
843 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
844 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
845 append_elem(OP_LIST, $3,
846 newCVREF(0, scalar($1))));
847 TOKEN_GETMAD($2,$$,'(');
848 TOKEN_GETMAD($4,$$,')');
850 | subscripted '(' ')' /* $foo->{bar}->() */
851 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
852 newCVREF(0, scalar($1)));
853 TOKEN_GETMAD($2,$$,'(');
854 TOKEN_GETMAD($3,$$,')');
856 | '(' expr ')' '[' expr ']' /* list slice */
857 { $$ = newSLICEOP(0, $5, $2);
858 TOKEN_GETMAD($1,$$,'(');
859 TOKEN_GETMAD($3,$$,')');
860 TOKEN_GETMAD($4,$$,'[');
861 TOKEN_GETMAD($6,$$,']');
863 | '(' ')' '[' expr ']' /* empty list slice! */
864 { $$ = newSLICEOP(0, $4, (OP*)NULL);
865 TOKEN_GETMAD($1,$$,'(');
866 TOKEN_GETMAD($2,$$,')');
867 TOKEN_GETMAD($3,$$,'[');
868 TOKEN_GETMAD($5,$$,']');
872 /* Binary operators between terms */
873 termbinop: term ASSIGNOP term /* $x = $y */
874 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
875 TOKEN_GETMAD($2,$$,'o');
877 | term POWOP term /* $x ** $y */
878 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
879 TOKEN_GETMAD($2,$$,'o');
881 | term MULOP term /* $x * $y, $x x $y */
882 { if (IVAL($2) != OP_REPEAT)
884 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
885 TOKEN_GETMAD($2,$$,'o');
887 | term ADDOP term /* $x + $y */
888 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
889 TOKEN_GETMAD($2,$$,'o');
891 | term SHIFTOP term /* $x >> $y, $x << $y */
892 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
893 TOKEN_GETMAD($2,$$,'o');
895 | term RELOP term /* $x > $y, etc. */
896 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
897 TOKEN_GETMAD($2,$$,'o');
899 | term EQOP term /* $x == $y, $x eq $y */
900 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
901 TOKEN_GETMAD($2,$$,'o');
903 | term BITANDOP term /* $x & $y */
904 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
905 TOKEN_GETMAD($2,$$,'o');
907 | term BITOROP term /* $x | $y */
908 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
909 TOKEN_GETMAD($2,$$,'o');
911 | term DOTDOT term /* $x..$y, $x...$y */
913 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
917 op = (UNOP*)op->op_first; /* get to flop */
918 op = (UNOP*)op->op_first; /* get to flip */
919 op = (UNOP*)op->op_first; /* get to range */
920 token_getmad($2,(OP*)op,'o');
923 | term ANDAND term /* $x && $y */
924 { $$ = newLOGOP(OP_AND, 0, $1, $3);
925 TOKEN_GETMAD($2,$$,'o');
927 | term OROR term /* $x || $y */
928 { $$ = newLOGOP(OP_OR, 0, $1, $3);
929 TOKEN_GETMAD($2,$$,'o');
931 | term DORDOR term /* $x // $y */
932 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
933 TOKEN_GETMAD($2,$$,'o');
935 | term MATCHOP term /* $x =~ /$y/ */
936 { $$ = bind_match(IVAL($2), $1, $3);
938 ($$->op_type == OP_NOT
939 ? ((UNOP*)$$)->op_first : $$),
944 /* Unary operators and terms */
945 termunop : '-' term %prec UMINUS /* -$x */
946 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
947 TOKEN_GETMAD($1,$$,'o');
949 | '+' term %prec UMINUS /* +$x */
951 newUNOP(OP_NULL, 0, $2),
954 TOKEN_GETMAD($1,$$,'+');
957 { $$ = newUNOP(OP_NOT, 0, scalar($2));
958 TOKEN_GETMAD($1,$$,'o');
961 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
962 TOKEN_GETMAD($1,$$,'o');
964 | term POSTINC /* $x++ */
965 { $$ = newUNOP(OP_POSTINC, 0,
966 mod(scalar($1), OP_POSTINC));
967 TOKEN_GETMAD($2,$$,'o');
969 | term POSTDEC /* $x-- */
970 { $$ = newUNOP(OP_POSTDEC, 0,
971 mod(scalar($1), OP_POSTDEC));
972 TOKEN_GETMAD($2,$$,'o');
974 | PREINC term /* ++$x */
975 { $$ = newUNOP(OP_PREINC, 0,
976 mod(scalar($2), OP_PREINC));
977 TOKEN_GETMAD($1,$$,'o');
979 | PREDEC term /* --$x */
980 { $$ = newUNOP(OP_PREDEC, 0,
981 mod(scalar($2), OP_PREDEC));
982 TOKEN_GETMAD($1,$$,'o');
987 /* Constructors for anonymous data */
988 anonymous: '[' expr ']'
989 { $$ = newANONLIST($2);
990 TOKEN_GETMAD($1,$$,'[');
991 TOKEN_GETMAD($3,$$,']');
994 { $$ = newANONLIST((OP*)NULL);
995 TOKEN_GETMAD($1,$$,'[');
996 TOKEN_GETMAD($2,$$,']');
998 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
999 { $$ = newANONHASH($2);
1000 TOKEN_GETMAD($1,$$,'{');
1001 TOKEN_GETMAD($3,$$,';');
1002 TOKEN_GETMAD($4,$$,'}');
1004 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1005 { $$ = newANONHASH((OP*)NULL);
1006 TOKEN_GETMAD($1,$$,'{');
1007 TOKEN_GETMAD($2,$$,';');
1008 TOKEN_GETMAD($3,$$,'}');
1010 | ANONSUB startanonsub proto subattrlist block %prec '('
1011 { SvREFCNT_inc_simple_void(PL_compcv);
1012 $$ = newANONATTRSUB($2, $3, $4, $5);
1013 TOKEN_GETMAD($1,$$,'o');
1014 OP_GETMAD($3,$$,'s');
1015 OP_GETMAD($4,$$,'a');
1020 /* Things called with "do" */
1021 termdo : DO term %prec UNIOP /* do $filename */
1022 { $$ = dofile($2, IVAL($1));
1023 TOKEN_GETMAD($1,$$,'o');
1025 | DO block %prec '(' /* do { code */
1026 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1027 TOKEN_GETMAD($1,$$,'D');
1029 | DO WORD '(' ')' /* do somesub() */
1030 { $$ = newUNOP(OP_ENTERSUB,
1031 OPf_SPECIAL|OPf_STACKED,
1032 prepend_elem(OP_LIST,
1034 (OPpENTERSUB_AMPER<<8),
1036 )),(OP*)NULL)); dep();
1037 TOKEN_GETMAD($1,$$,'o');
1038 TOKEN_GETMAD($3,$$,'(');
1039 TOKEN_GETMAD($4,$$,')');
1041 | DO WORD '(' expr ')' /* do somesub(@args) */
1042 { $$ = newUNOP(OP_ENTERSUB,
1043 OPf_SPECIAL|OPf_STACKED,
1044 append_elem(OP_LIST,
1047 (OPpENTERSUB_AMPER<<8),
1050 TOKEN_GETMAD($1,$$,'o');
1051 TOKEN_GETMAD($3,$$,'(');
1052 TOKEN_GETMAD($5,$$,')');
1054 | DO scalar '(' ')' /* do $subref () */
1055 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1056 prepend_elem(OP_LIST,
1057 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1058 TOKEN_GETMAD($1,$$,'o');
1059 TOKEN_GETMAD($3,$$,'(');
1060 TOKEN_GETMAD($4,$$,')');
1062 | DO scalar '(' expr ')' /* do $subref (@args) */
1063 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1064 prepend_elem(OP_LIST,
1066 scalar(newCVREF(0,scalar($2))))); dep();
1067 TOKEN_GETMAD($1,$$,'o');
1068 TOKEN_GETMAD($3,$$,'(');
1069 TOKEN_GETMAD($5,$$,')');
1078 | term '?' term ':' term
1079 { $$ = newCONDOP(0, $1, $3, $5);
1080 TOKEN_GETMAD($2,$$,'?');
1081 TOKEN_GETMAD($4,$$,':');
1083 | REFGEN term /* \$x, \@y, \%z */
1084 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1085 TOKEN_GETMAD($1,$$,'o');
1087 | myattrterm %prec UNIOP
1089 | LOCAL term %prec UNIOP
1090 { $$ = localize($2,IVAL($1));
1091 TOKEN_GETMAD($1,$$,'k');
1094 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1095 TOKEN_GETMAD($1,$$,'(');
1096 TOKEN_GETMAD($3,$$,')');
1099 { $$ = sawparens(newNULLLIST());
1100 TOKEN_GETMAD($1,$$,'(');
1101 TOKEN_GETMAD($2,$$,')');
1111 | arylen %prec '(' /* $#x, $#{ something } */
1112 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1115 | ary '[' expr ']' /* array slice */
1116 { $$ = prepend_elem(OP_ASLICE,
1117 newOP(OP_PUSHMARK, 0),
1118 newLISTOP(OP_ASLICE, 0,
1120 ref($1, OP_ASLICE)));
1121 TOKEN_GETMAD($2,$$,'[');
1122 TOKEN_GETMAD($4,$$,']');
1124 | ary '{' expr ';' '}' /* @hash{@keys} */
1125 { $$ = prepend_elem(OP_HSLICE,
1126 newOP(OP_PUSHMARK, 0),
1127 newLISTOP(OP_HSLICE, 0,
1129 ref(oopsHV($1), OP_HSLICE)));
1130 PL_parser->expect = XOPERATOR;
1131 TOKEN_GETMAD($2,$$,'{');
1132 TOKEN_GETMAD($4,$$,';');
1133 TOKEN_GETMAD($5,$$,'}');
1138 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1139 | amper '(' ')' /* &foo() */
1140 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1141 TOKEN_GETMAD($2,$$,'(');
1142 TOKEN_GETMAD($3,$$,')');
1144 | amper '(' expr ')' /* &foo(@args) */
1146 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1147 append_elem(OP_LIST, $3, scalar($1)));
1150 if (op->op_type == OP_CONST) { /* defeat const fold */
1151 op = (OP*)op->op_madprop->mad_val;
1153 token_getmad($2,op,'(');
1154 token_getmad($4,op,')');
1157 | NOAMP WORD listexpr /* foo(@args) */
1158 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1159 append_elem(OP_LIST, $3, scalar($2)));
1160 TOKEN_GETMAD($1,$$,'o');
1162 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1163 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1164 PL_hints |= HINT_BLOCK_SCOPE;
1165 TOKEN_GETMAD($1,$$,'o');
1168 { $$ = newLOOPEX(IVAL($1),$2);
1169 TOKEN_GETMAD($1,$$,'o');
1171 | NOTOP argexpr /* not $foo */
1172 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1173 TOKEN_GETMAD($1,$$,'o');
1175 | UNIOP /* Unary op, $_ implied */
1176 { $$ = newOP(IVAL($1), 0);
1177 TOKEN_GETMAD($1,$$,'o');
1179 | UNIOP block /* eval { foo }* */
1180 { $$ = newUNOP(IVAL($1), 0, $2);
1181 TOKEN_GETMAD($1,$$,'o');
1183 | UNIOP term /* Unary op */
1184 { $$ = newUNOP(IVAL($1), 0, $2);
1185 TOKEN_GETMAD($1,$$,'o');
1187 | REQUIRE /* require, $_ implied */
1188 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1189 TOKEN_GETMAD($1,$$,'o');
1191 | REQUIRE term /* require Foo */
1192 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1193 TOKEN_GETMAD($1,$$,'o');
1196 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1197 | UNIOPSUB term /* Sub treated as unop */
1198 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1199 append_elem(OP_LIST, $2, scalar($1))); }
1200 | FUNC0 /* Nullary operator */
1201 { $$ = newOP(IVAL($1), 0);
1202 TOKEN_GETMAD($1,$$,'o');
1205 { $$ = newOP(IVAL($1), 0);
1206 TOKEN_GETMAD($1,$$,'o');
1207 TOKEN_GETMAD($2,$$,'(');
1208 TOKEN_GETMAD($3,$$,')');
1210 | FUNC0SUB /* Sub treated as nullop */
1211 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1213 | FUNC1 '(' ')' /* not () */
1214 { $$ = (IVAL($1) == OP_NOT)
1215 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1216 : newOP(IVAL($1), OPf_SPECIAL);
1218 TOKEN_GETMAD($1,$$,'o');
1219 TOKEN_GETMAD($2,$$,'(');
1220 TOKEN_GETMAD($3,$$,')');
1222 | FUNC1 '(' expr ')' /* not($foo) */
1223 { $$ = newUNOP(IVAL($1), 0, $3);
1224 TOKEN_GETMAD($1,$$,'o');
1225 TOKEN_GETMAD($2,$$,'(');
1226 TOKEN_GETMAD($4,$$,')');
1228 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1229 { $$ = pmruntime($1, $3, 1);
1230 TOKEN_GETMAD($2,$$,'(');
1231 TOKEN_GETMAD($4,$$,')');
1237 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1238 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1242 /* "my" declarations, with optional attributes */
1243 myattrterm: MY myterm myattrlist
1244 { $$ = my_attrs($2,$3);
1246 token_getmad($1,$$,'d');
1247 append_madprops($3->op_madprop, $$, 'a');
1252 { $$ = localize($2,IVAL($1));
1253 TOKEN_GETMAD($1,$$,'d');
1257 /* Things that can be "my"'d */
1258 myterm : '(' expr ')'
1259 { $$ = sawparens($2);
1260 TOKEN_GETMAD($1,$$,'(');
1261 TOKEN_GETMAD($3,$$,')');
1264 { $$ = sawparens(newNULLLIST());
1265 TOKEN_GETMAD($1,$$,'(');
1266 TOKEN_GETMAD($2,$$,')');
1276 /* Basic list expressions */
1277 listexpr: /* NULL */ %prec PREC_LOW
1279 | argexpr %prec PREC_LOW
1283 listexprcom: /* NULL */
1290 OP* op = newNULLLIST();
1291 token_getmad($2,op,',');
1292 $$ = append_elem(OP_LIST, $1, op);
1300 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1303 { PL_parser->in_my = 0; $$ = my($1); }
1307 { $$ = newCVREF(IVAL($1),$2);
1308 TOKEN_GETMAD($1,$$,'&');
1312 scalar : '$' indirob
1313 { $$ = newSVREF($2);
1314 TOKEN_GETMAD($1,$$,'$');
1319 { $$ = newAVREF($2);
1320 TOKEN_GETMAD($1,$$,'@');
1325 { $$ = newHVREF($2);
1326 TOKEN_GETMAD($1,$$,'%');
1330 arylen : DOLSHARP indirob
1331 { $$ = newAVREF($2);
1332 TOKEN_GETMAD($1,$$,'l');
1337 { $$ = newGVREF(0,$2);
1338 TOKEN_GETMAD($1,$$,'*');
1342 /* Indirect objects */
1344 { $$ = scalar($1); }
1345 | scalar %prec PREC_LOW
1346 { $$ = scalar($1); }