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 SV *label = cSVOPx_sv($1);
257 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
258 savepv(SvPVX_const(label)), $2);
263 SV *label = cSVOPx_sv($1);
264 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
265 savepv(SvPVX_const(label)), $2);
270 /* A bare statement, lacking label and other aspects of state op */
273 | FORMAT startformsub formname formblock
275 CV *fmtcv = PL_compcv;
278 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
279 pad_add_weakref(fmtcv);
281 parser->parsed_sub = 1;
283 | SUB subname startsub
284 /* sub declaration or definition not within scope
285 of 'use feature "signatures"'*/
287 init_named_cv(PL_compcv, $2);
289 parser->in_my_stash = NULL;
291 proto subattrlist optsubbody
293 SvREFCNT_inc_simple_void(PL_compcv);
294 $2->op_type == OP_CONST
295 ? newATTRSUB($3, $2, $5, $6, $7)
296 : newMYSUB($3, $2, $5, $6, $7)
300 parser->parsed_sub = 1;
302 | SIGSUB subname startsub
303 /* sub declaration or definition under 'use feature
304 * "signatures"'. (Note that a signature isn't
305 * allowed in a declaration)
308 init_named_cv(PL_compcv, $2);
310 parser->in_my_stash = NULL;
312 subattrlist optsigsubbody
314 SvREFCNT_inc_simple_void(PL_compcv);
315 $2->op_type == OP_CONST
316 ? newATTRSUB($3, $2, NULL, $5, $6)
317 : newMYSUB( $3, $2, NULL, $5, $6)
321 parser->parsed_sub = 1;
323 | PACKAGE BAREWORD BAREWORD ';'
331 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
332 BAREWORD BAREWORD optlistexpr ';'
334 SvREFCNT_inc_simple_void(PL_compcv);
335 utilize($1, $2, $4, $5, $6);
336 parser->parsed_sub = 1;
339 | IF '(' remember mexpr ')' mblock else
342 newCONDOP(0, $4, op_scope($6), $7));
343 parser->copline = (line_t)$1;
345 | UNLESS '(' remember mexpr ')' mblock else
348 newCONDOP(0, $4, $7, op_scope($6)));
349 parser->copline = (line_t)$1;
351 | GIVEN '(' remember mexpr ')' mblock
353 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
354 parser->copline = (line_t)$1;
356 | WHEN '(' remember mexpr ')' mblock
357 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
359 { $$ = newWHENOP(0, op_scope($2)); }
360 | WHILE '(' remember texpr ')' mintro mblock cont
363 newWHILEOP(0, 1, NULL,
365 parser->copline = (line_t)$1;
367 | UNTIL '(' remember iexpr ')' mintro mblock cont
370 newWHILEOP(0, 1, NULL,
372 parser->copline = (line_t)$1;
374 | FOR '(' remember mnexpr ';'
375 { parser->expect = XTERM; }
377 { parser->expect = XTERM; }
382 OP *forop = newWHILEOP(0, 1, NULL,
383 scalar($7), $13, $11, $10);
385 forop = op_prepend_elem(OP_LINESEQ, initop,
386 op_append_elem(OP_LINESEQ,
387 newOP(OP_UNSTACK, OPf_SPECIAL),
390 PL_hints |= HINT_BLOCK_SCOPE;
391 $$ = block_end($3, forop);
392 parser->copline = (line_t)$1;
394 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
396 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
397 parser->copline = (line_t)$1;
399 | FOR scalar '(' remember mexpr ')' mblock cont
401 $$ = block_end($4, newFOROP(0,
402 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
403 parser->copline = (line_t)$1;
405 | FOR my_refgen remember my_var
406 { parser->in_my = 0; $<opval>$ = my($4); }
407 '(' mexpr ')' mblock cont
413 newUNOP(OP_REFGEN, 0,
418 parser->copline = (line_t)$1;
420 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
422 $$ = block_end($5, newFOROP(
423 0, op_lvalue(newUNOP(OP_REFGEN, 0,
425 OP_ENTERLOOP), $6, $8, $9));
426 parser->copline = (line_t)$1;
428 | FOR '(' remember mexpr ')' mblock cont
431 newFOROP(0, NULL, $4, $6, $7));
432 parser->copline = (line_t)$1;
436 /* a block is a loop that happens once */
437 $$ = newWHILEOP(0, 1, NULL,
440 | PACKAGE BAREWORD BAREWORD '{' remember
449 /* a block is a loop that happens once */
450 $$ = newWHILEOP(0, 1, NULL,
451 NULL, block_end($5, $7), NULL, 0);
452 if (parser->copline > (line_t)$4)
453 parser->copline = (line_t)$4;
461 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
462 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
467 parser->copline = NOLINE;
472 formline: THING formarg
476 list = op_append_elem(OP_LIST, $1, term);
481 if (parser->copline == NOLINE)
482 parser->copline = CopLINE(PL_curcop)-1;
483 else parser->copline--;
484 $$ = newSTATEOP(0, NULL,
485 op_convert_list(OP_FORMLINE, 0, list));
491 | FORMLBRACK stmtseq FORMRBRACK
492 { $$ = op_unscope($2); }
495 /* An expression which may have a side-effect */
501 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
503 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
505 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
507 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
509 { $$ = newFOROP(0, NULL, $3, $1, NULL);
510 parser->copline = (line_t)$2; }
512 { $$ = newWHENOP($3, op_scope($1)); }
515 /* else and elsif blocks */
520 ($2)->op_flags |= OPf_PARENS;
523 | ELSIF '(' mexpr ')' mblock else
524 { parser->copline = (line_t)$1;
526 newSTATEOP(OPf_SPECIAL,NULL,$3),
528 PL_hints |= HINT_BLOCK_SCOPE;
532 /* Continue blocks */
536 { $$ = op_scope($2); }
539 /* determine whether there are any new my declarations */
541 { $$ = (PL_min_intro_pending &&
542 PL_max_intro_pending >= PL_min_intro_pending);
545 /* Normal expression */
551 /* Boolean expression */
552 texpr : /* NULL means true */
554 (void)scan_num("1", &tmplval);
555 $$ = tmplval.opval; }
559 /* Inverted boolean expression */
561 { $$ = invert(scalar($1)); }
564 /* Expression with its own lexical scope */
566 { $$ = $1; intro_my(); }
570 { $$ = $1; intro_my(); }
573 formname: BAREWORD { $$ = $1; }
574 | /* NULL */ { $$ = NULL; }
577 startsub: /* NULL */ /* start a regular subroutine scope */
578 { $$ = start_subparse(FALSE, 0);
579 SAVEFREESV(PL_compcv); }
583 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
584 { $$ = start_subparse(FALSE, CVf_ANON);
585 SAVEFREESV(PL_compcv); }
588 startformsub: /* NULL */ /* start a format subroutine scope */
589 { $$ = start_subparse(TRUE, 0);
590 SAVEFREESV(PL_compcv); }
593 /* Name of a subroutine - must be a bareword, could be special */
598 /* Subroutine prototype */
604 /* Optional list of subroutine attributes */
605 subattrlist: /* NULL */
613 /* List of attributes for a "my" variable declaration */
614 myattrlist: COLONATTR THING
622 /* --------------------------------------
623 * subroutine signature parsing
626 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
627 sigvarname: /* NULL */
628 { parser->in_my = 0; $$ = NULL; }
630 { parser->in_my = 0; $$ = $1; }
639 /* @, %, @foo, %foo */
640 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
646 if (parser->sig_slurpy)
647 yyerror("Multiple slurpy parameters not allowed");
648 parser->sig_slurpy = (char)sigil;
651 yyerror("A slurpy parameter may not have "
654 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
658 /* default part of sub signature scalar element: i.e. '= default_expr' */
659 sigdefault: /* NULL */
662 { $$ = newOP(OP_NULL, 0); }
667 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
669 '$' sigvarname sigdefault
674 if (parser->sig_slurpy)
675 yyerror("Slurpy parameter not last");
680 parser->sig_optelems++;
682 if ( defexpr->op_type == OP_NULL
683 && !(defexpr->op_flags & OPf_KIDS))
685 /* handle '$=' special case */
687 yyerror("Optional parameter "
688 "lacks default expression");
692 /* a normal '=default' expression */
693 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
696 /* re-purpose op_targ to hold @_ index */
698 (PADOFFSET)(parser->sig_elems - 1);
701 var->op_flags |= OPf_STACKED;
702 (void)op_sibling_splice(var,
707 var = newUNOP(OP_NULL, 0, defop);
710 /* NB: normally the first child of a
711 * logop is executed before the logop,
712 * and it pushes a boolean result
713 * ready for the logop. For ARGDEFELEM,
714 * the op itself does the boolean
715 * calculation, so set the first op to
718 var->op_next = defop;
719 defexpr->op_next = var;
723 if (parser->sig_optelems)
724 yyerror("Mandatory parameter "
725 "follows optional parameter");
728 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
733 /* subroutine signature element: e.g. '$x = $default' or '%h' */
734 sigelem: sigscalarelem
735 { parser->in_my = KEY_sigvar; $$ = $1; }
737 { parser->in_my = KEY_sigvar; $$ = $1; }
740 /* list of subroutine signature elements */
744 | siglist ',' sigelem
746 $$ = op_append_list(OP_LINESEQ, $1, $3);
748 | sigelem %prec PREC_LOW
753 siglistornull: /* NULL */
758 /* optional subroutine signature */
759 optsubsignature: /* NULL */
764 /* Subroutine signature */
768 SAVEIV(parser->sig_elems);
769 SAVEIV(parser->sig_optelems);
770 SAVEI8(parser->sig_slurpy);
771 parser->sig_elems = 0;
772 parser->sig_optelems = 0;
773 parser->sig_slurpy = 0;
774 parser->in_my = KEY_sigvar;
783 if (!FEATURE_SIGNATURES_IS_ENABLED)
784 Perl_croak(aTHX_ "Experimental "
785 "subroutine signatures not enabled");
787 /* We shouldn't get here otherwise */
788 Perl_ck_warner_d(aTHX_
789 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
790 "The signatures feature is experimental");
792 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
793 sizeof(UNOP_AUX_item) * 3);
794 aux[0].iv = parser->sig_elems;
795 aux[1].iv = parser->sig_optelems;
796 aux[2].iv = parser->sig_slurpy;
797 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
798 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
799 sigops = op_prepend_elem(OP_LINESEQ,
800 newSTATEOP(0, NULL, NULL),
802 /* a nextstate at the end handles context
803 * correctly for an empty sub body */
804 $$ = op_append_elem(OP_LINESEQ,
806 newSTATEOP(0, NULL, NULL));
809 /* tell the toker that attrributes can follow
810 * this sig, but only so that the toker
811 * can skip through any (illegal) trailing
812 * attribute text then give a useful error
813 * message about "attributes before sig",
814 * rather than falling over ina mess at
815 * unrecognised syntax.
817 parser->expect = XATTRBLOCK;
818 parser->sig_seen = TRUE;
823 /* Optional subroutine body (for named subroutine declaration) */
824 optsubbody: subbody { $$ = $1; }
829 /* Subroutine body (without signature) */
830 subbody: remember '{' stmtseq '}'
832 if (parser->copline > (line_t)$2)
833 parser->copline = (line_t)$2;
834 $$ = block_end($1, $3);
839 /* optional [ Subroutine body with optional signature ] (for named
840 * subroutine declaration) */
841 optsigsubbody: sigsubbody { $$ = $1; }
844 /* Subroutine body with optional signature */
845 sigsubbody: remember optsubsignature '{' stmtseq '}'
847 if (parser->copline > (line_t)$3)
848 parser->copline = (line_t)$3;
850 op_append_list(OP_LINESEQ, $2, $4));
855 /* Ordinary expressions; logical combinations */
856 expr : expr ANDOP expr
857 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
859 { $$ = newLOGOP($2, 0, $1, $3); }
861 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
862 | listexpr %prec PREC_LOW
865 /* Expressions are a list of terms joined by commas */
866 listexpr: listexpr ','
871 $$ = op_append_elem(OP_LIST, $1, term);
873 | term %prec PREC_LOW
877 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
878 { $$ = op_convert_list($1, OPf_STACKED,
879 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
881 | FUNC '(' indirob expr ')' /* print ($fh @args */
882 { $$ = op_convert_list($1, OPf_STACKED,
883 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
885 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
886 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
887 op_append_elem(OP_LIST,
888 op_prepend_elem(OP_LIST, scalar($1), $5),
889 newMETHOP(OP_METHOD, 0, $3)));
891 | term ARROW method /* $foo->bar */
892 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
893 op_append_elem(OP_LIST, scalar($1),
894 newMETHOP(OP_METHOD, 0, $3)));
896 | METHOD indirob optlistexpr /* new Class @args */
897 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
898 op_append_elem(OP_LIST,
899 op_prepend_elem(OP_LIST, $2, $3),
900 newMETHOP(OP_METHOD, 0, $1)));
902 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
903 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
904 op_append_elem(OP_LIST,
905 op_prepend_elem(OP_LIST, $2, $4),
906 newMETHOP(OP_METHOD, 0, $1)));
908 | LSTOP optlistexpr /* print @args */
909 { $$ = op_convert_list($1, 0, $2); }
910 | FUNC '(' optexpr ')' /* print (@args) */
911 { $$ = op_convert_list($1, 0, $3); }
912 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
913 { SvREFCNT_inc_simple_void(PL_compcv);
914 $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
915 optlistexpr %prec LSTOP /* ... @bar */
916 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
917 op_append_elem(OP_LIST,
918 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
922 /* Names of methods. May use $object->$methodname */
927 /* Some kind of subscripted expression */
928 subscripted: gelem '{' expr ';' '}' /* *main::{something} */
929 /* In this and all the hash accessors, ';' is
930 * provided by the tokeniser */
931 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
932 | scalar '[' expr ']' /* $array[$element] */
933 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
935 | term ARROW '[' expr ']' /* somearef->[$element] */
936 { $$ = newBINOP(OP_AELEM, 0,
937 ref(newAVREF($1),OP_RV2AV),
940 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
941 { $$ = newBINOP(OP_AELEM, 0,
942 ref(newAVREF($1),OP_RV2AV),
945 | scalar '{' expr ';' '}' /* $foo{bar();} */
946 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
948 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
949 { $$ = newBINOP(OP_HELEM, 0,
950 ref(newHVREF($1),OP_RV2HV),
952 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
953 { $$ = newBINOP(OP_HELEM, 0,
954 ref(newHVREF($1),OP_RV2HV),
956 | term ARROW '(' ')' /* $subref->() */
957 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
958 newCVREF(0, scalar($1)));
959 if (parser->expect == XBLOCK)
960 parser->expect = XOPERATOR;
962 | term ARROW '(' expr ')' /* $subref->(@args) */
963 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
964 op_append_elem(OP_LIST, $4,
965 newCVREF(0, scalar($1))));
966 if (parser->expect == XBLOCK)
967 parser->expect = XOPERATOR;
970 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
971 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
972 op_append_elem(OP_LIST, $3,
973 newCVREF(0, scalar($1))));
974 if (parser->expect == XBLOCK)
975 parser->expect = XOPERATOR;
977 | subscripted '(' ')' /* $foo->{bar}->() */
978 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
979 newCVREF(0, scalar($1)));
980 if (parser->expect == XBLOCK)
981 parser->expect = XOPERATOR;
983 | '(' expr ')' '[' expr ']' /* list slice */
984 { $$ = newSLICEOP(0, $5, $2); }
985 | QWLIST '[' expr ']' /* list literal slice */
986 { $$ = newSLICEOP(0, $3, $1); }
987 | '(' ')' '[' expr ']' /* empty list slice! */
988 { $$ = newSLICEOP(0, $4, NULL); }
991 /* Binary operators between terms */
992 termbinop: term ASSIGNOP term /* $x = $y, $x += $y */
993 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
994 | term POWOP term /* $x ** $y */
995 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
996 | term MULOP term /* $x * $y, $x x $y */
997 { if ($2 != OP_REPEAT)
999 $$ = newBINOP($2, 0, $1, scalar($3));
1001 | term ADDOP term /* $x + $y */
1002 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1003 | term SHIFTOP term /* $x >> $y, $x << $y */
1004 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1005 | term RELOP term /* $x > $y, etc. */
1006 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1007 | term EQOP term /* $x == $y, $x eq $y */
1008 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1009 | term BITANDOP term /* $x & $y */
1010 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1011 | term BITOROP term /* $x | $y */
1012 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
1013 | term DOTDOT term /* $x..$y, $x...$y */
1014 { $$ = newRANGE($2, scalar($1), scalar($3)); }
1015 | term ANDAND term /* $x && $y */
1016 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
1017 | term OROR term /* $x || $y */
1018 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
1019 | term DORDOR term /* $x // $y */
1020 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
1021 | term MATCHOP term /* $x =~ /$y/ */
1022 { $$ = bind_match($2, $1, $3); }
1025 /* Unary operators and terms */
1026 termunop : '-' term %prec UMINUS /* -$x */
1027 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
1028 | '+' term %prec UMINUS /* +$x */
1031 | '!' term /* !$x */
1032 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1033 | '~' term /* ~$x */
1034 { $$ = newUNOP($1, 0, scalar($2)); }
1035 | term POSTINC /* $x++ */
1036 { $$ = newUNOP(OP_POSTINC, 0,
1037 op_lvalue(scalar($1), OP_POSTINC)); }
1038 | term POSTDEC /* $x-- */
1039 { $$ = newUNOP(OP_POSTDEC, 0,
1040 op_lvalue(scalar($1), OP_POSTDEC));}
1041 | term POSTJOIN /* implicit join after interpolated ->@ */
1042 { $$ = op_convert_list(OP_JOIN, 0,
1052 | PREINC term /* ++$x */
1053 { $$ = newUNOP(OP_PREINC, 0,
1054 op_lvalue(scalar($2), OP_PREINC)); }
1055 | PREDEC term /* --$x */
1056 { $$ = newUNOP(OP_PREDEC, 0,
1057 op_lvalue(scalar($2), OP_PREDEC)); }
1061 /* Constructors for anonymous data */
1062 anonymous: '[' expr ']'
1063 { $$ = newANONLIST($2); }
1065 { $$ = newANONLIST(NULL);}
1066 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1067 { $$ = newANONHASH($2); }
1068 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1069 { $$ = newANONHASH(NULL); }
1070 | ANONSUB startanonsub proto subattrlist subbody %prec '('
1071 { SvREFCNT_inc_simple_void(PL_compcv);
1072 $$ = newANONATTRSUB($2, $3, $4, $5); }
1073 | ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '('
1074 { SvREFCNT_inc_simple_void(PL_compcv);
1075 $$ = newANONATTRSUB($2, NULL, $3, $4); }
1078 /* Things called with "do" */
1079 termdo : DO term %prec UNIOP /* do $filename */
1080 { $$ = dofile($2, $1);}
1081 | DO block %prec '(' /* do { code */
1082 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
1089 | term '?' term ':' term
1090 { $$ = newCONDOP(0, $1, $3, $5); }
1091 | REFGEN term /* \$x, \@y, \%z */
1092 { $$ = newUNOP(OP_REFGEN, 0, $2); }
1094 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
1095 | myattrterm %prec UNIOP
1097 | LOCAL term %prec UNIOP
1098 { $$ = localize($2,0); }
1100 { $$ = sawparens($2); }
1104 { $$ = sawparens(newNULLLIST()); }
1113 | arylen %prec '(' /* $#x, $#{ something } */
1114 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1117 | sliceme '[' expr ']' /* array slice */
1118 { $$ = op_prepend_elem(OP_ASLICE,
1119 newOP(OP_PUSHMARK, 0),
1120 newLISTOP(OP_ASLICE, 0,
1122 ref($1, OP_ASLICE)));
1125 $1->op_private & OPpSLICEWARNING;
1127 | kvslice '[' expr ']' /* array key/value slice */
1128 { $$ = op_prepend_elem(OP_KVASLICE,
1129 newOP(OP_PUSHMARK, 0),
1130 newLISTOP(OP_KVASLICE, 0,
1132 ref(oopsAV($1), OP_KVASLICE)));
1135 $1->op_private & OPpSLICEWARNING;
1137 | sliceme '{' expr ';' '}' /* @hash{@keys} */
1138 { $$ = op_prepend_elem(OP_HSLICE,
1139 newOP(OP_PUSHMARK, 0),
1140 newLISTOP(OP_HSLICE, 0,
1142 ref(oopsHV($1), OP_HSLICE)));
1145 $1->op_private & OPpSLICEWARNING;
1147 | kvslice '{' expr ';' '}' /* %hash{@keys} */
1148 { $$ = op_prepend_elem(OP_KVHSLICE,
1149 newOP(OP_PUSHMARK, 0),
1150 newLISTOP(OP_KVHSLICE, 0,
1152 ref($1, OP_KVHSLICE)));
1155 $1->op_private & OPpSLICEWARNING;
1160 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1161 | amper '(' ')' /* &foo() or foo() */
1162 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1164 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1166 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1167 op_append_elem(OP_LIST, $3, scalar($1)));
1169 | NOAMP subname optlistexpr /* foo @args (no parens) */
1170 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1171 op_append_elem(OP_LIST, $3, scalar($2)));
1173 | term ARROW '$' '*'
1174 { $$ = newSVREF($1); }
1175 | term ARROW '@' '*'
1176 { $$ = newAVREF($1); }
1177 | term ARROW '%' '*'
1178 { $$ = newHVREF($1); }
1179 | term ARROW '&' '*'
1180 { $$ = newUNOP(OP_ENTERSUB, 0,
1181 scalar(newCVREF($3,$1))); }
1182 | term ARROW '*' '*' %prec '('
1183 { $$ = newGVREF(0,$1); }
1184 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1185 { $$ = newOP($1, OPf_SPECIAL);
1186 PL_hints |= HINT_BLOCK_SCOPE; }
1188 { $$ = newLOOPEX($1,$2); }
1189 | NOTOP listexpr /* not $foo */
1190 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1191 | UNIOP /* Unary op, $_ implied */
1192 { $$ = newOP($1, 0); }
1193 | UNIOP block /* eval { foo }* */
1194 { $$ = newUNOP($1, 0, $2); }
1195 | UNIOP term /* Unary op */
1196 { $$ = newUNOP($1, 0, $2); }
1197 | REQUIRE /* require, $_ implied */
1198 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1199 | REQUIRE term /* require Foo */
1200 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1202 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1203 | UNIOPSUB term /* Sub treated as unop */
1204 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1205 op_append_elem(OP_LIST, $2, scalar($1))); }
1206 | FUNC0 /* Nullary operator */
1207 { $$ = newOP($1, 0); }
1209 { $$ = newOP($1, 0);}
1210 | FUNC0OP /* Same as above, but op created in toke.c */
1214 | FUNC0SUB /* Sub treated as nullop */
1215 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1216 | FUNC1 '(' ')' /* not () */
1217 { $$ = ($1 == OP_NOT)
1218 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1219 : newOP($1, OPf_SPECIAL); }
1220 | FUNC1 '(' expr ')' /* not($foo) */
1221 { $$ = newUNOP($1, 0, $3); }
1222 | PMFUNC /* m//, s///, qr//, tr/// */
1224 if ( $1->op_type != OP_TRANS
1225 && $1->op_type != OP_TRANSR
1226 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1228 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1229 SAVEFREESV(PL_compcv);
1233 '(' listexpr optrepl ')'
1234 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1240 /* "my" declarations, with optional attributes */
1241 myattrterm: MY myterm myattrlist
1242 { $$ = my_attrs($2,$3); }
1244 { $$ = localize($2,1); }
1245 | MY REFGEN myterm myattrlist
1246 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
1249 /* Things that can be "my"'d */
1250 myterm : '(' expr ')'
1251 { $$ = sawparens($2); }
1253 { $$ = sawparens(newNULLLIST()); }
1263 /* Basic list expressions */
1264 optlistexpr: /* NULL */ %prec PREC_LOW
1266 | listexpr %prec PREC_LOW
1282 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1285 { parser->in_my = 0; $$ = my($1); }
1293 refgen_topic: my_var
1297 my_refgen: MY REFGEN
1302 { $$ = newCVREF($1,$2); }
1305 scalar : '$' indirob
1306 { $$ = newSVREF($2); }
1310 { $$ = newAVREF($2);
1311 if ($$) $$->op_private |= $1;
1316 { $$ = newHVREF($2);
1317 if ($$) $$->op_private |= $1;
1321 arylen : DOLSHARP indirob
1322 { $$ = newAVREF($2); }
1323 | term ARROW DOLSHARP '*'
1324 { $$ = newAVREF($1); }
1328 { $$ = newGVREF(0,$2); }
1333 { $$ = newAVREF($1); }
1338 { $$ = newHVREF($1); }
1343 { $$ = newGVREF(0,$1); }
1346 /* Indirect objects */
1348 { $$ = scalar($1); }
1349 | scalar %prec PREC_LOW
1350 { $$ = scalar($1); }
1352 { $$ = op_scope($1); }