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 ANONSUB 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 optsubbody cont my_scalar my_var
75 %type <opval> refgen_topic formblock
76 %type <opval> subattrlist myattrlist myattrterm myterm
77 %type <opval> realsubbody termbinop termunop anonymous termdo
78 %type <ival> sigslurpsigil
79 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
80 %type <opval> sigelem siglist siglistornull subsignature
81 %type <opval> formstmtseq formline formarg
83 %nonassoc <ival> PREC_LOW
86 %left <ival> OROP DOROP
89 %nonassoc LSTOP LSTOPSUB
91 %right <ival> ASSIGNOP
94 %left <ival> OROR DORDOR
100 %nonassoc UNIOP UNIOPSUB
106 %right <ival> '!' '~' UMINUS REFGEN
108 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
116 /* Top-level choice of what kind of thing yyparse was called to parse */
119 parser->expect = XSTATE;
124 newPROG(block_end($3,$4));
125 PL_compiling.cop_seq = 0;
130 parser->expect = XTERM;
140 parser->expect = XBLOCK;
145 PL_pad_reset_pending = TRUE;
149 parser->yychar = yytoken = YYEOF;
153 parser->expect = XSTATE;
158 PL_pad_reset_pending = TRUE;
162 parser->yychar = yytoken = YYEOF;
166 parser->expect = XSTATE;
171 PL_pad_reset_pending = TRUE;
175 parser->yychar = yytoken = YYEOF;
179 parser->expect = XSTATE;
189 /* An ordinary block */
190 block : '{' remember stmtseq '}'
191 { if (parser->copline > (line_t)$1)
192 parser->copline = (line_t)$1;
193 $$ = block_end($2, $3);
198 formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
199 { if (parser->copline > (line_t)$1)
200 parser->copline = (line_t)$1;
201 $$ = block_end($2, $5);
205 remember: /* NULL */ /* start a full lexical scope */
206 { $$ = block_start(TRUE);
207 parser->parsed_sub = 0; }
210 mblock : '{' mremember stmtseq '}'
211 { if (parser->copline > (line_t)$1)
212 parser->copline = (line_t)$1;
213 $$ = block_end($2, $3);
217 mremember: /* NULL */ /* start a partial lexical scope */
218 { $$ = block_start(FALSE);
219 parser->parsed_sub = 0; }
222 /* A sequence of statements in the program */
226 { $$ = op_append_list(OP_LINESEQ, $1, $2);
227 PL_pad_reset_pending = TRUE;
229 PL_hints |= HINT_BLOCK_SCOPE;
233 /* A sequence of format lines */
234 formstmtseq: /* NULL */
236 | formstmtseq formline
237 { $$ = op_append_list(OP_LINESEQ, $1, $2);
238 PL_pad_reset_pending = TRUE;
240 PL_hints |= HINT_BLOCK_SCOPE;
244 /* A statement in the program, including optional labels */
247 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
253 labfullstmt: LABEL barestmt
255 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
259 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
263 /* A bare statement, lacking label and other aspects of state op */
266 | FORMAT startformsub formname formblock
268 CV *fmtcv = PL_compcv;
271 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
272 pad_add_weakref(fmtcv);
274 parser->parsed_sub = 1;
276 | SUB subname startsub
278 if ($2->op_type == OP_CONST) {
279 const char *const name =
280 SvPV_nolen_const(((SVOP*)$2)->op_sv);
281 if (strEQ(name, "BEGIN") || strEQ(name, "END")
282 || strEQ(name, "INIT") || strEQ(name, "CHECK")
283 || strEQ(name, "UNITCHECK"))
284 CvSPECIAL_on(PL_compcv);
287 /* State subs inside anonymous subs need to be
288 clonable themselves. */
289 if (CvANON(CvOUTSIDE(PL_compcv))
290 || CvCLONE(CvOUTSIDE(PL_compcv))
291 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
294 CvCLONE_on(PL_compcv);
296 parser->in_my_stash = NULL;
298 proto subattrlist optsubbody
300 SvREFCNT_inc_simple_void(PL_compcv);
301 $2->op_type == OP_CONST
302 ? newATTRSUB($3, $2, $5, $6, $7)
303 : newMYSUB($3, $2, $5, $6, $7)
307 parser->parsed_sub = 1;
309 | PACKAGE BAREWORD BAREWORD ';'
317 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
318 BAREWORD BAREWORD optlistexpr ';'
320 SvREFCNT_inc_simple_void(PL_compcv);
321 utilize($1, $2, $4, $5, $6);
322 parser->parsed_sub = 1;
325 | IF '(' remember mexpr ')' mblock else
328 newCONDOP(0, $4, op_scope($6), $7));
329 parser->copline = (line_t)$1;
331 | UNLESS '(' remember mexpr ')' mblock else
334 newCONDOP(0, $4, $7, op_scope($6)));
335 parser->copline = (line_t)$1;
337 | GIVEN '(' remember mexpr ')' mblock
339 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
340 parser->copline = (line_t)$1;
342 | WHEN '(' remember mexpr ')' mblock
343 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
345 { $$ = newWHENOP(0, op_scope($2)); }
346 | WHILE '(' remember texpr ')' mintro mblock cont
349 newWHILEOP(0, 1, NULL,
351 parser->copline = (line_t)$1;
353 | UNTIL '(' remember iexpr ')' mintro mblock cont
356 newWHILEOP(0, 1, NULL,
358 parser->copline = (line_t)$1;
360 | FOR '(' remember mnexpr ';'
361 { parser->expect = XTERM; }
363 { parser->expect = XTERM; }
368 OP *forop = newWHILEOP(0, 1, NULL,
369 scalar($7), $13, $11, $10);
371 forop = op_prepend_elem(OP_LINESEQ, initop,
372 op_append_elem(OP_LINESEQ,
373 newOP(OP_UNSTACK, OPf_SPECIAL),
376 PL_hints |= HINT_BLOCK_SCOPE;
377 $$ = block_end($3, forop);
378 parser->copline = (line_t)$1;
380 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
382 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
383 parser->copline = (line_t)$1;
385 | FOR scalar '(' remember mexpr ')' mblock cont
387 $$ = block_end($4, newFOROP(0,
388 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
389 parser->copline = (line_t)$1;
391 | FOR my_refgen remember my_var
392 { parser->in_my = 0; $<opval>$ = my($4); }
393 '(' mexpr ')' mblock cont
399 newUNOP(OP_REFGEN, 0,
404 parser->copline = (line_t)$1;
406 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
408 $$ = block_end($5, newFOROP(
409 0, op_lvalue(newUNOP(OP_REFGEN, 0,
411 OP_ENTERLOOP), $6, $8, $9));
412 parser->copline = (line_t)$1;
414 | FOR '(' remember mexpr ')' mblock cont
417 newFOROP(0, NULL, $4, $6, $7));
418 parser->copline = (line_t)$1;
422 /* a block is a loop that happens once */
423 $$ = newWHILEOP(0, 1, NULL,
426 | PACKAGE BAREWORD BAREWORD '{' remember
435 /* a block is a loop that happens once */
436 $$ = newWHILEOP(0, 1, NULL,
437 NULL, block_end($5, $7), NULL, 0);
438 if (parser->copline > (line_t)$4)
439 parser->copline = (line_t)$4;
447 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
448 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
453 parser->copline = NOLINE;
458 formline: THING formarg
462 list = op_append_elem(OP_LIST, $1, term);
467 if (parser->copline == NOLINE)
468 parser->copline = CopLINE(PL_curcop)-1;
469 else parser->copline--;
470 $$ = newSTATEOP(0, NULL,
471 op_convert_list(OP_FORMLINE, 0, list));
477 | FORMLBRACK stmtseq FORMRBRACK
478 { $$ = op_unscope($2); }
481 /* An expression which may have a side-effect */
487 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
489 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
491 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
493 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
495 { $$ = newFOROP(0, NULL, $3, $1, NULL);
496 parser->copline = (line_t)$2; }
498 { $$ = newWHENOP($3, op_scope($1)); }
501 /* else and elsif blocks */
506 ($2)->op_flags |= OPf_PARENS;
509 | ELSIF '(' mexpr ')' mblock else
510 { parser->copline = (line_t)$1;
512 newSTATEOP(OPf_SPECIAL,NULL,$3),
514 PL_hints |= HINT_BLOCK_SCOPE;
518 /* Continue blocks */
522 { $$ = op_scope($2); }
525 /* determine whether there are any new my declarations */
527 { $$ = (PL_min_intro_pending &&
528 PL_max_intro_pending >= PL_min_intro_pending);
531 /* Normal expression */
537 /* Boolean expression */
538 texpr : /* NULL means true */
540 (void)scan_num("1", &tmplval);
541 $$ = tmplval.opval; }
545 /* Inverted boolean expression */
547 { $$ = invert(scalar($1)); }
550 /* Expression with its own lexical scope */
552 { $$ = $1; intro_my(); }
556 { $$ = $1; intro_my(); }
559 formname: BAREWORD { $$ = $1; }
560 | /* NULL */ { $$ = NULL; }
563 startsub: /* NULL */ /* start a regular subroutine scope */
564 { $$ = start_subparse(FALSE, 0);
565 SAVEFREESV(PL_compcv); }
569 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
570 { $$ = start_subparse(FALSE, CVf_ANON);
571 SAVEFREESV(PL_compcv); }
574 startformsub: /* NULL */ /* start a format subroutine scope */
575 { $$ = start_subparse(TRUE, 0);
576 SAVEFREESV(PL_compcv); }
579 /* Name of a subroutine - must be a bareword, could be special */
584 /* Subroutine prototype */
590 /* Optional list of subroutine attributes */
591 subattrlist: /* NULL */
599 /* List of attributes for a "my" variable declaration */
600 myattrlist: COLONATTR THING
608 /* --------------------------------------
609 * subroutine signature parsing
612 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
613 sigvarname: /* NULL */
614 { parser->in_my = 0; $$ = NULL; }
616 { parser->in_my = 0; $$ = $1; }
625 /* @, %, @foo, %foo */
626 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
632 if (parser->sig_slurpy)
633 yyerror("Multiple slurpy parameters not allowed");
634 parser->sig_slurpy = (char)sigil;
637 yyerror("A slurpy parameter may not have "
640 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
644 /* default part of sub signature scalar element: i.e. '= default_expr' */
645 sigdefault: /* NULL */
648 { $$ = newOP(OP_NULL, 0); }
653 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
655 '$' sigvarname sigdefault
660 if (parser->sig_slurpy)
661 yyerror("Slurpy parameter not last");
666 parser->sig_optelems++;
668 if ( defexpr->op_type == OP_NULL
669 && !(defexpr->op_flags & OPf_KIDS))
671 /* handle '$=' special case */
673 yyerror("Optional parameter "
674 "lacks default expression");
678 /* a normal '=default' expression */
679 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
682 /* re-purpose op_targ to hold @_ index */
684 (PADOFFSET)(parser->sig_elems - 1);
687 var->op_flags |= OPf_STACKED;
688 (void)op_sibling_splice(var,
693 var = newUNOP(OP_NULL, 0, defop);
696 /* NB: normally the first child of a
697 * logop is executed before the logop,
698 * and it pushes a boolean result
699 * ready for the logop. For ARGDEFELEM,
700 * the op itself does the boolean
701 * calculation, so set the first op to
704 var->op_next = defop;
705 defexpr->op_next = var;
709 if (parser->sig_optelems)
710 yyerror("Mandatory parameter "
711 "follows optional parameter");
714 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
719 /* subroutine signature element: e.g. '$x = $default' or '%h' */
720 sigelem: sigscalarelem
721 { parser->in_my = KEY_sigvar; $$ = $1; }
723 { parser->in_my = KEY_sigvar; $$ = $1; }
726 /* list of subroutine signature elements */
730 | siglist ',' sigelem
732 $$ = op_append_list(OP_LINESEQ, $1, $3);
734 | sigelem %prec PREC_LOW
739 siglistornull: /* NULL */
744 /* Subroutine signature */
745 subsignature: /* NULL */ { $$ = (OP*)NULL; }
749 SAVEIV(parser->sig_elems);
750 SAVEIV(parser->sig_optelems);
751 SAVEI8(parser->sig_slurpy);
752 parser->sig_elems = 0;
753 parser->sig_optelems = 0;
754 parser->sig_slurpy = 0;
755 parser->in_my = KEY_sigvar;
764 if (!FEATURE_SIGNATURES_IS_ENABLED)
765 Perl_croak(aTHX_ "Experimental "
766 "subroutine signatures not enabled");
768 /* We shouldn't get here otherwise */
769 Perl_ck_warner_d(aTHX_
770 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
771 "The signatures feature is experimental");
773 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
774 sizeof(UNOP_AUX_item) * 3);
775 aux[0].iv = parser->sig_elems;
776 aux[1].iv = parser->sig_optelems;
777 aux[2].iv = parser->sig_slurpy;
778 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
779 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
780 sigops = op_prepend_elem(OP_LINESEQ,
781 newSTATEOP(0, NULL, NULL),
783 /* a nextstate at the end handles context
784 * correctly for an empty sub body */
785 $$ = op_append_elem(OP_LINESEQ,
787 newSTATEOP(0, NULL, NULL));
790 parser->expect = XBLOCK;
796 /* Subroutine body - block with optional signature */
797 realsubbody: remember subsignature '{' stmtseq '}'
799 if (parser->copline > (line_t)$3)
800 parser->copline = (line_t)$3;
802 op_append_list(OP_LINESEQ, $2, $4));
807 /* Optional subroutine body, for named subroutine declaration */
808 optsubbody: realsubbody { $$ = $1; }
812 /* Ordinary expressions; logical combinations */
813 expr : expr ANDOP expr
814 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
816 { $$ = newLOGOP($2, 0, $1, $3); }
818 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
819 | listexpr %prec PREC_LOW
822 /* Expressions are a list of terms joined by commas */
823 listexpr: listexpr ','
828 $$ = op_append_elem(OP_LIST, $1, term);
830 | term %prec PREC_LOW
834 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
835 { $$ = op_convert_list($1, OPf_STACKED,
836 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
838 | FUNC '(' indirob expr ')' /* print ($fh @args */
839 { $$ = op_convert_list($1, OPf_STACKED,
840 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
842 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
843 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
844 op_append_elem(OP_LIST,
845 op_prepend_elem(OP_LIST, scalar($1), $5),
846 newMETHOP(OP_METHOD, 0, $3)));
848 | term ARROW method /* $foo->bar */
849 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
850 op_append_elem(OP_LIST, scalar($1),
851 newMETHOP(OP_METHOD, 0, $3)));
853 | METHOD indirob optlistexpr /* new Class @args */
854 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
855 op_append_elem(OP_LIST,
856 op_prepend_elem(OP_LIST, $2, $3),
857 newMETHOP(OP_METHOD, 0, $1)));
859 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
860 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
861 op_append_elem(OP_LIST,
862 op_prepend_elem(OP_LIST, $2, $4),
863 newMETHOP(OP_METHOD, 0, $1)));
865 | LSTOP optlistexpr /* print @args */
866 { $$ = op_convert_list($1, 0, $2); }
867 | FUNC '(' optexpr ')' /* print (@args) */
868 { $$ = op_convert_list($1, 0, $3); }
869 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
870 { SvREFCNT_inc_simple_void(PL_compcv);
871 $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
872 optlistexpr %prec LSTOP /* ... @bar */
873 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
874 op_append_elem(OP_LIST,
875 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
879 /* Names of methods. May use $object->$methodname */
884 /* Some kind of subscripted expression */
885 subscripted: gelem '{' expr ';' '}' /* *main::{something} */
886 /* In this and all the hash accessors, ';' is
887 * provided by the tokeniser */
888 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
889 | scalar '[' expr ']' /* $array[$element] */
890 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
892 | term ARROW '[' expr ']' /* somearef->[$element] */
893 { $$ = newBINOP(OP_AELEM, 0,
894 ref(newAVREF($1),OP_RV2AV),
897 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
898 { $$ = newBINOP(OP_AELEM, 0,
899 ref(newAVREF($1),OP_RV2AV),
902 | scalar '{' expr ';' '}' /* $foo{bar();} */
903 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
905 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
906 { $$ = newBINOP(OP_HELEM, 0,
907 ref(newHVREF($1),OP_RV2HV),
909 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
910 { $$ = newBINOP(OP_HELEM, 0,
911 ref(newHVREF($1),OP_RV2HV),
913 | term ARROW '(' ')' /* $subref->() */
914 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
915 newCVREF(0, scalar($1)));
916 if (parser->expect == XBLOCK)
917 parser->expect = XOPERATOR;
919 | term ARROW '(' expr ')' /* $subref->(@args) */
920 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
921 op_append_elem(OP_LIST, $4,
922 newCVREF(0, scalar($1))));
923 if (parser->expect == XBLOCK)
924 parser->expect = XOPERATOR;
927 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
928 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
929 op_append_elem(OP_LIST, $3,
930 newCVREF(0, scalar($1))));
931 if (parser->expect == XBLOCK)
932 parser->expect = XOPERATOR;
934 | subscripted '(' ')' /* $foo->{bar}->() */
935 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
936 newCVREF(0, scalar($1)));
937 if (parser->expect == XBLOCK)
938 parser->expect = XOPERATOR;
940 | '(' expr ')' '[' expr ']' /* list slice */
941 { $$ = newSLICEOP(0, $5, $2); }
942 | QWLIST '[' expr ']' /* list literal slice */
943 { $$ = newSLICEOP(0, $3, $1); }
944 | '(' ')' '[' expr ']' /* empty list slice! */
945 { $$ = newSLICEOP(0, $4, NULL); }
948 /* Binary operators between terms */
949 termbinop: term ASSIGNOP term /* $x = $y, $x += $y */
950 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
951 | term POWOP term /* $x ** $y */
952 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
953 | term MULOP term /* $x * $y, $x x $y */
954 { if ($2 != OP_REPEAT)
956 $$ = newBINOP($2, 0, $1, scalar($3));
958 | term ADDOP term /* $x + $y */
959 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
960 | term SHIFTOP term /* $x >> $y, $x << $y */
961 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
962 | term RELOP term /* $x > $y, etc. */
963 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
964 | term EQOP term /* $x == $y, $x eq $y */
965 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
966 | term BITANDOP term /* $x & $y */
967 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
968 | term BITOROP term /* $x | $y */
969 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
970 | term DOTDOT term /* $x..$y, $x...$y */
971 { $$ = newRANGE($2, scalar($1), scalar($3)); }
972 | term ANDAND term /* $x && $y */
973 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
974 | term OROR term /* $x || $y */
975 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
976 | term DORDOR term /* $x // $y */
977 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
978 | term MATCHOP term /* $x =~ /$y/ */
979 { $$ = bind_match($2, $1, $3); }
982 /* Unary operators and terms */
983 termunop : '-' term %prec UMINUS /* -$x */
984 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
985 | '+' term %prec UMINUS /* +$x */
989 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
991 { $$ = newUNOP($1, 0, scalar($2)); }
992 | term POSTINC /* $x++ */
993 { $$ = newUNOP(OP_POSTINC, 0,
994 op_lvalue(scalar($1), OP_POSTINC)); }
995 | term POSTDEC /* $x-- */
996 { $$ = newUNOP(OP_POSTDEC, 0,
997 op_lvalue(scalar($1), OP_POSTDEC));}
998 | term POSTJOIN /* implicit join after interpolated ->@ */
999 { $$ = op_convert_list(OP_JOIN, 0,
1009 | PREINC term /* ++$x */
1010 { $$ = newUNOP(OP_PREINC, 0,
1011 op_lvalue(scalar($2), OP_PREINC)); }
1012 | PREDEC term /* --$x */
1013 { $$ = newUNOP(OP_PREDEC, 0,
1014 op_lvalue(scalar($2), OP_PREDEC)); }
1018 /* Constructors for anonymous data */
1019 anonymous: '[' expr ']'
1020 { $$ = newANONLIST($2); }
1022 { $$ = newANONLIST(NULL);}
1023 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1024 { $$ = newANONHASH($2); }
1025 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1026 { $$ = newANONHASH(NULL); }
1027 | ANONSUB startanonsub proto subattrlist realsubbody %prec '('
1028 { SvREFCNT_inc_simple_void(PL_compcv);
1029 $$ = newANONATTRSUB($2, $3, $4, $5); }
1032 /* Things called with "do" */
1033 termdo : DO term %prec UNIOP /* do $filename */
1034 { $$ = dofile($2, $1);}
1035 | DO block %prec '(' /* do { code */
1036 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
1043 | term '?' term ':' term
1044 { $$ = newCONDOP(0, $1, $3, $5); }
1045 | REFGEN term /* \$x, \@y, \%z */
1046 { $$ = newUNOP(OP_REFGEN, 0, $2); }
1048 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
1049 | myattrterm %prec UNIOP
1051 | LOCAL term %prec UNIOP
1052 { $$ = localize($2,0); }
1054 { $$ = sawparens($2); }
1058 { $$ = sawparens(newNULLLIST()); }
1067 | arylen %prec '(' /* $#x, $#{ something } */
1068 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1071 | sliceme '[' expr ']' /* array slice */
1072 { $$ = op_prepend_elem(OP_ASLICE,
1073 newOP(OP_PUSHMARK, 0),
1074 newLISTOP(OP_ASLICE, 0,
1076 ref($1, OP_ASLICE)));
1079 $1->op_private & OPpSLICEWARNING;
1081 | kvslice '[' expr ']' /* array key/value slice */
1082 { $$ = op_prepend_elem(OP_KVASLICE,
1083 newOP(OP_PUSHMARK, 0),
1084 newLISTOP(OP_KVASLICE, 0,
1086 ref(oopsAV($1), OP_KVASLICE)));
1089 $1->op_private & OPpSLICEWARNING;
1091 | sliceme '{' expr ';' '}' /* @hash{@keys} */
1092 { $$ = op_prepend_elem(OP_HSLICE,
1093 newOP(OP_PUSHMARK, 0),
1094 newLISTOP(OP_HSLICE, 0,
1096 ref(oopsHV($1), OP_HSLICE)));
1099 $1->op_private & OPpSLICEWARNING;
1101 | kvslice '{' expr ';' '}' /* %hash{@keys} */
1102 { $$ = op_prepend_elem(OP_KVHSLICE,
1103 newOP(OP_PUSHMARK, 0),
1104 newLISTOP(OP_KVHSLICE, 0,
1106 ref($1, OP_KVHSLICE)));
1109 $1->op_private & OPpSLICEWARNING;
1114 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1115 | amper '(' ')' /* &foo() or foo() */
1116 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1118 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1120 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1121 op_append_elem(OP_LIST, $3, scalar($1)));
1123 | NOAMP subname optlistexpr /* foo @args (no parens) */
1124 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1125 op_append_elem(OP_LIST, $3, scalar($2)));
1127 | term ARROW '$' '*'
1128 { $$ = newSVREF($1); }
1129 | term ARROW '@' '*'
1130 { $$ = newAVREF($1); }
1131 | term ARROW '%' '*'
1132 { $$ = newHVREF($1); }
1133 | term ARROW '&' '*'
1134 { $$ = newUNOP(OP_ENTERSUB, 0,
1135 scalar(newCVREF($3,$1))); }
1136 | term ARROW '*' '*' %prec '('
1137 { $$ = newGVREF(0,$1); }
1138 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1139 { $$ = newOP($1, OPf_SPECIAL);
1140 PL_hints |= HINT_BLOCK_SCOPE; }
1142 { $$ = newLOOPEX($1,$2); }
1143 | NOTOP listexpr /* not $foo */
1144 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1145 | UNIOP /* Unary op, $_ implied */
1146 { $$ = newOP($1, 0); }
1147 | UNIOP block /* eval { foo }* */
1148 { $$ = newUNOP($1, 0, $2); }
1149 | UNIOP term /* Unary op */
1150 { $$ = newUNOP($1, 0, $2); }
1151 | REQUIRE /* require, $_ implied */
1152 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1153 | REQUIRE term /* require Foo */
1154 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1156 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1157 | UNIOPSUB term /* Sub treated as unop */
1158 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1159 op_append_elem(OP_LIST, $2, scalar($1))); }
1160 | FUNC0 /* Nullary operator */
1161 { $$ = newOP($1, 0); }
1163 { $$ = newOP($1, 0);}
1164 | FUNC0OP /* Same as above, but op created in toke.c */
1168 | FUNC0SUB /* Sub treated as nullop */
1169 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1170 | FUNC1 '(' ')' /* not () */
1171 { $$ = ($1 == OP_NOT)
1172 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1173 : newOP($1, OPf_SPECIAL); }
1174 | FUNC1 '(' expr ')' /* not($foo) */
1175 { $$ = newUNOP($1, 0, $3); }
1176 | PMFUNC /* m//, s///, qr//, tr/// */
1178 if ( $1->op_type != OP_TRANS
1179 && $1->op_type != OP_TRANSR
1180 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1182 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1183 SAVEFREESV(PL_compcv);
1187 '(' listexpr optrepl ')'
1188 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1194 /* "my" declarations, with optional attributes */
1195 myattrterm: MY myterm myattrlist
1196 { $$ = my_attrs($2,$3); }
1198 { $$ = localize($2,1); }
1199 | MY REFGEN myterm myattrlist
1200 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
1203 /* Things that can be "my"'d */
1204 myterm : '(' expr ')'
1205 { $$ = sawparens($2); }
1207 { $$ = sawparens(newNULLLIST()); }
1217 /* Basic list expressions */
1218 optlistexpr: /* NULL */ %prec PREC_LOW
1220 | listexpr %prec PREC_LOW
1236 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1239 { parser->in_my = 0; $$ = my($1); }
1247 refgen_topic: my_var
1251 my_refgen: MY REFGEN
1256 { $$ = newCVREF($1,$2); }
1259 scalar : '$' indirob
1260 { $$ = newSVREF($2); }
1264 { $$ = newAVREF($2);
1265 if ($$) $$->op_private |= $1;
1270 { $$ = newHVREF($2);
1271 if ($$) $$->op_private |= $1;
1275 arylen : DOLSHARP indirob
1276 { $$ = newAVREF($2); }
1277 | term ARROW DOLSHARP '*'
1278 { $$ = newAVREF($1); }
1282 { $$ = newGVREF(0,$2); }
1287 { $$ = newAVREF($1); }
1292 { $$ = newHVREF($1); }
1297 { $$ = newGVREF(0,$1); }
1300 /* Indirect objects */
1302 { $$ = scalar($1); }
1303 | scalar %prec PREC_LOW
1304 { $$ = scalar($1); }
1306 { $$ = op_scope($1); }