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 GRAMSUBSIGNATURE
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> 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 <opval> termrelop relopchain termeqop eqopchain
80 %type <ival> sigslurpsigil
81 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
82 %type <opval> sigelem siglist siglistornull subsigguts subsignature optsubsignature
83 %type <opval> subbody optsubbody sigsubbody optsigsubbody
84 %type <opval> formstmtseq formline formarg
86 %nonassoc <ival> PREC_LOW
89 %left <ival> OROP DOROP
92 %nonassoc LSTOP LSTOPSUB
94 %right <ival> ASSIGNOP
97 %left <ival> OROR DORDOR
100 %left <ival> BITANDOP
101 %left <ival> CHEQOP NCEQOP
102 %left <ival> CHRELOP NCRELOP
103 %nonassoc UNIOP UNIOPSUB
109 %right <ival> '!' '~' UMINUS REFGEN
111 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
119 /* Top-level choice of what kind of thing yyparse was called to parse */
122 parser->expect = XSTATE;
127 newPROG(block_end($3,$4));
128 PL_compiling.cop_seq = 0;
133 parser->expect = XTERM;
143 parser->expect = XBLOCK;
148 PL_pad_reset_pending = TRUE;
152 parser->yychar = yytoken = YYEOF;
156 parser->expect = XSTATE;
161 PL_pad_reset_pending = TRUE;
165 parser->yychar = yytoken = YYEOF;
169 parser->expect = XSTATE;
174 PL_pad_reset_pending = TRUE;
178 parser->yychar = yytoken = YYEOF;
182 parser->expect = XSTATE;
192 parser->expect = XSTATE;
202 /* An ordinary block */
203 block : '{' remember stmtseq '}'
204 { if (parser->copline > (line_t)$1)
205 parser->copline = (line_t)$1;
206 $$ = block_end($2, $3);
211 formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
212 { if (parser->copline > (line_t)$1)
213 parser->copline = (line_t)$1;
214 $$ = block_end($2, $5);
218 remember: /* NULL */ /* start a full lexical scope */
219 { $$ = block_start(TRUE);
220 parser->parsed_sub = 0; }
223 mblock : '{' mremember stmtseq '}'
224 { if (parser->copline > (line_t)$1)
225 parser->copline = (line_t)$1;
226 $$ = block_end($2, $3);
230 mremember: /* NULL */ /* start a partial lexical scope */
231 { $$ = block_start(FALSE);
232 parser->parsed_sub = 0; }
235 /* A sequence of statements in the program */
239 { $$ = op_append_list(OP_LINESEQ, $1, $2);
240 PL_pad_reset_pending = TRUE;
242 PL_hints |= HINT_BLOCK_SCOPE;
246 /* A sequence of format lines */
247 formstmtseq: /* NULL */
249 | formstmtseq formline
250 { $$ = op_append_list(OP_LINESEQ, $1, $2);
251 PL_pad_reset_pending = TRUE;
253 PL_hints |= HINT_BLOCK_SCOPE;
257 /* A statement in the program, including optional labels */
260 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
266 labfullstmt: LABEL barestmt
268 SV *label = cSVOPx_sv($1);
269 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
270 savepv(SvPVX_const(label)), $2);
275 SV *label = cSVOPx_sv($1);
276 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
277 savepv(SvPVX_const(label)), $2);
282 /* A bare statement, lacking label and other aspects of state op */
285 | FORMAT startformsub formname formblock
287 CV *fmtcv = PL_compcv;
290 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
291 pad_add_weakref(fmtcv);
293 parser->parsed_sub = 1;
295 | SUB subname startsub
296 /* sub declaration or definition not within scope
297 of 'use feature "signatures"'*/
299 init_named_cv(PL_compcv, $2);
301 parser->in_my_stash = NULL;
303 proto subattrlist optsubbody
305 SvREFCNT_inc_simple_void(PL_compcv);
306 $2->op_type == OP_CONST
307 ? newATTRSUB($3, $2, $5, $6, $7)
308 : newMYSUB($3, $2, $5, $6, $7)
312 parser->parsed_sub = 1;
314 | SIGSUB subname startsub
315 /* sub declaration or definition under 'use feature
316 * "signatures"'. (Note that a signature isn't
317 * allowed in a declaration)
320 init_named_cv(PL_compcv, $2);
322 parser->in_my_stash = NULL;
324 subattrlist optsigsubbody
326 SvREFCNT_inc_simple_void(PL_compcv);
327 $2->op_type == OP_CONST
328 ? newATTRSUB($3, $2, NULL, $5, $6)
329 : newMYSUB( $3, $2, NULL, $5, $6)
333 parser->parsed_sub = 1;
335 | PACKAGE BAREWORD BAREWORD ';'
343 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
344 BAREWORD BAREWORD optlistexpr ';'
346 SvREFCNT_inc_simple_void(PL_compcv);
347 utilize($1, $2, $4, $5, $6);
348 parser->parsed_sub = 1;
351 | IF '(' remember mexpr ')' mblock else
354 newCONDOP(0, $4, op_scope($6), $7));
355 parser->copline = (line_t)$1;
357 | UNLESS '(' remember mexpr ')' mblock else
360 newCONDOP(0, $4, $7, op_scope($6)));
361 parser->copline = (line_t)$1;
363 | GIVEN '(' remember mexpr ')' mblock
365 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
366 parser->copline = (line_t)$1;
368 | WHEN '(' remember mexpr ')' mblock
369 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
371 { $$ = newWHENOP(0, op_scope($2)); }
372 | WHILE '(' remember texpr ')' mintro mblock cont
375 newWHILEOP(0, 1, NULL,
377 parser->copline = (line_t)$1;
379 | UNTIL '(' remember iexpr ')' mintro mblock cont
382 newWHILEOP(0, 1, NULL,
384 parser->copline = (line_t)$1;
386 | FOR '(' remember mnexpr ';'
387 { parser->expect = XTERM; }
389 { parser->expect = XTERM; }
394 OP *forop = newWHILEOP(0, 1, NULL,
395 scalar($7), $13, $11, $10);
397 forop = op_prepend_elem(OP_LINESEQ, initop,
398 op_append_elem(OP_LINESEQ,
399 newOP(OP_UNSTACK, OPf_SPECIAL),
402 PL_hints |= HINT_BLOCK_SCOPE;
403 $$ = block_end($3, forop);
404 parser->copline = (line_t)$1;
406 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
408 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
409 parser->copline = (line_t)$1;
411 | FOR scalar '(' remember mexpr ')' mblock cont
413 $$ = block_end($4, newFOROP(0,
414 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
415 parser->copline = (line_t)$1;
417 | FOR my_refgen remember my_var
418 { parser->in_my = 0; $<opval>$ = my($4); }
419 '(' mexpr ')' mblock cont
425 newUNOP(OP_REFGEN, 0,
430 parser->copline = (line_t)$1;
432 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
434 $$ = block_end($5, newFOROP(
435 0, op_lvalue(newUNOP(OP_REFGEN, 0,
437 OP_ENTERLOOP), $6, $8, $9));
438 parser->copline = (line_t)$1;
440 | FOR '(' remember mexpr ')' mblock cont
443 newFOROP(0, NULL, $4, $6, $7));
444 parser->copline = (line_t)$1;
448 /* a block is a loop that happens once */
449 $$ = newWHILEOP(0, 1, NULL,
452 | PACKAGE BAREWORD BAREWORD '{' remember
461 /* a block is a loop that happens once */
462 $$ = newWHILEOP(0, 1, NULL,
463 NULL, block_end($5, $7), NULL, 0);
464 if (parser->copline > (line_t)$4)
465 parser->copline = (line_t)$4;
473 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
474 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
479 parser->copline = NOLINE;
484 formline: THING formarg
488 list = op_append_elem(OP_LIST, $1, term);
493 if (parser->copline == NOLINE)
494 parser->copline = CopLINE(PL_curcop)-1;
495 else parser->copline--;
496 $$ = newSTATEOP(0, NULL,
497 op_convert_list(OP_FORMLINE, 0, list));
503 | FORMLBRACK stmtseq FORMRBRACK
504 { $$ = op_unscope($2); }
507 /* An expression which may have a side-effect */
513 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
515 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
517 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
519 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
521 { $$ = newFOROP(0, NULL, $3, $1, NULL);
522 parser->copline = (line_t)$2; }
524 { $$ = newWHENOP($3, op_scope($1)); }
527 /* else and elsif blocks */
532 ($2)->op_flags |= OPf_PARENS;
535 | ELSIF '(' mexpr ')' mblock else
536 { parser->copline = (line_t)$1;
538 newSTATEOP(OPf_SPECIAL,NULL,$3),
540 PL_hints |= HINT_BLOCK_SCOPE;
544 /* Continue blocks */
548 { $$ = op_scope($2); }
551 /* determine whether there are any new my declarations */
553 { $$ = (PL_min_intro_pending &&
554 PL_max_intro_pending >= PL_min_intro_pending);
557 /* Normal expression */
563 /* Boolean expression */
564 texpr : /* NULL means true */
566 (void)scan_num("1", &tmplval);
567 $$ = tmplval.opval; }
571 /* Inverted boolean expression */
573 { $$ = invert(scalar($1)); }
576 /* Expression with its own lexical scope */
578 { $$ = $1; intro_my(); }
582 { $$ = $1; intro_my(); }
585 formname: BAREWORD { $$ = $1; }
586 | /* NULL */ { $$ = NULL; }
589 startsub: /* NULL */ /* start a regular subroutine scope */
590 { $$ = start_subparse(FALSE, 0);
591 SAVEFREESV(PL_compcv); }
595 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
596 { $$ = start_subparse(FALSE, CVf_ANON);
597 SAVEFREESV(PL_compcv); }
600 startformsub: /* NULL */ /* start a format subroutine scope */
601 { $$ = start_subparse(TRUE, 0);
602 SAVEFREESV(PL_compcv); }
605 /* Name of a subroutine - must be a bareword, could be special */
610 /* Subroutine prototype */
616 /* Optional list of subroutine attributes */
617 subattrlist: /* NULL */
625 /* List of attributes for a "my" variable declaration */
626 myattrlist: COLONATTR THING
634 /* --------------------------------------
635 * subroutine signature parsing
638 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
639 sigvarname: /* NULL */
640 { parser->in_my = 0; $$ = NULL; }
642 { parser->in_my = 0; $$ = $1; }
651 /* @, %, @foo, %foo */
652 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
658 if (parser->sig_slurpy)
659 yyerror("Multiple slurpy parameters not allowed");
660 parser->sig_slurpy = (char)sigil;
663 yyerror("A slurpy parameter may not have "
666 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
670 /* default part of sub signature scalar element: i.e. '= default_expr' */
671 sigdefault: /* NULL */
674 { $$ = newOP(OP_NULL, 0); }
679 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
681 '$' sigvarname sigdefault
686 if (parser->sig_slurpy)
687 yyerror("Slurpy parameter not last");
692 parser->sig_optelems++;
694 if ( defexpr->op_type == OP_NULL
695 && !(defexpr->op_flags & OPf_KIDS))
697 /* handle '$=' special case */
699 yyerror("Optional parameter "
700 "lacks default expression");
704 /* a normal '=default' expression */
705 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
708 /* re-purpose op_targ to hold @_ index */
710 (PADOFFSET)(parser->sig_elems - 1);
713 var->op_flags |= OPf_STACKED;
714 (void)op_sibling_splice(var,
719 var = newUNOP(OP_NULL, 0, defop);
722 /* NB: normally the first child of a
723 * logop is executed before the logop,
724 * and it pushes a boolean result
725 * ready for the logop. For ARGDEFELEM,
726 * the op itself does the boolean
727 * calculation, so set the first op to
730 var->op_next = defop;
731 defexpr->op_next = var;
735 if (parser->sig_optelems)
736 yyerror("Mandatory parameter "
737 "follows optional parameter");
740 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
745 /* subroutine signature element: e.g. '$x = $default' or '%h' */
746 sigelem: sigscalarelem
747 { parser->in_my = KEY_sigvar; $$ = $1; }
749 { parser->in_my = KEY_sigvar; $$ = $1; }
752 /* list of subroutine signature elements */
756 | siglist ',' sigelem
758 $$ = op_append_list(OP_LINESEQ, $1, $3);
760 | sigelem %prec PREC_LOW
765 siglistornull: /* NULL */
770 /* optional subroutine signature */
771 optsubsignature: /* NULL */
776 /* Subroutine signature */
777 subsignature: '(' subsigguts ')'
783 SAVEIV(parser->sig_elems);
784 SAVEIV(parser->sig_optelems);
785 SAVEI8(parser->sig_slurpy);
786 parser->sig_elems = 0;
787 parser->sig_optelems = 0;
788 parser->sig_slurpy = 0;
789 parser->in_my = KEY_sigvar;
794 struct op_argcheck_aux *aux;
797 if (!FEATURE_SIGNATURES_IS_ENABLED)
798 Perl_croak(aTHX_ "Experimental "
799 "subroutine signatures not enabled");
801 /* We shouldn't get here otherwise */
802 Perl_ck_warner_d(aTHX_
803 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
804 "The signatures feature is experimental");
806 aux = (struct op_argcheck_aux*)
807 PerlMemShared_malloc(
808 sizeof(struct op_argcheck_aux));
809 aux->params = parser->sig_elems;
810 aux->opt_params = parser->sig_optelems;
811 aux->slurpy = parser->sig_slurpy;
812 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL,
813 (UNOP_AUX_item *)aux);
814 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
815 sigops = op_prepend_elem(OP_LINESEQ,
816 newSTATEOP(0, NULL, NULL),
818 /* a nextstate at the end handles context
819 * correctly for an empty sub body */
820 sigops = op_append_elem(OP_LINESEQ,
822 newSTATEOP(0, NULL, NULL));
823 /* wrap the list of arg ops in a NULL aux op.
824 This serves two purposes. First, it makes
825 the arg list a separate subtree from the
826 body of the sub, and secondly the null op
827 may in future be upgraded to an OP_SIGNATURE
828 when implemented. For now leave it as
830 $$ = newUNOP_AUX(OP_ARGCHECK, 0, sigops, NULL);
834 /* tell the toker that attrributes can follow
835 * this sig, but only so that the toker
836 * can skip through any (illegal) trailing
837 * attribute text then give a useful error
838 * message about "attributes before sig",
839 * rather than falling over ina mess at
840 * unrecognised syntax.
842 parser->expect = XATTRBLOCK;
843 parser->sig_seen = TRUE;
848 /* Optional subroutine body (for named subroutine declaration) */
849 optsubbody: subbody { $$ = $1; }
854 /* Subroutine body (without signature) */
855 subbody: remember '{' stmtseq '}'
857 if (parser->copline > (line_t)$2)
858 parser->copline = (line_t)$2;
859 $$ = block_end($1, $3);
864 /* optional [ Subroutine body with optional signature ] (for named
865 * subroutine declaration) */
866 optsigsubbody: sigsubbody { $$ = $1; }
869 /* Subroutine body with optional signature */
870 sigsubbody: remember optsubsignature '{' stmtseq '}'
872 if (parser->copline > (line_t)$3)
873 parser->copline = (line_t)$3;
875 op_append_list(OP_LINESEQ, $2, $4));
880 /* Ordinary expressions; logical combinations */
881 expr : expr ANDOP expr
882 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
884 { $$ = newLOGOP($2, 0, $1, $3); }
886 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
887 | listexpr %prec PREC_LOW
890 /* Expressions are a list of terms joined by commas */
891 listexpr: listexpr ','
896 $$ = op_append_elem(OP_LIST, $1, term);
898 | term %prec PREC_LOW
902 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
903 { $$ = op_convert_list($1, OPf_STACKED,
904 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
906 | FUNC '(' indirob expr ')' /* print ($fh @args */
907 { $$ = op_convert_list($1, OPf_STACKED,
908 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
910 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
911 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
912 op_append_elem(OP_LIST,
913 op_prepend_elem(OP_LIST, scalar($1), $5),
914 newMETHOP(OP_METHOD, 0, $3)));
916 | term ARROW method /* $foo->bar */
917 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
918 op_append_elem(OP_LIST, scalar($1),
919 newMETHOP(OP_METHOD, 0, $3)));
921 | METHOD indirob optlistexpr /* new Class @args */
922 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
923 op_append_elem(OP_LIST,
924 op_prepend_elem(OP_LIST, $2, $3),
925 newMETHOP(OP_METHOD, 0, $1)));
927 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
928 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
929 op_append_elem(OP_LIST,
930 op_prepend_elem(OP_LIST, $2, $4),
931 newMETHOP(OP_METHOD, 0, $1)));
933 | LSTOP optlistexpr /* print @args */
934 { $$ = op_convert_list($1, 0, $2); }
935 | FUNC '(' optexpr ')' /* print (@args) */
936 { $$ = op_convert_list($1, 0, $3); }
937 | FUNC SUBLEXSTART optexpr SUBLEXEND /* uc($arg) from "\U..." */
938 { $$ = op_convert_list($1, 0, $3); }
939 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
940 { SvREFCNT_inc_simple_void(PL_compcv);
941 $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
942 optlistexpr %prec LSTOP /* ... @bar */
943 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
944 op_append_elem(OP_LIST,
945 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
949 /* Names of methods. May use $object->$methodname */
954 /* Some kind of subscripted expression */
955 subscripted: gelem '{' expr ';' '}' /* *main::{something} */
956 /* In this and all the hash accessors, ';' is
957 * provided by the tokeniser */
958 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
959 | scalar '[' expr ']' /* $array[$element] */
960 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
962 | term ARROW '[' expr ']' /* somearef->[$element] */
963 { $$ = newBINOP(OP_AELEM, 0,
964 ref(newAVREF($1),OP_RV2AV),
967 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
968 { $$ = newBINOP(OP_AELEM, 0,
969 ref(newAVREF($1),OP_RV2AV),
972 | scalar '{' expr ';' '}' /* $foo{bar();} */
973 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
975 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
976 { $$ = newBINOP(OP_HELEM, 0,
977 ref(newHVREF($1),OP_RV2HV),
979 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
980 { $$ = newBINOP(OP_HELEM, 0,
981 ref(newHVREF($1),OP_RV2HV),
983 | term ARROW '(' ')' /* $subref->() */
984 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
985 newCVREF(0, scalar($1)));
986 if (parser->expect == XBLOCK)
987 parser->expect = XOPERATOR;
989 | term ARROW '(' expr ')' /* $subref->(@args) */
990 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
991 op_append_elem(OP_LIST, $4,
992 newCVREF(0, scalar($1))));
993 if (parser->expect == XBLOCK)
994 parser->expect = XOPERATOR;
997 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
998 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
999 op_append_elem(OP_LIST, $3,
1000 newCVREF(0, scalar($1))));
1001 if (parser->expect == XBLOCK)
1002 parser->expect = XOPERATOR;
1004 | subscripted '(' ')' /* $foo->{bar}->() */
1005 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1006 newCVREF(0, scalar($1)));
1007 if (parser->expect == XBLOCK)
1008 parser->expect = XOPERATOR;
1010 | '(' expr ')' '[' expr ']' /* list slice */
1011 { $$ = newSLICEOP(0, $5, $2); }
1012 | QWLIST '[' expr ']' /* list literal slice */
1013 { $$ = newSLICEOP(0, $3, $1); }
1014 | '(' ')' '[' expr ']' /* empty list slice! */
1015 { $$ = newSLICEOP(0, $4, NULL); }
1018 /* Binary operators between terms */
1019 termbinop: term ASSIGNOP term /* $x = $y, $x += $y */
1020 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
1021 | term POWOP term /* $x ** $y */
1022 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1023 | term MULOP term /* $x * $y, $x x $y */
1024 { if ($2 != OP_REPEAT)
1026 $$ = newBINOP($2, 0, $1, scalar($3));
1028 | term ADDOP term /* $x + $y */
1029 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1030 | term SHIFTOP term /* $x >> $y, $x << $y */
1031 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1032 | termrelop %prec PREC_LOW /* $x > $y, etc. */
1034 | termeqop %prec PREC_LOW /* $x == $y, $x cmp $y */
1036 | term BITANDOP term /* $x & $y */
1037 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1038 | term BITOROP term /* $x | $y */
1039 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1040 | term DOTDOT term /* $x..$y, $x...$y */
1041 { $$ = newRANGE($2, scalar($1), scalar($3)); }
1042 | term ANDAND term /* $x && $y */
1043 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
1044 | term OROR term /* $x || $y */
1045 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
1046 | term DORDOR term /* $x // $y */
1047 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
1048 | term MATCHOP term /* $x =~ /$y/ */
1049 { $$ = bind_match($2, $1, $3); }
1052 termrelop: relopchain %prec PREC_LOW
1053 { $$ = cmpchain_finish($1); }
1055 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1057 { yyerror("syntax error"); YYERROR; }
1059 { yyerror("syntax error"); YYERROR; }
1062 relopchain: term CHRELOP term
1063 { $$ = cmpchain_start($2, $1, $3); }
1064 | relopchain CHRELOP term
1065 { $$ = cmpchain_extend($2, $1, $3); }
1068 termeqop: eqopchain %prec PREC_LOW
1069 { $$ = cmpchain_finish($1); }
1071 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1073 { yyerror("syntax error"); YYERROR; }
1075 { yyerror("syntax error"); YYERROR; }
1078 eqopchain: term CHEQOP term
1079 { $$ = cmpchain_start($2, $1, $3); }
1080 | eqopchain CHEQOP term
1081 { $$ = cmpchain_extend($2, $1, $3); }
1084 /* Unary operators and terms */
1085 termunop : '-' term %prec UMINUS /* -$x */
1086 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
1087 | '+' term %prec UMINUS /* +$x */
1090 | '!' term /* !$x */
1091 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1092 | '~' term /* ~$x */
1093 { $$ = newUNOP($1, 0, scalar($2)); }
1094 | term POSTINC /* $x++ */
1095 { $$ = newUNOP(OP_POSTINC, 0,
1096 op_lvalue(scalar($1), OP_POSTINC)); }
1097 | term POSTDEC /* $x-- */
1098 { $$ = newUNOP(OP_POSTDEC, 0,
1099 op_lvalue(scalar($1), OP_POSTDEC));}
1100 | term POSTJOIN /* implicit join after interpolated ->@ */
1101 { $$ = op_convert_list(OP_JOIN, 0,
1111 | PREINC term /* ++$x */
1112 { $$ = newUNOP(OP_PREINC, 0,
1113 op_lvalue(scalar($2), OP_PREINC)); }
1114 | PREDEC term /* --$x */
1115 { $$ = newUNOP(OP_PREDEC, 0,
1116 op_lvalue(scalar($2), OP_PREDEC)); }
1120 /* Constructors for anonymous data */
1121 anonymous: '[' expr ']'
1122 { $$ = newANONLIST($2); }
1124 { $$ = newANONLIST(NULL);}
1125 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1126 { $$ = newANONHASH($2); }
1127 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1128 { $$ = newANONHASH(NULL); }
1129 | ANONSUB startanonsub proto subattrlist subbody %prec '('
1130 { SvREFCNT_inc_simple_void(PL_compcv);
1131 $$ = newANONATTRSUB($2, $3, $4, $5); }
1132 | ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '('
1133 { SvREFCNT_inc_simple_void(PL_compcv);
1134 $$ = newANONATTRSUB($2, NULL, $3, $4); }
1137 /* Things called with "do" */
1138 termdo : DO term %prec UNIOP /* do $filename */
1139 { $$ = dofile($2, $1);}
1140 | DO block %prec '(' /* do { code */
1141 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
1148 | term '?' term ':' term
1149 { $$ = newCONDOP(0, $1, $3, $5); }
1150 | REFGEN term /* \$x, \@y, \%z */
1151 { $$ = newUNOP(OP_REFGEN, 0, $2); }
1153 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
1154 | myattrterm %prec UNIOP
1156 | LOCAL term %prec UNIOP
1157 { $$ = localize($2,0); }
1159 { $$ = sawparens($2); }
1163 { $$ = sawparens(newNULLLIST()); }
1172 | arylen %prec '(' /* $#x, $#{ something } */
1173 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1176 | sliceme '[' expr ']' /* array slice */
1177 { $$ = op_prepend_elem(OP_ASLICE,
1178 newOP(OP_PUSHMARK, 0),
1179 newLISTOP(OP_ASLICE, 0,
1181 ref($1, OP_ASLICE)));
1184 $1->op_private & OPpSLICEWARNING;
1186 | kvslice '[' expr ']' /* array key/value slice */
1187 { $$ = op_prepend_elem(OP_KVASLICE,
1188 newOP(OP_PUSHMARK, 0),
1189 newLISTOP(OP_KVASLICE, 0,
1191 ref(oopsAV($1), OP_KVASLICE)));
1194 $1->op_private & OPpSLICEWARNING;
1196 | sliceme '{' expr ';' '}' /* @hash{@keys} */
1197 { $$ = op_prepend_elem(OP_HSLICE,
1198 newOP(OP_PUSHMARK, 0),
1199 newLISTOP(OP_HSLICE, 0,
1201 ref(oopsHV($1), OP_HSLICE)));
1204 $1->op_private & OPpSLICEWARNING;
1206 | kvslice '{' expr ';' '}' /* %hash{@keys} */
1207 { $$ = op_prepend_elem(OP_KVHSLICE,
1208 newOP(OP_PUSHMARK, 0),
1209 newLISTOP(OP_KVHSLICE, 0,
1211 ref($1, OP_KVHSLICE)));
1214 $1->op_private & OPpSLICEWARNING;
1219 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1220 | amper '(' ')' /* &foo() or foo() */
1221 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1223 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1225 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1226 op_append_elem(OP_LIST, $3, scalar($1)));
1228 | NOAMP subname optlistexpr /* foo @args (no parens) */
1229 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1230 op_append_elem(OP_LIST, $3, scalar($2)));
1232 | term ARROW '$' '*'
1233 { $$ = newSVREF($1); }
1234 | term ARROW '@' '*'
1235 { $$ = newAVREF($1); }
1236 | term ARROW '%' '*'
1237 { $$ = newHVREF($1); }
1238 | term ARROW '&' '*'
1239 { $$ = newUNOP(OP_ENTERSUB, 0,
1240 scalar(newCVREF($3,$1))); }
1241 | term ARROW '*' '*' %prec '('
1242 { $$ = newGVREF(0,$1); }
1243 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1244 { $$ = newOP($1, OPf_SPECIAL);
1245 PL_hints |= HINT_BLOCK_SCOPE; }
1247 { $$ = newLOOPEX($1,$2); }
1248 | NOTOP listexpr /* not $foo */
1249 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1250 | UNIOP /* Unary op, $_ implied */
1251 { $$ = newOP($1, 0); }
1252 | UNIOP block /* eval { foo }* */
1253 { $$ = newUNOP($1, 0, $2); }
1254 | UNIOP term /* Unary op */
1255 { $$ = newUNOP($1, 0, $2); }
1256 | REQUIRE /* require, $_ implied */
1257 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1258 | REQUIRE term /* require Foo */
1259 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1261 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1262 | UNIOPSUB term /* Sub treated as unop */
1263 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1264 op_append_elem(OP_LIST, $2, scalar($1))); }
1265 | FUNC0 /* Nullary operator */
1266 { $$ = newOP($1, 0); }
1268 { $$ = newOP($1, 0);}
1269 | FUNC0OP /* Same as above, but op created in toke.c */
1273 | FUNC0SUB /* Sub treated as nullop */
1274 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1275 | FUNC1 '(' ')' /* not () */
1276 { $$ = ($1 == OP_NOT)
1277 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1278 : newOP($1, OPf_SPECIAL); }
1279 | FUNC1 '(' expr ')' /* not($foo) */
1280 { $$ = newUNOP($1, 0, $3); }
1281 | PMFUNC /* m//, s///, qr//, tr/// */
1283 if ( $1->op_type != OP_TRANS
1284 && $1->op_type != OP_TRANSR
1285 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1287 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1288 SAVEFREESV(PL_compcv);
1292 SUBLEXSTART listexpr optrepl SUBLEXEND
1293 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1299 /* "my" declarations, with optional attributes */
1300 myattrterm: MY myterm myattrlist
1301 { $$ = my_attrs($2,$3); }
1303 { $$ = localize($2,1); }
1304 | MY REFGEN myterm myattrlist
1305 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
1308 /* Things that can be "my"'d */
1309 myterm : '(' expr ')'
1310 { $$ = sawparens($2); }
1312 { $$ = sawparens(newNULLLIST()); }
1322 /* Basic list expressions */
1323 optlistexpr: /* NULL */ %prec PREC_LOW
1325 | listexpr %prec PREC_LOW
1341 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1344 { parser->in_my = 0; $$ = my($1); }
1352 refgen_topic: my_var
1356 my_refgen: MY REFGEN
1361 { $$ = newCVREF($1,$2); }
1364 scalar : '$' indirob
1365 { $$ = newSVREF($2); }
1369 { $$ = newAVREF($2);
1370 if ($$) $$->op_private |= $1;
1375 { $$ = newHVREF($2);
1376 if ($$) $$->op_private |= $1;
1380 arylen : DOLSHARP indirob
1381 { $$ = newAVREF($2); }
1382 | term ARROW DOLSHARP '*'
1383 { $$ = newAVREF($1); }
1387 { $$ = newGVREF(0,$2); }
1392 { $$ = newAVREF($1); }
1397 { $$ = newHVREF($1); }
1402 { $$ = newGVREF(0,$1); }
1405 /* Indirect objects */
1407 { $$ = scalar($1); }
1408 | scalar %prec PREC_LOW
1409 { $$ = scalar($1); }
1411 { $$ = op_scope($1); }