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) Perl_allocmy(aTHX_ STR_WITH_LEN("$_"), 0); }
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)IVAL($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)IVAL($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) IVAL($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
515 CV *fmtcv = PL_compcv;
516 SvREFCNT_inc_simple_void(PL_compcv);
518 $$ = newFORM($2, $3, $4);
519 prepend_madprops($1->tk_mad, $$, 'F');
526 if (CvOUTSIDE(fmtcv) && !CvUNIQUE(CvOUTSIDE(fmtcv))) {
527 SvREFCNT_inc_simple_void(fmtcv);
528 pad_add_anon((SV*)fmtcv, OP_NULL);
533 formname: WORD { $$ = $1; }
534 | /* NULL */ { $$ = (OP*)NULL; }
537 /* Unimplemented "my sub foo { }" */
538 mysubrout: MYSUB startsub subname proto subattrlist subbody
539 { SvREFCNT_inc_simple_void(PL_compcv);
541 $$ = newMYSUB($2, $3, $4, $5, $6);
542 token_getmad($1,$$,'d');
544 newMYSUB($2, $3, $4, $5, $6);
550 /* Subroutine definition */
551 subrout : SUB startsub subname proto subattrlist subbody
552 { SvREFCNT_inc_simple_void(PL_compcv);
555 OP* o = newSVOP(OP_ANONCODE, 0,
556 (SV*)newATTRSUB($2, $3, $4, $5, $6));
557 $$ = newOP(OP_NULL,0);
559 op_getmad($3,$$,'n');
560 op_getmad($4,$$,'s');
561 op_getmad($5,$$,'a');
562 token_getmad($1,$$,'d');
563 append_madprops($6->op_madprop, $$, 0);
567 newATTRSUB($2, $3, $4, $5, $6);
573 startsub: /* NULL */ /* start a regular subroutine scope */
574 { $$ = start_subparse(FALSE, 0);
575 SAVEFREESV(PL_compcv); }
579 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
580 { $$ = start_subparse(FALSE, CVf_ANON);
581 SAVEFREESV(PL_compcv); }
584 startformsub: /* NULL */ /* start a format subroutine scope */
585 { $$ = start_subparse(TRUE, 0);
586 SAVEFREESV(PL_compcv); }
589 /* Name of a subroutine - must be a bareword, could be special */
590 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
591 if (strEQ(name, "BEGIN") || strEQ(name, "END")
592 || strEQ(name, "INIT") || strEQ(name, "CHECK")
593 || strEQ(name, "UNITCHECK"))
594 CvSPECIAL_on(PL_compcv);
598 /* Subroutine prototype */
604 /* Optional list of subroutine attributes */
605 subattrlist: /* NULL */
609 TOKEN_GETMAD($1,$$,':');
616 TOKEN_GETMAD($1,$$,':');
620 /* List of attributes for a "my" variable declaration */
621 myattrlist: COLONATTR THING
623 TOKEN_GETMAD($1,$$,':');
630 TOKEN_GETMAD($1,$$,':');
634 /* Subroutine body - either null or a block */
635 subbody : block { $$ = $1; }
640 PL_parser->expect = XSTATE;
641 TOKEN_GETMAD($1,$$,';');
645 package : PACKAGE WORD WORD ';'
649 token_getmad($1,$$,'o');
652 token_getmad($4,$$,';');
663 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
664 WORD WORD listexpr ';'
665 { SvREFCNT_inc_simple_void(PL_compcv);
667 $$ = utilize(IVAL($1), $2, $4, $5, $6);
668 token_getmad($1,$$,'o');
669 token_getmad($7,$$,';');
670 if (PL_parser->rsfp_filters &&
671 AvFILLp(PL_parser->rsfp_filters) >= 0)
672 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
674 utilize(IVAL($1), $2, $4, $5, $6);
680 /* Ordinary expressions; logical combinations */
681 expr : expr ANDOP expr
682 { $$ = newLOGOP(OP_AND, 0, $1, $3);
683 TOKEN_GETMAD($2,$$,'o');
686 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
687 TOKEN_GETMAD($2,$$,'o');
690 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
691 TOKEN_GETMAD($2,$$,'o');
693 | argexpr %prec PREC_LOW
696 /* Expressions are a list of terms joined by commas */
697 argexpr : argexpr ','
700 OP* op = newNULLLIST();
701 token_getmad($2,op,',');
702 $$ = append_elem(OP_LIST, $1, op);
711 term = newUNOP(OP_NULL, 0, term);
712 token_getmad($2,term,',');
714 $$ = append_elem(OP_LIST, $1, term);
716 | term %prec PREC_LOW
720 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
721 { $$ = convert(IVAL($1), OPf_STACKED,
722 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
723 TOKEN_GETMAD($1,$$,'o');
725 | FUNC '(' indirob expr ')' /* print ($fh @args */
726 { $$ = convert(IVAL($1), OPf_STACKED,
727 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
728 TOKEN_GETMAD($1,$$,'o');
729 TOKEN_GETMAD($2,$$,'(');
730 TOKEN_GETMAD($5,$$,')');
732 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
733 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
735 prepend_elem(OP_LIST, scalar($1), $5),
736 newUNOP(OP_METHOD, 0, $3)));
737 TOKEN_GETMAD($2,$$,'A');
738 TOKEN_GETMAD($4,$$,'(');
739 TOKEN_GETMAD($6,$$,')');
741 | term ARROW method /* $foo->bar */
742 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
743 append_elem(OP_LIST, scalar($1),
744 newUNOP(OP_METHOD, 0, $3)));
745 TOKEN_GETMAD($2,$$,'A');
747 | METHOD indirob listexpr /* new Class @args */
748 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
750 prepend_elem(OP_LIST, $2, $3),
751 newUNOP(OP_METHOD, 0, $1)));
753 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
754 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
756 prepend_elem(OP_LIST, $2, $4),
757 newUNOP(OP_METHOD, 0, $1)));
758 TOKEN_GETMAD($3,$$,'(');
759 TOKEN_GETMAD($5,$$,')');
761 | LSTOP listexpr /* print @args */
762 { $$ = convert(IVAL($1), 0, $2);
763 TOKEN_GETMAD($1,$$,'o');
765 | FUNC '(' listexprcom ')' /* print (@args) */
766 { $$ = convert(IVAL($1), 0, $3);
767 TOKEN_GETMAD($1,$$,'o');
768 TOKEN_GETMAD($2,$$,'(');
769 TOKEN_GETMAD($4,$$,')');
771 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
772 { SvREFCNT_inc_simple_void(PL_compcv);
773 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
774 listexpr %prec LSTOP /* ... @bar */
775 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
777 prepend_elem(OP_LIST, $<opval>4, $5), $1));
781 /* Names of methods. May use $object->$methodname */
786 /* Some kind of subscripted expression */
787 subscripted: star '{' expr ';' '}' /* *main::{something} */
788 /* In this and all the hash accessors, ';' is
789 * provided by the tokeniser */
790 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
791 PL_parser->expect = XOPERATOR;
792 TOKEN_GETMAD($2,$$,'{');
793 TOKEN_GETMAD($4,$$,';');
794 TOKEN_GETMAD($5,$$,'}');
796 | scalar '[' expr ']' /* $array[$element] */
797 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
798 TOKEN_GETMAD($2,$$,'[');
799 TOKEN_GETMAD($4,$$,']');
801 | term ARROW '[' expr ']' /* somearef->[$element] */
802 { $$ = newBINOP(OP_AELEM, 0,
803 ref(newAVREF($1),OP_RV2AV),
805 TOKEN_GETMAD($2,$$,'a');
806 TOKEN_GETMAD($3,$$,'[');
807 TOKEN_GETMAD($5,$$,']');
809 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
810 { $$ = newBINOP(OP_AELEM, 0,
811 ref(newAVREF($1),OP_RV2AV),
813 TOKEN_GETMAD($2,$$,'[');
814 TOKEN_GETMAD($4,$$,']');
816 | scalar '{' expr ';' '}' /* $foo->{bar();} */
817 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
818 PL_parser->expect = XOPERATOR;
819 TOKEN_GETMAD($2,$$,'{');
820 TOKEN_GETMAD($4,$$,';');
821 TOKEN_GETMAD($5,$$,'}');
823 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
824 { $$ = newBINOP(OP_HELEM, 0,
825 ref(newHVREF($1),OP_RV2HV),
827 PL_parser->expect = XOPERATOR;
828 TOKEN_GETMAD($2,$$,'a');
829 TOKEN_GETMAD($3,$$,'{');
830 TOKEN_GETMAD($5,$$,';');
831 TOKEN_GETMAD($6,$$,'}');
833 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
834 { $$ = newBINOP(OP_HELEM, 0,
835 ref(newHVREF($1),OP_RV2HV),
837 PL_parser->expect = XOPERATOR;
838 TOKEN_GETMAD($2,$$,'{');
839 TOKEN_GETMAD($4,$$,';');
840 TOKEN_GETMAD($5,$$,'}');
842 | term ARROW '(' ')' /* $subref->() */
843 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
844 newCVREF(0, scalar($1)));
845 TOKEN_GETMAD($2,$$,'a');
846 TOKEN_GETMAD($3,$$,'(');
847 TOKEN_GETMAD($4,$$,')');
849 | term ARROW '(' expr ')' /* $subref->(@args) */
850 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
851 append_elem(OP_LIST, $4,
852 newCVREF(0, scalar($1))));
853 TOKEN_GETMAD($2,$$,'a');
854 TOKEN_GETMAD($3,$$,'(');
855 TOKEN_GETMAD($5,$$,')');
858 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
859 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
860 append_elem(OP_LIST, $3,
861 newCVREF(0, scalar($1))));
862 TOKEN_GETMAD($2,$$,'(');
863 TOKEN_GETMAD($4,$$,')');
865 | subscripted '(' ')' /* $foo->{bar}->() */
866 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
867 newCVREF(0, scalar($1)));
868 TOKEN_GETMAD($2,$$,'(');
869 TOKEN_GETMAD($3,$$,')');
871 | '(' expr ')' '[' expr ']' /* list slice */
872 { $$ = newSLICEOP(0, $5, $2);
873 TOKEN_GETMAD($1,$$,'(');
874 TOKEN_GETMAD($3,$$,')');
875 TOKEN_GETMAD($4,$$,'[');
876 TOKEN_GETMAD($6,$$,']');
878 | '(' ')' '[' expr ']' /* empty list slice! */
879 { $$ = newSLICEOP(0, $4, (OP*)NULL);
880 TOKEN_GETMAD($1,$$,'(');
881 TOKEN_GETMAD($2,$$,')');
882 TOKEN_GETMAD($3,$$,'[');
883 TOKEN_GETMAD($5,$$,']');
887 /* Binary operators between terms */
888 termbinop: term ASSIGNOP term /* $x = $y */
889 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
890 TOKEN_GETMAD($2,$$,'o');
892 | term POWOP term /* $x ** $y */
893 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
894 TOKEN_GETMAD($2,$$,'o');
896 | term MULOP term /* $x * $y, $x x $y */
897 { if (IVAL($2) != OP_REPEAT)
899 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
900 TOKEN_GETMAD($2,$$,'o');
902 | term ADDOP term /* $x + $y */
903 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
904 TOKEN_GETMAD($2,$$,'o');
906 | term SHIFTOP term /* $x >> $y, $x << $y */
907 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
908 TOKEN_GETMAD($2,$$,'o');
910 | term RELOP term /* $x > $y, etc. */
911 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
912 TOKEN_GETMAD($2,$$,'o');
914 | term EQOP term /* $x == $y, $x eq $y */
915 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
916 TOKEN_GETMAD($2,$$,'o');
918 | term BITANDOP term /* $x & $y */
919 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
920 TOKEN_GETMAD($2,$$,'o');
922 | term BITOROP term /* $x | $y */
923 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
924 TOKEN_GETMAD($2,$$,'o');
926 | term DOTDOT term /* $x..$y, $x...$y */
928 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
932 op = (UNOP*)op->op_first; /* get to flop */
933 op = (UNOP*)op->op_first; /* get to flip */
934 op = (UNOP*)op->op_first; /* get to range */
935 token_getmad($2,(OP*)op,'o');
938 | term ANDAND term /* $x && $y */
939 { $$ = newLOGOP(OP_AND, 0, $1, $3);
940 TOKEN_GETMAD($2,$$,'o');
942 | term OROR term /* $x || $y */
943 { $$ = newLOGOP(OP_OR, 0, $1, $3);
944 TOKEN_GETMAD($2,$$,'o');
946 | term DORDOR term /* $x // $y */
947 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
948 TOKEN_GETMAD($2,$$,'o');
950 | term MATCHOP term /* $x =~ /$y/ */
951 { $$ = bind_match(IVAL($2), $1, $3);
953 ($$->op_type == OP_NOT
954 ? ((UNOP*)$$)->op_first : $$),
959 /* Unary operators and terms */
960 termunop : '-' term %prec UMINUS /* -$x */
961 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
962 TOKEN_GETMAD($1,$$,'o');
964 | '+' term %prec UMINUS /* +$x */
966 newUNOP(OP_NULL, 0, $2),
969 TOKEN_GETMAD($1,$$,'+');
972 { $$ = newUNOP(OP_NOT, 0, scalar($2));
973 TOKEN_GETMAD($1,$$,'o');
976 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
977 TOKEN_GETMAD($1,$$,'o');
979 | term POSTINC /* $x++ */
980 { $$ = newUNOP(OP_POSTINC, 0,
981 mod(scalar($1), OP_POSTINC));
982 TOKEN_GETMAD($2,$$,'o');
984 | term POSTDEC /* $x-- */
985 { $$ = newUNOP(OP_POSTDEC, 0,
986 mod(scalar($1), OP_POSTDEC));
987 TOKEN_GETMAD($2,$$,'o');
989 | PREINC term /* ++$x */
990 { $$ = newUNOP(OP_PREINC, 0,
991 mod(scalar($2), OP_PREINC));
992 TOKEN_GETMAD($1,$$,'o');
994 | PREDEC term /* --$x */
995 { $$ = newUNOP(OP_PREDEC, 0,
996 mod(scalar($2), OP_PREDEC));
997 TOKEN_GETMAD($1,$$,'o');
1002 /* Constructors for anonymous data */
1003 anonymous: '[' expr ']'
1004 { $$ = newANONLIST($2);
1005 TOKEN_GETMAD($1,$$,'[');
1006 TOKEN_GETMAD($3,$$,']');
1009 { $$ = newANONLIST((OP*)NULL);
1010 TOKEN_GETMAD($1,$$,'[');
1011 TOKEN_GETMAD($2,$$,']');
1013 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1014 { $$ = newANONHASH($2);
1015 TOKEN_GETMAD($1,$$,'{');
1016 TOKEN_GETMAD($3,$$,';');
1017 TOKEN_GETMAD($4,$$,'}');
1019 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1020 { $$ = newANONHASH((OP*)NULL);
1021 TOKEN_GETMAD($1,$$,'{');
1022 TOKEN_GETMAD($2,$$,';');
1023 TOKEN_GETMAD($3,$$,'}');
1025 | ANONSUB startanonsub proto subattrlist block %prec '('
1026 { SvREFCNT_inc_simple_void(PL_compcv);
1027 $$ = newANONATTRSUB($2, $3, $4, $5);
1028 TOKEN_GETMAD($1,$$,'o');
1029 OP_GETMAD($3,$$,'s');
1030 OP_GETMAD($4,$$,'a');
1035 /* Things called with "do" */
1036 termdo : DO term %prec UNIOP /* do $filename */
1037 { $$ = dofile($2, IVAL($1));
1038 TOKEN_GETMAD($1,$$,'o');
1040 | DO block %prec '(' /* do { code */
1041 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1042 TOKEN_GETMAD($1,$$,'D');
1044 | DO WORD '(' ')' /* do somesub() */
1045 { $$ = newUNOP(OP_ENTERSUB,
1046 OPf_SPECIAL|OPf_STACKED,
1047 prepend_elem(OP_LIST,
1049 (OPpENTERSUB_AMPER<<8),
1051 )),(OP*)NULL)); dep();
1052 TOKEN_GETMAD($1,$$,'o');
1053 TOKEN_GETMAD($3,$$,'(');
1054 TOKEN_GETMAD($4,$$,')');
1056 | DO WORD '(' expr ')' /* do somesub(@args) */
1057 { $$ = newUNOP(OP_ENTERSUB,
1058 OPf_SPECIAL|OPf_STACKED,
1059 append_elem(OP_LIST,
1062 (OPpENTERSUB_AMPER<<8),
1065 TOKEN_GETMAD($1,$$,'o');
1066 TOKEN_GETMAD($3,$$,'(');
1067 TOKEN_GETMAD($5,$$,')');
1069 | DO scalar '(' ')' /* do $subref () */
1070 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1071 prepend_elem(OP_LIST,
1072 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1073 TOKEN_GETMAD($1,$$,'o');
1074 TOKEN_GETMAD($3,$$,'(');
1075 TOKEN_GETMAD($4,$$,')');
1077 | DO scalar '(' expr ')' /* do $subref (@args) */
1078 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1079 prepend_elem(OP_LIST,
1081 scalar(newCVREF(0,scalar($2))))); dep();
1082 TOKEN_GETMAD($1,$$,'o');
1083 TOKEN_GETMAD($3,$$,'(');
1084 TOKEN_GETMAD($5,$$,')');
1093 | term '?' term ':' term
1094 { $$ = newCONDOP(0, $1, $3, $5);
1095 TOKEN_GETMAD($2,$$,'?');
1096 TOKEN_GETMAD($4,$$,':');
1098 | REFGEN term /* \$x, \@y, \%z */
1099 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1100 TOKEN_GETMAD($1,$$,'o');
1102 | myattrterm %prec UNIOP
1104 | LOCAL term %prec UNIOP
1105 { $$ = localize($2,IVAL($1));
1106 TOKEN_GETMAD($1,$$,'k');
1109 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1110 TOKEN_GETMAD($1,$$,'(');
1111 TOKEN_GETMAD($3,$$,')');
1114 { $$ = sawparens(newNULLLIST());
1115 TOKEN_GETMAD($1,$$,'(');
1116 TOKEN_GETMAD($2,$$,')');
1126 | arylen %prec '(' /* $#x, $#{ something } */
1127 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1130 | ary '[' expr ']' /* array slice */
1131 { $$ = prepend_elem(OP_ASLICE,
1132 newOP(OP_PUSHMARK, 0),
1133 newLISTOP(OP_ASLICE, 0,
1135 ref($1, OP_ASLICE)));
1136 TOKEN_GETMAD($2,$$,'[');
1137 TOKEN_GETMAD($4,$$,']');
1139 | ary '{' expr ';' '}' /* @hash{@keys} */
1140 { $$ = prepend_elem(OP_HSLICE,
1141 newOP(OP_PUSHMARK, 0),
1142 newLISTOP(OP_HSLICE, 0,
1144 ref(oopsHV($1), OP_HSLICE)));
1145 PL_parser->expect = XOPERATOR;
1146 TOKEN_GETMAD($2,$$,'{');
1147 TOKEN_GETMAD($4,$$,';');
1148 TOKEN_GETMAD($5,$$,'}');
1153 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1154 | amper '(' ')' /* &foo() */
1155 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1156 TOKEN_GETMAD($2,$$,'(');
1157 TOKEN_GETMAD($3,$$,')');
1159 | amper '(' expr ')' /* &foo(@args) */
1161 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1162 append_elem(OP_LIST, $3, scalar($1)));
1165 if (op->op_type == OP_CONST) { /* defeat const fold */
1166 op = (OP*)op->op_madprop->mad_val;
1168 token_getmad($2,op,'(');
1169 token_getmad($4,op,')');
1172 | NOAMP WORD listexpr /* foo(@args) */
1173 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1174 append_elem(OP_LIST, $3, scalar($2)));
1175 TOKEN_GETMAD($1,$$,'o');
1177 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1178 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1179 PL_hints |= HINT_BLOCK_SCOPE;
1180 TOKEN_GETMAD($1,$$,'o');
1183 { $$ = newLOOPEX(IVAL($1),$2);
1184 TOKEN_GETMAD($1,$$,'o');
1186 | NOTOP argexpr /* not $foo */
1187 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1188 TOKEN_GETMAD($1,$$,'o');
1190 | UNIOP /* Unary op, $_ implied */
1191 { $$ = newOP(IVAL($1), 0);
1192 TOKEN_GETMAD($1,$$,'o');
1194 | UNIOP block /* eval { foo }* */
1195 { $$ = newUNOP(IVAL($1), 0, $2);
1196 TOKEN_GETMAD($1,$$,'o');
1198 | UNIOP term /* Unary op */
1199 { $$ = newUNOP(IVAL($1), 0, $2);
1200 TOKEN_GETMAD($1,$$,'o');
1202 | REQUIRE /* require, $_ implied */
1203 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1204 TOKEN_GETMAD($1,$$,'o');
1206 | REQUIRE term /* require Foo */
1207 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1208 TOKEN_GETMAD($1,$$,'o');
1211 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1212 | UNIOPSUB term /* Sub treated as unop */
1213 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1214 append_elem(OP_LIST, $2, scalar($1))); }
1215 | FUNC0 /* Nullary operator */
1216 { $$ = newOP(IVAL($1), 0);
1217 TOKEN_GETMAD($1,$$,'o');
1220 { $$ = newOP(IVAL($1), 0);
1221 TOKEN_GETMAD($1,$$,'o');
1222 TOKEN_GETMAD($2,$$,'(');
1223 TOKEN_GETMAD($3,$$,')');
1225 | FUNC0SUB /* Sub treated as nullop */
1226 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1228 | FUNC1 '(' ')' /* not () */
1229 { $$ = (IVAL($1) == OP_NOT)
1230 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1231 : newOP(IVAL($1), OPf_SPECIAL);
1233 TOKEN_GETMAD($1,$$,'o');
1234 TOKEN_GETMAD($2,$$,'(');
1235 TOKEN_GETMAD($3,$$,')');
1237 | FUNC1 '(' expr ')' /* not($foo) */
1238 { $$ = newUNOP(IVAL($1), 0, $3);
1239 TOKEN_GETMAD($1,$$,'o');
1240 TOKEN_GETMAD($2,$$,'(');
1241 TOKEN_GETMAD($4,$$,')');
1243 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1244 { $$ = pmruntime($1, $3, 1);
1245 TOKEN_GETMAD($2,$$,'(');
1246 TOKEN_GETMAD($4,$$,')');
1252 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1253 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1254 TOKEN_GETMAD($1,$$,'X');
1259 /* "my" declarations, with optional attributes */
1260 myattrterm: MY myterm myattrlist
1261 { $$ = my_attrs($2,$3);
1263 token_getmad($1,$$,'d');
1264 append_madprops($3->op_madprop, $$, 'a');
1269 { $$ = localize($2,IVAL($1));
1270 TOKEN_GETMAD($1,$$,'d');
1274 /* Things that can be "my"'d */
1275 myterm : '(' expr ')'
1276 { $$ = sawparens($2);
1277 TOKEN_GETMAD($1,$$,'(');
1278 TOKEN_GETMAD($3,$$,')');
1281 { $$ = sawparens(newNULLLIST());
1282 TOKEN_GETMAD($1,$$,'(');
1283 TOKEN_GETMAD($2,$$,')');
1293 /* Basic list expressions */
1294 listexpr: /* NULL */ %prec PREC_LOW
1296 | argexpr %prec PREC_LOW
1300 listexprcom: /* NULL */
1307 OP* op = newNULLLIST();
1308 token_getmad($2,op,',');
1309 $$ = append_elem(OP_LIST, $1, op);
1317 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1320 { PL_parser->in_my = 0; $$ = my($1); }
1324 { $$ = newCVREF(IVAL($1),$2);
1325 TOKEN_GETMAD($1,$$,'&');
1329 scalar : '$' indirob
1330 { $$ = newSVREF($2);
1331 TOKEN_GETMAD($1,$$,'$');
1336 { $$ = newAVREF($2);
1337 TOKEN_GETMAD($1,$$,'@');
1342 { $$ = newHVREF($2);
1343 TOKEN_GETMAD($1,$$,'%');
1347 arylen : DOLSHARP indirob
1348 { $$ = newAVREF($2);
1349 TOKEN_GETMAD($1,$$,'l');
1354 { $$ = newGVREF(0,$2);
1355 TOKEN_GETMAD($1,$$,'*');
1359 /* Indirect objects */
1361 { $$ = scalar($1); }
1362 | scalar %prec PREC_LOW
1363 { $$ = scalar($1); }