3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
4 * Copyright (c) 2007, 2008, 2009, 2010, 2011 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 * The main job of of this grammar is to call the various newFOO()
24 * functions in op.c to build a syntax tree of OP structs.
25 * It relies on the lexer in toke.c to do the tokenizing.
27 * Note: due to the way that the cleanup code works WRT to freeing ops on
28 * the parse stack, it is dangerous to assign to the $n variables within
32 /* Make the parser re-entrant. */
39 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
40 must always be 1st union member) */
46 %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
48 %token <ival> '{' '}' '[' ']' '-' '+' '@' '%' '&' '=' '.'
50 %token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
51 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
52 %token <opval> PLUGEXPR PLUGSTMT
54 %token <ival> FORMAT SUB SIGSUB ANONSUB ANON_SIGSUB PACKAGE USE
55 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
56 %token <ival> GIVEN WHEN DEFAULT
57 %token <ival> LOOPEX DOTDOT YADAYADA
58 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
59 %token <ival> RELOP EQOP MULOP ADDOP
60 %token <ival> DOLSHARP DO HASHBRACK NOAMP
61 %token <ival> LOCAL MY REQUIRE
62 %token <ival> COLONATTR FORMLBRACK FORMRBRACK
63 %token <ival> SUBLEXSTART SUBLEXEND
65 %type <ival> grammar remember mremember
66 %type <ival> startsub startanonsub startformsub
70 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
71 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
72 %type <opval> sliceme kvslice gelem
73 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
74 %type <opval> optlistexpr optexpr optrepl indirob listop method
75 %type <opval> formname subname proto cont my_scalar my_var
76 %type <opval> refgen_topic formblock
77 %type <opval> subattrlist myattrlist myattrterm myterm
78 %type <opval> termbinop termunop anonymous termdo
79 %type <ival> sigslurpsigil
80 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
81 %type <opval> sigelem siglist siglistornull subsignature optsubsignature
82 %type <opval> subbody optsubbody sigsubbody optsigsubbody
83 %type <opval> formstmtseq formline formarg
85 %nonassoc <ival> PREC_LOW
88 %left <ival> OROP DOROP
91 %nonassoc LSTOP LSTOPSUB
93 %right <ival> ASSIGNOP
96 %left <ival> OROR DORDOR
102 %nonassoc UNIOP UNIOPSUB
108 %right <ival> '!' '~' UMINUS REFGEN
110 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
118 /* Top-level choice of what kind of thing yyparse was called to parse */
121 parser->expect = XSTATE;
126 newPROG(block_end($3,$4));
127 PL_compiling.cop_seq = 0;
132 parser->expect = XTERM;
142 parser->expect = XBLOCK;
147 PL_pad_reset_pending = TRUE;
151 parser->yychar = yytoken = YYEOF;
155 parser->expect = XSTATE;
160 PL_pad_reset_pending = TRUE;
164 parser->yychar = yytoken = YYEOF;
168 parser->expect = XSTATE;
173 PL_pad_reset_pending = TRUE;
177 parser->yychar = yytoken = YYEOF;
181 parser->expect = XSTATE;
191 /* An ordinary block */
192 block : '{' remember stmtseq '}'
193 { if (parser->copline > (line_t)$1)
194 parser->copline = (line_t)$1;
195 $$ = block_end($2, $3);
200 formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
201 { if (parser->copline > (line_t)$1)
202 parser->copline = (line_t)$1;
203 $$ = block_end($2, $5);
207 remember: /* NULL */ /* start a full lexical scope */
208 { $$ = block_start(TRUE);
209 parser->parsed_sub = 0; }
212 mblock : '{' mremember stmtseq '}'
213 { if (parser->copline > (line_t)$1)
214 parser->copline = (line_t)$1;
215 $$ = block_end($2, $3);
219 mremember: /* NULL */ /* start a partial lexical scope */
220 { $$ = block_start(FALSE);
221 parser->parsed_sub = 0; }
224 /* A sequence of statements in the program */
228 { $$ = op_append_list(OP_LINESEQ, $1, $2);
229 PL_pad_reset_pending = TRUE;
231 PL_hints |= HINT_BLOCK_SCOPE;
235 /* A sequence of format lines */
236 formstmtseq: /* NULL */
238 | formstmtseq formline
239 { $$ = op_append_list(OP_LINESEQ, $1, $2);
240 PL_pad_reset_pending = TRUE;
242 PL_hints |= HINT_BLOCK_SCOPE;
246 /* A statement in the program, including optional labels */
249 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
255 labfullstmt: LABEL barestmt
257 SV *label = cSVOPx_sv($1);
258 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
259 savepv(SvPVX_const(label)), $2);
264 SV *label = cSVOPx_sv($1);
265 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
266 savepv(SvPVX_const(label)), $2);
271 /* A bare statement, lacking label and other aspects of state op */
274 | FORMAT startformsub formname formblock
276 CV *fmtcv = PL_compcv;
279 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
280 pad_add_weakref(fmtcv);
282 parser->parsed_sub = 1;
284 | SUB subname startsub
285 /* sub declaration or definition not within scope
286 of 'use feature "signatures"'*/
288 init_named_cv(PL_compcv, $2);
290 parser->in_my_stash = NULL;
292 proto subattrlist optsubbody
294 SvREFCNT_inc_simple_void(PL_compcv);
295 $2->op_type == OP_CONST
296 ? newATTRSUB($3, $2, $5, $6, $7)
297 : newMYSUB($3, $2, $5, $6, $7)
301 parser->parsed_sub = 1;
303 | SIGSUB subname startsub
304 /* sub declaration or definition under 'use feature
305 * "signatures"'. (Note that a signature isn't
306 * allowed in a declaration)
309 init_named_cv(PL_compcv, $2);
311 parser->in_my_stash = NULL;
313 subattrlist optsigsubbody
315 SvREFCNT_inc_simple_void(PL_compcv);
316 $2->op_type == OP_CONST
317 ? newATTRSUB($3, $2, NULL, $5, $6)
318 : newMYSUB( $3, $2, NULL, $5, $6)
322 parser->parsed_sub = 1;
324 | PACKAGE BAREWORD BAREWORD ';'
332 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
333 BAREWORD BAREWORD optlistexpr ';'
335 SvREFCNT_inc_simple_void(PL_compcv);
336 utilize($1, $2, $4, $5, $6);
337 parser->parsed_sub = 1;
340 | IF '(' remember mexpr ')' mblock else
343 newCONDOP(0, $4, op_scope($6), $7));
344 parser->copline = (line_t)$1;
346 | UNLESS '(' remember mexpr ')' mblock else
349 newCONDOP(0, $4, $7, op_scope($6)));
350 parser->copline = (line_t)$1;
352 | GIVEN '(' remember mexpr ')' mblock
354 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
355 parser->copline = (line_t)$1;
357 | WHEN '(' remember mexpr ')' mblock
358 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
360 { $$ = newWHENOP(0, op_scope($2)); }
361 | WHILE '(' remember texpr ')' mintro mblock cont
364 newWHILEOP(0, 1, NULL,
366 parser->copline = (line_t)$1;
368 | UNTIL '(' remember iexpr ')' mintro mblock cont
371 newWHILEOP(0, 1, NULL,
373 parser->copline = (line_t)$1;
375 | FOR '(' remember mnexpr ';'
376 { parser->expect = XTERM; }
378 { parser->expect = XTERM; }
383 OP *forop = newWHILEOP(0, 1, NULL,
384 scalar($7), $13, $11, $10);
386 forop = op_prepend_elem(OP_LINESEQ, initop,
387 op_append_elem(OP_LINESEQ,
388 newOP(OP_UNSTACK, OPf_SPECIAL),
391 PL_hints |= HINT_BLOCK_SCOPE;
392 $$ = block_end($3, forop);
393 parser->copline = (line_t)$1;
395 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
397 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
398 parser->copline = (line_t)$1;
400 | FOR scalar '(' remember mexpr ')' mblock cont
402 $$ = block_end($4, newFOROP(0,
403 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
404 parser->copline = (line_t)$1;
406 | FOR my_refgen remember my_var
407 { parser->in_my = 0; $<opval>$ = my($4); }
408 '(' mexpr ')' mblock cont
414 newUNOP(OP_REFGEN, 0,
419 parser->copline = (line_t)$1;
421 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
423 $$ = block_end($5, newFOROP(
424 0, op_lvalue(newUNOP(OP_REFGEN, 0,
426 OP_ENTERLOOP), $6, $8, $9));
427 parser->copline = (line_t)$1;
429 | FOR '(' remember mexpr ')' mblock cont
432 newFOROP(0, NULL, $4, $6, $7));
433 parser->copline = (line_t)$1;
437 /* a block is a loop that happens once */
438 $$ = newWHILEOP(0, 1, NULL,
441 | PACKAGE BAREWORD BAREWORD '{' remember
450 /* a block is a loop that happens once */
451 $$ = newWHILEOP(0, 1, NULL,
452 NULL, block_end($5, $7), NULL, 0);
453 if (parser->copline > (line_t)$4)
454 parser->copline = (line_t)$4;
462 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
463 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
468 parser->copline = NOLINE;
473 formline: THING formarg
477 list = op_append_elem(OP_LIST, $1, term);
482 if (parser->copline == NOLINE)
483 parser->copline = CopLINE(PL_curcop)-1;
484 else parser->copline--;
485 $$ = newSTATEOP(0, NULL,
486 op_convert_list(OP_FORMLINE, 0, list));
492 | FORMLBRACK stmtseq FORMRBRACK
493 { $$ = op_unscope($2); }
496 /* An expression which may have a side-effect */
502 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
504 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
506 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
508 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
510 { $$ = newFOROP(0, NULL, $3, $1, NULL);
511 parser->copline = (line_t)$2; }
513 { $$ = newWHENOP($3, op_scope($1)); }
516 /* else and elsif blocks */
521 ($2)->op_flags |= OPf_PARENS;
524 | ELSIF '(' mexpr ')' mblock else
525 { parser->copline = (line_t)$1;
527 newSTATEOP(OPf_SPECIAL,NULL,$3),
529 PL_hints |= HINT_BLOCK_SCOPE;
533 /* Continue blocks */
537 { $$ = op_scope($2); }
540 /* determine whether there are any new my declarations */
542 { $$ = (PL_min_intro_pending &&
543 PL_max_intro_pending >= PL_min_intro_pending);
546 /* Normal expression */
552 /* Boolean expression */
553 texpr : /* NULL means true */
555 (void)scan_num("1", &tmplval);
556 $$ = tmplval.opval; }
560 /* Inverted boolean expression */
562 { $$ = invert(scalar($1)); }
565 /* Expression with its own lexical scope */
567 { $$ = $1; intro_my(); }
571 { $$ = $1; intro_my(); }
574 formname: BAREWORD { $$ = $1; }
575 | /* NULL */ { $$ = NULL; }
578 startsub: /* NULL */ /* start a regular subroutine scope */
579 { $$ = start_subparse(FALSE, 0);
580 SAVEFREESV(PL_compcv); }
584 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
585 { $$ = start_subparse(FALSE, CVf_ANON);
586 SAVEFREESV(PL_compcv); }
589 startformsub: /* NULL */ /* start a format subroutine scope */
590 { $$ = start_subparse(TRUE, 0);
591 SAVEFREESV(PL_compcv); }
594 /* Name of a subroutine - must be a bareword, could be special */
599 /* Subroutine prototype */
605 /* Optional list of subroutine attributes */
606 subattrlist: /* NULL */
614 /* List of attributes for a "my" variable declaration */
615 myattrlist: COLONATTR THING
623 /* --------------------------------------
624 * subroutine signature parsing
627 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
628 sigvarname: /* NULL */
629 { parser->in_my = 0; $$ = NULL; }
631 { parser->in_my = 0; $$ = $1; }
640 /* @, %, @foo, %foo */
641 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
647 if (parser->sig_slurpy)
648 yyerror("Multiple slurpy parameters not allowed");
649 parser->sig_slurpy = (char)sigil;
652 yyerror("A slurpy parameter may not have "
655 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
659 /* default part of sub signature scalar element: i.e. '= default_expr' */
660 sigdefault: /* NULL */
663 { $$ = newOP(OP_NULL, 0); }
668 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
670 '$' sigvarname sigdefault
675 if (parser->sig_slurpy)
676 yyerror("Slurpy parameter not last");
681 parser->sig_optelems++;
683 if ( defexpr->op_type == OP_NULL
684 && !(defexpr->op_flags & OPf_KIDS))
686 /* handle '$=' special case */
688 yyerror("Optional parameter "
689 "lacks default expression");
693 /* a normal '=default' expression */
694 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
697 /* re-purpose op_targ to hold @_ index */
699 (PADOFFSET)(parser->sig_elems - 1);
702 var->op_flags |= OPf_STACKED;
703 (void)op_sibling_splice(var,
708 var = newUNOP(OP_NULL, 0, defop);
711 /* NB: normally the first child of a
712 * logop is executed before the logop,
713 * and it pushes a boolean result
714 * ready for the logop. For ARGDEFELEM,
715 * the op itself does the boolean
716 * calculation, so set the first op to
719 var->op_next = defop;
720 defexpr->op_next = var;
724 if (parser->sig_optelems)
725 yyerror("Mandatory parameter "
726 "follows optional parameter");
729 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
734 /* subroutine signature element: e.g. '$x = $default' or '%h' */
735 sigelem: sigscalarelem
736 { parser->in_my = KEY_sigvar; $$ = $1; }
738 { parser->in_my = KEY_sigvar; $$ = $1; }
741 /* list of subroutine signature elements */
745 | siglist ',' sigelem
747 $$ = op_append_list(OP_LINESEQ, $1, $3);
749 | sigelem %prec PREC_LOW
754 siglistornull: /* NULL */
759 /* optional subroutine signature */
760 optsubsignature: /* NULL */
765 /* Subroutine signature */
769 SAVEIV(parser->sig_elems);
770 SAVEIV(parser->sig_optelems);
771 SAVEI8(parser->sig_slurpy);
772 parser->sig_elems = 0;
773 parser->sig_optelems = 0;
774 parser->sig_slurpy = 0;
775 parser->in_my = KEY_sigvar;
784 if (!FEATURE_SIGNATURES_IS_ENABLED)
785 Perl_croak(aTHX_ "Experimental "
786 "subroutine signatures not enabled");
788 /* We shouldn't get here otherwise */
789 Perl_ck_warner_d(aTHX_
790 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
791 "The signatures feature is experimental");
793 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
794 sizeof(UNOP_AUX_item) * 3);
795 aux[0].iv = parser->sig_elems;
796 aux[1].iv = parser->sig_optelems;
797 aux[2].iv = parser->sig_slurpy;
798 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
799 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
800 sigops = op_prepend_elem(OP_LINESEQ,
801 newSTATEOP(0, NULL, NULL),
803 /* a nextstate at the end handles context
804 * correctly for an empty sub body */
805 $$ = op_append_elem(OP_LINESEQ,
807 newSTATEOP(0, NULL, NULL));
810 /* tell the toker that attrributes can follow
811 * this sig, but only so that the toker
812 * can skip through any (illegal) trailing
813 * attribute text then give a useful error
814 * message about "attributes before sig",
815 * rather than falling over ina mess at
816 * unrecognised syntax.
818 parser->expect = XATTRBLOCK;
819 parser->sig_seen = TRUE;
824 /* Optional subroutine body (for named subroutine declaration) */
825 optsubbody: subbody { $$ = $1; }
830 /* Subroutine body (without signature) */
831 subbody: remember '{' stmtseq '}'
833 if (parser->copline > (line_t)$2)
834 parser->copline = (line_t)$2;
835 $$ = block_end($1, $3);
840 /* optional [ Subroutine body with optional signature ] (for named
841 * subroutine declaration) */
842 optsigsubbody: sigsubbody { $$ = $1; }
845 /* Subroutine body with optional signature */
846 sigsubbody: remember optsubsignature '{' stmtseq '}'
848 if (parser->copline > (line_t)$3)
849 parser->copline = (line_t)$3;
851 op_append_list(OP_LINESEQ, $2, $4));
856 /* Ordinary expressions; logical combinations */
857 expr : expr ANDOP expr
858 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
860 { $$ = newLOGOP($2, 0, $1, $3); }
862 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
863 | listexpr %prec PREC_LOW
866 /* Expressions are a list of terms joined by commas */
867 listexpr: listexpr ','
872 $$ = op_append_elem(OP_LIST, $1, term);
874 | term %prec PREC_LOW
878 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
879 { $$ = op_convert_list($1, OPf_STACKED,
880 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
882 | FUNC '(' indirob expr ')' /* print ($fh @args */
883 { $$ = op_convert_list($1, OPf_STACKED,
884 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
886 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
887 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
888 op_append_elem(OP_LIST,
889 op_prepend_elem(OP_LIST, scalar($1), $5),
890 newMETHOP(OP_METHOD, 0, $3)));
892 | term ARROW method /* $foo->bar */
893 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
894 op_append_elem(OP_LIST, scalar($1),
895 newMETHOP(OP_METHOD, 0, $3)));
897 | METHOD indirob optlistexpr /* new Class @args */
898 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
899 op_append_elem(OP_LIST,
900 op_prepend_elem(OP_LIST, $2, $3),
901 newMETHOP(OP_METHOD, 0, $1)));
903 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
904 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
905 op_append_elem(OP_LIST,
906 op_prepend_elem(OP_LIST, $2, $4),
907 newMETHOP(OP_METHOD, 0, $1)));
909 | LSTOP optlistexpr /* print @args */
910 { $$ = op_convert_list($1, 0, $2); }
911 | FUNC '(' optexpr ')' /* print (@args) */
912 { $$ = op_convert_list($1, 0, $3); }
913 | FUNC SUBLEXSTART optexpr SUBLEXEND /* uc($arg) from "\U..." */
914 { $$ = op_convert_list($1, 0, $3); }
915 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
916 { SvREFCNT_inc_simple_void(PL_compcv);
917 $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
918 optlistexpr %prec LSTOP /* ... @bar */
919 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
920 op_append_elem(OP_LIST,
921 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
925 /* Names of methods. May use $object->$methodname */
930 /* Some kind of subscripted expression */
931 subscripted: gelem '{' expr ';' '}' /* *main::{something} */
932 /* In this and all the hash accessors, ';' is
933 * provided by the tokeniser */
934 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
935 | scalar '[' expr ']' /* $array[$element] */
936 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
938 | term ARROW '[' expr ']' /* somearef->[$element] */
939 { $$ = newBINOP(OP_AELEM, 0,
940 ref(newAVREF($1),OP_RV2AV),
943 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
944 { $$ = newBINOP(OP_AELEM, 0,
945 ref(newAVREF($1),OP_RV2AV),
948 | scalar '{' expr ';' '}' /* $foo{bar();} */
949 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
951 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
952 { $$ = newBINOP(OP_HELEM, 0,
953 ref(newHVREF($1),OP_RV2HV),
955 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
956 { $$ = newBINOP(OP_HELEM, 0,
957 ref(newHVREF($1),OP_RV2HV),
959 | term ARROW '(' ')' /* $subref->() */
960 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
961 newCVREF(0, scalar($1)));
962 if (parser->expect == XBLOCK)
963 parser->expect = XOPERATOR;
965 | term ARROW '(' expr ')' /* $subref->(@args) */
966 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
967 op_append_elem(OP_LIST, $4,
968 newCVREF(0, scalar($1))));
969 if (parser->expect == XBLOCK)
970 parser->expect = XOPERATOR;
973 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
974 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
975 op_append_elem(OP_LIST, $3,
976 newCVREF(0, scalar($1))));
977 if (parser->expect == XBLOCK)
978 parser->expect = XOPERATOR;
980 | subscripted '(' ')' /* $foo->{bar}->() */
981 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
982 newCVREF(0, scalar($1)));
983 if (parser->expect == XBLOCK)
984 parser->expect = XOPERATOR;
986 | '(' expr ')' '[' expr ']' /* list slice */
987 { $$ = newSLICEOP(0, $5, $2); }
988 | QWLIST '[' expr ']' /* list literal slice */
989 { $$ = newSLICEOP(0, $3, $1); }
990 | '(' ')' '[' expr ']' /* empty list slice! */
991 { $$ = newSLICEOP(0, $4, NULL); }
994 /* Binary operators between terms */
995 termbinop: term ASSIGNOP term /* $x = $y, $x += $y */
996 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
997 | term POWOP term /* $x ** $y */
998 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
999 | term MULOP term /* $x * $y, $x x $y */
1000 { if ($2 != OP_REPEAT)
1002 $$ = newBINOP($2, 0, $1, scalar($3));
1004 | term ADDOP term /* $x + $y */
1005 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1006 | term SHIFTOP term /* $x >> $y, $x << $y */
1007 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1008 | term RELOP term /* $x > $y, etc. */
1009 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1010 | term EQOP term /* $x == $y, $x eq $y */
1011 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1012 | term BITANDOP term /* $x & $y */
1013 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1014 | term BITOROP term /* $x | $y */
1015 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1016 | term DOTDOT term /* $x..$y, $x...$y */
1017 { $$ = newRANGE($2, scalar($1), scalar($3)); }
1018 | term ANDAND term /* $x && $y */
1019 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
1020 | term OROR term /* $x || $y */
1021 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
1022 | term DORDOR term /* $x // $y */
1023 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
1024 | term MATCHOP term /* $x =~ /$y/ */
1025 { $$ = bind_match($2, $1, $3); }
1028 /* Unary operators and terms */
1029 termunop : '-' term %prec UMINUS /* -$x */
1030 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
1031 | '+' term %prec UMINUS /* +$x */
1034 | '!' term /* !$x */
1035 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1036 | '~' term /* ~$x */
1037 { $$ = newUNOP($1, 0, scalar($2)); }
1038 | term POSTINC /* $x++ */
1039 { $$ = newUNOP(OP_POSTINC, 0,
1040 op_lvalue(scalar($1), OP_POSTINC)); }
1041 | term POSTDEC /* $x-- */
1042 { $$ = newUNOP(OP_POSTDEC, 0,
1043 op_lvalue(scalar($1), OP_POSTDEC));}
1044 | term POSTJOIN /* implicit join after interpolated ->@ */
1045 { $$ = op_convert_list(OP_JOIN, 0,
1055 | PREINC term /* ++$x */
1056 { $$ = newUNOP(OP_PREINC, 0,
1057 op_lvalue(scalar($2), OP_PREINC)); }
1058 | PREDEC term /* --$x */
1059 { $$ = newUNOP(OP_PREDEC, 0,
1060 op_lvalue(scalar($2), OP_PREDEC)); }
1064 /* Constructors for anonymous data */
1065 anonymous: '[' expr ']'
1066 { $$ = newANONLIST($2); }
1068 { $$ = newANONLIST(NULL);}
1069 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1070 { $$ = newANONHASH($2); }
1071 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1072 { $$ = newANONHASH(NULL); }
1073 | ANONSUB startanonsub proto subattrlist subbody %prec '('
1074 { SvREFCNT_inc_simple_void(PL_compcv);
1075 $$ = newANONATTRSUB($2, $3, $4, $5); }
1076 | ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '('
1077 { SvREFCNT_inc_simple_void(PL_compcv);
1078 $$ = newANONATTRSUB($2, NULL, $3, $4); }
1081 /* Things called with "do" */
1082 termdo : DO term %prec UNIOP /* do $filename */
1083 { $$ = dofile($2, $1);}
1084 | DO block %prec '(' /* do { code */
1085 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
1092 | term '?' term ':' term
1093 { $$ = newCONDOP(0, $1, $3, $5); }
1094 | REFGEN term /* \$x, \@y, \%z */
1095 { $$ = newUNOP(OP_REFGEN, 0, $2); }
1097 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
1098 | myattrterm %prec UNIOP
1100 | LOCAL term %prec UNIOP
1101 { $$ = localize($2,0); }
1103 { $$ = sawparens($2); }
1107 { $$ = sawparens(newNULLLIST()); }
1116 | arylen %prec '(' /* $#x, $#{ something } */
1117 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1120 | sliceme '[' expr ']' /* array slice */
1121 { $$ = op_prepend_elem(OP_ASLICE,
1122 newOP(OP_PUSHMARK, 0),
1123 newLISTOP(OP_ASLICE, 0,
1125 ref($1, OP_ASLICE)));
1128 $1->op_private & OPpSLICEWARNING;
1130 | kvslice '[' expr ']' /* array key/value slice */
1131 { $$ = op_prepend_elem(OP_KVASLICE,
1132 newOP(OP_PUSHMARK, 0),
1133 newLISTOP(OP_KVASLICE, 0,
1135 ref(oopsAV($1), OP_KVASLICE)));
1138 $1->op_private & OPpSLICEWARNING;
1140 | sliceme '{' expr ';' '}' /* @hash{@keys} */
1141 { $$ = op_prepend_elem(OP_HSLICE,
1142 newOP(OP_PUSHMARK, 0),
1143 newLISTOP(OP_HSLICE, 0,
1145 ref(oopsHV($1), OP_HSLICE)));
1148 $1->op_private & OPpSLICEWARNING;
1150 | kvslice '{' expr ';' '}' /* %hash{@keys} */
1151 { $$ = op_prepend_elem(OP_KVHSLICE,
1152 newOP(OP_PUSHMARK, 0),
1153 newLISTOP(OP_KVHSLICE, 0,
1155 ref($1, OP_KVHSLICE)));
1158 $1->op_private & OPpSLICEWARNING;
1163 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1164 | amper '(' ')' /* &foo() or foo() */
1165 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1167 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1169 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1170 op_append_elem(OP_LIST, $3, scalar($1)));
1172 | NOAMP subname optlistexpr /* foo @args (no parens) */
1173 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1174 op_append_elem(OP_LIST, $3, scalar($2)));
1176 | term ARROW '$' '*'
1177 { $$ = newSVREF($1); }
1178 | term ARROW '@' '*'
1179 { $$ = newAVREF($1); }
1180 | term ARROW '%' '*'
1181 { $$ = newHVREF($1); }
1182 | term ARROW '&' '*'
1183 { $$ = newUNOP(OP_ENTERSUB, 0,
1184 scalar(newCVREF($3,$1))); }
1185 | term ARROW '*' '*' %prec '('
1186 { $$ = newGVREF(0,$1); }
1187 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1188 { $$ = newOP($1, OPf_SPECIAL);
1189 PL_hints |= HINT_BLOCK_SCOPE; }
1191 { $$ = newLOOPEX($1,$2); }
1192 | NOTOP listexpr /* not $foo */
1193 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1194 | UNIOP /* Unary op, $_ implied */
1195 { $$ = newOP($1, 0); }
1196 | UNIOP block /* eval { foo }* */
1197 { $$ = newUNOP($1, 0, $2); }
1198 | UNIOP term /* Unary op */
1199 { $$ = newUNOP($1, 0, $2); }
1200 | REQUIRE /* require, $_ implied */
1201 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1202 | REQUIRE term /* require Foo */
1203 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1205 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1206 | UNIOPSUB term /* Sub treated as unop */
1207 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1208 op_append_elem(OP_LIST, $2, scalar($1))); }
1209 | FUNC0 /* Nullary operator */
1210 { $$ = newOP($1, 0); }
1212 { $$ = newOP($1, 0);}
1213 | FUNC0OP /* Same as above, but op created in toke.c */
1217 | FUNC0SUB /* Sub treated as nullop */
1218 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1219 | FUNC1 '(' ')' /* not () */
1220 { $$ = ($1 == OP_NOT)
1221 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1222 : newOP($1, OPf_SPECIAL); }
1223 | FUNC1 '(' expr ')' /* not($foo) */
1224 { $$ = newUNOP($1, 0, $3); }
1225 | PMFUNC /* m//, s///, qr//, tr/// */
1227 if ( $1->op_type != OP_TRANS
1228 && $1->op_type != OP_TRANSR
1229 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1231 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1232 SAVEFREESV(PL_compcv);
1236 SUBLEXSTART listexpr optrepl SUBLEXEND
1237 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1243 /* "my" declarations, with optional attributes */
1244 myattrterm: MY myterm myattrlist
1245 { $$ = my_attrs($2,$3); }
1247 { $$ = localize($2,1); }
1248 | MY REFGEN myterm myattrlist
1249 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
1252 /* Things that can be "my"'d */
1253 myterm : '(' expr ')'
1254 { $$ = sawparens($2); }
1256 { $$ = sawparens(newNULLLIST()); }
1266 /* Basic list expressions */
1267 optlistexpr: /* NULL */ %prec PREC_LOW
1269 | listexpr %prec PREC_LOW
1285 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1288 { parser->in_my = 0; $$ = my($1); }
1296 refgen_topic: my_var
1300 my_refgen: MY REFGEN
1305 { $$ = newCVREF($1,$2); }
1308 scalar : '$' indirob
1309 { $$ = newSVREF($2); }
1313 { $$ = newAVREF($2);
1314 if ($$) $$->op_private |= $1;
1319 { $$ = newHVREF($2);
1320 if ($$) $$->op_private |= $1;
1324 arylen : DOLSHARP indirob
1325 { $$ = newAVREF($2); }
1326 | term ARROW DOLSHARP '*'
1327 { $$ = newAVREF($1); }
1331 { $$ = newGVREF(0,$2); }
1336 { $$ = newAVREF($1); }
1341 { $$ = newHVREF($1); }
1346 { $$ = newGVREF(0,$1); }
1349 /* Indirect objects */
1351 { $$ = scalar($1); }
1352 | scalar %prec PREC_LOW
1353 { $$ = scalar($1); }
1355 { $$ = op_scope($1); }