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 <ival> GRAMPROG GRAMFULLSTMT
74 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
76 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
77 %token <opval> 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 MYSUB REQUIRE
88 %token <i_tkval> COLONATTR
90 %type <i_tkval> lpar_or_qw
92 %type <ival> grammar prog progstart remember mremember
93 %type <ival> startsub startanonsub startformsub
94 /* FIXME for MAD - are these two ival? */
95 %type <ival> mydefsv mintro
97 %type <opval> fullstmt decl format subrout mysubrout package use peg
98 %type <opval> block package_block mblock lineseq line loop cond else
99 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
100 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
101 %type <opval> listexpr listexprcom indirob listop method
102 %type <opval> formname subname proto subbody cont my_scalar
103 %type <opval> subattrlist myattrlist myattrterm myterm
104 %type <opval> termbinop termunop anonymous termdo
105 %type <opval> switch case
106 %type <p_tkval> label
108 %nonassoc <i_tkval> PREC_LOW
111 %left <i_tkval> OROP DOROP
112 %left <i_tkval> ANDOP
113 %right <i_tkval> NOTOP
114 %nonassoc LSTOP LSTOPSUB
116 %right <i_tkval> ASSIGNOP
117 %right <i_tkval> '?' ':'
118 %nonassoc DOTDOT YADAYADA
119 %left <i_tkval> OROR DORDOR
120 %left <i_tkval> ANDAND
121 %left <i_tkval> BITOROP
122 %left <i_tkval> BITANDOP
125 %nonassoc UNIOP UNIOPSUB
127 %left <i_tkval> SHIFTOP
130 %left <i_tkval> MATCHOP
131 %right <i_tkval> '!' '~' UMINUS REFGEN
132 %right <i_tkval> POWOP
133 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
134 %left <i_tkval> ARROW
135 %nonassoc <i_tkval> ')'
143 /* Top-level choice of what kind of thing yyparse was called to parse */
144 grammar : GRAMPROG prog
146 | GRAMFULLSTMT fullstmt
151 parser->yychar = YYEOF;
155 /* The whole program */
157 /*CONTINUED*/ lineseq
158 { $$ = $1; newPROG(block_end($1,$2)); }
161 /* An ordinary block */
162 block : '{' remember lineseq '}'
163 { if (PL_parser->copline > (line_t)IVAL($1))
164 PL_parser->copline = (line_t)IVAL($1);
165 $$ = block_end($2, $3);
166 TOKEN_GETMAD($1,$$,'{');
167 TOKEN_GETMAD($4,$$,'}');
171 remember: /* NULL */ /* start a full lexical scope */
172 { $$ = block_start(TRUE); }
175 mydefsv: /* NULL */ /* lexicalize $_ */
176 { $$ = (I32) Perl_allocmy(aTHX_ STR_WITH_LEN("$_"), 0); }
181 PL_parser->expect = XSTATE; $$ = block_start(TRUE);
186 mblock : '{' mremember lineseq '}'
187 { if (PL_parser->copline > (line_t)IVAL($1))
188 PL_parser->copline = (line_t)IVAL($1);
189 $$ = block_end($2, $3);
190 TOKEN_GETMAD($1,$$,'{');
191 TOKEN_GETMAD($4,$$,'}');
195 mremember: /* NULL */ /* start a partial lexical scope */
196 { $$ = block_start(FALSE); }
199 /* A collection of "lines" in the program */
205 append_list(OP_LINESEQ,
206 (LISTOP*)$1, (LISTOP*)$2),
210 { $$ = append_list(OP_LINESEQ,
211 (LISTOP*)$1, (LISTOP*)$2);
212 PL_pad_reset_pending = TRUE;
214 PL_hints |= HINT_BLOCK_SCOPE;
218 /* A statement, or "line", in the program */
223 PL_pad_reset_pending = TRUE;
228 /* A non-declaration statement */
230 { $$ = newSTATEOP(0, PVAL($1), $2);
231 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
232 | loop /* loops add their own labels */
233 | switch /* ... and so do switches */
236 { $$ = newSTATEOP(0, PVAL($1), $2); }
240 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
241 TOKEN_GETMAD($1,$$,'L');
242 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
248 PL_parser->copline = NOLINE;
250 TOKEN_GETMAD($2,$$,';');
252 PL_parser->expect = XSTATE;
256 $$ = newSTATEOP(0, PVAL($1), $2);
257 PL_parser->expect = XSTATE;
259 /* sideff might already have a nexstate */
260 OP* op = ((LISTOP*)$$)->op_first;
262 while (op->op_sibling &&
263 op->op_sibling->op_type == OP_NEXTSTATE)
265 token_getmad($1,op,'L');
266 token_getmad($3,op,';');
271 { $$ = newSTATEOP(0, NULL,
272 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
273 NOLINE, (OP*)NULL, $1,
276 { $$ = newSTATEOP(0, PVAL($1), $2); }
279 /* An expression which may have a side-effect */
285 { $$ = newLOGOP(OP_AND, 0, $3, $1);
286 TOKEN_GETMAD($2,$$,'i');
289 { $$ = newLOGOP(OP_OR, 0, $3, $1);
290 TOKEN_GETMAD($2,$$,'i');
293 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
294 TOKEN_GETMAD($2,$$,'w');
297 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
298 TOKEN_GETMAD($2,$$,'w');
301 { $$ = newFOROP(0, NULL, (line_t)IVAL($2),
302 (OP*)NULL, $3, $1, (OP*)NULL);
303 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
306 { $$ = newWHENOP($3, scope($1)); }
309 /* else and elsif blocks */
313 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
314 TOKEN_GETMAD($1,$$,'o');
316 | ELSIF lpar_or_qw mexpr ')' mblock else
317 { PL_parser->copline = (line_t)IVAL($1);
318 $$ = newCONDOP(0, newSTATEOP(OPf_SPECIAL,NULL,$3), scope($5), $6);
319 PL_hints |= HINT_BLOCK_SCOPE;
320 TOKEN_GETMAD($1,$$,'I');
321 TOKEN_GETMAD($2,$$,'(');
322 TOKEN_GETMAD($4,$$,')');
326 /* Real conditional expressions */
327 cond : IF lpar_or_qw remember mexpr ')' mblock else
328 { PL_parser->copline = (line_t)IVAL($1);
330 newCONDOP(0, $4, scope($6), $7));
331 TOKEN_GETMAD($1,$$,'I');
332 TOKEN_GETMAD($2,$$,'(');
333 TOKEN_GETMAD($5,$$,')');
335 | UNLESS lpar_or_qw remember miexpr ')' mblock else
336 { PL_parser->copline = (line_t)IVAL($1);
338 newCONDOP(0, $4, scope($6), $7));
339 TOKEN_GETMAD($1,$$,'I');
340 TOKEN_GETMAD($2,$$,'(');
341 TOKEN_GETMAD($5,$$,')');
345 /* Cases for a switch statement */
346 case : WHEN lpar_or_qw remember mexpr ')' mblock
348 newWHENOP($4, scope($6))); }
350 { $$ = newWHENOP(0, scope($2)); }
353 /* Continue blocks */
358 TOKEN_GETMAD($1,$$,'o');
362 /* Loops: while, until, for, and a bare block */
363 loop : label WHILE lpar_or_qw remember texpr ')' mintro mblock cont
365 PL_parser->copline = (line_t)IVAL($2);
367 newSTATEOP(0, PVAL($1),
368 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
369 IVAL($2), $5, $8, $9, $7)));
370 TOKEN_GETMAD($1,innerop,'L');
371 TOKEN_GETMAD($2,innerop,'W');
372 TOKEN_GETMAD($3,innerop,'(');
373 TOKEN_GETMAD($6,innerop,')');
376 | label UNTIL lpar_or_qw remember iexpr ')' mintro mblock cont
378 PL_parser->copline = (line_t)IVAL($2);
380 newSTATEOP(0, PVAL($1),
381 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
382 IVAL($2), $5, $8, $9, $7)));
383 TOKEN_GETMAD($1,innerop,'L');
384 TOKEN_GETMAD($2,innerop,'W');
385 TOKEN_GETMAD($3,innerop,'(');
386 TOKEN_GETMAD($6,innerop,')');
388 | label FOR MY remember my_scalar lpar_or_qw mexpr ')' mblock cont
391 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
393 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
394 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
395 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
396 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
397 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
399 | label FOR scalar lpar_or_qw remember mexpr ')' mblock cont
402 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
403 mod($3, OP_ENTERLOOP), $6, $8, $9));
404 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
405 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
406 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
407 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
409 | label FOR lpar_or_qw remember mexpr ')' mblock cont
412 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
413 (OP*)NULL, $5, $7, $8));
414 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
415 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
416 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
417 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
419 | label FOR lpar_or_qw remember mnexpr ';' texpr ';' mintro mnexpr ')'
421 /* basically fake up an initialize-while lineseq */
423 PL_parser->copline = (line_t)IVAL($2);
424 forop = newSTATEOP(0, PVAL($1),
425 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
426 IVAL($2), scalar($7),
429 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
431 CopLABEL_alloc(($1)->tk_lval.pval),
432 ($5 ? $5 : newOP(OP_NULL, 0)) ),
435 token_getmad($2,forop,'3');
436 token_getmad($3,forop,'(');
437 token_getmad($6,forop,'1');
438 token_getmad($8,forop,'2');
439 token_getmad($11,forop,')');
440 token_getmad($1,forop,'L');
443 forop = append_elem(OP_LINESEQ,
444 newSTATEOP(0, CopLABEL_alloc($1), $5),
450 $$ = block_end($4, forop); }
451 | label block cont /* a block is a loop that happens once */
452 { $$ = newSTATEOP(0, PVAL($1),
453 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
454 NOLINE, (OP*)NULL, $2, $3, 0));
455 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
459 switch : label GIVEN lpar_or_qw remember mydefsv mexpr ')' mblock
460 { PL_parser->copline = (line_t) IVAL($2);
462 newSTATEOP(0, PVAL($1),
463 newGIVENOP($6, scope($8),
464 (PADOFFSET) $5) )); }
467 /* determine whether there are any new my declarations */
469 { $$ = (PL_min_intro_pending &&
470 PL_max_intro_pending >= PL_min_intro_pending);
473 /* Normal expression */
479 /* Boolean expression */
480 texpr : /* NULL means true */
482 (void)scan_num("1", &tmplval);
483 $$ = tmplval.opval; }
487 /* Inverted boolean expression */
489 { $$ = invert(scalar($1)); }
492 /* Expression with its own lexical scope */
494 { $$ = $1; intro_my(); }
498 { $$ = $1; intro_my(); }
502 { $$ = $1; intro_my(); }
505 /* Optional "MAIN:"-style loop labels */
511 $$ = newTOKEN(OP_NULL, tmplval, 0);
519 /* Some kind of declaration - just hang on peg in the parse tree */
531 /* these two are only used by MAD */
538 { $$ = newOP(OP_NULL,0);
539 TOKEN_GETMAD($1,$$,'p');
543 format : FORMAT startformsub formname block
545 CV *fmtcv = PL_compcv;
546 SvREFCNT_inc_simple_void(PL_compcv);
548 $$ = newFORM($2, $3, $4);
549 prepend_madprops($1->tk_mad, $$, 'F');
556 if (CvOUTSIDE(fmtcv) && !CvUNIQUE(CvOUTSIDE(fmtcv))) {
557 SvREFCNT_inc_simple_void(fmtcv);
558 pad_add_anon((SV*)fmtcv, OP_NULL);
563 formname: WORD { $$ = $1; }
564 | /* NULL */ { $$ = (OP*)NULL; }
567 /* Unimplemented "my sub foo { }" */
568 mysubrout: MYSUB startsub subname proto subattrlist subbody
569 { SvREFCNT_inc_simple_void(PL_compcv);
571 $$ = newMYSUB($2, $3, $4, $5, $6);
572 token_getmad($1,$$,'d');
574 newMYSUB($2, $3, $4, $5, $6);
580 /* Subroutine definition */
581 subrout : SUB startsub subname proto subattrlist subbody
582 { SvREFCNT_inc_simple_void(PL_compcv);
585 OP* o = newSVOP(OP_ANONCODE, 0,
586 (SV*)newATTRSUB($2, $3, $4, $5, $6));
587 $$ = newOP(OP_NULL,0);
589 op_getmad($3,$$,'n');
590 op_getmad($4,$$,'s');
591 op_getmad($5,$$,'a');
592 token_getmad($1,$$,'d');
593 append_madprops($6->op_madprop, $$, 0);
597 newATTRSUB($2, $3, $4, $5, $6);
603 startsub: /* NULL */ /* start a regular subroutine scope */
604 { $$ = start_subparse(FALSE, 0);
605 SAVEFREESV(PL_compcv); }
609 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
610 { $$ = start_subparse(FALSE, CVf_ANON);
611 SAVEFREESV(PL_compcv); }
614 startformsub: /* NULL */ /* start a format subroutine scope */
615 { $$ = start_subparse(TRUE, 0);
616 SAVEFREESV(PL_compcv); }
619 /* Name of a subroutine - must be a bareword, could be special */
620 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
621 if (strEQ(name, "BEGIN") || strEQ(name, "END")
622 || strEQ(name, "INIT") || strEQ(name, "CHECK")
623 || strEQ(name, "UNITCHECK"))
624 CvSPECIAL_on(PL_compcv);
628 /* Subroutine prototype */
634 /* Optional list of subroutine attributes */
635 subattrlist: /* NULL */
639 TOKEN_GETMAD($1,$$,':');
646 TOKEN_GETMAD($1,$$,':');
650 /* List of attributes for a "my" variable declaration */
651 myattrlist: COLONATTR THING
653 TOKEN_GETMAD($1,$$,':');
660 TOKEN_GETMAD($1,$$,':');
664 /* Subroutine body - either null or a block */
665 subbody : block { $$ = $1; }
670 PL_parser->expect = XSTATE;
671 TOKEN_GETMAD($1,$$,';');
675 package : PACKAGE WORD WORD ';'
679 token_getmad($1,$$,'o');
682 token_getmad($4,$$,';');
692 package_block: PACKAGE WORD WORD '{' remember
694 int save_3_latefree = $3->op_latefree;
697 $3->op_latefree = save_3_latefree;
699 int save_2_latefree = $2->op_latefree;
702 $2->op_latefree = save_2_latefree;
706 { if (PL_parser->copline > (line_t)IVAL($4))
707 PL_parser->copline = (line_t)IVAL($4);
708 $$ = block_end($5, $7);
709 TOKEN_GETMAD($4,$$,'{');
710 TOKEN_GETMAD($8,$$,'}');
718 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
719 WORD WORD listexpr ';'
720 { SvREFCNT_inc_simple_void(PL_compcv);
722 $$ = utilize(IVAL($1), $2, $4, $5, $6);
723 token_getmad($1,$$,'o');
724 token_getmad($7,$$,';');
725 if (PL_parser->rsfp_filters &&
726 AvFILLp(PL_parser->rsfp_filters) >= 0)
727 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
729 utilize(IVAL($1), $2, $4, $5, $6);
735 /* Ordinary expressions; logical combinations */
736 expr : expr ANDOP expr
737 { $$ = newLOGOP(OP_AND, 0, $1, $3);
738 TOKEN_GETMAD($2,$$,'o');
741 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
742 TOKEN_GETMAD($2,$$,'o');
745 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
746 TOKEN_GETMAD($2,$$,'o');
748 | argexpr %prec PREC_LOW
751 /* Expressions are a list of terms joined by commas */
752 argexpr : argexpr ','
755 OP* op = newNULLLIST();
756 token_getmad($2,op,',');
757 $$ = append_elem(OP_LIST, $1, op);
766 term = newUNOP(OP_NULL, 0, term);
767 token_getmad($2,term,',');
769 $$ = append_elem(OP_LIST, $1, term);
771 | term %prec PREC_LOW
775 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
776 { $$ = convert(IVAL($1), OPf_STACKED,
777 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
778 TOKEN_GETMAD($1,$$,'o');
780 | FUNC '(' indirob expr ')' /* print ($fh @args */
781 { $$ = convert(IVAL($1), OPf_STACKED,
782 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
783 TOKEN_GETMAD($1,$$,'o');
784 TOKEN_GETMAD($2,$$,'(');
785 TOKEN_GETMAD($5,$$,')');
787 | term ARROW method lpar_or_qw listexprcom ')' /* $foo->bar(list) */
788 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
790 prepend_elem(OP_LIST, scalar($1), $5),
791 newUNOP(OP_METHOD, 0, $3)));
792 TOKEN_GETMAD($2,$$,'A');
793 TOKEN_GETMAD($4,$$,'(');
794 TOKEN_GETMAD($6,$$,')');
796 | term ARROW method /* $foo->bar */
797 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
798 append_elem(OP_LIST, scalar($1),
799 newUNOP(OP_METHOD, 0, $3)));
800 TOKEN_GETMAD($2,$$,'A');
802 | METHOD indirob listexpr /* new Class @args */
803 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
805 prepend_elem(OP_LIST, $2, $3),
806 newUNOP(OP_METHOD, 0, $1)));
808 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
809 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
811 prepend_elem(OP_LIST, $2, $4),
812 newUNOP(OP_METHOD, 0, $1)));
813 TOKEN_GETMAD($3,$$,'(');
814 TOKEN_GETMAD($5,$$,')');
816 | LSTOP listexpr /* print @args */
817 { $$ = convert(IVAL($1), 0, $2);
818 TOKEN_GETMAD($1,$$,'o');
820 | FUNC '(' listexprcom ')' /* print (@args) */
821 { $$ = convert(IVAL($1), 0, $3);
822 TOKEN_GETMAD($1,$$,'o');
823 TOKEN_GETMAD($2,$$,'(');
824 TOKEN_GETMAD($4,$$,')');
826 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
827 { SvREFCNT_inc_simple_void(PL_compcv);
828 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
829 listexpr %prec LSTOP /* ... @bar */
830 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
832 prepend_elem(OP_LIST, $<opval>4, $5), $1));
836 /* Names of methods. May use $object->$methodname */
841 /* Some kind of subscripted expression */
842 subscripted: star '{' expr ';' '}' /* *main::{something} */
843 /* In this and all the hash accessors, ';' is
844 * provided by the tokeniser */
845 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
846 PL_parser->expect = XOPERATOR;
847 TOKEN_GETMAD($2,$$,'{');
848 TOKEN_GETMAD($4,$$,';');
849 TOKEN_GETMAD($5,$$,'}');
851 | scalar '[' expr ']' /* $array[$element] */
852 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
853 TOKEN_GETMAD($2,$$,'[');
854 TOKEN_GETMAD($4,$$,']');
856 | term ARROW '[' expr ']' /* somearef->[$element] */
857 { $$ = newBINOP(OP_AELEM, 0,
858 ref(newAVREF($1),OP_RV2AV),
860 TOKEN_GETMAD($2,$$,'a');
861 TOKEN_GETMAD($3,$$,'[');
862 TOKEN_GETMAD($5,$$,']');
864 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
865 { $$ = newBINOP(OP_AELEM, 0,
866 ref(newAVREF($1),OP_RV2AV),
868 TOKEN_GETMAD($2,$$,'[');
869 TOKEN_GETMAD($4,$$,']');
871 | scalar '{' expr ';' '}' /* $foo->{bar();} */
872 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
873 PL_parser->expect = XOPERATOR;
874 TOKEN_GETMAD($2,$$,'{');
875 TOKEN_GETMAD($4,$$,';');
876 TOKEN_GETMAD($5,$$,'}');
878 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
879 { $$ = newBINOP(OP_HELEM, 0,
880 ref(newHVREF($1),OP_RV2HV),
882 PL_parser->expect = XOPERATOR;
883 TOKEN_GETMAD($2,$$,'a');
884 TOKEN_GETMAD($3,$$,'{');
885 TOKEN_GETMAD($5,$$,';');
886 TOKEN_GETMAD($6,$$,'}');
888 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
889 { $$ = newBINOP(OP_HELEM, 0,
890 ref(newHVREF($1),OP_RV2HV),
892 PL_parser->expect = XOPERATOR;
893 TOKEN_GETMAD($2,$$,'{');
894 TOKEN_GETMAD($4,$$,';');
895 TOKEN_GETMAD($5,$$,'}');
897 | term ARROW '(' ')' /* $subref->() */
898 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
899 newCVREF(0, scalar($1)));
900 TOKEN_GETMAD($2,$$,'a');
901 TOKEN_GETMAD($3,$$,'(');
902 TOKEN_GETMAD($4,$$,')');
904 | term ARROW '(' expr ')' /* $subref->(@args) */
905 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
906 append_elem(OP_LIST, $4,
907 newCVREF(0, scalar($1))));
908 TOKEN_GETMAD($2,$$,'a');
909 TOKEN_GETMAD($3,$$,'(');
910 TOKEN_GETMAD($5,$$,')');
913 | subscripted lpar_or_qw expr ')' /* $foo->{bar}->(@args) */
914 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
915 append_elem(OP_LIST, $3,
916 newCVREF(0, scalar($1))));
917 TOKEN_GETMAD($2,$$,'(');
918 TOKEN_GETMAD($4,$$,')');
920 | subscripted lpar_or_qw ')' /* $foo->{bar}->() */
921 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
922 newCVREF(0, scalar($1)));
923 TOKEN_GETMAD($2,$$,'(');
924 TOKEN_GETMAD($3,$$,')');
926 | '(' expr ')' '[' expr ']' /* list slice */
927 { $$ = newSLICEOP(0, $5, $2);
928 TOKEN_GETMAD($1,$$,'(');
929 TOKEN_GETMAD($3,$$,')');
930 TOKEN_GETMAD($4,$$,'[');
931 TOKEN_GETMAD($6,$$,']');
933 | QWLIST '[' expr ']' /* list literal slice */
934 { $$ = newSLICEOP(0, $3, $1);
935 TOKEN_GETMAD($2,$$,'[');
936 TOKEN_GETMAD($4,$$,']');
938 | '(' ')' '[' expr ']' /* empty list slice! */
939 { $$ = newSLICEOP(0, $4, (OP*)NULL);
940 TOKEN_GETMAD($1,$$,'(');
941 TOKEN_GETMAD($2,$$,')');
942 TOKEN_GETMAD($3,$$,'[');
943 TOKEN_GETMAD($5,$$,']');
947 /* Binary operators between terms */
948 termbinop: term ASSIGNOP term /* $x = $y */
949 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
950 TOKEN_GETMAD($2,$$,'o');
952 | term POWOP term /* $x ** $y */
953 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
954 TOKEN_GETMAD($2,$$,'o');
956 | term MULOP term /* $x * $y, $x x $y */
957 { if (IVAL($2) != OP_REPEAT)
959 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
960 TOKEN_GETMAD($2,$$,'o');
962 | term ADDOP term /* $x + $y */
963 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
964 TOKEN_GETMAD($2,$$,'o');
966 | term SHIFTOP term /* $x >> $y, $x << $y */
967 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
968 TOKEN_GETMAD($2,$$,'o');
970 | term RELOP term /* $x > $y, etc. */
971 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
972 TOKEN_GETMAD($2,$$,'o');
974 | term EQOP term /* $x == $y, $x eq $y */
975 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
976 TOKEN_GETMAD($2,$$,'o');
978 | term BITANDOP term /* $x & $y */
979 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
980 TOKEN_GETMAD($2,$$,'o');
982 | term BITOROP term /* $x | $y */
983 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
984 TOKEN_GETMAD($2,$$,'o');
986 | term DOTDOT term /* $x..$y, $x...$y */
988 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
992 op = (UNOP*)op->op_first; /* get to flop */
993 op = (UNOP*)op->op_first; /* get to flip */
994 op = (UNOP*)op->op_first; /* get to range */
995 token_getmad($2,(OP*)op,'o');
998 | term ANDAND term /* $x && $y */
999 { $$ = newLOGOP(OP_AND, 0, $1, $3);
1000 TOKEN_GETMAD($2,$$,'o');
1002 | term OROR term /* $x || $y */
1003 { $$ = newLOGOP(OP_OR, 0, $1, $3);
1004 TOKEN_GETMAD($2,$$,'o');
1006 | term DORDOR term /* $x // $y */
1007 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1008 TOKEN_GETMAD($2,$$,'o');
1010 | term MATCHOP term /* $x =~ /$y/ */
1011 { $$ = bind_match(IVAL($2), $1, $3);
1013 ($$->op_type == OP_NOT
1014 ? ((UNOP*)$$)->op_first : $$),
1019 /* Unary operators and terms */
1020 termunop : '-' term %prec UMINUS /* -$x */
1021 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1022 TOKEN_GETMAD($1,$$,'o');
1024 | '+' term %prec UMINUS /* +$x */
1026 newUNOP(OP_NULL, 0, $2),
1029 TOKEN_GETMAD($1,$$,'+');
1031 | '!' term /* !$x */
1032 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1033 TOKEN_GETMAD($1,$$,'o');
1035 | '~' term /* ~$x */
1036 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1037 TOKEN_GETMAD($1,$$,'o');
1039 | term POSTINC /* $x++ */
1040 { $$ = newUNOP(OP_POSTINC, 0,
1041 mod(scalar($1), OP_POSTINC));
1042 TOKEN_GETMAD($2,$$,'o');
1044 | term POSTDEC /* $x-- */
1045 { $$ = newUNOP(OP_POSTDEC, 0,
1046 mod(scalar($1), OP_POSTDEC));
1047 TOKEN_GETMAD($2,$$,'o');
1049 | PREINC term /* ++$x */
1050 { $$ = newUNOP(OP_PREINC, 0,
1051 mod(scalar($2), OP_PREINC));
1052 TOKEN_GETMAD($1,$$,'o');
1054 | PREDEC term /* --$x */
1055 { $$ = newUNOP(OP_PREDEC, 0,
1056 mod(scalar($2), OP_PREDEC));
1057 TOKEN_GETMAD($1,$$,'o');
1062 /* Constructors for anonymous data */
1063 anonymous: '[' expr ']'
1064 { $$ = newANONLIST($2);
1065 TOKEN_GETMAD($1,$$,'[');
1066 TOKEN_GETMAD($3,$$,']');
1069 { $$ = newANONLIST((OP*)NULL);
1070 TOKEN_GETMAD($1,$$,'[');
1071 TOKEN_GETMAD($2,$$,']');
1073 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1074 { $$ = newANONHASH($2);
1075 TOKEN_GETMAD($1,$$,'{');
1076 TOKEN_GETMAD($3,$$,';');
1077 TOKEN_GETMAD($4,$$,'}');
1079 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1080 { $$ = newANONHASH((OP*)NULL);
1081 TOKEN_GETMAD($1,$$,'{');
1082 TOKEN_GETMAD($2,$$,';');
1083 TOKEN_GETMAD($3,$$,'}');
1085 | ANONSUB startanonsub proto subattrlist block %prec '('
1086 { SvREFCNT_inc_simple_void(PL_compcv);
1087 $$ = newANONATTRSUB($2, $3, $4, $5);
1088 TOKEN_GETMAD($1,$$,'o');
1089 OP_GETMAD($3,$$,'s');
1090 OP_GETMAD($4,$$,'a');
1095 /* Things called with "do" */
1096 termdo : DO term %prec UNIOP /* do $filename */
1097 { $$ = dofile($2, IVAL($1));
1098 TOKEN_GETMAD($1,$$,'o');
1100 | DO block %prec '(' /* do { code */
1101 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1102 TOKEN_GETMAD($1,$$,'D');
1104 | DO WORD lpar_or_qw ')' /* do somesub() */
1105 { $$ = newUNOP(OP_ENTERSUB,
1106 OPf_SPECIAL|OPf_STACKED,
1107 prepend_elem(OP_LIST,
1109 (OPpENTERSUB_AMPER<<8),
1111 )),(OP*)NULL)); dep();
1112 TOKEN_GETMAD($1,$$,'o');
1113 TOKEN_GETMAD($3,$$,'(');
1114 TOKEN_GETMAD($4,$$,')');
1116 | DO WORD lpar_or_qw expr ')' /* do somesub(@args) */
1117 { $$ = newUNOP(OP_ENTERSUB,
1118 OPf_SPECIAL|OPf_STACKED,
1119 append_elem(OP_LIST,
1122 (OPpENTERSUB_AMPER<<8),
1125 TOKEN_GETMAD($1,$$,'o');
1126 TOKEN_GETMAD($3,$$,'(');
1127 TOKEN_GETMAD($5,$$,')');
1129 | DO scalar lpar_or_qw ')' /* do $subref () */
1130 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1131 prepend_elem(OP_LIST,
1132 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1133 TOKEN_GETMAD($1,$$,'o');
1134 TOKEN_GETMAD($3,$$,'(');
1135 TOKEN_GETMAD($4,$$,')');
1137 | DO scalar lpar_or_qw expr ')' /* do $subref (@args) */
1138 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1139 prepend_elem(OP_LIST,
1141 scalar(newCVREF(0,scalar($2))))); dep();
1142 TOKEN_GETMAD($1,$$,'o');
1143 TOKEN_GETMAD($3,$$,'(');
1144 TOKEN_GETMAD($5,$$,')');
1153 | term '?' term ':' term
1154 { $$ = newCONDOP(0, $1, $3, $5);
1155 TOKEN_GETMAD($2,$$,'?');
1156 TOKEN_GETMAD($4,$$,':');
1158 | REFGEN term /* \$x, \@y, \%z */
1159 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1160 TOKEN_GETMAD($1,$$,'o');
1162 | myattrterm %prec UNIOP
1164 | LOCAL term %prec UNIOP
1165 { $$ = localize($2,IVAL($1));
1166 TOKEN_GETMAD($1,$$,'k');
1169 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1170 TOKEN_GETMAD($1,$$,'(');
1171 TOKEN_GETMAD($3,$$,')');
1174 { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
1176 { $$ = sawparens(newNULLLIST());
1177 TOKEN_GETMAD($1,$$,'(');
1178 TOKEN_GETMAD($2,$$,')');
1188 | arylen %prec '(' /* $#x, $#{ something } */
1189 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1192 | ary '[' expr ']' /* array slice */
1193 { $$ = prepend_elem(OP_ASLICE,
1194 newOP(OP_PUSHMARK, 0),
1195 newLISTOP(OP_ASLICE, 0,
1197 ref($1, OP_ASLICE)));
1198 TOKEN_GETMAD($2,$$,'[');
1199 TOKEN_GETMAD($4,$$,']');
1201 | ary '{' expr ';' '}' /* @hash{@keys} */
1202 { $$ = prepend_elem(OP_HSLICE,
1203 newOP(OP_PUSHMARK, 0),
1204 newLISTOP(OP_HSLICE, 0,
1206 ref(oopsHV($1), OP_HSLICE)));
1207 PL_parser->expect = XOPERATOR;
1208 TOKEN_GETMAD($2,$$,'{');
1209 TOKEN_GETMAD($4,$$,';');
1210 TOKEN_GETMAD($5,$$,'}');
1215 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1216 | amper lpar_or_qw ')' /* &foo() */
1217 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1218 TOKEN_GETMAD($2,$$,'(');
1219 TOKEN_GETMAD($3,$$,')');
1221 | amper lpar_or_qw expr ')' /* &foo(@args) */
1223 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1224 append_elem(OP_LIST, $3, scalar($1)));
1227 if (op->op_type == OP_CONST) { /* defeat const fold */
1228 op = (OP*)op->op_madprop->mad_val;
1230 token_getmad($2,op,'(');
1231 token_getmad($4,op,')');
1234 | NOAMP WORD listexpr /* foo(@args) */
1235 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1236 append_elem(OP_LIST, $3, scalar($2)));
1237 TOKEN_GETMAD($1,$$,'o');
1239 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1240 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1241 PL_hints |= HINT_BLOCK_SCOPE;
1242 TOKEN_GETMAD($1,$$,'o');
1245 { $$ = newLOOPEX(IVAL($1),$2);
1246 TOKEN_GETMAD($1,$$,'o');
1248 | NOTOP argexpr /* not $foo */
1249 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1250 TOKEN_GETMAD($1,$$,'o');
1252 | UNIOP /* Unary op, $_ implied */
1253 { $$ = newOP(IVAL($1), 0);
1254 TOKEN_GETMAD($1,$$,'o');
1256 | UNIOP block /* eval { foo }* */
1257 { $$ = newUNOP(IVAL($1), 0, $2);
1258 TOKEN_GETMAD($1,$$,'o');
1260 | UNIOP term /* Unary op */
1261 { $$ = newUNOP(IVAL($1), 0, $2);
1262 TOKEN_GETMAD($1,$$,'o');
1264 | REQUIRE /* require, $_ implied */
1265 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1266 TOKEN_GETMAD($1,$$,'o');
1268 | REQUIRE term /* require Foo */
1269 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1270 TOKEN_GETMAD($1,$$,'o');
1273 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1274 | UNIOPSUB term /* Sub treated as unop */
1275 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1276 append_elem(OP_LIST, $2, scalar($1))); }
1277 | FUNC0 /* Nullary operator */
1278 { $$ = newOP(IVAL($1), 0);
1279 TOKEN_GETMAD($1,$$,'o');
1282 { $$ = newOP(IVAL($1), 0);
1283 TOKEN_GETMAD($1,$$,'o');
1284 TOKEN_GETMAD($2,$$,'(');
1285 TOKEN_GETMAD($3,$$,')');
1287 | FUNC0SUB /* Sub treated as nullop */
1288 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1290 | FUNC1 '(' ')' /* not () */
1291 { $$ = (IVAL($1) == OP_NOT)
1292 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1293 : newOP(IVAL($1), OPf_SPECIAL);
1295 TOKEN_GETMAD($1,$$,'o');
1296 TOKEN_GETMAD($2,$$,'(');
1297 TOKEN_GETMAD($3,$$,')');
1299 | FUNC1 '(' expr ')' /* not($foo) */
1300 { $$ = newUNOP(IVAL($1), 0, $3);
1301 TOKEN_GETMAD($1,$$,'o');
1302 TOKEN_GETMAD($2,$$,'(');
1303 TOKEN_GETMAD($4,$$,')');
1305 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1306 { $$ = pmruntime($1, $3, 1);
1307 TOKEN_GETMAD($2,$$,'(');
1308 TOKEN_GETMAD($4,$$,')');
1314 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1315 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1316 TOKEN_GETMAD($1,$$,'X');
1321 /* "my" declarations, with optional attributes */
1322 myattrterm: MY myterm myattrlist
1323 { $$ = my_attrs($2,$3);
1325 token_getmad($1,$$,'d');
1326 append_madprops($3->op_madprop, $$, 'a');
1331 { $$ = localize($2,IVAL($1));
1332 TOKEN_GETMAD($1,$$,'d');
1336 /* Things that can be "my"'d */
1337 myterm : '(' expr ')'
1338 { $$ = sawparens($2);
1339 TOKEN_GETMAD($1,$$,'(');
1340 TOKEN_GETMAD($3,$$,')');
1343 { $$ = sawparens(newNULLLIST());
1344 TOKEN_GETMAD($1,$$,'(');
1345 TOKEN_GETMAD($2,$$,')');
1355 /* Basic list expressions */
1356 listexpr: /* NULL */ %prec PREC_LOW
1358 | argexpr %prec PREC_LOW
1362 listexprcom: /* NULL */
1371 { coerce_qwlist_to_paren_list($1); }
1376 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1379 { PL_parser->in_my = 0; $$ = my($1); }
1383 { $$ = newCVREF(IVAL($1),$2);
1384 TOKEN_GETMAD($1,$$,'&');
1388 scalar : '$' indirob
1389 { $$ = newSVREF($2);
1390 TOKEN_GETMAD($1,$$,'$');
1395 { $$ = newAVREF($2);
1396 TOKEN_GETMAD($1,$$,'@');
1401 { $$ = newHVREF($2);
1402 TOKEN_GETMAD($1,$$,'%');
1406 arylen : DOLSHARP indirob
1407 { $$ = newAVREF($2);
1408 TOKEN_GETMAD($1,$$,'l');
1413 { $$ = newGVREF(0,$2);
1414 TOKEN_GETMAD($1,$$,'*');
1418 /* Indirect objects */
1420 { $$ = scalar($1); }
1421 | scalar %prec PREC_LOW
1422 { $$ = scalar($1); }