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.
37 /* Make the parser re-entrant. */
41 /* FIXME for MAD - is the new mintro on while and until important? */
46 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
47 must always be 1st union member) */
51 #ifdef PERL_IN_MADLY_C
63 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
65 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
66 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
67 %token <p_tkval> LABEL
68 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
69 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
70 %token <i_tkval> GIVEN WHEN DEFAULT
71 %token <i_tkval> LOOPEX DOTDOT
72 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
73 %token <i_tkval> RELOP EQOP MULOP ADDOP
74 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
75 %token <i_tkval> LOCAL MY MYSUB REQUIRE
76 %token <i_tkval> COLONATTR
78 %type <ival> prog progstart remember mremember savescope
79 %type <ival> startsub startanonsub startformsub
80 /* FIXME for MAD - are these two ival? */
81 %type <ival> mydefsv mintro
83 %type <opval> decl format subrout mysubrout package use peg
85 %type <opval> block mblock lineseq line loop cond else
86 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
87 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
88 %type <opval> listexpr listexprcom indirob listop method
89 %type <opval> formname subname proto subbody cont my_scalar
90 %type <opval> subattrlist myattrlist myattrterm myterm
91 %type <opval> termbinop termunop anonymous termdo
92 %type <opval> switch case
95 %nonassoc <i_tkval> PREC_LOW
98 %left <i_tkval> OROP DOROP
100 %right <i_tkval> NOTOP
101 %nonassoc LSTOP LSTOPSUB
103 %right <i_tkval> ASSIGNOP
104 %right <i_tkval> '?' ':'
106 %left <i_tkval> OROR DORDOR
107 %left <i_tkval> ANDAND
108 %left <i_tkval> BITOROP
109 %left <i_tkval> BITANDOP
112 %nonassoc UNIOP UNIOPSUB
114 %left <i_tkval> SHIFTOP
117 %left <i_tkval> MATCHOP
118 %right <i_tkval> '!' '~' UMINUS REFGEN
119 %right <i_tkval> POWOP
120 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
121 %left <i_tkval> ARROW
122 %nonassoc <i_tkval> ')'
130 /* The whole program */
132 /*CONTINUED*/ lineseq
133 { $$ = $1; newPROG(block_end($1,$2)); }
136 /* An ordinary block */
137 block : '{' remember lineseq '}'
138 { if (PL_copline > (line_t)IVAL($1))
139 PL_copline = (line_t)IVAL($1);
140 $$ = block_end($2, $3);
141 TOKEN_GETMAD($1,$$,'{');
142 TOKEN_GETMAD($4,$$,'}');
146 remember: /* NULL */ /* start a full lexical scope */
147 { $$ = block_start(TRUE); }
150 mydefsv: /* NULL */ /* lexicalize $_ */
151 { $$ = (I32) allocmy("$_"); }
156 PL_expect = XSTATE; $$ = block_start(TRUE);
161 mblock : '{' mremember lineseq '}'
162 { if (PL_copline > (line_t)IVAL($1))
163 PL_copline = (line_t)IVAL($1);
164 $$ = block_end($2, $3);
165 TOKEN_GETMAD($1,$$,'{');
166 TOKEN_GETMAD($4,$$,'}');
170 mremember: /* NULL */ /* start a partial lexical scope */
171 { $$ = block_start(FALSE); }
174 savescope: /* NULL */ /* remember stack pos in case of error */
175 { $$ = PL_savestack_ix; }
177 /* A collection of "lines" in the program */
183 append_list(OP_LINESEQ,
184 (LISTOP*)$1, (LISTOP*)$2),
187 | lineseq savescope line
189 $$ = append_list(OP_LINESEQ,
190 (LISTOP*)$1, (LISTOP*)$3);
191 PL_pad_reset_pending = TRUE;
192 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
195 /* A "line" in the program */
197 { $$ = newSTATEOP(0, PVAL($1), $2);
198 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
199 | loop /* loops add their own labels */
200 | switch /* ... and so do switches */
203 { $$ = newSTATEOP(0, PVAL($1), $2); }
207 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
208 TOKEN_GETMAD($1,$$,'L');
209 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
217 TOKEN_GETMAD($2,$$,';');
223 $$ = newSTATEOP(0, PVAL($1), $2);
226 /* sideff might already have a nexstate */
227 OP* op = ((LISTOP*)$$)->op_first;
229 while (op->op_sibling &&
230 op->op_sibling->op_type == OP_NEXTSTATE)
232 token_getmad($1,op,'L');
233 token_getmad($3,op,';');
239 /* An expression which may have a side-effect */
245 { $$ = newLOGOP(OP_AND, 0, $3, $1);
246 TOKEN_GETMAD($2,$$,'i');
249 { $$ = newLOGOP(OP_OR, 0, $3, $1);
250 TOKEN_GETMAD($2,$$,'i');
253 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
254 TOKEN_GETMAD($2,$$,'w');
257 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
258 TOKEN_GETMAD($2,$$,'w');
261 { $$ = newFOROP(0, Nullch, (line_t)IVAL($2),
262 Nullop, $3, $1, Nullop);
263 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
267 /* else and elsif blocks */
271 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
272 TOKEN_GETMAD($1,$$,'o');
274 | ELSIF '(' mexpr ')' mblock else
275 { PL_copline = (line_t)IVAL($1);
276 $$ = newCONDOP(0, $3, scope($5), $6);
277 PL_hints |= HINT_BLOCK_SCOPE;
278 TOKEN_GETMAD($1,$$,'I');
279 TOKEN_GETMAD($2,$$,'(');
280 TOKEN_GETMAD($4,$$,')');
284 /* Real conditional expressions */
285 cond : IF '(' remember mexpr ')' mblock else
286 { PL_copline = (line_t)IVAL($1);
288 newCONDOP(0, $4, scope($6), $7));
289 TOKEN_GETMAD($1,$$,'I');
290 TOKEN_GETMAD($2,$$,'(');
291 TOKEN_GETMAD($5,$$,')');
293 | UNLESS '(' remember miexpr ')' mblock else
294 { PL_copline = (line_t)IVAL($1);
296 newCONDOP(0, $4, scope($6), $7));
297 TOKEN_GETMAD($1,$$,'I');
298 TOKEN_GETMAD($2,$$,'(');
299 TOKEN_GETMAD($5,$$,')');
303 /* Cases for a switch statement */
304 case : WHEN '(' remember mexpr ')' mblock
306 newWHENOP($4, scope($6))); }
308 { $$ = newWHENOP(0, scope($2)); }
311 /* Continue blocks */
316 TOKEN_GETMAD($1,$$,'o');
320 /* Loops: while, until, for, and a bare block */
321 loop : label WHILE '(' remember texpr ')' mintro mblock cont
323 PL_copline = (line_t)$2;
325 newSTATEOP(0, PVAL($1),
326 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
327 IVAL($2), $5, $8, $9, $7)));
328 TOKEN_GETMAD($1,innerop,'L');
329 TOKEN_GETMAD($2,innerop,'W');
330 TOKEN_GETMAD($3,innerop,'(');
331 TOKEN_GETMAD($6,innerop,')');
334 | label UNTIL '(' remember iexpr ')' mintro mblock cont
336 PL_copline = (line_t)$2;
338 newSTATEOP(0, PVAL($1),
339 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
340 IVAL($2), $5, $8, $9, $7)));
341 TOKEN_GETMAD($1,innerop,'L');
342 TOKEN_GETMAD($2,innerop,'W');
343 TOKEN_GETMAD($3,innerop,'(');
344 TOKEN_GETMAD($6,innerop,')');
346 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
349 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
351 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
352 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
353 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
354 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
355 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
357 | label FOR scalar '(' remember mexpr ')' mblock cont
360 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
361 mod($3, OP_ENTERLOOP), $6, $8, $9));
362 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
363 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
364 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
365 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
367 | label FOR '(' remember mexpr ')' mblock cont
370 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
371 Nullop, $5, $7, $8));
372 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
373 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
374 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
375 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
377 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
379 /* basically fake up an initialize-while lineseq */
381 PL_copline = (line_t)IVAL($2);
382 forop = newSTATEOP(0, PVAL($1),
383 newWHILEOP(0, 1, (LOOP*)Nullop,
384 IVAL($2), scalar($7),
388 $5 = newOP(OP_NULL, 0);
389 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
392 ?savepv(($1)->tk_lval.pval):Nullch),
396 token_getmad($2,forop,'3');
397 token_getmad($3,forop,'(');
398 token_getmad($6,forop,'1');
399 token_getmad($8,forop,'2');
400 token_getmad($11,forop,')');
401 token_getmad($1,forop,'L');
404 forop = append_elem(OP_LINESEQ,
405 newSTATEOP(0, ($1?savepv($1):Nullch),
412 $$ = block_end($4, forop); }
413 | label block cont /* a block is a loop that happens once */
414 { $$ = newSTATEOP(0, PVAL($1),
415 newWHILEOP(0, 1, (LOOP*)Nullop,
416 NOLINE, Nullop, $2, $3, 0));
417 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
421 switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
422 { PL_copline = (line_t) $2;
424 newSTATEOP(0, PVAL($1),
425 newGIVENOP($6, scope($8),
426 (PADOFFSET) $5) )); }
429 /* determine whether there are any new my declarations */
431 { $$ = (PL_min_intro_pending &&
432 PL_max_intro_pending >= PL_min_intro_pending);
435 /* Normal expression */
441 /* Boolean expression */
442 texpr : /* NULL means true */
444 (void)scan_num("1", &tmplval);
445 $$ = tmplval.opval; }
449 /* Inverted boolean expression */
451 { $$ = invert(scalar($1)); }
454 /* Expression with its own lexical scope */
456 { $$ = $1; intro_my(); }
460 { $$ = $1; intro_my(); }
464 { $$ = $1; intro_my(); }
467 /* Optional "MAIN:"-style loop labels */
472 tmplval.pval = Nullch;
473 $$ = newTOKEN(OP_NULL, tmplval, 0);
481 /* Some kind of declaration - just hang on peg in the parse tree */
493 /* these two are only used by MAD */
500 { $$ = newOP(OP_NULL,0);
501 TOKEN_GETMAD($1,$$,'p');
505 format : FORMAT startformsub formname block
506 { SvREFCNT_inc(PL_compcv);
508 $$ = newFORM($2, $3, $4);
509 prepend_madprops($1->tk_mad, $$, 'F');
519 formname: WORD { $$ = $1; }
520 | /* NULL */ { $$ = Nullop; }
523 /* Unimplemented "my sub foo { }" */
524 mysubrout: MYSUB startsub subname proto subattrlist subbody
525 { SvREFCNT_inc(PL_compcv);
527 $$ = newMYSUB($2, $3, $4, $5, $6);
528 token_getmad($1,$$,'d');
530 newMYSUB($2, $3, $4, $5, $6);
536 /* Subroutine definition */
537 subrout : SUB startsub subname proto subattrlist subbody
538 { SvREFCNT_inc(PL_compcv);
540 OP* o = newSVOP(OP_ANONCODE, 0,
541 (SV*)newATTRSUB($2, $3, $4, $5, $6));
542 $$ = newOP(OP_NULL,0);
544 op_getmad($3,$$,'n');
545 op_getmad($4,$$,'s');
546 op_getmad($5,$$,'a');
547 token_getmad($1,$$,'d');
548 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; }
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(PL_compcv);
647 $$ = utilize(IVAL($1), $2, $4, $5, $6);
648 token_getmad($1,$$,'o');
649 token_getmad($7,$$,';');
650 if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0)
651 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
653 utilize(IVAL($1), $2, $4, $5, $6);
659 /* Ordinary expressions; logical combinations */
660 expr : expr ANDOP expr
661 { $$ = newLOGOP(OP_AND, 0, $1, $3);
662 TOKEN_GETMAD($2,$$,'o');
665 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
666 TOKEN_GETMAD($2,$$,'o');
669 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
670 TOKEN_GETMAD($2,$$,'o');
672 | argexpr %prec PREC_LOW
675 /* Expressions are a list of terms joined by commas */
676 argexpr : argexpr ','
679 OP* op = newNULLLIST();
680 token_getmad($2,op,',');
681 $$ = append_elem(OP_LIST, $1, op);
689 $3 = newUNOP(OP_NULL, 0, $3);
690 token_getmad($2,$3,',');
692 $$ = append_elem(OP_LIST, $1, $3);
694 | term %prec PREC_LOW
698 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
699 { $$ = convert(IVAL($1), OPf_STACKED,
700 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
701 TOKEN_GETMAD($1,$$,'o');
703 | FUNC '(' indirob expr ')' /* print ($fh @args */
704 { $$ = convert(IVAL($1), OPf_STACKED,
705 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
706 TOKEN_GETMAD($1,$$,'o');
707 TOKEN_GETMAD($2,$$,'(');
708 TOKEN_GETMAD($5,$$,')');
710 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
711 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
713 prepend_elem(OP_LIST, scalar($1), $5),
714 newUNOP(OP_METHOD, 0, $3)));
715 TOKEN_GETMAD($2,$$,'A');
716 TOKEN_GETMAD($4,$$,'(');
717 TOKEN_GETMAD($6,$$,')');
719 | term ARROW method /* $foo->bar */
720 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
721 append_elem(OP_LIST, scalar($1),
722 newUNOP(OP_METHOD, 0, $3)));
723 TOKEN_GETMAD($2,$$,'A');
725 | METHOD indirob listexpr /* new Class @args */
726 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
728 prepend_elem(OP_LIST, $2, $3),
729 newUNOP(OP_METHOD, 0, $1)));
731 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
732 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
734 prepend_elem(OP_LIST, $2, $4),
735 newUNOP(OP_METHOD, 0, $1)));
736 TOKEN_GETMAD($3,$$,'(');
737 TOKEN_GETMAD($5,$$,')');
739 | LSTOP listexpr /* print @args */
740 { $$ = convert(IVAL($1), 0, $2);
741 TOKEN_GETMAD($1,$$,'o');
743 | FUNC '(' listexprcom ')' /* print (@args) */
744 { $$ = convert(IVAL($1), 0, $3);
745 TOKEN_GETMAD($1,$$,'o');
746 TOKEN_GETMAD($2,$$,'(');
747 TOKEN_GETMAD($4,$$,')');
749 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
750 { SvREFCNT_inc(PL_compcv);
751 $3 = newANONATTRSUB($2, 0, Nullop, $3); }
752 listexpr %prec LSTOP /* ... @bar */
753 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
755 prepend_elem(OP_LIST, $3, $5), $1));
759 /* Names of methods. May use $object->$methodname */
764 /* Some kind of subscripted expression */
765 subscripted: star '{' expr ';' '}' /* *main::{something} */
766 /* In this and all the hash accessors, ';' is
767 * provided by the tokeniser */
768 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
769 PL_expect = XOPERATOR;
770 TOKEN_GETMAD($2,$$,'{');
771 TOKEN_GETMAD($4,$$,';');
772 TOKEN_GETMAD($5,$$,'}');
774 | scalar '[' expr ']' /* $array[$element] */
775 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
776 TOKEN_GETMAD($2,$$,'[');
777 TOKEN_GETMAD($4,$$,']');
779 | term ARROW '[' expr ']' /* somearef->[$element] */
780 { $$ = newBINOP(OP_AELEM, 0,
781 ref(newAVREF($1),OP_RV2AV),
783 TOKEN_GETMAD($2,$$,'a');
784 TOKEN_GETMAD($3,$$,'[');
785 TOKEN_GETMAD($5,$$,']');
787 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
788 { $$ = newBINOP(OP_AELEM, 0,
789 ref(newAVREF($1),OP_RV2AV),
791 TOKEN_GETMAD($2,$$,'[');
792 TOKEN_GETMAD($4,$$,']');
794 | scalar '{' expr ';' '}' /* $foo->{bar();} */
795 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
796 PL_expect = XOPERATOR;
797 TOKEN_GETMAD($2,$$,'{');
798 TOKEN_GETMAD($4,$$,';');
799 TOKEN_GETMAD($5,$$,'}');
801 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
802 { $$ = newBINOP(OP_HELEM, 0,
803 ref(newHVREF($1),OP_RV2HV),
805 PL_expect = XOPERATOR;
806 TOKEN_GETMAD($2,$$,'a');
807 TOKEN_GETMAD($3,$$,'{');
808 TOKEN_GETMAD($5,$$,';');
809 TOKEN_GETMAD($6,$$,'}');
811 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
812 { $$ = newBINOP(OP_HELEM, 0,
813 ref(newHVREF($1),OP_RV2HV),
815 PL_expect = XOPERATOR;
816 TOKEN_GETMAD($2,$$,'{');
817 TOKEN_GETMAD($4,$$,';');
818 TOKEN_GETMAD($5,$$,'}');
820 | term ARROW '(' ')' /* $subref->() */
821 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
822 newCVREF(0, scalar($1)));
823 TOKEN_GETMAD($2,$$,'a');
824 TOKEN_GETMAD($3,$$,'(');
825 TOKEN_GETMAD($4,$$,')');
827 | term ARROW '(' expr ')' /* $subref->(@args) */
828 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
829 append_elem(OP_LIST, $4,
830 newCVREF(0, scalar($1))));
831 TOKEN_GETMAD($2,$$,'a');
832 TOKEN_GETMAD($3,$$,'(');
833 TOKEN_GETMAD($5,$$,')');
836 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
837 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
838 append_elem(OP_LIST, $3,
839 newCVREF(0, scalar($1))));
840 TOKEN_GETMAD($2,$$,'(');
841 TOKEN_GETMAD($4,$$,')');
843 | subscripted '(' ')' /* $foo->{bar}->() */
844 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
845 newCVREF(0, scalar($1)));
846 TOKEN_GETMAD($2,$$,'(');
847 TOKEN_GETMAD($3,$$,')');
849 | '(' expr ')' '[' expr ']' /* list slice */
850 { $$ = newSLICEOP(0, $5, $2);
851 TOKEN_GETMAD($1,$$,'(');
852 TOKEN_GETMAD($3,$$,')');
853 TOKEN_GETMAD($4,$$,'[');
854 TOKEN_GETMAD($6,$$,']');
856 | '(' ')' '[' expr ']' /* empty list slice! */
857 { $$ = newSLICEOP(0, $4, Nullop);
858 TOKEN_GETMAD($1,$$,'(');
859 TOKEN_GETMAD($2,$$,')');
860 TOKEN_GETMAD($3,$$,'[');
861 TOKEN_GETMAD($5,$$,']');
865 /* Binary operators between terms */
866 termbinop: term ASSIGNOP term /* $x = $y */
867 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
868 TOKEN_GETMAD($2,$$,'o');
870 | term POWOP term /* $x ** $y */
871 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
872 TOKEN_GETMAD($2,$$,'o');
874 | term MULOP term /* $x * $y, $x x $y */
875 { if (IVAL($2) != OP_REPEAT)
877 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
878 TOKEN_GETMAD($2,$$,'o');
880 | term ADDOP term /* $x + $y */
881 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
882 TOKEN_GETMAD($2,$$,'o');
884 | term SHIFTOP term /* $x >> $y, $x << $y */
885 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
886 TOKEN_GETMAD($2,$$,'o');
888 | term RELOP term /* $x > $y, etc. */
889 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
890 TOKEN_GETMAD($2,$$,'o');
892 | term EQOP term /* $x == $y, $x eq $y */
893 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
894 TOKEN_GETMAD($2,$$,'o');
896 | term BITANDOP term /* $x & $y */
897 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
898 TOKEN_GETMAD($2,$$,'o');
900 | term BITOROP term /* $x | $y */
901 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
902 TOKEN_GETMAD($2,$$,'o');
904 | term DOTDOT term /* $x..$y, $x...$y */
906 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
910 op = (UNOP*)op->op_first; /* get to flop */
911 op = (UNOP*)op->op_first; /* get to flip */
912 op = (UNOP*)op->op_first; /* get to range */
913 token_getmad($2,(OP*)op,'o');
916 | term ANDAND term /* $x && $y */
917 { $$ = newLOGOP(OP_AND, 0, $1, $3);
918 TOKEN_GETMAD($2,$$,'o');
920 | term OROR term /* $x || $y */
921 { $$ = newLOGOP(OP_OR, 0, $1, $3);
922 TOKEN_GETMAD($2,$$,'o');
924 | term DORDOR term /* $x // $y */
925 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
926 TOKEN_GETMAD($2,$$,'o');
928 | term MATCHOP term /* $x =~ /$y/ */
929 { $$ = bind_match(IVAL($2), $1, $3);
931 ($$->op_type == OP_NOT
932 ? ((UNOP*)$$)->op_first : $$),
937 /* Unary operators and terms */
938 termunop : '-' term %prec UMINUS /* -$x */
939 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
940 TOKEN_GETMAD($1,$$,'o');
942 | '+' term %prec UMINUS /* +$x */
944 newUNOP(OP_NULL, 0, $2),
947 TOKEN_GETMAD($1,$$,'+');
950 { $$ = newUNOP(OP_NOT, 0, scalar($2));
951 TOKEN_GETMAD($1,$$,'o');
954 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
955 TOKEN_GETMAD($1,$$,'o');
957 | term POSTINC /* $x++ */
958 { $$ = newUNOP(OP_POSTINC, 0,
959 mod(scalar($1), OP_POSTINC));
960 TOKEN_GETMAD($2,$$,'o');
962 | term POSTDEC /* $x-- */
963 { $$ = newUNOP(OP_POSTDEC, 0,
964 mod(scalar($1), OP_POSTDEC));
965 TOKEN_GETMAD($2,$$,'o');
967 | PREINC term /* ++$x */
968 { $$ = newUNOP(OP_PREINC, 0,
969 mod(scalar($2), OP_PREINC));
970 TOKEN_GETMAD($1,$$,'o');
972 | PREDEC term /* --$x */
973 { $$ = newUNOP(OP_PREDEC, 0,
974 mod(scalar($2), OP_PREDEC));
975 TOKEN_GETMAD($1,$$,'o');
980 /* Constructors for anonymous data */
981 anonymous: '[' expr ']'
982 { $$ = newANONLIST($2);
983 TOKEN_GETMAD($1,$$,'[');
984 TOKEN_GETMAD($3,$$,']');
987 { $$ = newANONLIST(Nullop);
988 TOKEN_GETMAD($1,$$,'[');
989 TOKEN_GETMAD($2,$$,']');
991 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
992 { $$ = newANONHASH($2);
993 TOKEN_GETMAD($1,$$,'{');
994 TOKEN_GETMAD($3,$$,';');
995 TOKEN_GETMAD($4,$$,'}');
997 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
998 { $$ = newANONHASH(Nullop);
999 TOKEN_GETMAD($1,$$,'{');
1000 TOKEN_GETMAD($2,$$,';');
1001 TOKEN_GETMAD($3,$$,'}');
1003 | ANONSUB startanonsub proto subattrlist block %prec '('
1004 { SvREFCNT_inc(PL_compcv);
1005 $$ = newANONATTRSUB($2, $3, $4, $5);
1006 TOKEN_GETMAD($1,$$,'o');
1007 OP_GETMAD($3,$$,'s');
1008 OP_GETMAD($4,$$,'a');
1013 /* Things called with "do" */
1014 termdo : DO term %prec UNIOP /* do $filename */
1015 { $$ = dofile($2, IVAL($1));
1016 TOKEN_GETMAD($1,$$,'o');
1018 | DO block %prec '(' /* do { code */
1019 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1020 TOKEN_GETMAD($1,$$,'D');
1022 | DO WORD '(' ')' /* do somesub() */
1023 { $$ = newUNOP(OP_ENTERSUB,
1024 OPf_SPECIAL|OPf_STACKED,
1025 prepend_elem(OP_LIST,
1027 (OPpENTERSUB_AMPER<<8),
1030 TOKEN_GETMAD($1,$$,'o');
1031 TOKEN_GETMAD($3,$$,'(');
1032 TOKEN_GETMAD($4,$$,')');
1034 | DO WORD '(' expr ')' /* do somesub(@args) */
1035 { $$ = newUNOP(OP_ENTERSUB,
1036 OPf_SPECIAL|OPf_STACKED,
1037 append_elem(OP_LIST,
1040 (OPpENTERSUB_AMPER<<8),
1043 TOKEN_GETMAD($1,$$,'o');
1044 TOKEN_GETMAD($3,$$,'(');
1045 TOKEN_GETMAD($5,$$,')');
1047 | DO scalar '(' ')' /* do $subref () */
1048 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1049 prepend_elem(OP_LIST,
1050 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
1051 TOKEN_GETMAD($1,$$,'o');
1052 TOKEN_GETMAD($3,$$,'(');
1053 TOKEN_GETMAD($4,$$,')');
1055 | DO scalar '(' expr ')' /* do $subref (@args) */
1056 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1057 prepend_elem(OP_LIST,
1059 scalar(newCVREF(0,scalar($2))))); dep();
1060 TOKEN_GETMAD($1,$$,'o');
1061 TOKEN_GETMAD($3,$$,'(');
1062 TOKEN_GETMAD($5,$$,')');
1071 | term '?' term ':' term
1072 { $$ = newCONDOP(0, $1, $3, $5);
1073 TOKEN_GETMAD($2,$$,'?');
1074 TOKEN_GETMAD($4,$$,':');
1076 | REFGEN term /* \$x, \@y, \%z */
1077 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1078 TOKEN_GETMAD($1,$$,'o');
1080 | myattrterm %prec UNIOP
1082 | LOCAL term %prec UNIOP
1083 { $$ = localize($2,IVAL($1));
1084 TOKEN_GETMAD($1,$$,'d');
1087 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1088 TOKEN_GETMAD($1,$$,'(');
1089 TOKEN_GETMAD($3,$$,')');
1092 { $$ = sawparens(newNULLLIST());
1093 TOKEN_GETMAD($1,$$,'(');
1094 TOKEN_GETMAD($2,$$,')');
1104 | arylen %prec '(' /* $#x, $#{ something } */
1105 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1108 | ary '[' expr ']' /* array slice */
1109 { $$ = prepend_elem(OP_ASLICE,
1110 newOP(OP_PUSHMARK, 0),
1111 newLISTOP(OP_ASLICE, 0,
1113 ref($1, OP_ASLICE)));
1114 TOKEN_GETMAD($2,$$,'[');
1115 TOKEN_GETMAD($4,$$,']');
1117 | ary '{' expr ';' '}' /* @hash{@keys} */
1118 { $$ = prepend_elem(OP_HSLICE,
1119 newOP(OP_PUSHMARK, 0),
1120 newLISTOP(OP_HSLICE, 0,
1122 ref(oopsHV($1), OP_HSLICE)));
1123 PL_expect = XOPERATOR;
1124 TOKEN_GETMAD($2,$$,'{');
1125 TOKEN_GETMAD($4,$$,';');
1126 TOKEN_GETMAD($5,$$,'}');
1131 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1132 | amper '(' ')' /* &foo() */
1133 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1134 TOKEN_GETMAD($2,$$,'(');
1135 TOKEN_GETMAD($3,$$,')');
1137 | amper '(' expr ')' /* &foo(@args) */
1139 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1140 append_elem(OP_LIST, $3, scalar($1)));
1143 if (op->op_type == OP_CONST) { /* defeat const fold */
1144 op = (OP*)op->op_madprop->mad_val;
1146 token_getmad($2,op,'(');
1147 token_getmad($4,op,')');
1150 | NOAMP WORD listexpr /* foo(@args) */
1151 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1152 append_elem(OP_LIST, $3, scalar($2)));
1153 TOKEN_GETMAD($1,$$,'o');
1155 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1156 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1157 PL_hints |= HINT_BLOCK_SCOPE;
1158 TOKEN_GETMAD($1,$$,'o');
1161 { $$ = newLOOPEX(IVAL($1),$2);
1162 TOKEN_GETMAD($1,$$,'o');
1164 | NOTOP argexpr /* not $foo */
1165 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1166 TOKEN_GETMAD($1,$$,'o');
1168 | UNIOP /* Unary op, $_ implied */
1169 { $$ = newOP(IVAL($1), 0);
1170 TOKEN_GETMAD($1,$$,'o');
1172 | UNIOP block /* eval { foo }* */
1173 { $$ = newUNOP(IVAL($1), 0, $2);
1174 TOKEN_GETMAD($1,$$,'o');
1176 | UNIOP term /* Unary op */
1177 { $$ = newUNOP(IVAL($1), 0, $2);
1178 TOKEN_GETMAD($1,$$,'o');
1180 | REQUIRE /* require, $_ implied *//* FIMXE for MAD needed? */
1181 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1182 | REQUIRE term /* require Foo *//* FIMXE for MAD needed? */
1183 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1185 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1186 | UNIOPSUB term /* Sub treated as unop */
1187 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1188 append_elem(OP_LIST, $2, scalar($1))); }
1189 | FUNC0 /* Nullary operator */
1190 { $$ = newOP(IVAL($1), 0);
1191 TOKEN_GETMAD($1,$$,'o');
1194 { $$ = newOP(IVAL($1), 0);
1195 TOKEN_GETMAD($1,$$,'o');
1196 TOKEN_GETMAD($2,$$,'(');
1197 TOKEN_GETMAD($3,$$,')');
1199 | FUNC0SUB /* Sub treated as nullop */
1200 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1202 | FUNC1 '(' ')' /* not () */
1203 { $$ = (IVAL($1) == OP_NOT)
1204 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1205 : newOP(IVAL($1), OPf_SPECIAL);
1207 TOKEN_GETMAD($1,$$,'o');
1208 TOKEN_GETMAD($2,$$,'(');
1209 TOKEN_GETMAD($3,$$,')');
1211 | FUNC1 '(' expr ')' /* not($foo) */
1212 { $$ = newUNOP(IVAL($1), 0, $3);
1213 TOKEN_GETMAD($1,$$,'o');
1214 TOKEN_GETMAD($2,$$,'(');
1215 TOKEN_GETMAD($4,$$,')');
1217 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1218 { $$ = pmruntime($1, $3, 1);
1219 TOKEN_GETMAD($2,$$,'(');
1220 TOKEN_GETMAD($4,$$,')');
1226 /* "my" declarations, with optional attributes */
1227 myattrterm: MY myterm myattrlist
1228 { $$ = my_attrs($2,$3);
1230 token_getmad($1,$$,'d');
1231 append_madprops($3->op_madprop, $$, 'a');
1236 { $$ = localize($2,IVAL($1));
1237 TOKEN_GETMAD($1,$$,'d');
1241 /* Things that can be "my"'d */
1242 myterm : '(' expr ')'
1243 { $$ = sawparens($2);
1244 TOKEN_GETMAD($1,$$,'(');
1245 TOKEN_GETMAD($3,$$,')');
1248 { $$ = sawparens(newNULLLIST());
1249 TOKEN_GETMAD($1,$$,'(');
1250 TOKEN_GETMAD($2,$$,')');
1260 /* Basic list expressions */
1261 listexpr: /* NULL */ %prec PREC_LOW
1263 | argexpr %prec PREC_LOW
1267 listexprcom: /* NULL */
1274 OP* op = newNULLLIST();
1275 token_getmad($2,op,',');
1276 $$ = append_elem(OP_LIST, $1, op);
1284 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1287 { PL_in_my = 0; $$ = my($1); }
1291 { $$ = newCVREF(IVAL($1),$2);
1292 TOKEN_GETMAD($1,$$,'&');
1296 scalar : '$' indirob
1297 { $$ = newSVREF($2);
1298 TOKEN_GETMAD($1,$$,'$');
1303 { $$ = newAVREF($2);
1304 TOKEN_GETMAD($1,$$,'@');
1309 { $$ = newHVREF($2);
1310 TOKEN_GETMAD($1,$$,'%');
1314 arylen : DOLSHARP indirob
1315 { $$ = newAVREF($2);
1316 TOKEN_GETMAD($1,$$,'l');
1321 { $$ = newGVREF(0,$2);
1322 TOKEN_GETMAD($1,$$,'*');
1326 /* Indirect objects */
1328 { $$ = scalar($1); }
1329 | scalar %prec PREC_LOW
1330 { $$ = scalar($1); }