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 * This file holds the grammar for the Perl language. If edited, you need
16 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
17 * and perly.act which are derived from this.
19 * Note that these derived files are included and compiled twice; once
20 * from perly.c, and once from madly.c. The second time, a number of MAD
21 * macros are defined, which compile in extra code that allows the parse
22 * tree to be accurately dumped. In particular:
24 * MAD defined if compiling madly.c
25 * DO_MAD(A) expands to A under madly.c, to null otherwise
26 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
27 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
28 * TOKEN_FREE() similarly
29 * OP_GETMAD() similarly
30 * IVAL(i) expands to (i)->tk_lval.ival or (i)
31 * PVAL(p) expands to (p)->tk_lval.pval or (p)
33 * The main job of of this grammar is to call the various newFOO()
34 * functions in op.c to build a syntax tree of OP structs.
35 * It relies on the lexer in toke.c to do the tokenizing.
37 * Note: due to the way that the cleanup code works WRT to freeing ops on
38 * the parse stack, it is dangerous to assign to the $n variables within
42 /* Make the parser re-entrant. */
46 /* FIXME for MAD - is the new mintro on while and until important? */
51 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
52 must always be 1st union member) */
56 #ifdef PERL_IN_MADLY_C
68 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
70 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
71 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
72 %token <p_tkval> LABEL
73 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
74 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
75 %token <i_tkval> GIVEN WHEN DEFAULT
76 %token <i_tkval> LOOPEX DOTDOT YADAYADA
77 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
78 %token <i_tkval> RELOP EQOP MULOP ADDOP
79 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
80 %token <i_tkval> LOCAL MY MYSUB REQUIRE
81 %token <i_tkval> COLONATTR
83 %type <ival> prog progstart remember mremember
84 %type <ival> startsub startanonsub startformsub
85 /* FIXME for MAD - are these two ival? */
86 %type <ival> mydefsv mintro
88 %type <opval> decl format subrout mysubrout package use peg
90 %type <opval> block mblock lineseq line loop cond else
91 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
92 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
93 %type <opval> listexpr listexprcom indirob listop method
94 %type <opval> formname subname proto subbody cont my_scalar
95 %type <opval> subattrlist myattrlist myattrterm myterm
96 %type <opval> termbinop termunop anonymous termdo
97 %type <opval> switch case
100 %nonassoc <i_tkval> PREC_LOW
103 %left <i_tkval> OROP DOROP
104 %left <i_tkval> ANDOP
105 %right <i_tkval> NOTOP
106 %nonassoc LSTOP LSTOPSUB
108 %right <i_tkval> ASSIGNOP
109 %right <i_tkval> '?' ':'
110 %nonassoc DOTDOT YADAYADA
111 %left <i_tkval> OROR DORDOR
112 %left <i_tkval> ANDAND
113 %left <i_tkval> BITOROP
114 %left <i_tkval> BITANDOP
117 %nonassoc UNIOP UNIOPSUB
119 %left <i_tkval> SHIFTOP
122 %left <i_tkval> MATCHOP
123 %right <i_tkval> '!' '~' UMINUS REFGEN
124 %right <i_tkval> POWOP
125 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
126 %left <i_tkval> ARROW
127 %nonassoc <i_tkval> ')'
135 /* The whole program */
137 /*CONTINUED*/ lineseq
138 { $$ = $1; newPROG(block_end($1,$2)); }
141 /* An ordinary block */
142 block : '{' remember lineseq '}'
143 { if (PL_parser->copline > (line_t)IVAL($1))
144 PL_parser->copline = (line_t)IVAL($1);
145 $$ = block_end($2, $3);
146 TOKEN_GETMAD($1,$$,'{');
147 TOKEN_GETMAD($4,$$,'}');
151 remember: /* NULL */ /* start a full lexical scope */
152 { $$ = block_start(TRUE); }
155 mydefsv: /* NULL */ /* lexicalize $_ */
156 { $$ = (I32) allocmy("$_"); }
161 PL_parser->expect = XSTATE; $$ = block_start(TRUE);
166 mblock : '{' mremember lineseq '}'
167 { if (PL_parser->copline > (line_t)IVAL($1))
168 PL_parser->copline = (line_t)IVAL($1);
169 $$ = block_end($2, $3);
170 TOKEN_GETMAD($1,$$,'{');
171 TOKEN_GETMAD($4,$$,'}');
175 mremember: /* NULL */ /* start a partial lexical scope */
176 { $$ = block_start(FALSE); }
179 /* A collection of "lines" in the program */
185 append_list(OP_LINESEQ,
186 (LISTOP*)$1, (LISTOP*)$2),
190 { $$ = append_list(OP_LINESEQ,
191 (LISTOP*)$1, (LISTOP*)$2);
192 PL_pad_reset_pending = TRUE;
194 PL_hints |= HINT_BLOCK_SCOPE;
198 /* A "line" in the program */
200 { $$ = newSTATEOP(0, PVAL($1), $2);
201 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
202 | loop /* loops add their own labels */
203 | switch /* ... and so do switches */
206 { $$ = newSTATEOP(0, PVAL($1), $2); }
210 $$ = newSTATEOP(0, PVAL($1), newOP(OP_NULL, 0));
211 TOKEN_GETMAD($1,$$,'L');
212 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first,';');
218 PL_parser->copline = NOLINE;
220 TOKEN_GETMAD($2,$$,';');
222 PL_parser->expect = XSTATE;
226 $$ = newSTATEOP(0, PVAL($1), $2);
227 PL_parser->expect = XSTATE;
229 /* sideff might already have a nexstate */
230 OP* op = ((LISTOP*)$$)->op_first;
232 while (op->op_sibling &&
233 op->op_sibling->op_type == OP_NEXTSTATE)
235 token_getmad($1,op,'L');
236 token_getmad($3,op,';');
242 /* An expression which may have a side-effect */
248 { $$ = newLOGOP(OP_AND, 0, $3, $1);
249 TOKEN_GETMAD($2,$$,'i');
252 { $$ = newLOGOP(OP_OR, 0, $3, $1);
253 TOKEN_GETMAD($2,$$,'i');
256 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
257 TOKEN_GETMAD($2,$$,'w');
260 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
261 TOKEN_GETMAD($2,$$,'w');
264 { $$ = newFOROP(0, NULL, (line_t)IVAL($2),
265 (OP*)NULL, $3, $1, (OP*)NULL);
266 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
270 /* else and elsif blocks */
274 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2);
275 TOKEN_GETMAD($1,$$,'o');
277 | ELSIF '(' mexpr ')' mblock else
278 { PL_parser->copline = (line_t)IVAL($1);
279 $$ = newCONDOP(0, newSTATEOP(OPf_SPECIAL,NULL,$3), scope($5), $6);
280 PL_hints |= HINT_BLOCK_SCOPE;
281 TOKEN_GETMAD($1,$$,'I');
282 TOKEN_GETMAD($2,$$,'(');
283 TOKEN_GETMAD($4,$$,')');
287 /* Real conditional expressions */
288 cond : IF '(' remember mexpr ')' mblock else
289 { PL_parser->copline = (line_t)IVAL($1);
291 newCONDOP(0, $4, scope($6), $7));
292 TOKEN_GETMAD($1,$$,'I');
293 TOKEN_GETMAD($2,$$,'(');
294 TOKEN_GETMAD($5,$$,')');
296 | UNLESS '(' remember miexpr ')' mblock else
297 { PL_parser->copline = (line_t)IVAL($1);
299 newCONDOP(0, $4, scope($6), $7));
300 TOKEN_GETMAD($1,$$,'I');
301 TOKEN_GETMAD($2,$$,'(');
302 TOKEN_GETMAD($5,$$,')');
306 /* Cases for a switch statement */
307 case : WHEN '(' remember mexpr ')' mblock
309 newWHENOP($4, scope($6))); }
311 { $$ = newWHENOP(0, scope($2)); }
314 /* Continue blocks */
319 TOKEN_GETMAD($1,$$,'o');
323 /* Loops: while, until, for, and a bare block */
324 loop : label WHILE '(' remember texpr ')' mintro mblock cont
326 PL_parser->copline = (line_t)$2;
328 newSTATEOP(0, PVAL($1),
329 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
330 IVAL($2), $5, $8, $9, $7)));
331 TOKEN_GETMAD($1,innerop,'L');
332 TOKEN_GETMAD($2,innerop,'W');
333 TOKEN_GETMAD($3,innerop,'(');
334 TOKEN_GETMAD($6,innerop,')');
337 | label UNTIL '(' remember iexpr ')' mintro mblock cont
339 PL_parser->copline = (line_t)$2;
341 newSTATEOP(0, PVAL($1),
342 innerop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
343 IVAL($2), $5, $8, $9, $7)));
344 TOKEN_GETMAD($1,innerop,'L');
345 TOKEN_GETMAD($2,innerop,'W');
346 TOKEN_GETMAD($3,innerop,'(');
347 TOKEN_GETMAD($6,innerop,')');
349 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
352 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
354 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
355 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
356 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'d');
357 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,'(');
358 TOKEN_GETMAD($8,((LISTOP*)innerop)->op_first->op_sibling,')');
360 | label FOR scalar '(' remember mexpr ')' mblock cont
363 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
364 mod($3, OP_ENTERLOOP), $6, $8, $9));
365 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
366 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
367 TOKEN_GETMAD($4,((LISTOP*)innerop)->op_first->op_sibling,'(');
368 TOKEN_GETMAD($7,((LISTOP*)innerop)->op_first->op_sibling,')');
370 | label FOR '(' remember mexpr ')' mblock cont
373 innerop = newFOROP(0, PVAL($1), (line_t)IVAL($2),
374 (OP*)NULL, $5, $7, $8));
375 TOKEN_GETMAD($1,((LISTOP*)innerop)->op_first,'L');
376 TOKEN_GETMAD($2,((LISTOP*)innerop)->op_first->op_sibling,'W');
377 TOKEN_GETMAD($3,((LISTOP*)innerop)->op_first->op_sibling,'(');
378 TOKEN_GETMAD($6,((LISTOP*)innerop)->op_first->op_sibling,')');
380 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
382 /* basically fake up an initialize-while lineseq */
384 PL_parser->copline = (line_t)IVAL($2);
385 forop = newSTATEOP(0, PVAL($1),
386 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
387 IVAL($2), scalar($7),
390 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
392 CopLABEL_alloc(($1)->tk_lval.pval),
393 ($5 ? $5 : newOP(OP_NULL, 0)) ),
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, CopLABEL_alloc($1), $5),
411 $$ = block_end($4, forop); }
412 | label block cont /* a block is a loop that happens once */
413 { $$ = newSTATEOP(0, PVAL($1),
414 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
415 NOLINE, (OP*)NULL, $2, $3, 0));
416 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
420 switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
421 { PL_parser->copline = (line_t) $2;
423 newSTATEOP(0, PVAL($1),
424 newGIVENOP($6, scope($8),
425 (PADOFFSET) $5) )); }
428 /* determine whether there are any new my declarations */
430 { $$ = (PL_min_intro_pending &&
431 PL_max_intro_pending >= PL_min_intro_pending);
434 /* Normal expression */
440 /* Boolean expression */
441 texpr : /* NULL means true */
443 (void)scan_num("1", &tmplval);
444 $$ = tmplval.opval; }
448 /* Inverted boolean expression */
450 { $$ = invert(scalar($1)); }
453 /* Expression with its own lexical scope */
455 { $$ = $1; intro_my(); }
459 { $$ = $1; intro_my(); }
463 { $$ = $1; intro_my(); }
466 /* Optional "MAIN:"-style loop labels */
472 $$ = newTOKEN(OP_NULL, tmplval, 0);
480 /* Some kind of declaration - just hang on peg in the parse tree */
492 /* these two are only used by MAD */
499 { $$ = newOP(OP_NULL,0);
500 TOKEN_GETMAD($1,$$,'p');
504 format : FORMAT startformsub formname block
505 { SvREFCNT_inc_simple_void(PL_compcv);
507 $$ = newFORM($2, $3, $4);
508 prepend_madprops($1->tk_mad, $$, 'F');
518 formname: WORD { $$ = $1; }
519 | /* NULL */ { $$ = (OP*)NULL; }
522 /* Unimplemented "my sub foo { }" */
523 mysubrout: MYSUB startsub subname proto subattrlist subbody
524 { SvREFCNT_inc_simple_void(PL_compcv);
526 $$ = newMYSUB($2, $3, $4, $5, $6);
527 token_getmad($1,$$,'d');
529 newMYSUB($2, $3, $4, $5, $6);
535 /* Subroutine definition */
536 subrout : SUB startsub subname proto subattrlist subbody
537 { SvREFCNT_inc_simple_void(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);
552 newATTRSUB($2, $3, $4, $5, $6);
558 startsub: /* NULL */ /* start a regular subroutine scope */
559 { $$ = start_subparse(FALSE, 0);
560 SAVEFREESV(PL_compcv); }
564 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
565 { $$ = start_subparse(FALSE, CVf_ANON);
566 SAVEFREESV(PL_compcv); }
569 startformsub: /* NULL */ /* start a format subroutine scope */
570 { $$ = start_subparse(TRUE, 0);
571 SAVEFREESV(PL_compcv); }
574 /* Name of a subroutine - must be a bareword, could be special */
575 subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
576 if (strEQ(name, "BEGIN") || strEQ(name, "END")
577 || strEQ(name, "INIT") || strEQ(name, "CHECK")
578 || strEQ(name, "UNITCHECK"))
579 CvSPECIAL_on(PL_compcv);
583 /* Subroutine prototype */
589 /* Optional list of subroutine attributes */
590 subattrlist: /* NULL */
594 TOKEN_GETMAD($1,$$,':');
601 TOKEN_GETMAD($1,$$,':');
605 /* List of attributes for a "my" variable declaration */
606 myattrlist: COLONATTR THING
608 TOKEN_GETMAD($1,$$,':');
615 TOKEN_GETMAD($1,$$,':');
619 /* Subroutine body - either null or a block */
620 subbody : block { $$ = $1; }
625 PL_parser->expect = XSTATE;
626 TOKEN_GETMAD($1,$$,';');
630 package : PACKAGE WORD ';'
634 token_getmad($1,$$,'o');
635 token_getmad($3,$$,';');
644 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
645 WORD WORD listexpr ';'
646 { SvREFCNT_inc_simple_void(PL_compcv);
648 $$ = utilize(IVAL($1), $2, $4, $5, $6);
649 token_getmad($1,$$,'o');
650 token_getmad($7,$$,';');
651 if (PL_parser->rsfp_filters &&
652 AvFILLp(PL_parser->rsfp_filters) >= 0)
653 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
655 utilize(IVAL($1), $2, $4, $5, $6);
661 /* Ordinary expressions; logical combinations */
662 expr : expr ANDOP expr
663 { $$ = newLOGOP(OP_AND, 0, $1, $3);
664 TOKEN_GETMAD($2,$$,'o');
667 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
668 TOKEN_GETMAD($2,$$,'o');
671 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
672 TOKEN_GETMAD($2,$$,'o');
674 | argexpr %prec PREC_LOW
677 /* Expressions are a list of terms joined by commas */
678 argexpr : argexpr ','
681 OP* op = newNULLLIST();
682 token_getmad($2,op,',');
683 $$ = append_elem(OP_LIST, $1, op);
692 term = newUNOP(OP_NULL, 0, term);
693 token_getmad($2,term,',');
695 $$ = append_elem(OP_LIST, $1, term);
697 | term %prec PREC_LOW
701 listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
702 { $$ = convert(IVAL($1), OPf_STACKED,
703 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
704 TOKEN_GETMAD($1,$$,'o');
706 | FUNC '(' indirob expr ')' /* print ($fh @args */
707 { $$ = convert(IVAL($1), OPf_STACKED,
708 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
709 TOKEN_GETMAD($1,$$,'o');
710 TOKEN_GETMAD($2,$$,'(');
711 TOKEN_GETMAD($5,$$,')');
713 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
714 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
716 prepend_elem(OP_LIST, scalar($1), $5),
717 newUNOP(OP_METHOD, 0, $3)));
718 TOKEN_GETMAD($2,$$,'A');
719 TOKEN_GETMAD($4,$$,'(');
720 TOKEN_GETMAD($6,$$,')');
722 | term ARROW method /* $foo->bar */
723 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
724 append_elem(OP_LIST, scalar($1),
725 newUNOP(OP_METHOD, 0, $3)));
726 TOKEN_GETMAD($2,$$,'A');
728 | METHOD indirob listexpr /* new Class @args */
729 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
731 prepend_elem(OP_LIST, $2, $3),
732 newUNOP(OP_METHOD, 0, $1)));
734 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
735 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
737 prepend_elem(OP_LIST, $2, $4),
738 newUNOP(OP_METHOD, 0, $1)));
739 TOKEN_GETMAD($3,$$,'(');
740 TOKEN_GETMAD($5,$$,')');
742 | LSTOP listexpr /* print @args */
743 { $$ = convert(IVAL($1), 0, $2);
744 TOKEN_GETMAD($1,$$,'o');
746 | FUNC '(' listexprcom ')' /* print (@args) */
747 { $$ = convert(IVAL($1), 0, $3);
748 TOKEN_GETMAD($1,$$,'o');
749 TOKEN_GETMAD($2,$$,'(');
750 TOKEN_GETMAD($4,$$,')');
752 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
753 { SvREFCNT_inc_simple_void(PL_compcv);
754 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
755 listexpr %prec LSTOP /* ... @bar */
756 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
758 prepend_elem(OP_LIST, $<opval>4, $5), $1));
762 /* Names of methods. May use $object->$methodname */
767 /* Some kind of subscripted expression */
768 subscripted: star '{' expr ';' '}' /* *main::{something} */
769 /* In this and all the hash accessors, ';' is
770 * provided by the tokeniser */
771 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
772 PL_parser->expect = XOPERATOR;
773 TOKEN_GETMAD($2,$$,'{');
774 TOKEN_GETMAD($4,$$,';');
775 TOKEN_GETMAD($5,$$,'}');
777 | scalar '[' expr ']' /* $array[$element] */
778 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
779 TOKEN_GETMAD($2,$$,'[');
780 TOKEN_GETMAD($4,$$,']');
782 | term ARROW '[' expr ']' /* somearef->[$element] */
783 { $$ = newBINOP(OP_AELEM, 0,
784 ref(newAVREF($1),OP_RV2AV),
786 TOKEN_GETMAD($2,$$,'a');
787 TOKEN_GETMAD($3,$$,'[');
788 TOKEN_GETMAD($5,$$,']');
790 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
791 { $$ = newBINOP(OP_AELEM, 0,
792 ref(newAVREF($1),OP_RV2AV),
794 TOKEN_GETMAD($2,$$,'[');
795 TOKEN_GETMAD($4,$$,']');
797 | scalar '{' expr ';' '}' /* $foo->{bar();} */
798 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
799 PL_parser->expect = XOPERATOR;
800 TOKEN_GETMAD($2,$$,'{');
801 TOKEN_GETMAD($4,$$,';');
802 TOKEN_GETMAD($5,$$,'}');
804 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
805 { $$ = newBINOP(OP_HELEM, 0,
806 ref(newHVREF($1),OP_RV2HV),
808 PL_parser->expect = XOPERATOR;
809 TOKEN_GETMAD($2,$$,'a');
810 TOKEN_GETMAD($3,$$,'{');
811 TOKEN_GETMAD($5,$$,';');
812 TOKEN_GETMAD($6,$$,'}');
814 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
815 { $$ = newBINOP(OP_HELEM, 0,
816 ref(newHVREF($1),OP_RV2HV),
818 PL_parser->expect = XOPERATOR;
819 TOKEN_GETMAD($2,$$,'{');
820 TOKEN_GETMAD($4,$$,';');
821 TOKEN_GETMAD($5,$$,'}');
823 | term ARROW '(' ')' /* $subref->() */
824 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
825 newCVREF(0, scalar($1)));
826 TOKEN_GETMAD($2,$$,'a');
827 TOKEN_GETMAD($3,$$,'(');
828 TOKEN_GETMAD($4,$$,')');
830 | term ARROW '(' expr ')' /* $subref->(@args) */
831 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
832 append_elem(OP_LIST, $4,
833 newCVREF(0, scalar($1))));
834 TOKEN_GETMAD($2,$$,'a');
835 TOKEN_GETMAD($3,$$,'(');
836 TOKEN_GETMAD($5,$$,')');
839 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
840 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
841 append_elem(OP_LIST, $3,
842 newCVREF(0, scalar($1))));
843 TOKEN_GETMAD($2,$$,'(');
844 TOKEN_GETMAD($4,$$,')');
846 | subscripted '(' ')' /* $foo->{bar}->() */
847 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
848 newCVREF(0, scalar($1)));
849 TOKEN_GETMAD($2,$$,'(');
850 TOKEN_GETMAD($3,$$,')');
852 | '(' expr ')' '[' expr ']' /* list slice */
853 { $$ = newSLICEOP(0, $5, $2);
854 TOKEN_GETMAD($1,$$,'(');
855 TOKEN_GETMAD($3,$$,')');
856 TOKEN_GETMAD($4,$$,'[');
857 TOKEN_GETMAD($6,$$,']');
859 | '(' ')' '[' expr ']' /* empty list slice! */
860 { $$ = newSLICEOP(0, $4, (OP*)NULL);
861 TOKEN_GETMAD($1,$$,'(');
862 TOKEN_GETMAD($2,$$,')');
863 TOKEN_GETMAD($3,$$,'[');
864 TOKEN_GETMAD($5,$$,']');
868 /* Binary operators between terms */
869 termbinop: term ASSIGNOP term /* $x = $y */
870 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
871 TOKEN_GETMAD($2,$$,'o');
873 | term POWOP term /* $x ** $y */
874 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
875 TOKEN_GETMAD($2,$$,'o');
877 | term MULOP term /* $x * $y, $x x $y */
878 { if (IVAL($2) != OP_REPEAT)
880 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
881 TOKEN_GETMAD($2,$$,'o');
883 | term ADDOP term /* $x + $y */
884 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
885 TOKEN_GETMAD($2,$$,'o');
887 | term SHIFTOP term /* $x >> $y, $x << $y */
888 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
889 TOKEN_GETMAD($2,$$,'o');
891 | term RELOP term /* $x > $y, etc. */
892 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
893 TOKEN_GETMAD($2,$$,'o');
895 | term EQOP term /* $x == $y, $x eq $y */
896 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
897 TOKEN_GETMAD($2,$$,'o');
899 | term BITANDOP term /* $x & $y */
900 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
901 TOKEN_GETMAD($2,$$,'o');
903 | term BITOROP term /* $x | $y */
904 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
905 TOKEN_GETMAD($2,$$,'o');
907 | term DOTDOT term /* $x..$y, $x...$y */
909 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
913 op = (UNOP*)op->op_first; /* get to flop */
914 op = (UNOP*)op->op_first; /* get to flip */
915 op = (UNOP*)op->op_first; /* get to range */
916 token_getmad($2,(OP*)op,'o');
919 | term ANDAND term /* $x && $y */
920 { $$ = newLOGOP(OP_AND, 0, $1, $3);
921 TOKEN_GETMAD($2,$$,'o');
923 | term OROR term /* $x || $y */
924 { $$ = newLOGOP(OP_OR, 0, $1, $3);
925 TOKEN_GETMAD($2,$$,'o');
927 | term DORDOR term /* $x // $y */
928 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
929 TOKEN_GETMAD($2,$$,'o');
931 | term MATCHOP term /* $x =~ /$y/ */
932 { $$ = bind_match(IVAL($2), $1, $3);
934 ($$->op_type == OP_NOT
935 ? ((UNOP*)$$)->op_first : $$),
940 /* Unary operators and terms */
941 termunop : '-' term %prec UMINUS /* -$x */
942 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
943 TOKEN_GETMAD($1,$$,'o');
945 | '+' term %prec UMINUS /* +$x */
947 newUNOP(OP_NULL, 0, $2),
950 TOKEN_GETMAD($1,$$,'+');
953 { $$ = newUNOP(OP_NOT, 0, scalar($2));
954 TOKEN_GETMAD($1,$$,'o');
957 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
958 TOKEN_GETMAD($1,$$,'o');
960 | term POSTINC /* $x++ */
961 { $$ = newUNOP(OP_POSTINC, 0,
962 mod(scalar($1), OP_POSTINC));
963 TOKEN_GETMAD($2,$$,'o');
965 | term POSTDEC /* $x-- */
966 { $$ = newUNOP(OP_POSTDEC, 0,
967 mod(scalar($1), OP_POSTDEC));
968 TOKEN_GETMAD($2,$$,'o');
970 | PREINC term /* ++$x */
971 { $$ = newUNOP(OP_PREINC, 0,
972 mod(scalar($2), OP_PREINC));
973 TOKEN_GETMAD($1,$$,'o');
975 | PREDEC term /* --$x */
976 { $$ = newUNOP(OP_PREDEC, 0,
977 mod(scalar($2), OP_PREDEC));
978 TOKEN_GETMAD($1,$$,'o');
983 /* Constructors for anonymous data */
984 anonymous: '[' expr ']'
985 { $$ = newANONLIST($2);
986 TOKEN_GETMAD($1,$$,'[');
987 TOKEN_GETMAD($3,$$,']');
990 { $$ = newANONLIST((OP*)NULL);
991 TOKEN_GETMAD($1,$$,'[');
992 TOKEN_GETMAD($2,$$,']');
994 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
995 { $$ = newANONHASH($2);
996 TOKEN_GETMAD($1,$$,'{');
997 TOKEN_GETMAD($3,$$,';');
998 TOKEN_GETMAD($4,$$,'}');
1000 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1001 { $$ = newANONHASH((OP*)NULL);
1002 TOKEN_GETMAD($1,$$,'{');
1003 TOKEN_GETMAD($2,$$,';');
1004 TOKEN_GETMAD($3,$$,'}');
1006 | ANONSUB startanonsub proto subattrlist block %prec '('
1007 { SvREFCNT_inc_simple_void(PL_compcv);
1008 $$ = newANONATTRSUB($2, $3, $4, $5);
1009 TOKEN_GETMAD($1,$$,'o');
1010 OP_GETMAD($3,$$,'s');
1011 OP_GETMAD($4,$$,'a');
1016 /* Things called with "do" */
1017 termdo : DO term %prec UNIOP /* do $filename */
1018 { $$ = dofile($2, IVAL($1));
1019 TOKEN_GETMAD($1,$$,'o');
1021 | DO block %prec '(' /* do { code */
1022 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1023 TOKEN_GETMAD($1,$$,'D');
1025 | DO WORD '(' ')' /* do somesub() */
1026 { $$ = newUNOP(OP_ENTERSUB,
1027 OPf_SPECIAL|OPf_STACKED,
1028 prepend_elem(OP_LIST,
1030 (OPpENTERSUB_AMPER<<8),
1032 )),(OP*)NULL)); dep();
1033 TOKEN_GETMAD($1,$$,'o');
1034 TOKEN_GETMAD($3,$$,'(');
1035 TOKEN_GETMAD($4,$$,')');
1037 | DO WORD '(' expr ')' /* do somesub(@args) */
1038 { $$ = newUNOP(OP_ENTERSUB,
1039 OPf_SPECIAL|OPf_STACKED,
1040 append_elem(OP_LIST,
1043 (OPpENTERSUB_AMPER<<8),
1046 TOKEN_GETMAD($1,$$,'o');
1047 TOKEN_GETMAD($3,$$,'(');
1048 TOKEN_GETMAD($5,$$,')');
1050 | DO scalar '(' ')' /* do $subref () */
1051 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1052 prepend_elem(OP_LIST,
1053 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1054 TOKEN_GETMAD($1,$$,'o');
1055 TOKEN_GETMAD($3,$$,'(');
1056 TOKEN_GETMAD($4,$$,')');
1058 | DO scalar '(' expr ')' /* do $subref (@args) */
1059 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1060 prepend_elem(OP_LIST,
1062 scalar(newCVREF(0,scalar($2))))); dep();
1063 TOKEN_GETMAD($1,$$,'o');
1064 TOKEN_GETMAD($3,$$,'(');
1065 TOKEN_GETMAD($5,$$,')');
1074 | term '?' term ':' term
1075 { $$ = newCONDOP(0, $1, $3, $5);
1076 TOKEN_GETMAD($2,$$,'?');
1077 TOKEN_GETMAD($4,$$,':');
1079 | REFGEN term /* \$x, \@y, \%z */
1080 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1081 TOKEN_GETMAD($1,$$,'o');
1083 | myattrterm %prec UNIOP
1085 | LOCAL term %prec UNIOP
1086 { $$ = localize($2,IVAL($1));
1087 TOKEN_GETMAD($1,$$,'k');
1090 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1091 TOKEN_GETMAD($1,$$,'(');
1092 TOKEN_GETMAD($3,$$,')');
1095 { $$ = sawparens(newNULLLIST());
1096 TOKEN_GETMAD($1,$$,'(');
1097 TOKEN_GETMAD($2,$$,')');
1107 | arylen %prec '(' /* $#x, $#{ something } */
1108 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1111 | ary '[' expr ']' /* array slice */
1112 { $$ = prepend_elem(OP_ASLICE,
1113 newOP(OP_PUSHMARK, 0),
1114 newLISTOP(OP_ASLICE, 0,
1116 ref($1, OP_ASLICE)));
1117 TOKEN_GETMAD($2,$$,'[');
1118 TOKEN_GETMAD($4,$$,']');
1120 | ary '{' expr ';' '}' /* @hash{@keys} */
1121 { $$ = prepend_elem(OP_HSLICE,
1122 newOP(OP_PUSHMARK, 0),
1123 newLISTOP(OP_HSLICE, 0,
1125 ref(oopsHV($1), OP_HSLICE)));
1126 PL_parser->expect = XOPERATOR;
1127 TOKEN_GETMAD($2,$$,'{');
1128 TOKEN_GETMAD($4,$$,';');
1129 TOKEN_GETMAD($5,$$,'}');
1134 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1135 | amper '(' ')' /* &foo() */
1136 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1137 TOKEN_GETMAD($2,$$,'(');
1138 TOKEN_GETMAD($3,$$,')');
1140 | amper '(' expr ')' /* &foo(@args) */
1142 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1143 append_elem(OP_LIST, $3, scalar($1)));
1146 if (op->op_type == OP_CONST) { /* defeat const fold */
1147 op = (OP*)op->op_madprop->mad_val;
1149 token_getmad($2,op,'(');
1150 token_getmad($4,op,')');
1153 | NOAMP WORD listexpr /* foo(@args) */
1154 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1155 append_elem(OP_LIST, $3, scalar($2)));
1156 TOKEN_GETMAD($1,$$,'o');
1158 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1159 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1160 PL_hints |= HINT_BLOCK_SCOPE;
1161 TOKEN_GETMAD($1,$$,'o');
1164 { $$ = newLOOPEX(IVAL($1),$2);
1165 TOKEN_GETMAD($1,$$,'o');
1167 | NOTOP argexpr /* not $foo */
1168 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1169 TOKEN_GETMAD($1,$$,'o');
1171 | UNIOP /* Unary op, $_ implied */
1172 { $$ = newOP(IVAL($1), 0);
1173 TOKEN_GETMAD($1,$$,'o');
1175 | UNIOP block /* eval { foo }* */
1176 { $$ = newUNOP(IVAL($1), 0, $2);
1177 TOKEN_GETMAD($1,$$,'o');
1179 | UNIOP term /* Unary op */
1180 { $$ = newUNOP(IVAL($1), 0, $2);
1181 TOKEN_GETMAD($1,$$,'o');
1183 | REQUIRE /* require, $_ implied */
1184 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1185 TOKEN_GETMAD($1,$$,'o');
1187 | REQUIRE term /* require Foo */
1188 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1189 TOKEN_GETMAD($1,$$,'o');
1192 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1193 | UNIOPSUB term /* Sub treated as unop */
1194 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1195 append_elem(OP_LIST, $2, scalar($1))); }
1196 | FUNC0 /* Nullary operator */
1197 { $$ = newOP(IVAL($1), 0);
1198 TOKEN_GETMAD($1,$$,'o');
1201 { $$ = newOP(IVAL($1), 0);
1202 TOKEN_GETMAD($1,$$,'o');
1203 TOKEN_GETMAD($2,$$,'(');
1204 TOKEN_GETMAD($3,$$,')');
1206 | FUNC0SUB /* Sub treated as nullop */
1207 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1209 | FUNC1 '(' ')' /* not () */
1210 { $$ = (IVAL($1) == OP_NOT)
1211 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1212 : newOP(IVAL($1), OPf_SPECIAL);
1214 TOKEN_GETMAD($1,$$,'o');
1215 TOKEN_GETMAD($2,$$,'(');
1216 TOKEN_GETMAD($3,$$,')');
1218 | FUNC1 '(' expr ')' /* not($foo) */
1219 { $$ = newUNOP(IVAL($1), 0, $3);
1220 TOKEN_GETMAD($1,$$,'o');
1221 TOKEN_GETMAD($2,$$,'(');
1222 TOKEN_GETMAD($4,$$,')');
1224 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1225 { $$ = pmruntime($1, $3, 1);
1226 TOKEN_GETMAD($2,$$,'(');
1227 TOKEN_GETMAD($4,$$,')');
1233 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1234 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1238 /* "my" declarations, with optional attributes */
1239 myattrterm: MY myterm myattrlist
1240 { $$ = my_attrs($2,$3);
1242 token_getmad($1,$$,'d');
1243 append_madprops($3->op_madprop, $$, 'a');
1248 { $$ = localize($2,IVAL($1));
1249 TOKEN_GETMAD($1,$$,'d');
1253 /* Things that can be "my"'d */
1254 myterm : '(' expr ')'
1255 { $$ = sawparens($2);
1256 TOKEN_GETMAD($1,$$,'(');
1257 TOKEN_GETMAD($3,$$,')');
1260 { $$ = sawparens(newNULLLIST());
1261 TOKEN_GETMAD($1,$$,'(');
1262 TOKEN_GETMAD($2,$$,')');
1272 /* Basic list expressions */
1273 listexpr: /* NULL */ %prec PREC_LOW
1275 | argexpr %prec PREC_LOW
1279 listexprcom: /* NULL */
1286 OP* op = newNULLLIST();
1287 token_getmad($2,op,',');
1288 $$ = append_elem(OP_LIST, $1, op);
1296 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1299 { PL_parser->in_my = 0; $$ = my($1); }
1303 { $$ = newCVREF(IVAL($1),$2);
1304 TOKEN_GETMAD($1,$$,'&');
1308 scalar : '$' indirob
1309 { $$ = newSVREF($2);
1310 TOKEN_GETMAD($1,$$,'$');
1315 { $$ = newAVREF($2);
1316 TOKEN_GETMAD($1,$$,'@');
1321 { $$ = newHVREF($2);
1322 TOKEN_GETMAD($1,$$,'%');
1326 arylen : DOLSHARP indirob
1327 { $$ = newAVREF($2);
1328 TOKEN_GETMAD($1,$$,'l');
1333 { $$ = newGVREF(0,$2);
1334 TOKEN_GETMAD($1,$$,'*');
1338 /* Indirect objects */
1340 { $$ = scalar($1); }
1341 | scalar %prec PREC_LOW
1342 { $$ = scalar($1); }