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');
273 { $$ = newWHENOP($3, scope($1)); }
276 /* else and elsif blocks */
280 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
281 TOKEN_GETMAD($1,$$,'o');
283 | ELSIF '(' mexpr ')' mblock else
284 { PL_parser->copline = (line_t)IVAL($1);
285 $$ = newCONDOP(0, newSTATEOP(OPf_SPECIAL,NULL,$3), scope($5), $6);
286 PL_hints |= HINT_BLOCK_SCOPE;
287 TOKEN_GETMAD($1,$$,'I');
288 TOKEN_GETMAD($2,$$,'(');
289 TOKEN_GETMAD($4,$$,')');
293 /* Real conditional expressions */
294 cond : IF '(' remember mexpr ')' mblock else
295 { PL_parser->copline = (line_t)IVAL($1);
297 newCONDOP(0, $4, scope($6), $7));
298 TOKEN_GETMAD($1,$$,'I');
299 TOKEN_GETMAD($2,$$,'(');
300 TOKEN_GETMAD($5,$$,')');
302 | UNLESS '(' remember miexpr ')' mblock else
303 { PL_parser->copline = (line_t)IVAL($1);
305 newCONDOP(0, $4, scope($6), $7));
306 TOKEN_GETMAD($1,$$,'I');
307 TOKEN_GETMAD($2,$$,'(');
308 TOKEN_GETMAD($5,$$,')');
312 /* Cases for a switch statement */
313 case : WHEN '(' remember mexpr ')' mblock
315 newWHENOP($4, scope($6))); }
317 { $$ = newWHENOP(0, scope($2)); }
320 /* Continue blocks */
325 TOKEN_GETMAD($1,$$,'o');
329 /* Loops: while, until, for, and a bare block */
330 loop : label WHILE '(' remember texpr ')' mintro mblock cont
332 PL_parser->copline = (line_t)$2;
334 newSTATEOP(0, PVAL($1),
335 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
336 IVAL($2), $5, $8, $9, $7)));
337 TOKEN_GETMAD($1,innerop,'L');
338 TOKEN_GETMAD($2,innerop,'W');
339 TOKEN_GETMAD($3,innerop,'(');
340 TOKEN_GETMAD($6,innerop,')');
343 | label UNTIL '(' remember iexpr ')' mintro mblock cont
345 PL_parser->copline = (line_t)$2;
347 newSTATEOP(0, PVAL($1),
348 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
349 IVAL($2), $5, $8, $9, $7)));
350 TOKEN_GETMAD($1,innerop,'L');
351 TOKEN_GETMAD($2,innerop,'W');
352 TOKEN_GETMAD($3,innerop,'(');
353 TOKEN_GETMAD($6,innerop,')');
355 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
358 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
360 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
361 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
362 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
363 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
364 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
366 | label FOR scalar '(' remember mexpr ')' mblock cont
369 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
370 mod($3, OP_ENTERLOOP), $6, $8, $9));
371 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
372 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
373 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
374 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
376 | label FOR '(' remember mexpr ')' mblock cont
379 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
380 (OP*)NULL, $5, $7, $8));
381 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
382 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
383 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
384 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
386 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
388 /* basically fake up an initialize-while lineseq */
390 PL_parser->copline = (line_t)IVAL($2);
391 forop = newSTATEOP(0, PVAL($1),
392 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
393 IVAL($2), scalar($7),
396 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
398 CopLABEL_alloc(($1)->tk_lval.pval),
399 ($5 ? $5 : newOP(OP_NULL, 0)) ),
402 token_getmad($2,forop,'3');
403 token_getmad($3,forop,'(');
404 token_getmad($6,forop,'1');
405 token_getmad($8,forop,'2');
406 token_getmad($11,forop,')');
407 token_getmad($1,forop,'L');
410 forop = append_elem(OP_LINESEQ,
411 newSTATEOP(0, CopLABEL_alloc($1), $5),
417 $$ = block_end($4, forop); }
418 | label block cont /* a block is a loop that happens once */
419 { $$ = newSTATEOP(0, PVAL($1),
420 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
421 NOLINE, (OP*)NULL, $2, $3, 0));
422 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
426 switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
427 { PL_parser->copline = (line_t) $2;
429 newSTATEOP(0, PVAL($1),
430 newGIVENOP($6, scope($8),
431 (PADOFFSET) $5) )); }
434 /* determine whether there are any new my declarations */
436 { $$ = (PL_min_intro_pending &&
437 PL_max_intro_pending >= PL_min_intro_pending);
440 /* Normal expression */
446 /* Boolean expression */
447 texpr : /* NULL means true */
449 (void)scan_num("1", &tmplval);
450 $$ = tmplval.opval; }
454 /* Inverted boolean expression */
456 { $$ = invert(scalar($1)); }
459 /* Expression with its own lexical scope */
461 { $$ = $1; intro_my(); }
465 { $$ = $1; intro_my(); }
469 { $$ = $1; intro_my(); }
472 /* Optional "MAIN:"-style loop labels */
478 $$ = newTOKEN(OP_NULL, tmplval, 0);
486 /* Some kind of declaration - just hang on peg in the parse tree */
498 /* these two are only used by MAD */
505 { $$ = newOP(OP_NULL,0);
506 TOKEN_GETMAD($1,$$,'p');
510 format : FORMAT startformsub formname block
511 { SvREFCNT_inc_simple_void(PL_compcv);
513 $$ = newFORM($2, $3, $4);
514 prepend_madprops($1->tk_mad, $$, 'F');
524 formname: WORD { $$ = $1; }
525 | /* NULL */ { $$ = (OP*)NULL; }
528 /* Unimplemented "my sub foo { }" */
529 mysubrout: MYSUB startsub subname proto subattrlist subbody
530 { SvREFCNT_inc_simple_void(PL_compcv);
532 $$ = newMYSUB($2, $3, $4, $5, $6);
533 token_getmad($1,$$,'d');
535 newMYSUB($2, $3, $4, $5, $6);
541 /* Subroutine definition */
542 subrout : SUB startsub subname proto subattrlist subbody
543 { SvREFCNT_inc_simple_void(PL_compcv);
546 OP* o = newSVOP(OP_ANONCODE, 0,
547 (SV*)newATTRSUB($2, $3, $4, $5, $6));
548 $$ = newOP(OP_NULL,0);
550 op_getmad($3,$$,'n');
551 op_getmad($4,$$,'s');
552 op_getmad($5,$$,'a');
553 token_getmad($1,$$,'d');
554 append_madprops($6->op_madprop, $$, 0);
558 newATTRSUB($2, $3, $4, $5, $6);
564 startsub: /* NULL */ /* start a regular subroutine scope */
565 { $$ = start_subparse(FALSE, 0);
566 SAVEFREESV(PL_compcv); }
570 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
571 { $$ = start_subparse(FALSE, CVf_ANON);
572 SAVEFREESV(PL_compcv); }
575 startformsub: /* NULL */ /* start a format subroutine scope */
576 { $$ = start_subparse(TRUE, 0);
577 SAVEFREESV(PL_compcv); }
580 /* Name of a subroutine - must be a bareword, could be special */
581 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
582 if (strEQ(name, "BEGIN") || strEQ(name, "END")
583 || strEQ(name, "INIT") || strEQ(name, "CHECK")
584 || strEQ(name, "UNITCHECK"))
585 CvSPECIAL_on(PL_compcv);
589 /* Subroutine prototype */
595 /* Optional list of subroutine attributes */
596 subattrlist: /* NULL */
600 TOKEN_GETMAD($1,$$,':');
607 TOKEN_GETMAD($1,$$,':');
611 /* List of attributes for a "my" variable declaration */
612 myattrlist: COLONATTR THING
614 TOKEN_GETMAD($1,$$,':');
621 TOKEN_GETMAD($1,$$,':');
625 /* Subroutine body - either null or a block */
626 subbody : block { $$ = $1; }
631 PL_parser->expect = XSTATE;
632 TOKEN_GETMAD($1,$$,';');
636 package : PACKAGE WORD ';'
640 token_getmad($1,$$,'o');
641 token_getmad($3,$$,';');
650 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
651 WORD WORD listexpr ';'
652 { SvREFCNT_inc_simple_void(PL_compcv);
654 $$ = utilize(IVAL($1), $2, $4, $5, $6);
655 token_getmad($1,$$,'o');
656 token_getmad($7,$$,';');
657 if (PL_parser->rsfp_filters &&
658 AvFILLp(PL_parser->rsfp_filters) >= 0)
659 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
661 utilize(IVAL($1), $2, $4, $5, $6);
667 /* Ordinary expressions; logical combinations */
668 expr : expr ANDOP expr
669 { $$ = newLOGOP(OP_AND, 0, $1, $3);
670 TOKEN_GETMAD($2,$$,'o');
673 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
674 TOKEN_GETMAD($2,$$,'o');
677 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
678 TOKEN_GETMAD($2,$$,'o');
680 | argexpr %prec PREC_LOW
683 /* Expressions are a list of terms joined by commas */
684 argexpr : argexpr ','
687 OP* op = newNULLLIST();
688 token_getmad($2,op,',');
689 $$ = append_elem(OP_LIST, $1, op);
698 term = newUNOP(OP_NULL, 0, term);
699 token_getmad($2,term,',');
701 $$ = append_elem(OP_LIST, $1, term);
703 | term %prec PREC_LOW
707 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
708 { $$ = convert(IVAL($1), OPf_STACKED,
709 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
710 TOKEN_GETMAD($1,$$,'o');
712 | FUNC '(' indirob expr ')' /* print ($fh @args */
713 { $$ = convert(IVAL($1), OPf_STACKED,
714 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
715 TOKEN_GETMAD($1,$$,'o');
716 TOKEN_GETMAD($2,$$,'(');
717 TOKEN_GETMAD($5,$$,')');
719 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
720 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
722 prepend_elem(OP_LIST, scalar($1), $5),
723 newUNOP(OP_METHOD, 0, $3)));
724 TOKEN_GETMAD($2,$$,'A');
725 TOKEN_GETMAD($4,$$,'(');
726 TOKEN_GETMAD($6,$$,')');
728 | term ARROW method /* $foo->bar */
729 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
730 append_elem(OP_LIST, scalar($1),
731 newUNOP(OP_METHOD, 0, $3)));
732 TOKEN_GETMAD($2,$$,'A');
734 | METHOD indirob listexpr /* new Class @args */
735 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
737 prepend_elem(OP_LIST, $2, $3),
738 newUNOP(OP_METHOD, 0, $1)));
740 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
741 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
743 prepend_elem(OP_LIST, $2, $4),
744 newUNOP(OP_METHOD, 0, $1)));
745 TOKEN_GETMAD($3,$$,'(');
746 TOKEN_GETMAD($5,$$,')');
748 | LSTOP listexpr /* print @args */
749 { $$ = convert(IVAL($1), 0, $2);
750 TOKEN_GETMAD($1,$$,'o');
752 | FUNC '(' listexprcom ')' /* print (@args) */
753 { $$ = convert(IVAL($1), 0, $3);
754 TOKEN_GETMAD($1,$$,'o');
755 TOKEN_GETMAD($2,$$,'(');
756 TOKEN_GETMAD($4,$$,')');
758 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
759 { SvREFCNT_inc_simple_void(PL_compcv);
760 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
761 listexpr %prec LSTOP /* ... @bar */
762 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
764 prepend_elem(OP_LIST, $<opval>4, $5), $1));
768 /* Names of methods. May use $object->$methodname */
773 /* Some kind of subscripted expression */
774 subscripted: star '{' expr ';' '}' /* *main::{something} */
775 /* In this and all the hash accessors, ';' is
776 * provided by the tokeniser */
777 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
778 PL_parser->expect = XOPERATOR;
779 TOKEN_GETMAD($2,$$,'{');
780 TOKEN_GETMAD($4,$$,';');
781 TOKEN_GETMAD($5,$$,'}');
783 | scalar '[' expr ']' /* $array[$element] */
784 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
785 TOKEN_GETMAD($2,$$,'[');
786 TOKEN_GETMAD($4,$$,']');
788 | term ARROW '[' expr ']' /* somearef->[$element] */
789 { $$ = newBINOP(OP_AELEM, 0,
790 ref(newAVREF($1),OP_RV2AV),
792 TOKEN_GETMAD($2,$$,'a');
793 TOKEN_GETMAD($3,$$,'[');
794 TOKEN_GETMAD($5,$$,']');
796 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
797 { $$ = newBINOP(OP_AELEM, 0,
798 ref(newAVREF($1),OP_RV2AV),
800 TOKEN_GETMAD($2,$$,'[');
801 TOKEN_GETMAD($4,$$,']');
803 | scalar '{' expr ';' '}' /* $foo->{bar();} */
804 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
805 PL_parser->expect = XOPERATOR;
806 TOKEN_GETMAD($2,$$,'{');
807 TOKEN_GETMAD($4,$$,';');
808 TOKEN_GETMAD($5,$$,'}');
810 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
811 { $$ = newBINOP(OP_HELEM, 0,
812 ref(newHVREF($1),OP_RV2HV),
814 PL_parser->expect = XOPERATOR;
815 TOKEN_GETMAD($2,$$,'a');
816 TOKEN_GETMAD($3,$$,'{');
817 TOKEN_GETMAD($5,$$,';');
818 TOKEN_GETMAD($6,$$,'}');
820 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
821 { $$ = newBINOP(OP_HELEM, 0,
822 ref(newHVREF($1),OP_RV2HV),
824 PL_parser->expect = XOPERATOR;
825 TOKEN_GETMAD($2,$$,'{');
826 TOKEN_GETMAD($4,$$,';');
827 TOKEN_GETMAD($5,$$,'}');
829 | term ARROW '(' ')' /* $subref->() */
830 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
831 newCVREF(0, scalar($1)));
832 TOKEN_GETMAD($2,$$,'a');
833 TOKEN_GETMAD($3,$$,'(');
834 TOKEN_GETMAD($4,$$,')');
836 | term ARROW '(' expr ')' /* $subref->(@args) */
837 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
838 append_elem(OP_LIST, $4,
839 newCVREF(0, scalar($1))));
840 TOKEN_GETMAD($2,$$,'a');
841 TOKEN_GETMAD($3,$$,'(');
842 TOKEN_GETMAD($5,$$,')');
845 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
846 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
847 append_elem(OP_LIST, $3,
848 newCVREF(0, scalar($1))));
849 TOKEN_GETMAD($2,$$,'(');
850 TOKEN_GETMAD($4,$$,')');
852 | subscripted '(' ')' /* $foo->{bar}->() */
853 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
854 newCVREF(0, scalar($1)));
855 TOKEN_GETMAD($2,$$,'(');
856 TOKEN_GETMAD($3,$$,')');
858 | '(' expr ')' '[' expr ']' /* list slice */
859 { $$ = newSLICEOP(0, $5, $2);
860 TOKEN_GETMAD($1,$$,'(');
861 TOKEN_GETMAD($3,$$,')');
862 TOKEN_GETMAD($4,$$,'[');
863 TOKEN_GETMAD($6,$$,']');
865 | '(' ')' '[' expr ']' /* empty list slice! */
866 { $$ = newSLICEOP(0, $4, (OP*)NULL);
867 TOKEN_GETMAD($1,$$,'(');
868 TOKEN_GETMAD($2,$$,')');
869 TOKEN_GETMAD($3,$$,'[');
870 TOKEN_GETMAD($5,$$,']');
874 /* Binary operators between terms */
875 termbinop: term ASSIGNOP term /* $x = $y */
876 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
877 TOKEN_GETMAD($2,$$,'o');
879 | term POWOP term /* $x ** $y */
880 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
881 TOKEN_GETMAD($2,$$,'o');
883 | term MULOP term /* $x * $y, $x x $y */
884 { if (IVAL($2) != OP_REPEAT)
886 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
887 TOKEN_GETMAD($2,$$,'o');
889 | term ADDOP term /* $x + $y */
890 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
891 TOKEN_GETMAD($2,$$,'o');
893 | term SHIFTOP term /* $x >> $y, $x << $y */
894 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
895 TOKEN_GETMAD($2,$$,'o');
897 | term RELOP term /* $x > $y, etc. */
898 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
899 TOKEN_GETMAD($2,$$,'o');
901 | term EQOP term /* $x == $y, $x eq $y */
902 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
903 TOKEN_GETMAD($2,$$,'o');
905 | term BITANDOP term /* $x & $y */
906 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
907 TOKEN_GETMAD($2,$$,'o');
909 | term BITOROP term /* $x | $y */
910 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
911 TOKEN_GETMAD($2,$$,'o');
913 | term DOTDOT term /* $x..$y, $x...$y */
915 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
919 op = (UNOP*)op->op_first; /* get to flop */
920 op = (UNOP*)op->op_first; /* get to flip */
921 op = (UNOP*)op->op_first; /* get to range */
922 token_getmad($2,(OP*)op,'o');
925 | term ANDAND term /* $x && $y */
926 { $$ = newLOGOP(OP_AND, 0, $1, $3);
927 TOKEN_GETMAD($2,$$,'o');
929 | term OROR term /* $x || $y */
930 { $$ = newLOGOP(OP_OR, 0, $1, $3);
931 TOKEN_GETMAD($2,$$,'o');
933 | term DORDOR term /* $x // $y */
934 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
935 TOKEN_GETMAD($2,$$,'o');
937 | term MATCHOP term /* $x =~ /$y/ */
938 { $$ = bind_match(IVAL($2), $1, $3);
940 ($$->op_type == OP_NOT
941 ? ((UNOP*)$$)->op_first : $$),
946 /* Unary operators and terms */
947 termunop : '-' term %prec UMINUS /* -$x */
948 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
949 TOKEN_GETMAD($1,$$,'o');
951 | '+' term %prec UMINUS /* +$x */
953 newUNOP(OP_NULL, 0, $2),
956 TOKEN_GETMAD($1,$$,'+');
959 { $$ = newUNOP(OP_NOT, 0, scalar($2));
960 TOKEN_GETMAD($1,$$,'o');
963 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
964 TOKEN_GETMAD($1,$$,'o');
966 | term POSTINC /* $x++ */
967 { $$ = newUNOP(OP_POSTINC, 0,
968 mod(scalar($1), OP_POSTINC));
969 TOKEN_GETMAD($2,$$,'o');
971 | term POSTDEC /* $x-- */
972 { $$ = newUNOP(OP_POSTDEC, 0,
973 mod(scalar($1), OP_POSTDEC));
974 TOKEN_GETMAD($2,$$,'o');
976 | PREINC term /* ++$x */
977 { $$ = newUNOP(OP_PREINC, 0,
978 mod(scalar($2), OP_PREINC));
979 TOKEN_GETMAD($1,$$,'o');
981 | PREDEC term /* --$x */
982 { $$ = newUNOP(OP_PREDEC, 0,
983 mod(scalar($2), OP_PREDEC));
984 TOKEN_GETMAD($1,$$,'o');
989 /* Constructors for anonymous data */
990 anonymous: '[' expr ']'
991 { $$ = newANONLIST($2);
992 TOKEN_GETMAD($1,$$,'[');
993 TOKEN_GETMAD($3,$$,']');
996 { $$ = newANONLIST((OP*)NULL);
997 TOKEN_GETMAD($1,$$,'[');
998 TOKEN_GETMAD($2,$$,']');
1000 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1001 { $$ = newANONHASH($2);
1002 TOKEN_GETMAD($1,$$,'{');
1003 TOKEN_GETMAD($3,$$,';');
1004 TOKEN_GETMAD($4,$$,'}');
1006 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1007 { $$ = newANONHASH((OP*)NULL);
1008 TOKEN_GETMAD($1,$$,'{');
1009 TOKEN_GETMAD($2,$$,';');
1010 TOKEN_GETMAD($3,$$,'}');
1012 | ANONSUB startanonsub proto subattrlist block %prec '('
1013 { SvREFCNT_inc_simple_void(PL_compcv);
1014 $$ = newANONATTRSUB($2, $3, $4, $5);
1015 TOKEN_GETMAD($1,$$,'o');
1016 OP_GETMAD($3,$$,'s');
1017 OP_GETMAD($4,$$,'a');
1022 /* Things called with "do" */
1023 termdo : DO term %prec UNIOP /* do $filename */
1024 { $$ = dofile($2, IVAL($1));
1025 TOKEN_GETMAD($1,$$,'o');
1027 | DO block %prec '(' /* do { code */
1028 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1029 TOKEN_GETMAD($1,$$,'D');
1031 | DO WORD '(' ')' /* do somesub() */
1032 { $$ = newUNOP(OP_ENTERSUB,
1033 OPf_SPECIAL|OPf_STACKED,
1034 prepend_elem(OP_LIST,
1036 (OPpENTERSUB_AMPER<<8),
1038 )),(OP*)NULL)); dep();
1039 TOKEN_GETMAD($1,$$,'o');
1040 TOKEN_GETMAD($3,$$,'(');
1041 TOKEN_GETMAD($4,$$,')');
1043 | DO WORD '(' expr ')' /* do somesub(@args) */
1044 { $$ = newUNOP(OP_ENTERSUB,
1045 OPf_SPECIAL|OPf_STACKED,
1046 append_elem(OP_LIST,
1049 (OPpENTERSUB_AMPER<<8),
1052 TOKEN_GETMAD($1,$$,'o');
1053 TOKEN_GETMAD($3,$$,'(');
1054 TOKEN_GETMAD($5,$$,')');
1056 | DO scalar '(' ')' /* do $subref () */
1057 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1058 prepend_elem(OP_LIST,
1059 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1060 TOKEN_GETMAD($1,$$,'o');
1061 TOKEN_GETMAD($3,$$,'(');
1062 TOKEN_GETMAD($4,$$,')');
1064 | DO scalar '(' expr ')' /* do $subref (@args) */
1065 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1066 prepend_elem(OP_LIST,
1068 scalar(newCVREF(0,scalar($2))))); dep();
1069 TOKEN_GETMAD($1,$$,'o');
1070 TOKEN_GETMAD($3,$$,'(');
1071 TOKEN_GETMAD($5,$$,')');
1080 | term '?' term ':' term
1081 { $$ = newCONDOP(0, $1, $3, $5);
1082 TOKEN_GETMAD($2,$$,'?');
1083 TOKEN_GETMAD($4,$$,':');
1085 | REFGEN term /* \$x, \@y, \%z */
1086 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1087 TOKEN_GETMAD($1,$$,'o');
1089 | myattrterm %prec UNIOP
1091 | LOCAL term %prec UNIOP
1092 { $$ = localize($2,IVAL($1));
1093 TOKEN_GETMAD($1,$$,'k');
1096 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1097 TOKEN_GETMAD($1,$$,'(');
1098 TOKEN_GETMAD($3,$$,')');
1101 { $$ = sawparens(newNULLLIST());
1102 TOKEN_GETMAD($1,$$,'(');
1103 TOKEN_GETMAD($2,$$,')');
1113 | arylen %prec '(' /* $#x, $#{ something } */
1114 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1117 | ary '[' expr ']' /* array slice */
1118 { $$ = prepend_elem(OP_ASLICE,
1119 newOP(OP_PUSHMARK, 0),
1120 newLISTOP(OP_ASLICE, 0,
1122 ref($1, OP_ASLICE)));
1123 TOKEN_GETMAD($2,$$,'[');
1124 TOKEN_GETMAD($4,$$,']');
1126 | ary '{' expr ';' '}' /* @hash{@keys} */
1127 { $$ = prepend_elem(OP_HSLICE,
1128 newOP(OP_PUSHMARK, 0),
1129 newLISTOP(OP_HSLICE, 0,
1131 ref(oopsHV($1), OP_HSLICE)));
1132 PL_parser->expect = XOPERATOR;
1133 TOKEN_GETMAD($2,$$,'{');
1134 TOKEN_GETMAD($4,$$,';');
1135 TOKEN_GETMAD($5,$$,'}');
1140 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1141 | amper '(' ')' /* &foo() */
1142 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1143 TOKEN_GETMAD($2,$$,'(');
1144 TOKEN_GETMAD($3,$$,')');
1146 | amper '(' expr ')' /* &foo(@args) */
1148 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1149 append_elem(OP_LIST, $3, scalar($1)));
1152 if (op->op_type == OP_CONST) { /* defeat const fold */
1153 op = (OP*)op->op_madprop->mad_val;
1155 token_getmad($2,op,'(');
1156 token_getmad($4,op,')');
1159 | NOAMP WORD listexpr /* foo(@args) */
1160 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1161 append_elem(OP_LIST, $3, scalar($2)));
1162 TOKEN_GETMAD($1,$$,'o');
1164 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1165 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1166 PL_hints |= HINT_BLOCK_SCOPE;
1167 TOKEN_GETMAD($1,$$,'o');
1170 { $$ = newLOOPEX(IVAL($1),$2);
1171 TOKEN_GETMAD($1,$$,'o');
1173 | NOTOP argexpr /* not $foo */
1174 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1175 TOKEN_GETMAD($1,$$,'o');
1177 | UNIOP /* Unary op, $_ implied */
1178 { $$ = newOP(IVAL($1), 0);
1179 TOKEN_GETMAD($1,$$,'o');
1181 | UNIOP block /* eval { foo }* */
1182 { $$ = newUNOP(IVAL($1), 0, $2);
1183 TOKEN_GETMAD($1,$$,'o');
1185 | UNIOP term /* Unary op */
1186 { $$ = newUNOP(IVAL($1), 0, $2);
1187 TOKEN_GETMAD($1,$$,'o');
1189 | REQUIRE /* require, $_ implied */
1190 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1191 TOKEN_GETMAD($1,$$,'o');
1193 | REQUIRE term /* require Foo */
1194 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1195 TOKEN_GETMAD($1,$$,'o');
1198 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1199 | UNIOPSUB term /* Sub treated as unop */
1200 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1201 append_elem(OP_LIST, $2, scalar($1))); }
1202 | FUNC0 /* Nullary operator */
1203 { $$ = newOP(IVAL($1), 0);
1204 TOKEN_GETMAD($1,$$,'o');
1207 { $$ = newOP(IVAL($1), 0);
1208 TOKEN_GETMAD($1,$$,'o');
1209 TOKEN_GETMAD($2,$$,'(');
1210 TOKEN_GETMAD($3,$$,')');
1212 | FUNC0SUB /* Sub treated as nullop */
1213 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1215 | FUNC1 '(' ')' /* not () */
1216 { $$ = (IVAL($1) == OP_NOT)
1217 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1218 : newOP(IVAL($1), OPf_SPECIAL);
1220 TOKEN_GETMAD($1,$$,'o');
1221 TOKEN_GETMAD($2,$$,'(');
1222 TOKEN_GETMAD($3,$$,')');
1224 | FUNC1 '(' expr ')' /* not($foo) */
1225 { $$ = newUNOP(IVAL($1), 0, $3);
1226 TOKEN_GETMAD($1,$$,'o');
1227 TOKEN_GETMAD($2,$$,'(');
1228 TOKEN_GETMAD($4,$$,')');
1230 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1231 { $$ = pmruntime($1, $3, 1);
1232 TOKEN_GETMAD($2,$$,'(');
1233 TOKEN_GETMAD($4,$$,')');
1239 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1240 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1241 TOKEN_GETMAD($1,$$,'X');
1245 /* "my" declarations, with optional attributes */
1246 myattrterm: MY myterm myattrlist
1247 { $$ = my_attrs($2,$3);
1249 token_getmad($1,$$,'d');
1250 append_madprops($3->op_madprop, $$, 'a');
1255 { $$ = localize($2,IVAL($1));
1256 TOKEN_GETMAD($1,$$,'d');
1260 /* Things that can be "my"'d */
1261 myterm : '(' expr ')'
1262 { $$ = sawparens($2);
1263 TOKEN_GETMAD($1,$$,'(');
1264 TOKEN_GETMAD($3,$$,')');
1267 { $$ = sawparens(newNULLLIST());
1268 TOKEN_GETMAD($1,$$,'(');
1269 TOKEN_GETMAD($2,$$,')');
1279 /* Basic list expressions */
1280 listexpr: /* NULL */ %prec PREC_LOW
1282 | argexpr %prec PREC_LOW
1286 listexprcom: /* NULL */
1293 OP* op = newNULLLIST();
1294 token_getmad($2,op,',');
1295 $$ = append_elem(OP_LIST, $1, op);
1303 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1306 { PL_parser->in_my = 0; $$ = my($1); }
1310 { $$ = newCVREF(IVAL($1),$2);
1311 TOKEN_GETMAD($1,$$,'&');
1315 scalar : '$' indirob
1316 { $$ = newSVREF($2);
1317 TOKEN_GETMAD($1,$$,'$');
1322 { $$ = newAVREF($2);
1323 TOKEN_GETMAD($1,$$,'@');
1328 { $$ = newHVREF($2);
1329 TOKEN_GETMAD($1,$$,'%');
1333 arylen : DOLSHARP indirob
1334 { $$ = newAVREF($2);
1335 TOKEN_GETMAD($1,$$,'l');
1340 { $$ = newGVREF(0,$2);
1341 TOKEN_GETMAD($1,$$,'*');
1345 /* Indirect objects */
1347 { $$ = scalar($1); }
1348 | scalar %prec PREC_LOW
1349 { $$ = scalar($1); }