3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
12 * All that is gold does not glitter, not all those who wander are lost.'
14 * This file holds the grammar for the Perl language. If edited, you need
15 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
16 * and perly.act which are derived from this.
18 * Note that these derived files are included and compiled twice; once
19 * from perly.c, and once from madly.c. The second time, a number of MAD
20 * macros are defined, which compile in extra code that allows the parse
21 * tree to be accurately dumped. In particular:
23 * MAD defined if compiling madly.c
24 * DO_MAD(A) expands to A under madly.c, to null otherwise
25 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
26 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
27 * TOKEN_FREE() similarly
28 * OP_GETMAD() similarly
29 * IVAL(i) expands to (i)->tk_lval.ival or (i)
30 * PVAL(p) expands to (p)->tk_lval.pval or (p)
32 * The main job of of this grammar is to call the various newFOO()
33 * functions in op.c to build a syntax tree of OP structs.
34 * It relies on the lexer in toke.c to do the tokenizing.
36 * Note: due to the way that the cleanup code works WRT to freeing ops on
37 * the parse stack, it is dangerous to assign to the $n variables within
41 /* Make the parser re-entrant. */
45 /* FIXME for MAD - is the new mintro on while and until important? */
50 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
51 must always be 1st union member) */
55 #ifdef PERL_IN_MADLY_C
67 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
69 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
70 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
71 %token <p_tkval> LABEL
72 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
73 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
74 %token <i_tkval> GIVEN WHEN DEFAULT
75 %token <i_tkval> LOOPEX DOTDOT
76 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
77 %token <i_tkval> RELOP EQOP MULOP ADDOP
78 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
79 %token <i_tkval> LOCAL MY MYSUB REQUIRE
80 %token <i_tkval> COLONATTR
82 %type <ival> prog progstart remember mremember
83 %type <ival> startsub startanonsub startformsub
84 /* FIXME for MAD - are these two ival? */
85 %type <ival> mydefsv mintro
87 %type <opval> decl format subrout mysubrout package use peg
89 %type <opval> block mblock lineseq line loop cond else
90 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
91 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
92 %type <opval> listexpr listexprcom indirob listop method
93 %type <opval> formname subname proto subbody cont my_scalar
94 %type <opval> subattrlist myattrlist myattrterm myterm
95 %type <opval> termbinop termunop anonymous termdo
96 %type <opval> switch case
99 %nonassoc <i_tkval> PREC_LOW
102 %left <i_tkval> OROP DOROP
103 %left <i_tkval> ANDOP
104 %right <i_tkval> NOTOP
105 %nonassoc LSTOP LSTOPSUB
107 %right <i_tkval> ASSIGNOP
108 %right <i_tkval> '?' ':'
110 %left <i_tkval> OROR DORDOR
111 %left <i_tkval> ANDAND
112 %left <i_tkval> BITOROP
113 %left <i_tkval> BITANDOP
116 %nonassoc UNIOP UNIOPSUB
118 %left <i_tkval> SHIFTOP
121 %left <i_tkval> MATCHOP
122 %right <i_tkval> '!' '~' UMINUS REFGEN
123 %right <i_tkval> POWOP
124 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
125 %left <i_tkval> ARROW
126 %nonassoc <i_tkval> ')'
134 /* The whole program */
136 /*CONTINUED*/ lineseq
137 { $$ = $1; newPROG(block_end($1,$2)); }
140 /* An ordinary block */
141 block : '{' remember lineseq '}'
142 { if (PL_parser->copline > (line_t)IVAL($1))
143 PL_parser->copline = (line_t)IVAL($1);
144 $$ = block_end($2, $3);
145 TOKEN_GETMAD($1,$$,'{');
146 TOKEN_GETMAD($4,$$,'}');
150 remember: /* NULL */ /* start a full lexical scope */
151 { $$ = block_start(TRUE); }
154 mydefsv: /* NULL */ /* lexicalize $_ */
155 { $$ = (I32) allocmy("$_"); }
160 PL_parser->expect = XSTATE; $$ = block_start(TRUE);
165 mblock : '{' mremember lineseq '}'
166 { if (PL_parser->copline > (line_t)IVAL($1))
167 PL_parser->copline = (line_t)IVAL($1);
168 $$ = block_end($2, $3);
169 TOKEN_GETMAD($1,$$,'{');
170 TOKEN_GETMAD($4,$$,'}');
174 mremember: /* NULL */ /* start a partial lexical scope */
175 { $$ = block_start(FALSE); }
178 /* A collection of "lines" in the program */
184 append_list(OP_LINESEQ,
185 (LISTOP*)$1, (LISTOP*)$2),
189 { $$ = append_list(OP_LINESEQ,
190 (LISTOP*)$1, (LISTOP*)$2);
191 PL_pad_reset_pending = TRUE;
193 PL_hints |= HINT_BLOCK_SCOPE;
197 /* A "line" in the program */
199 { $$ = newSTATEOP(0, PVAL($1), $2);
200 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
201 | loop /* loops add their own labels */
202 | switch /* ... and so do switches */
205 { $$ = newSTATEOP(0, PVAL($1), $2); }
209 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
210 TOKEN_GETMAD($1,$$,'L');
211 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
217 PL_parser->copline = NOLINE;
219 TOKEN_GETMAD($2,$$,';');
221 PL_parser->expect = XSTATE;
225 $$ = newSTATEOP(0, PVAL($1), $2);
226 PL_parser->expect = XSTATE;
228 /* sideff might already have a nexstate */
229 OP* op = ((LISTOP*)$$)->op_first;
231 while (op->op_sibling &&
232 op->op_sibling->op_type == OP_NEXTSTATE)
234 token_getmad($1,op,'L');
235 token_getmad($3,op,';');
241 /* An expression which may have a side-effect */
247 { $$ = newLOGOP(OP_AND, 0, $3, $1);
248 TOKEN_GETMAD($2,$$,'i');
251 { $$ = newLOGOP(OP_OR, 0, $3, $1);
252 TOKEN_GETMAD($2,$$,'i');
255 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
256 TOKEN_GETMAD($2,$$,'w');
259 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
260 TOKEN_GETMAD($2,$$,'w');
263 { $$ = newFOROP(0, Nullch, (line_t)IVAL($2),
264 Nullop, $3, $1, Nullop);
265 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
269 /* else and elsif blocks */
273 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
274 TOKEN_GETMAD($1,$$,'o');
276 | ELSIF '(' mexpr ')' mblock else
277 { PL_parser->copline = (line_t)IVAL($1);
278 $$ = newCONDOP(0, $3, scope($5), $6);
279 PL_hints |= HINT_BLOCK_SCOPE;
280 TOKEN_GETMAD($1,$$,'I');
281 TOKEN_GETMAD($2,$$,'(');
282 TOKEN_GETMAD($4,$$,')');
286 /* Real conditional expressions */
287 cond : IF '(' remember mexpr ')' mblock else
288 { PL_parser->copline = (line_t)IVAL($1);
290 newCONDOP(0, $4, scope($6), $7));
291 TOKEN_GETMAD($1,$$,'I');
292 TOKEN_GETMAD($2,$$,'(');
293 TOKEN_GETMAD($5,$$,')');
295 | UNLESS '(' remember miexpr ')' mblock else
296 { PL_parser->copline = (line_t)IVAL($1);
298 newCONDOP(0, $4, scope($6), $7));
299 TOKEN_GETMAD($1,$$,'I');
300 TOKEN_GETMAD($2,$$,'(');
301 TOKEN_GETMAD($5,$$,')');
305 /* Cases for a switch statement */
306 case : WHEN '(' remember mexpr ')' mblock
308 newWHENOP($4, scope($6))); }
310 { $$ = newWHENOP(0, scope($2)); }
313 /* Continue blocks */
318 TOKEN_GETMAD($1,$$,'o');
322 /* Loops: while, until, for, and a bare block */
323 loop : label WHILE '(' remember texpr ')' mintro mblock cont
325 PL_parser->copline = (line_t)$2;
327 newSTATEOP(0, PVAL($1),
328 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
329 IVAL($2), $5, $8, $9, $7)));
330 TOKEN_GETMAD($1,innerop,'L');
331 TOKEN_GETMAD($2,innerop,'W');
332 TOKEN_GETMAD($3,innerop,'(');
333 TOKEN_GETMAD($6,innerop,')');
336 | label UNTIL '(' remember iexpr ')' mintro mblock cont
338 PL_parser->copline = (line_t)$2;
340 newSTATEOP(0, PVAL($1),
341 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
342 IVAL($2), $5, $8, $9, $7)));
343 TOKEN_GETMAD($1,innerop,'L');
344 TOKEN_GETMAD($2,innerop,'W');
345 TOKEN_GETMAD($3,innerop,'(');
346 TOKEN_GETMAD($6,innerop,')');
348 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
351 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
353 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
354 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
355 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
356 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
357 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
359 | label FOR scalar '(' remember mexpr ')' mblock cont
362 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
363 mod($3, OP_ENTERLOOP), $6, $8, $9));
364 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
365 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
366 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
367 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
369 | label FOR '(' remember mexpr ')' mblock cont
372 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
373 Nullop, $5, $7, $8));
374 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
375 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
376 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
377 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
379 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
381 /* basically fake up an initialize-while lineseq */
383 PL_parser->copline = (line_t)IVAL($2);
384 forop = newSTATEOP(0, PVAL($1),
385 newWHILEOP(0, 1, (LOOP*)Nullop,
386 IVAL($2), scalar($7),
389 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
391 CopLABEL_alloc(($1)->tk_lval.pval),
392 ($5 ? $5 : newOP(OP_NULL, 0)) ),
395 token_getmad($2,forop,'3');
396 token_getmad($3,forop,'(');
397 token_getmad($6,forop,'1');
398 token_getmad($8,forop,'2');
399 token_getmad($11,forop,')');
400 token_getmad($1,forop,'L');
403 forop = append_elem(OP_LINESEQ,
404 newSTATEOP(0, CopLABEL_alloc($1), $5),
410 $$ = block_end($4, forop); }
411 | label block cont /* a block is a loop that happens once */
412 { $$ = newSTATEOP(0, PVAL($1),
413 newWHILEOP(0, 1, (LOOP*)Nullop,
414 NOLINE, Nullop, $2, $3, 0));
415 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
419 switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
420 { PL_parser->copline = (line_t) $2;
422 newSTATEOP(0, PVAL($1),
423 newGIVENOP($6, scope($8),
424 (PADOFFSET) $5) )); }
427 /* determine whether there are any new my declarations */
429 { $$ = (PL_min_intro_pending &&
430 PL_max_intro_pending >= PL_min_intro_pending);
433 /* Normal expression */
439 /* Boolean expression */
440 texpr : /* NULL means true */
442 (void)scan_num("1", &tmplval);
443 $$ = tmplval.opval; }
447 /* Inverted boolean expression */
449 { $$ = invert(scalar($1)); }
452 /* Expression with its own lexical scope */
454 { $$ = $1; intro_my(); }
458 { $$ = $1; intro_my(); }
462 { $$ = $1; intro_my(); }
465 /* Optional "MAIN:"-style loop labels */
470 tmplval.pval = Nullch;
471 $$ = newTOKEN(OP_NULL, tmplval, 0);
479 /* Some kind of declaration - just hang on peg in the parse tree */
491 /* these two are only used by MAD */
498 { $$ = newOP(OP_NULL,0);
499 TOKEN_GETMAD($1,$$,'p');
503 format : FORMAT startformsub formname block
504 { SvREFCNT_inc_simple_void(PL_compcv);
506 $$ = newFORM($2, $3, $4);
507 prepend_madprops($1->tk_mad, $$, 'F');
517 formname: WORD { $$ = $1; }
518 | /* NULL */ { $$ = Nullop; }
521 /* Unimplemented "my sub foo { }" */
522 mysubrout: MYSUB startsub subname proto subattrlist subbody
523 { SvREFCNT_inc_simple_void(PL_compcv);
525 $$ = newMYSUB($2, $3, $4, $5, $6);
526 token_getmad($1,$$,'d');
528 newMYSUB($2, $3, $4, $5, $6);
534 /* Subroutine definition */
535 subrout : SUB startsub subname proto subattrlist subbody
536 { SvREFCNT_inc_simple_void(PL_compcv);
539 OP* o = newSVOP(OP_ANONCODE, 0,
540 (SV*)newATTRSUB($2, $3, $4, $5, $6));
541 $$ = newOP(OP_NULL,0);
543 op_getmad($3,$$,'n');
544 op_getmad($4,$$,'s');
545 op_getmad($5,$$,'a');
546 token_getmad($1,$$,'d');
547 append_madprops($6->op_madprop, $$, 0);
551 newATTRSUB($2, $3, $4, $5, $6);
557 startsub: /* NULL */ /* start a regular subroutine scope */
558 { $$ = start_subparse(FALSE, 0);
559 SAVEFREESV(PL_compcv); }
563 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
564 { $$ = start_subparse(FALSE, CVf_ANON);
565 SAVEFREESV(PL_compcv); }
568 startformsub: /* NULL */ /* start a format subroutine scope */
569 { $$ = start_subparse(TRUE, 0);
570 SAVEFREESV(PL_compcv); }
573 /* Name of a subroutine - must be a bareword, could be special */
574 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
575 if (strEQ(name, "BEGIN") || strEQ(name, "END")
576 || strEQ(name, "INIT") || strEQ(name, "CHECK")
577 || strEQ(name, "UNITCHECK"))
578 CvSPECIAL_on(PL_compcv);
582 /* Subroutine prototype */
588 /* Optional list of subroutine attributes */
589 subattrlist: /* NULL */
593 TOKEN_GETMAD($1,$$,':');
600 TOKEN_GETMAD($1,$$,':');
604 /* List of attributes for a "my" variable declaration */
605 myattrlist: COLONATTR THING
607 TOKEN_GETMAD($1,$$,':');
614 TOKEN_GETMAD($1,$$,':');
618 /* Subroutine body - either null or a block */
619 subbody : block { $$ = $1; }
624 PL_parser->expect = XSTATE;
625 TOKEN_GETMAD($1,$$,';');
629 package : PACKAGE WORD ';'
633 token_getmad($1,$$,'o');
634 token_getmad($3,$$,';');
643 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
644 WORD WORD listexpr ';'
645 { SvREFCNT_inc_simple_void(PL_compcv);
647 $$ = utilize(IVAL($1), $2, $4, $5, $6);
648 token_getmad($1,$$,'o');
649 token_getmad($7,$$,';');
650 if (PL_parser->rsfp_filters &&
651 AvFILLp(PL_parser->rsfp_filters) >= 0)
652 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
654 utilize(IVAL($1), $2, $4, $5, $6);
660 /* Ordinary expressions; logical combinations */
661 expr : expr ANDOP expr
662 { $$ = newLOGOP(OP_AND, 0, $1, $3);
663 TOKEN_GETMAD($2,$$,'o');
666 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
667 TOKEN_GETMAD($2,$$,'o');
670 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
671 TOKEN_GETMAD($2,$$,'o');
673 | argexpr %prec PREC_LOW
676 /* Expressions are a list of terms joined by commas */
677 argexpr : argexpr ','
680 OP* op = newNULLLIST();
681 token_getmad($2,op,',');
682 $$ = append_elem(OP_LIST, $1, op);
691 term = newUNOP(OP_NULL, 0, term);
692 token_getmad($2,term,',');
694 $$ = append_elem(OP_LIST, $1, term);
696 | term %prec PREC_LOW
700 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
701 { $$ = convert(IVAL($1), OPf_STACKED,
702 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
703 TOKEN_GETMAD($1,$$,'o');
705 | FUNC '(' indirob expr ')' /* print ($fh @args */
706 { $$ = convert(IVAL($1), OPf_STACKED,
707 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
708 TOKEN_GETMAD($1,$$,'o');
709 TOKEN_GETMAD($2,$$,'(');
710 TOKEN_GETMAD($5,$$,')');
712 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
713 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
715 prepend_elem(OP_LIST, scalar($1), $5),
716 newUNOP(OP_METHOD, 0, $3)));
717 TOKEN_GETMAD($2,$$,'A');
718 TOKEN_GETMAD($4,$$,'(');
719 TOKEN_GETMAD($6,$$,')');
721 | term ARROW method /* $foo->bar */
722 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
723 append_elem(OP_LIST, scalar($1),
724 newUNOP(OP_METHOD, 0, $3)));
725 TOKEN_GETMAD($2,$$,'A');
727 | METHOD indirob listexpr /* new Class @args */
728 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
730 prepend_elem(OP_LIST, $2, $3),
731 newUNOP(OP_METHOD, 0, $1)));
733 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
734 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
736 prepend_elem(OP_LIST, $2, $4),
737 newUNOP(OP_METHOD, 0, $1)));
738 TOKEN_GETMAD($3,$$,'(');
739 TOKEN_GETMAD($5,$$,')');
741 | LSTOP listexpr /* print @args */
742 { $$ = convert(IVAL($1), 0, $2);
743 TOKEN_GETMAD($1,$$,'o');
745 | FUNC '(' listexprcom ')' /* print (@args) */
746 { $$ = convert(IVAL($1), 0, $3);
747 TOKEN_GETMAD($1,$$,'o');
748 TOKEN_GETMAD($2,$$,'(');
749 TOKEN_GETMAD($4,$$,')');
751 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
752 { SvREFCNT_inc_simple_void(PL_compcv);
753 $<opval>$ = newANONATTRSUB($2, 0, Nullop, $3); }
754 listexpr %prec LSTOP /* ... @bar */
755 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
757 prepend_elem(OP_LIST, $<opval>4, $5), $1));
761 /* Names of methods. May use $object->$methodname */
766 /* Some kind of subscripted expression */
767 subscripted: star '{' expr ';' '}' /* *main::{something} */
768 /* In this and all the hash accessors, ';' is
769 * provided by the tokeniser */
770 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
771 PL_parser->expect = XOPERATOR;
772 TOKEN_GETMAD($2,$$,'{');
773 TOKEN_GETMAD($4,$$,';');
774 TOKEN_GETMAD($5,$$,'}');
776 | scalar '[' expr ']' /* $array[$element] */
777 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
778 TOKEN_GETMAD($2,$$,'[');
779 TOKEN_GETMAD($4,$$,']');
781 | term ARROW '[' expr ']' /* somearef->[$element] */
782 { $$ = newBINOP(OP_AELEM, 0,
783 ref(newAVREF($1),OP_RV2AV),
785 TOKEN_GETMAD($2,$$,'a');
786 TOKEN_GETMAD($3,$$,'[');
787 TOKEN_GETMAD($5,$$,']');
789 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
790 { $$ = newBINOP(OP_AELEM, 0,
791 ref(newAVREF($1),OP_RV2AV),
793 TOKEN_GETMAD($2,$$,'[');
794 TOKEN_GETMAD($4,$$,']');
796 | scalar '{' expr ';' '}' /* $foo->{bar();} */
797 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
798 PL_parser->expect = XOPERATOR;
799 TOKEN_GETMAD($2,$$,'{');
800 TOKEN_GETMAD($4,$$,';');
801 TOKEN_GETMAD($5,$$,'}');
803 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
804 { $$ = newBINOP(OP_HELEM, 0,
805 ref(newHVREF($1),OP_RV2HV),
807 PL_parser->expect = XOPERATOR;
808 TOKEN_GETMAD($2,$$,'a');
809 TOKEN_GETMAD($3,$$,'{');
810 TOKEN_GETMAD($5,$$,';');
811 TOKEN_GETMAD($6,$$,'}');
813 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
814 { $$ = newBINOP(OP_HELEM, 0,
815 ref(newHVREF($1),OP_RV2HV),
817 PL_parser->expect = XOPERATOR;
818 TOKEN_GETMAD($2,$$,'{');
819 TOKEN_GETMAD($4,$$,';');
820 TOKEN_GETMAD($5,$$,'}');
822 | term ARROW '(' ')' /* $subref->() */
823 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
824 newCVREF(0, scalar($1)));
825 TOKEN_GETMAD($2,$$,'a');
826 TOKEN_GETMAD($3,$$,'(');
827 TOKEN_GETMAD($4,$$,')');
829 | term ARROW '(' expr ')' /* $subref->(@args) */
830 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
831 append_elem(OP_LIST, $4,
832 newCVREF(0, scalar($1))));
833 TOKEN_GETMAD($2,$$,'a');
834 TOKEN_GETMAD($3,$$,'(');
835 TOKEN_GETMAD($5,$$,')');
838 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
839 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
840 append_elem(OP_LIST, $3,
841 newCVREF(0, scalar($1))));
842 TOKEN_GETMAD($2,$$,'(');
843 TOKEN_GETMAD($4,$$,')');
845 | subscripted '(' ')' /* $foo->{bar}->() */
846 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
847 newCVREF(0, scalar($1)));
848 TOKEN_GETMAD($2,$$,'(');
849 TOKEN_GETMAD($3,$$,')');
851 | '(' expr ')' '[' expr ']' /* list slice */
852 { $$ = newSLICEOP(0, $5, $2);
853 TOKEN_GETMAD($1,$$,'(');
854 TOKEN_GETMAD($3,$$,')');
855 TOKEN_GETMAD($4,$$,'[');
856 TOKEN_GETMAD($6,$$,']');
858 | '(' ')' '[' expr ']' /* empty list slice! */
859 { $$ = newSLICEOP(0, $4, Nullop);
860 TOKEN_GETMAD($1,$$,'(');
861 TOKEN_GETMAD($2,$$,')');
862 TOKEN_GETMAD($3,$$,'[');
863 TOKEN_GETMAD($5,$$,']');
867 /* Binary operators between terms */
868 termbinop: term ASSIGNOP term /* $x = $y */
869 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
870 TOKEN_GETMAD($2,$$,'o');
872 | term POWOP term /* $x ** $y */
873 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
874 TOKEN_GETMAD($2,$$,'o');
876 | term MULOP term /* $x * $y, $x x $y */
877 { if (IVAL($2) != OP_REPEAT)
879 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
880 TOKEN_GETMAD($2,$$,'o');
882 | term ADDOP term /* $x + $y */
883 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
884 TOKEN_GETMAD($2,$$,'o');
886 | term SHIFTOP term /* $x >> $y, $x << $y */
887 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
888 TOKEN_GETMAD($2,$$,'o');
890 | term RELOP term /* $x > $y, etc. */
891 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
892 TOKEN_GETMAD($2,$$,'o');
894 | term EQOP term /* $x == $y, $x eq $y */
895 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
896 TOKEN_GETMAD($2,$$,'o');
898 | term BITANDOP term /* $x & $y */
899 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
900 TOKEN_GETMAD($2,$$,'o');
902 | term BITOROP term /* $x | $y */
903 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
904 TOKEN_GETMAD($2,$$,'o');
906 | term DOTDOT term /* $x..$y, $x...$y */
908 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
912 op = (UNOP*)op->op_first; /* get to flop */
913 op = (UNOP*)op->op_first; /* get to flip */
914 op = (UNOP*)op->op_first; /* get to range */
915 token_getmad($2,(OP*)op,'o');
918 | term ANDAND term /* $x && $y */
919 { $$ = newLOGOP(OP_AND, 0, $1, $3);
920 TOKEN_GETMAD($2,$$,'o');
922 | term OROR term /* $x || $y */
923 { $$ = newLOGOP(OP_OR, 0, $1, $3);
924 TOKEN_GETMAD($2,$$,'o');
926 | term DORDOR term /* $x // $y */
927 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
928 TOKEN_GETMAD($2,$$,'o');
930 | term MATCHOP term /* $x =~ /$y/ */
931 { $$ = bind_match(IVAL($2), $1, $3);
933 ($$->op_type == OP_NOT
934 ? ((UNOP*)$$)->op_first : $$),
939 /* Unary operators and terms */
940 termunop : '-' term %prec UMINUS /* -$x */
941 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
942 TOKEN_GETMAD($1,$$,'o');
944 | '+' term %prec UMINUS /* +$x */
946 newUNOP(OP_NULL, 0, $2),
949 TOKEN_GETMAD($1,$$,'+');
952 { $$ = newUNOP(OP_NOT, 0, scalar($2));
953 TOKEN_GETMAD($1,$$,'o');
956 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
957 TOKEN_GETMAD($1,$$,'o');
959 | term POSTINC /* $x++ */
960 { $$ = newUNOP(OP_POSTINC, 0,
961 mod(scalar($1), OP_POSTINC));
962 TOKEN_GETMAD($2,$$,'o');
964 | term POSTDEC /* $x-- */
965 { $$ = newUNOP(OP_POSTDEC, 0,
966 mod(scalar($1), OP_POSTDEC));
967 TOKEN_GETMAD($2,$$,'o');
969 | PREINC term /* ++$x */
970 { $$ = newUNOP(OP_PREINC, 0,
971 mod(scalar($2), OP_PREINC));
972 TOKEN_GETMAD($1,$$,'o');
974 | PREDEC term /* --$x */
975 { $$ = newUNOP(OP_PREDEC, 0,
976 mod(scalar($2), OP_PREDEC));
977 TOKEN_GETMAD($1,$$,'o');
982 /* Constructors for anonymous data */
983 anonymous: '[' expr ']'
984 { $$ = newANONLIST($2);
985 TOKEN_GETMAD($1,$$,'[');
986 TOKEN_GETMAD($3,$$,']');
989 { $$ = newANONLIST(Nullop);
990 TOKEN_GETMAD($1,$$,'[');
991 TOKEN_GETMAD($2,$$,']');
993 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
994 { $$ = newANONHASH($2);
995 TOKEN_GETMAD($1,$$,'{');
996 TOKEN_GETMAD($3,$$,';');
997 TOKEN_GETMAD($4,$$,'}');
999 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1000 { $$ = newANONHASH(Nullop);
1001 TOKEN_GETMAD($1,$$,'{');
1002 TOKEN_GETMAD($2,$$,';');
1003 TOKEN_GETMAD($3,$$,'}');
1005 | ANONSUB startanonsub proto subattrlist block %prec '('
1006 { SvREFCNT_inc_simple_void(PL_compcv);
1007 $$ = newANONATTRSUB($2, $3, $4, $5);
1008 TOKEN_GETMAD($1,$$,'o');
1009 OP_GETMAD($3,$$,'s');
1010 OP_GETMAD($4,$$,'a');
1015 /* Things called with "do" */
1016 termdo : DO term %prec UNIOP /* do $filename */
1017 { $$ = dofile($2, IVAL($1));
1018 TOKEN_GETMAD($1,$$,'o');
1020 | DO block %prec '(' /* do { code */
1021 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1022 TOKEN_GETMAD($1,$$,'D');
1024 | DO WORD '(' ')' /* do somesub() */
1025 { $$ = newUNOP(OP_ENTERSUB,
1026 OPf_SPECIAL|OPf_STACKED,
1027 prepend_elem(OP_LIST,
1029 (OPpENTERSUB_AMPER<<8),
1032 TOKEN_GETMAD($1,$$,'o');
1033 TOKEN_GETMAD($3,$$,'(');
1034 TOKEN_GETMAD($4,$$,')');
1036 | DO WORD '(' expr ')' /* do somesub(@args) */
1037 { $$ = newUNOP(OP_ENTERSUB,
1038 OPf_SPECIAL|OPf_STACKED,
1039 append_elem(OP_LIST,
1042 (OPpENTERSUB_AMPER<<8),
1045 TOKEN_GETMAD($1,$$,'o');
1046 TOKEN_GETMAD($3,$$,'(');
1047 TOKEN_GETMAD($5,$$,')');
1049 | DO scalar '(' ')' /* do $subref () */
1050 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1051 prepend_elem(OP_LIST,
1052 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
1053 TOKEN_GETMAD($1,$$,'o');
1054 TOKEN_GETMAD($3,$$,'(');
1055 TOKEN_GETMAD($4,$$,')');
1057 | DO scalar '(' expr ')' /* do $subref (@args) */
1058 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1059 prepend_elem(OP_LIST,
1061 scalar(newCVREF(0,scalar($2))))); dep();
1062 TOKEN_GETMAD($1,$$,'o');
1063 TOKEN_GETMAD($3,$$,'(');
1064 TOKEN_GETMAD($5,$$,')');
1073 | term '?' term ':' term
1074 { $$ = newCONDOP(0, $1, $3, $5);
1075 TOKEN_GETMAD($2,$$,'?');
1076 TOKEN_GETMAD($4,$$,':');
1078 | REFGEN term /* \$x, \@y, \%z */
1079 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1080 TOKEN_GETMAD($1,$$,'o');
1082 | myattrterm %prec UNIOP
1084 | LOCAL term %prec UNIOP
1085 { $$ = localize($2,IVAL($1));
1086 TOKEN_GETMAD($1,$$,'k');
1089 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1090 TOKEN_GETMAD($1,$$,'(');
1091 TOKEN_GETMAD($3,$$,')');
1094 { $$ = sawparens(newNULLLIST());
1095 TOKEN_GETMAD($1,$$,'(');
1096 TOKEN_GETMAD($2,$$,')');
1106 | arylen %prec '(' /* $#x, $#{ something } */
1107 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1110 | ary '[' expr ']' /* array slice */
1111 { $$ = prepend_elem(OP_ASLICE,
1112 newOP(OP_PUSHMARK, 0),
1113 newLISTOP(OP_ASLICE, 0,
1115 ref($1, OP_ASLICE)));
1116 TOKEN_GETMAD($2,$$,'[');
1117 TOKEN_GETMAD($4,$$,']');
1119 | ary '{' expr ';' '}' /* @hash{@keys} */
1120 { $$ = prepend_elem(OP_HSLICE,
1121 newOP(OP_PUSHMARK, 0),
1122 newLISTOP(OP_HSLICE, 0,
1124 ref(oopsHV($1), OP_HSLICE)));
1125 PL_parser->expect = XOPERATOR;
1126 TOKEN_GETMAD($2,$$,'{');
1127 TOKEN_GETMAD($4,$$,';');
1128 TOKEN_GETMAD($5,$$,'}');
1133 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1134 | amper '(' ')' /* &foo() */
1135 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1136 TOKEN_GETMAD($2,$$,'(');
1137 TOKEN_GETMAD($3,$$,')');
1139 | amper '(' expr ')' /* &foo(@args) */
1141 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1142 append_elem(OP_LIST, $3, scalar($1)));
1145 if (op->op_type == OP_CONST) { /* defeat const fold */
1146 op = (OP*)op->op_madprop->mad_val;
1148 token_getmad($2,op,'(');
1149 token_getmad($4,op,')');
1152 | NOAMP WORD listexpr /* foo(@args) */
1153 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1154 append_elem(OP_LIST, $3, scalar($2)));
1155 TOKEN_GETMAD($1,$$,'o');
1157 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1158 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1159 PL_hints |= HINT_BLOCK_SCOPE;
1160 TOKEN_GETMAD($1,$$,'o');
1163 { $$ = newLOOPEX(IVAL($1),$2);
1164 TOKEN_GETMAD($1,$$,'o');
1166 | NOTOP argexpr /* not $foo */
1167 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1168 TOKEN_GETMAD($1,$$,'o');
1170 | UNIOP /* Unary op, $_ implied */
1171 { $$ = newOP(IVAL($1), 0);
1172 TOKEN_GETMAD($1,$$,'o');
1174 | UNIOP block /* eval { foo }* */
1175 { $$ = newUNOP(IVAL($1), 0, $2);
1176 TOKEN_GETMAD($1,$$,'o');
1178 | UNIOP term /* Unary op */
1179 { $$ = newUNOP(IVAL($1), 0, $2);
1180 TOKEN_GETMAD($1,$$,'o');
1182 | REQUIRE /* require, $_ implied */
1183 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1184 TOKEN_GETMAD($1,$$,'o');
1186 | REQUIRE term /* require Foo */
1187 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1188 TOKEN_GETMAD($1,$$,'o');
1191 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1192 | UNIOPSUB term /* Sub treated as unop */
1193 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1194 append_elem(OP_LIST, $2, scalar($1))); }
1195 | FUNC0 /* Nullary operator */
1196 { $$ = newOP(IVAL($1), 0);
1197 TOKEN_GETMAD($1,$$,'o');
1200 { $$ = newOP(IVAL($1), 0);
1201 TOKEN_GETMAD($1,$$,'o');
1202 TOKEN_GETMAD($2,$$,'(');
1203 TOKEN_GETMAD($3,$$,')');
1205 | FUNC0SUB /* Sub treated as nullop */
1206 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1208 | FUNC1 '(' ')' /* not () */
1209 { $$ = (IVAL($1) == OP_NOT)
1210 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1211 : newOP(IVAL($1), OPf_SPECIAL);
1213 TOKEN_GETMAD($1,$$,'o');
1214 TOKEN_GETMAD($2,$$,'(');
1215 TOKEN_GETMAD($3,$$,')');
1217 | FUNC1 '(' expr ')' /* not($foo) */
1218 { $$ = newUNOP(IVAL($1), 0, $3);
1219 TOKEN_GETMAD($1,$$,'o');
1220 TOKEN_GETMAD($2,$$,'(');
1221 TOKEN_GETMAD($4,$$,')');
1223 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1224 { $$ = pmruntime($1, $3, 1);
1225 TOKEN_GETMAD($2,$$,'(');
1226 TOKEN_GETMAD($4,$$,')');
1232 /* "my" declarations, with optional attributes */
1233 myattrterm: MY myterm myattrlist
1234 { $$ = my_attrs($2,$3);
1236 token_getmad($1,$$,'d');
1237 append_madprops($3->op_madprop, $$, 'a');
1242 { $$ = localize($2,IVAL($1));
1243 TOKEN_GETMAD($1,$$,'d');
1247 /* Things that can be "my"'d */
1248 myterm : '(' expr ')'
1249 { $$ = sawparens($2);
1250 TOKEN_GETMAD($1,$$,'(');
1251 TOKEN_GETMAD($3,$$,')');
1254 { $$ = sawparens(newNULLLIST());
1255 TOKEN_GETMAD($1,$$,'(');
1256 TOKEN_GETMAD($2,$$,')');
1266 /* Basic list expressions */
1267 listexpr: /* NULL */ %prec PREC_LOW
1269 | argexpr %prec PREC_LOW
1273 listexprcom: /* NULL */
1280 OP* op = newNULLLIST();
1281 token_getmad($2,op,',');
1282 $$ = append_elem(OP_LIST, $1, op);
1290 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1293 { PL_parser->in_my = 0; $$ = my($1); }
1297 { $$ = newCVREF(IVAL($1),$2);
1298 TOKEN_GETMAD($1,$$,'&');
1302 scalar : '$' indirob
1303 { $$ = newSVREF($2);
1304 TOKEN_GETMAD($1,$$,'$');
1309 { $$ = newAVREF($2);
1310 TOKEN_GETMAD($1,$$,'@');
1315 { $$ = newHVREF($2);
1316 TOKEN_GETMAD($1,$$,'%');
1320 arylen : DOLSHARP indirob
1321 { $$ = newAVREF($2);
1322 TOKEN_GETMAD($1,$$,'l');
1327 { $$ = newGVREF(0,$2);
1328 TOKEN_GETMAD($1,$$,'*');
1332 /* Indirect objects */
1334 { $$ = scalar($1); }
1335 | scalar %prec PREC_LOW
1336 { $$ = scalar($1); }