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
64 %type <ival> grammar remember mremember
65 %type <ival> startsub startanonsub startformsub
69 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
70 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
71 %type <opval> sliceme kvslice gelem
72 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
73 %type <opval> optlistexpr optexpr optrepl indirob listop method
74 %type <opval> formname subname proto cont my_scalar my_var
75 %type <opval> refgen_topic formblock
76 %type <opval> subattrlist myattrlist myattrterm myterm
77 %type <opval> termbinop termunop anonymous termdo
78 %type <ival> sigslurpsigil
79 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
80 %type <opval> sigelem siglist siglistornull subsignature optsubsignature
81 %type <opval> subbody optsubbody sigsubbody optsigsubbody
82 %type <opval> formstmtseq formline formarg
84 %nonassoc <ival> PREC_LOW
87 %left <ival> OROP DOROP
90 %nonassoc LSTOP LSTOPSUB
92 %right <ival> ASSIGNOP
95 %left <ival> OROR DORDOR
101 %nonassoc UNIOP UNIOPSUB
107 %right <ival> '!' '~' UMINUS REFGEN
109 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
117 /* Top-level choice of what kind of thing yyparse was called to parse */
120 parser->expect = XSTATE;
125 newPROG(block_end($3,$4));
126 PL_compiling.cop_seq = 0;
131 parser->expect = XTERM;
141 parser->expect = XBLOCK;
146 PL_pad_reset_pending = TRUE;
150 parser->yychar = yytoken = YYEOF;
154 parser->expect = XSTATE;
159 PL_pad_reset_pending = TRUE;
163 parser->yychar = yytoken = YYEOF;
167 parser->expect = XSTATE;
172 PL_pad_reset_pending = TRUE;
176 parser->yychar = yytoken = YYEOF;
180 parser->expect = XSTATE;
190 /* An ordinary block */
191 block : '{' remember stmtseq '}'
192 { if (parser->copline > (line_t)$1)
193 parser->copline = (line_t)$1;
194 $$ = block_end($2, $3);
199 formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
200 { if (parser->copline > (line_t)$1)
201 parser->copline = (line_t)$1;
202 $$ = block_end($2, $5);
206 remember: /* NULL */ /* start a full lexical scope */
207 { $$ = block_start(TRUE);
208 parser->parsed_sub = 0; }
211 mblock : '{' mremember stmtseq '}'
212 { if (parser->copline > (line_t)$1)
213 parser->copline = (line_t)$1;
214 $$ = block_end($2, $3);
218 mremember: /* NULL */ /* start a partial lexical scope */
219 { $$ = block_start(FALSE);
220 parser->parsed_sub = 0; }
223 /* A sequence of statements in the program */
227 { $$ = op_append_list(OP_LINESEQ, $1, $2);
228 PL_pad_reset_pending = TRUE;
230 PL_hints |= HINT_BLOCK_SCOPE;
234 /* A sequence of format lines */
235 formstmtseq: /* NULL */
237 | formstmtseq formline
238 { $$ = op_append_list(OP_LINESEQ, $1, $2);
239 PL_pad_reset_pending = TRUE;
241 PL_hints |= HINT_BLOCK_SCOPE;
245 /* A statement in the program, including optional labels */
248 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
254 labfullstmt: LABEL barestmt
256 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
260 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
264 /* A bare statement, lacking label and other aspects of state op */
267 | FORMAT startformsub formname formblock
269 CV *fmtcv = PL_compcv;
272 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
273 pad_add_weakref(fmtcv);
275 parser->parsed_sub = 1;
277 | SUB subname startsub
278 /* sub declaration or definition not within scope
279 of 'use feature "signatures"'*/
281 init_named_cv(PL_compcv, $2);
283 parser->in_my_stash = NULL;
285 proto subattrlist optsubbody
287 SvREFCNT_inc_simple_void(PL_compcv);
288 $2->op_type == OP_CONST
289 ? newATTRSUB($3, $2, $5, $6, $7)
290 : newMYSUB($3, $2, $5, $6, $7)
294 parser->parsed_sub = 1;
296 | SIGSUB subname startsub
297 /* sub declaration or definition under 'use feature
298 * "signatures"'. (Note that a signature isn't
299 * allowed in a declaration)
302 init_named_cv(PL_compcv, $2);
304 parser->in_my_stash = NULL;
306 subattrlist optsigsubbody
308 SvREFCNT_inc_simple_void(PL_compcv);
309 $2->op_type == OP_CONST
310 ? newATTRSUB($3, $2, NULL, $5, $6)
311 : newMYSUB( $3, $2, NULL, $5, $6)
315 parser->parsed_sub = 1;
317 | PACKAGE BAREWORD BAREWORD ';'
325 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
326 BAREWORD BAREWORD optlistexpr ';'
328 SvREFCNT_inc_simple_void(PL_compcv);
329 utilize($1, $2, $4, $5, $6);
330 parser->parsed_sub = 1;
333 | IF '(' remember mexpr ')' mblock else
336 newCONDOP(0, $4, op_scope($6), $7));
337 parser->copline = (line_t)$1;
339 | UNLESS '(' remember mexpr ')' mblock else
342 newCONDOP(0, $4, $7, op_scope($6)));
343 parser->copline = (line_t)$1;
345 | GIVEN '(' remember mexpr ')' mblock
347 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
348 parser->copline = (line_t)$1;
350 | WHEN '(' remember mexpr ')' mblock
351 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
353 { $$ = newWHENOP(0, op_scope($2)); }
354 | WHILE '(' remember texpr ')' mintro mblock cont
357 newWHILEOP(0, 1, NULL,
359 parser->copline = (line_t)$1;
361 | UNTIL '(' remember iexpr ')' mintro mblock cont
364 newWHILEOP(0, 1, NULL,
366 parser->copline = (line_t)$1;
368 | FOR '(' remember mnexpr ';'
369 { parser->expect = XTERM; }
371 { parser->expect = XTERM; }
376 OP *forop = newWHILEOP(0, 1, NULL,
377 scalar($7), $13, $11, $10);
379 forop = op_prepend_elem(OP_LINESEQ, initop,
380 op_append_elem(OP_LINESEQ,
381 newOP(OP_UNSTACK, OPf_SPECIAL),
384 PL_hints |= HINT_BLOCK_SCOPE;
385 $$ = block_end($3, forop);
386 parser->copline = (line_t)$1;
388 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
390 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
391 parser->copline = (line_t)$1;
393 | FOR scalar '(' remember mexpr ')' mblock cont
395 $$ = block_end($4, newFOROP(0,
396 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
397 parser->copline = (line_t)$1;
399 | FOR my_refgen remember my_var
400 { parser->in_my = 0; $<opval>$ = my($4); }
401 '(' mexpr ')' mblock cont
407 newUNOP(OP_REFGEN, 0,
412 parser->copline = (line_t)$1;
414 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
416 $$ = block_end($5, newFOROP(
417 0, op_lvalue(newUNOP(OP_REFGEN, 0,
419 OP_ENTERLOOP), $6, $8, $9));
420 parser->copline = (line_t)$1;
422 | FOR '(' remember mexpr ')' mblock cont
425 newFOROP(0, NULL, $4, $6, $7));
426 parser->copline = (line_t)$1;
430 /* a block is a loop that happens once */
431 $$ = newWHILEOP(0, 1, NULL,
434 | PACKAGE BAREWORD BAREWORD '{' remember
443 /* a block is a loop that happens once */
444 $$ = newWHILEOP(0, 1, NULL,
445 NULL, block_end($5, $7), NULL, 0);
446 if (parser->copline > (line_t)$4)
447 parser->copline = (line_t)$4;
455 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
456 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
461 parser->copline = NOLINE;
466 formline: THING formarg
470 list = op_append_elem(OP_LIST, $1, term);
475 if (parser->copline == NOLINE)
476 parser->copline = CopLINE(PL_curcop)-1;
477 else parser->copline--;
478 $$ = newSTATEOP(0, NULL,
479 op_convert_list(OP_FORMLINE, 0, list));
485 | FORMLBRACK stmtseq FORMRBRACK
486 { $$ = op_unscope($2); }
489 /* An expression which may have a side-effect */
495 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
497 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
499 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
501 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
503 { $$ = newFOROP(0, NULL, $3, $1, NULL);
504 parser->copline = (line_t)$2; }
506 { $$ = newWHENOP($3, op_scope($1)); }
509 /* else and elsif blocks */
514 ($2)->op_flags |= OPf_PARENS;
517 | ELSIF '(' mexpr ')' mblock else
518 { parser->copline = (line_t)$1;
520 newSTATEOP(OPf_SPECIAL,NULL,$3),
522 PL_hints |= HINT_BLOCK_SCOPE;
526 /* Continue blocks */
530 { $$ = op_scope($2); }
533 /* determine whether there are any new my declarations */
535 { $$ = (PL_min_intro_pending &&
536 PL_max_intro_pending >= PL_min_intro_pending);
539 /* Normal expression */
545 /* Boolean expression */
546 texpr : /* NULL means true */
548 (void)scan_num("1", &tmplval);
549 $$ = tmplval.opval; }
553 /* Inverted boolean expression */
555 { $$ = invert(scalar($1)); }
558 /* Expression with its own lexical scope */
560 { $$ = $1; intro_my(); }
564 { $$ = $1; intro_my(); }
567 formname: BAREWORD { $$ = $1; }
568 | /* NULL */ { $$ = NULL; }
571 startsub: /* NULL */ /* start a regular subroutine scope */
572 { $$ = start_subparse(FALSE, 0);
573 SAVEFREESV(PL_compcv); }
577 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
578 { $$ = start_subparse(FALSE, CVf_ANON);
579 SAVEFREESV(PL_compcv); }
582 startformsub: /* NULL */ /* start a format subroutine scope */
583 { $$ = start_subparse(TRUE, 0);
584 SAVEFREESV(PL_compcv); }
587 /* Name of a subroutine - must be a bareword, could be special */
592 /* Subroutine prototype */
598 /* Optional list of subroutine attributes */
599 subattrlist: /* NULL */
607 /* List of attributes for a "my" variable declaration */
608 myattrlist: COLONATTR THING
616 /* --------------------------------------
617 * subroutine signature parsing
620 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
621 sigvarname: /* NULL */
622 { parser->in_my = 0; $$ = NULL; }
624 { parser->in_my = 0; $$ = $1; }
633 /* @, %, @foo, %foo */
634 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
640 if (parser->sig_slurpy)
641 yyerror("Multiple slurpy parameters not allowed");
642 parser->sig_slurpy = (char)sigil;
645 yyerror("A slurpy parameter may not have "
648 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
652 /* default part of sub signature scalar element: i.e. '= default_expr' */
653 sigdefault: /* NULL */
656 { $$ = newOP(OP_NULL, 0); }
661 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
663 '$' sigvarname sigdefault
668 if (parser->sig_slurpy)
669 yyerror("Slurpy parameter not last");
674 parser->sig_optelems++;
676 if ( defexpr->op_type == OP_NULL
677 && !(defexpr->op_flags & OPf_KIDS))
679 /* handle '$=' special case */
681 yyerror("Optional parameter "
682 "lacks default expression");
686 /* a normal '=default' expression */
687 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
690 /* re-purpose op_targ to hold @_ index */
692 (PADOFFSET)(parser->sig_elems - 1);
695 var->op_flags |= OPf_STACKED;
696 (void)op_sibling_splice(var,
701 var = newUNOP(OP_NULL, 0, defop);
704 /* NB: normally the first child of a
705 * logop is executed before the logop,
706 * and it pushes a boolean result
707 * ready for the logop. For ARGDEFELEM,
708 * the op itself does the boolean
709 * calculation, so set the first op to
712 var->op_next = defop;
713 defexpr->op_next = var;
717 if (parser->sig_optelems)
718 yyerror("Mandatory parameter "
719 "follows optional parameter");
722 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
727 /* subroutine signature element: e.g. '$x = $default' or '%h' */
728 sigelem: sigscalarelem
729 { parser->in_my = KEY_sigvar; $$ = $1; }
731 { parser->in_my = KEY_sigvar; $$ = $1; }
734 /* list of subroutine signature elements */
738 | siglist ',' sigelem
740 $$ = op_append_list(OP_LINESEQ, $1, $3);
742 | sigelem %prec PREC_LOW
747 siglistornull: /* NULL */
752 /* optional subroutine signature */
753 optsubsignature: /* NULL */
758 /* Subroutine signature */
762 SAVEIV(parser->sig_elems);
763 SAVEIV(parser->sig_optelems);
764 SAVEI8(parser->sig_slurpy);
765 parser->sig_elems = 0;
766 parser->sig_optelems = 0;
767 parser->sig_slurpy = 0;
768 parser->in_my = KEY_sigvar;
777 if (!FEATURE_SIGNATURES_IS_ENABLED)
778 Perl_croak(aTHX_ "Experimental "
779 "subroutine signatures not enabled");
781 /* We shouldn't get here otherwise */
782 Perl_ck_warner_d(aTHX_
783 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
784 "The signatures feature is experimental");
786 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
787 sizeof(UNOP_AUX_item) * 3);
788 aux[0].iv = parser->sig_elems;
789 aux[1].iv = parser->sig_optelems;
790 aux[2].iv = parser->sig_slurpy;
791 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
792 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
793 sigops = op_prepend_elem(OP_LINESEQ,
794 newSTATEOP(0, NULL, NULL),
796 /* a nextstate at the end handles context
797 * correctly for an empty sub body */
798 $$ = op_append_elem(OP_LINESEQ,
800 newSTATEOP(0, NULL, NULL));
803 /* tell the toker that attrributes can follow
804 * this sig, but only so that the toker
805 * can skip through any (illegal) trailing
806 * attribute text then give a useful error
807 * message about "attributes before sig",
808 * rather than falling over ina mess at
809 * unrecognised syntax.
811 parser->expect = XATTRBLOCK;
812 parser->sig_seen = TRUE;
817 /* Optional subroutine body (for named subroutine declaration) */
818 optsubbody: subbody { $$ = $1; }
823 /* Subroutine body (without signature) */
824 subbody: remember '{' stmtseq '}'
826 if (parser->copline > (line_t)$2)
827 parser->copline = (line_t)$2;
828 $$ = block_end($1, $3);
833 /* optional [ Subroutine body with optional signature ] (for named
834 * subroutine declaration) */
835 optsigsubbody: sigsubbody { $$ = $1; }
838 /* Subroutine body with optional signature */
839 sigsubbody: remember optsubsignature '{' stmtseq '}'
841 if (parser->copline > (line_t)$3)
842 parser->copline = (line_t)$3;
844 op_append_list(OP_LINESEQ, $2, $4));
849 /* Ordinary expressions; logical combinations */
850 expr : expr ANDOP expr
851 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
853 { $$ = newLOGOP($2, 0, $1, $3); }
855 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
856 | listexpr %prec PREC_LOW
859 /* Expressions are a list of terms joined by commas */
860 listexpr: listexpr ','
865 $$ = op_append_elem(OP_LIST, $1, term);
867 | term %prec PREC_LOW
871 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
872 { $$ = op_convert_list($1, OPf_STACKED,
873 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
875 | FUNC '(' indirob expr ')' /* print ($fh @args */
876 { $$ = op_convert_list($1, OPf_STACKED,
877 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
879 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
880 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
881 op_append_elem(OP_LIST,
882 op_prepend_elem(OP_LIST, scalar($1), $5),
883 newMETHOP(OP_METHOD, 0, $3)));
885 | term ARROW method /* $foo->bar */
886 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
887 op_append_elem(OP_LIST, scalar($1),
888 newMETHOP(OP_METHOD, 0, $3)));
890 | METHOD indirob optlistexpr /* new Class @args */
891 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
892 op_append_elem(OP_LIST,
893 op_prepend_elem(OP_LIST, $2, $3),
894 newMETHOP(OP_METHOD, 0, $1)));
896 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
897 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
898 op_append_elem(OP_LIST,
899 op_prepend_elem(OP_LIST, $2, $4),
900 newMETHOP(OP_METHOD, 0, $1)));
902 | LSTOP optlistexpr /* print @args */
903 { $$ = op_convert_list($1, 0, $2); }
904 | FUNC '(' optexpr ')' /* print (@args) */
905 { $$ = op_convert_list($1, 0, $3); }
906 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
907 { SvREFCNT_inc_simple_void(PL_compcv);
908 $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
909 optlistexpr %prec LSTOP /* ... @bar */
910 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
911 op_append_elem(OP_LIST,
912 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
916 /* Names of methods. May use $object->$methodname */
921 /* Some kind of subscripted expression */
922 subscripted: gelem '{' expr ';' '}' /* *main::{something} */
923 /* In this and all the hash accessors, ';' is
924 * provided by the tokeniser */
925 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
926 | scalar '[' expr ']' /* $array[$element] */
927 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
929 | term ARROW '[' expr ']' /* somearef->[$element] */
930 { $$ = newBINOP(OP_AELEM, 0,
931 ref(newAVREF($1),OP_RV2AV),
934 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
935 { $$ = newBINOP(OP_AELEM, 0,
936 ref(newAVREF($1),OP_RV2AV),
939 | scalar '{' expr ';' '}' /* $foo{bar();} */
940 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
942 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
943 { $$ = newBINOP(OP_HELEM, 0,
944 ref(newHVREF($1),OP_RV2HV),
946 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
947 { $$ = newBINOP(OP_HELEM, 0,
948 ref(newHVREF($1),OP_RV2HV),
950 | term ARROW '(' ')' /* $subref->() */
951 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
952 newCVREF(0, scalar($1)));
953 if (parser->expect == XBLOCK)
954 parser->expect = XOPERATOR;
956 | term ARROW '(' expr ')' /* $subref->(@args) */
957 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
958 op_append_elem(OP_LIST, $4,
959 newCVREF(0, scalar($1))));
960 if (parser->expect == XBLOCK)
961 parser->expect = XOPERATOR;
964 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
965 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
966 op_append_elem(OP_LIST, $3,
967 newCVREF(0, scalar($1))));
968 if (parser->expect == XBLOCK)
969 parser->expect = XOPERATOR;
971 | subscripted '(' ')' /* $foo->{bar}->() */
972 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
973 newCVREF(0, scalar($1)));
974 if (parser->expect == XBLOCK)
975 parser->expect = XOPERATOR;
977 | '(' expr ')' '[' expr ']' /* list slice */
978 { $$ = newSLICEOP(0, $5, $2); }
979 | QWLIST '[' expr ']' /* list literal slice */
980 { $$ = newSLICEOP(0, $3, $1); }
981 | '(' ')' '[' expr ']' /* empty list slice! */
982 { $$ = newSLICEOP(0, $4, NULL); }
985 /* Binary operators between terms */
986 termbinop: term ASSIGNOP term /* $x = $y, $x += $y */
987 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
988 | term POWOP term /* $x ** $y */
989 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
990 | term MULOP term /* $x * $y, $x x $y */
991 { if ($2 != OP_REPEAT)
993 $$ = newBINOP($2, 0, $1, scalar($3));
995 | term ADDOP term /* $x + $y */
996 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
997 | term SHIFTOP term /* $x >> $y, $x << $y */
998 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
999 | term RELOP term /* $x > $y, etc. */
1000 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1001 | term EQOP term /* $x == $y, $x eq $y */
1002 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1003 | term BITANDOP term /* $x & $y */
1004 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1005 | term BITOROP term /* $x | $y */
1006 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1007 | term DOTDOT term /* $x..$y, $x...$y */
1008 { $$ = newRANGE($2, scalar($1), scalar($3)); }
1009 | term ANDAND term /* $x && $y */
1010 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
1011 | term OROR term /* $x || $y */
1012 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
1013 | term DORDOR term /* $x // $y */
1014 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
1015 | term MATCHOP term /* $x =~ /$y/ */
1016 { $$ = bind_match($2, $1, $3); }
1019 /* Unary operators and terms */
1020 termunop : '-' term %prec UMINUS /* -$x */
1021 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
1022 | '+' term %prec UMINUS /* +$x */
1025 | '!' term /* !$x */
1026 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1027 | '~' term /* ~$x */
1028 { $$ = newUNOP($1, 0, scalar($2)); }
1029 | term POSTINC /* $x++ */
1030 { $$ = newUNOP(OP_POSTINC, 0,
1031 op_lvalue(scalar($1), OP_POSTINC)); }
1032 | term POSTDEC /* $x-- */
1033 { $$ = newUNOP(OP_POSTDEC, 0,
1034 op_lvalue(scalar($1), OP_POSTDEC));}
1035 | term POSTJOIN /* implicit join after interpolated ->@ */
1036 { $$ = op_convert_list(OP_JOIN, 0,
1046 | PREINC term /* ++$x */
1047 { $$ = newUNOP(OP_PREINC, 0,
1048 op_lvalue(scalar($2), OP_PREINC)); }
1049 | PREDEC term /* --$x */
1050 { $$ = newUNOP(OP_PREDEC, 0,
1051 op_lvalue(scalar($2), OP_PREDEC)); }
1055 /* Constructors for anonymous data */
1056 anonymous: '[' expr ']'
1057 { $$ = newANONLIST($2); }
1059 { $$ = newANONLIST(NULL);}
1060 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1061 { $$ = newANONHASH($2); }
1062 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1063 { $$ = newANONHASH(NULL); }
1064 | ANONSUB startanonsub proto subattrlist subbody %prec '('
1065 { SvREFCNT_inc_simple_void(PL_compcv);
1066 $$ = newANONATTRSUB($2, $3, $4, $5); }
1067 | ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '('
1068 { SvREFCNT_inc_simple_void(PL_compcv);
1069 $$ = newANONATTRSUB($2, NULL, $3, $4); }
1072 /* Things called with "do" */
1073 termdo : DO term %prec UNIOP /* do $filename */
1074 { $$ = dofile($2, $1);}
1075 | DO block %prec '(' /* do { code */
1076 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
1083 | term '?' term ':' term
1084 { $$ = newCONDOP(0, $1, $3, $5); }
1085 | REFGEN term /* \$x, \@y, \%z */
1086 { $$ = newUNOP(OP_REFGEN, 0, $2); }
1088 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
1089 | myattrterm %prec UNIOP
1091 | LOCAL term %prec UNIOP
1092 { $$ = localize($2,0); }
1094 { $$ = sawparens($2); }
1098 { $$ = sawparens(newNULLLIST()); }
1107 | arylen %prec '(' /* $#x, $#{ something } */
1108 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1111 | sliceme '[' expr ']' /* array slice */
1112 { $$ = op_prepend_elem(OP_ASLICE,
1113 newOP(OP_PUSHMARK, 0),
1114 newLISTOP(OP_ASLICE, 0,
1116 ref($1, OP_ASLICE)));
1119 $1->op_private & OPpSLICEWARNING;
1121 | kvslice '[' expr ']' /* array key/value slice */
1122 { $$ = op_prepend_elem(OP_KVASLICE,
1123 newOP(OP_PUSHMARK, 0),
1124 newLISTOP(OP_KVASLICE, 0,
1126 ref(oopsAV($1), OP_KVASLICE)));
1129 $1->op_private & OPpSLICEWARNING;
1131 | sliceme '{' expr ';' '}' /* @hash{@keys} */
1132 { $$ = op_prepend_elem(OP_HSLICE,
1133 newOP(OP_PUSHMARK, 0),
1134 newLISTOP(OP_HSLICE, 0,
1136 ref(oopsHV($1), OP_HSLICE)));
1139 $1->op_private & OPpSLICEWARNING;
1141 | kvslice '{' expr ';' '}' /* %hash{@keys} */
1142 { $$ = op_prepend_elem(OP_KVHSLICE,
1143 newOP(OP_PUSHMARK, 0),
1144 newLISTOP(OP_KVHSLICE, 0,
1146 ref($1, OP_KVHSLICE)));
1149 $1->op_private & OPpSLICEWARNING;
1154 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1155 | amper '(' ')' /* &foo() or foo() */
1156 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1158 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1160 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1161 op_append_elem(OP_LIST, $3, scalar($1)));
1163 | NOAMP subname optlistexpr /* foo @args (no parens) */
1164 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1165 op_append_elem(OP_LIST, $3, scalar($2)));
1167 | term ARROW '$' '*'
1168 { $$ = newSVREF($1); }
1169 | term ARROW '@' '*'
1170 { $$ = newAVREF($1); }
1171 | term ARROW '%' '*'
1172 { $$ = newHVREF($1); }
1173 | term ARROW '&' '*'
1174 { $$ = newUNOP(OP_ENTERSUB, 0,
1175 scalar(newCVREF($3,$1))); }
1176 | term ARROW '*' '*' %prec '('
1177 { $$ = newGVREF(0,$1); }
1178 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1179 { $$ = newOP($1, OPf_SPECIAL);
1180 PL_hints |= HINT_BLOCK_SCOPE; }
1182 { $$ = newLOOPEX($1,$2); }
1183 | NOTOP listexpr /* not $foo */
1184 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1185 | UNIOP /* Unary op, $_ implied */
1186 { $$ = newOP($1, 0); }
1187 | UNIOP block /* eval { foo }* */
1188 { $$ = newUNOP($1, 0, $2); }
1189 | UNIOP term /* Unary op */
1190 { $$ = newUNOP($1, 0, $2); }
1191 | REQUIRE /* require, $_ implied */
1192 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1193 | REQUIRE term /* require Foo */
1194 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1196 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1197 | UNIOPSUB term /* Sub treated as unop */
1198 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1199 op_append_elem(OP_LIST, $2, scalar($1))); }
1200 | FUNC0 /* Nullary operator */
1201 { $$ = newOP($1, 0); }
1203 { $$ = newOP($1, 0);}
1204 | FUNC0OP /* Same as above, but op created in toke.c */
1208 | FUNC0SUB /* Sub treated as nullop */
1209 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1210 | FUNC1 '(' ')' /* not () */
1211 { $$ = ($1 == OP_NOT)
1212 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1213 : newOP($1, OPf_SPECIAL); }
1214 | FUNC1 '(' expr ')' /* not($foo) */
1215 { $$ = newUNOP($1, 0, $3); }
1216 | PMFUNC /* m//, s///, qr//, tr/// */
1218 if ( $1->op_type != OP_TRANS
1219 && $1->op_type != OP_TRANSR
1220 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1222 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1223 SAVEFREESV(PL_compcv);
1227 '(' listexpr optrepl ')'
1228 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1234 /* "my" declarations, with optional attributes */
1235 myattrterm: MY myterm myattrlist
1236 { $$ = my_attrs($2,$3); }
1238 { $$ = localize($2,1); }
1239 | MY REFGEN myterm myattrlist
1240 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
1243 /* Things that can be "my"'d */
1244 myterm : '(' expr ')'
1245 { $$ = sawparens($2); }
1247 { $$ = sawparens(newNULLLIST()); }
1257 /* Basic list expressions */
1258 optlistexpr: /* NULL */ %prec PREC_LOW
1260 | listexpr %prec PREC_LOW
1276 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1279 { parser->in_my = 0; $$ = my($1); }
1287 refgen_topic: my_var
1291 my_refgen: MY REFGEN
1296 { $$ = newCVREF($1,$2); }
1299 scalar : '$' indirob
1300 { $$ = newSVREF($2); }
1304 { $$ = newAVREF($2);
1305 if ($$) $$->op_private |= $1;
1310 { $$ = newHVREF($2);
1311 if ($$) $$->op_private |= $1;
1315 arylen : DOLSHARP indirob
1316 { $$ = newAVREF($2); }
1317 | term ARROW DOLSHARP '*'
1318 { $$ = newAVREF($1); }
1322 { $$ = newGVREF(0,$2); }
1327 { $$ = newAVREF($1); }
1332 { $$ = newHVREF($1); }
1337 { $$ = newGVREF(0,$1); }
1340 /* Indirect objects */
1342 { $$ = scalar($1); }
1343 | scalar %prec PREC_LOW
1344 { $$ = scalar($1); }
1346 { $$ = op_scope($1); }