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> 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
93 %nonassoc DOTDOT YADAYADA
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;
123 newPROG(block_end($3,$4));
124 PL_compiling.cop_seq = 0;
129 parser->expect = XTERM;
138 parser->expect = XBLOCK;
142 PL_pad_reset_pending = TRUE;
146 parser->yychar = YYEOF;
150 parser->expect = XSTATE;
154 PL_pad_reset_pending = TRUE;
158 parser->yychar = YYEOF;
162 parser->expect = XSTATE;
166 PL_pad_reset_pending = TRUE;
170 parser->yychar = YYEOF;
174 parser->expect = XSTATE;
183 /* An ordinary block */
184 block : '{' remember stmtseq '}'
185 { if (parser->copline > (line_t)$1)
186 parser->copline = (line_t)$1;
187 $$ = block_end($2, $3);
192 formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
193 { if (parser->copline > (line_t)$1)
194 parser->copline = (line_t)$1;
195 $$ = block_end($2, $5);
199 remember: /* NULL */ /* start a full lexical scope */
200 { $$ = block_start(TRUE);
201 parser->parsed_sub = 0; }
204 mblock : '{' mremember stmtseq '}'
205 { if (parser->copline > (line_t)$1)
206 parser->copline = (line_t)$1;
207 $$ = block_end($2, $3);
211 mremember: /* NULL */ /* start a partial lexical scope */
212 { $$ = block_start(FALSE);
213 parser->parsed_sub = 0; }
216 /* A sequence of statements in the program */
220 { $$ = op_append_list(OP_LINESEQ, $1, $2);
221 PL_pad_reset_pending = TRUE;
223 PL_hints |= HINT_BLOCK_SCOPE;
227 /* A sequence of format lines */
228 formstmtseq: /* NULL */
230 | formstmtseq formline
231 { $$ = op_append_list(OP_LINESEQ, $1, $2);
232 PL_pad_reset_pending = TRUE;
234 PL_hints |= HINT_BLOCK_SCOPE;
238 /* A statement in the program, including optional labels */
241 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
247 labfullstmt: LABEL barestmt
249 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
253 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
257 /* A bare statement, lacking label and other aspects of state op */
260 | FORMAT startformsub formname formblock
262 CV *fmtcv = PL_compcv;
265 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
266 pad_add_weakref(fmtcv);
268 parser->parsed_sub = 1;
270 | SUB subname startsub
272 if ($2->op_type == OP_CONST) {
273 const char *const name =
274 SvPV_nolen_const(((SVOP*)$2)->op_sv);
275 if (strEQ(name, "BEGIN") || strEQ(name, "END")
276 || strEQ(name, "INIT") || strEQ(name, "CHECK")
277 || strEQ(name, "UNITCHECK"))
278 CvSPECIAL_on(PL_compcv);
281 /* State subs inside anonymous subs need to be
282 clonable themselves. */
283 if (CvANON(CvOUTSIDE(PL_compcv))
284 || CvCLONE(CvOUTSIDE(PL_compcv))
285 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
288 CvCLONE_on(PL_compcv);
290 parser->in_my_stash = NULL;
292 proto subattrlist optsubbody
294 SvREFCNT_inc_simple_void(PL_compcv);
295 $2->op_type == OP_CONST
296 ? newATTRSUB($3, $2, $5, $6, $7)
297 : newMYSUB($3, $2, $5, $6, $7)
301 parser->parsed_sub = 1;
303 | SUB subname startsub
305 if ($2->op_type == OP_CONST) {
306 const char *const name =
307 SvPV_nolen_const(((SVOP*)$2)->op_sv);
308 if (strEQ(name, "BEGIN") || strEQ(name, "END")
309 || strEQ(name, "INIT") || strEQ(name, "CHECK")
310 || strEQ(name, "UNITCHECK"))
311 CvSPECIAL_on(PL_compcv);
314 /* State subs inside anonymous subs need to be
315 clonable themselves. */
316 if (CvANON(CvOUTSIDE(PL_compcv))
317 || CvCLONE(CvOUTSIDE(PL_compcv))
318 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
321 CvCLONE_on(PL_compcv);
323 parser->in_my_stash = NULL;
325 remember subsignature subattrlist '{' stmtseq '}'
328 if (parser->copline > (line_t)$8)
329 parser->copline = (line_t)$8;
331 op_append_list(OP_LINESEQ, $6, $9));
333 SvREFCNT_inc_simple_void(PL_compcv);
334 $2->op_type == OP_CONST
335 ? newATTRSUB($3, $2, NULL, $7, body)
336 : newMYSUB($3, $2, NULL, $7, body)
340 parser->parsed_sub = 1;
342 | PACKAGE BAREWORD BAREWORD ';'
350 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
351 BAREWORD BAREWORD optlistexpr ';'
353 SvREFCNT_inc_simple_void(PL_compcv);
354 utilize($1, $2, $4, $5, $6);
355 parser->parsed_sub = 1;
358 | IF '(' remember mexpr ')' mblock else
361 newCONDOP(0, $4, op_scope($6), $7));
362 parser->copline = (line_t)$1;
364 | UNLESS '(' remember mexpr ')' mblock else
367 newCONDOP(0, $4, $7, op_scope($6)));
368 parser->copline = (line_t)$1;
370 | GIVEN '(' remember mexpr ')' mblock
372 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
373 parser->copline = (line_t)$1;
375 | WHEN '(' remember mexpr ')' mblock
376 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
378 { $$ = newWHENOP(0, op_scope($2)); }
379 | WHILE '(' remember texpr ')' mintro mblock cont
382 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
384 parser->copline = (line_t)$1;
386 | UNTIL '(' remember iexpr ')' mintro mblock cont
389 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
391 parser->copline = (line_t)$1;
393 | FOR '(' remember mnexpr ';'
394 { parser->expect = XTERM; }
396 { parser->expect = XTERM; }
401 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
402 scalar($7), $13, $11, $10);
404 forop = op_prepend_elem(OP_LINESEQ, initop,
405 op_append_elem(OP_LINESEQ,
406 newOP(OP_UNSTACK, OPf_SPECIAL),
409 PL_hints |= HINT_BLOCK_SCOPE;
410 $$ = block_end($3, forop);
411 parser->copline = (line_t)$1;
413 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
415 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
416 parser->copline = (line_t)$1;
418 | FOR scalar '(' remember mexpr ')' mblock cont
420 $$ = block_end($4, newFOROP(0,
421 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
422 parser->copline = (line_t)$1;
424 | FOR my_refgen remember my_var
425 { parser->in_my = 0; $<opval>$ = my($4); }
426 '(' mexpr ')' mblock cont
432 newUNOP(OP_REFGEN, 0,
437 parser->copline = (line_t)$1;
439 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
441 $$ = block_end($5, newFOROP(
442 0, op_lvalue(newUNOP(OP_REFGEN, 0,
444 OP_ENTERLOOP), $6, $8, $9));
445 parser->copline = (line_t)$1;
447 | FOR '(' remember mexpr ')' mblock cont
450 newFOROP(0, (OP*)NULL, $4, $6, $7));
451 parser->copline = (line_t)$1;
455 /* a block is a loop that happens once */
456 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
457 (OP*)NULL, $1, $2, 0);
459 | PACKAGE BAREWORD BAREWORD '{' remember
468 /* a block is a loop that happens once */
469 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
470 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
471 if (parser->copline > (line_t)$4)
472 parser->copline = (line_t)$4;
481 parser->copline = NOLINE;
486 formline: THING formarg
490 list = op_append_elem(OP_LIST, $1, term);
495 if (parser->copline == NOLINE)
496 parser->copline = CopLINE(PL_curcop)-1;
497 else parser->copline--;
498 $$ = newSTATEOP(0, NULL,
499 op_convert_list(OP_FORMLINE, 0, list));
505 | FORMLBRACK stmtseq FORMRBRACK
506 { $$ = op_unscope($2); }
509 /* An expression which may have a side-effect */
515 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
517 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
519 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
521 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
523 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
524 parser->copline = (line_t)$2; }
526 { $$ = newWHENOP($3, op_scope($1)); }
529 /* else and elsif blocks */
534 ($2)->op_flags |= OPf_PARENS;
537 | ELSIF '(' mexpr ')' mblock else
538 { parser->copline = (line_t)$1;
540 newSTATEOP(OPf_SPECIAL,NULL,$3),
542 PL_hints |= HINT_BLOCK_SCOPE;
546 /* Continue blocks */
550 { $$ = op_scope($2); }
553 /* determine whether there are any new my declarations */
555 { $$ = (PL_min_intro_pending &&
556 PL_max_intro_pending >= PL_min_intro_pending);
559 /* Normal expression */
565 /* Boolean expression */
566 texpr : /* NULL means true */
568 (void)scan_num("1", &tmplval);
569 $$ = tmplval.opval; }
573 /* Inverted boolean expression */
575 { $$ = invert(scalar($1)); }
578 /* Expression with its own lexical scope */
580 { $$ = $1; intro_my(); }
584 { $$ = $1; intro_my(); }
587 formname: BAREWORD { $$ = $1; }
588 | /* NULL */ { $$ = (OP*)NULL; }
591 startsub: /* NULL */ /* start a regular subroutine scope */
592 { $$ = start_subparse(FALSE, 0);
593 SAVEFREESV(PL_compcv); }
597 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
598 { $$ = start_subparse(FALSE, CVf_ANON);
599 SAVEFREESV(PL_compcv); }
602 startformsub: /* NULL */ /* start a format subroutine scope */
603 { $$ = start_subparse(TRUE, 0);
604 SAVEFREESV(PL_compcv); }
607 /* Name of a subroutine - must be a bareword, could be special */
612 /* Subroutine prototype */
618 /* Optional list of subroutine attributes */
619 subattrlist: /* NULL */
627 /* List of attributes for a "my" variable declaration */
628 myattrlist: COLONATTR THING
636 /* --------------------------------------
637 * subroutine signature parsing
640 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
641 sigvarname: /* NULL */
653 /* @, %, @foo, %foo */
654 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
660 if (parser->sig_slurpy)
661 yyerror("Multiple slurpy parameters not allowed");
662 parser->sig_slurpy = (char)sigil;
665 yyerror("A slurpy parameter may not have "
668 $$ = var ? newSTATEOP(0, NULL, var) : (OP*)NULL;
672 /* default part of sub signature scalar element: i.e. '= default_expr' */
673 sigdefault: /* NULL */
676 { $$ = newOP(OP_NULL, 0); }
681 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
683 '$' sigvarname sigdefault
688 if (parser->sig_slurpy)
689 yyerror("Slurpy parameter not last");
694 parser->sig_optelems++;
696 if ( defexpr->op_type == OP_NULL
697 && !(defexpr->op_flags & OPf_KIDS))
699 /* handle '$=' special case */
701 yyerror("Optional parameter "
702 "lacks default expression");
706 /* a normal '=default' expression */
707 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
710 /* re-purpose op_targ to hold @_ index */
712 (PADOFFSET)(parser->sig_elems - 1);
715 var->op_flags |= OPf_STACKED;
716 (void)op_sibling_splice(var,
721 var = newUNOP(OP_NULL, 0, defop);
724 /* NB: normally the first child of a
725 * logop is executed before the logop,
726 * and it pushes a boolean result
727 * ready for the logop. For ARGDEFELEM,
728 * the op itself does the boolean
729 * calculation, so set the first op to
732 var->op_next = defop;
733 defexpr->op_next = var;
737 if (parser->sig_optelems)
738 yyerror("Mandatory parameter "
739 "follows optional parameter");
742 $$ = var ? newSTATEOP(0, NULL, var) : (OP*)NULL;
747 /* subroutine signature element: e.g. '$x = $default' or '%h' */
748 sigelem: sigscalarelem
749 { parser->expect = XSIGVAR; $$ = $1; }
751 { parser->expect = XSIGVAR; $$ = $1; }
754 /* list of subroutine signature elements */
758 | siglist ',' sigelem
760 $$ = op_append_list(OP_LINESEQ, $1, $3);
762 | sigelem %prec PREC_LOW
767 siglistornull: /* NULL */
772 /* Subroutine signature */
776 SAVEIV(parser->sig_elems);
777 SAVEIV(parser->sig_optelems);
778 SAVEI8(parser->sig_slurpy);
779 parser->sig_elems = 0;
780 parser->sig_optelems = 0;
781 parser->sig_slurpy = 0;
782 parser->expect = XSIGVAR;
791 assert(FEATURE_SIGNATURES_IS_ENABLED);
793 /* We shouldn't get here otherwise */
794 Perl_ck_warner_d(aTHX_
795 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
796 "The signatures feature is experimental");
798 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
799 sizeof(UNOP_AUX_item) * 3);
800 aux[0].iv = parser->sig_elems;
801 aux[1].iv = parser->sig_optelems;
802 aux[2].iv = parser->sig_slurpy;
803 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
804 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
805 sigops = op_prepend_elem(OP_LINESEQ,
806 newSTATEOP(0, NULL, NULL),
808 /* a nextstate at the end handles context
809 * correctly for an empty sub body */
810 $$ = op_append_elem(OP_LINESEQ,
812 newSTATEOP(0, NULL, NULL));
814 parser->expect = XATTRBLOCK;
821 /* Optional subroutine body, for named subroutine declaration */
823 | ';' { $$ = (OP*)NULL; }
826 /* Ordinary expressions; logical combinations */
827 expr : expr ANDOP expr
828 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
830 { $$ = newLOGOP($2, 0, $1, $3); }
832 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
833 | listexpr %prec PREC_LOW
836 /* Expressions are a list of terms joined by commas */
837 listexpr: listexpr ','
842 $$ = op_append_elem(OP_LIST, $1, term);
844 | term %prec PREC_LOW
848 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
849 { $$ = op_convert_list($1, OPf_STACKED,
850 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
852 | FUNC '(' indirob expr ')' /* print ($fh @args */
853 { $$ = op_convert_list($1, OPf_STACKED,
854 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
856 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
857 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
858 op_append_elem(OP_LIST,
859 op_prepend_elem(OP_LIST, scalar($1), $5),
860 newMETHOP(OP_METHOD, 0, $3)));
862 | term ARROW method /* $foo->bar */
863 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
864 op_append_elem(OP_LIST, scalar($1),
865 newMETHOP(OP_METHOD, 0, $3)));
867 | METHOD indirob optlistexpr /* new Class @args */
868 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
869 op_append_elem(OP_LIST,
870 op_prepend_elem(OP_LIST, $2, $3),
871 newMETHOP(OP_METHOD, 0, $1)));
873 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
874 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
875 op_append_elem(OP_LIST,
876 op_prepend_elem(OP_LIST, $2, $4),
877 newMETHOP(OP_METHOD, 0, $1)));
879 | LSTOP optlistexpr /* print @args */
880 { $$ = op_convert_list($1, 0, $2); }
881 | FUNC '(' optexpr ')' /* print (@args) */
882 { $$ = op_convert_list($1, 0, $3); }
883 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
884 { SvREFCNT_inc_simple_void(PL_compcv);
885 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
886 optlistexpr %prec LSTOP /* ... @bar */
887 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
888 op_append_elem(OP_LIST,
889 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
893 /* Names of methods. May use $object->$methodname */
898 /* Some kind of subscripted expression */
899 subscripted: gelem '{' expr ';' '}' /* *main::{something} */
900 /* In this and all the hash accessors, ';' is
901 * provided by the tokeniser */
902 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
903 | scalar '[' expr ']' /* $array[$element] */
904 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
906 | term ARROW '[' expr ']' /* somearef->[$element] */
907 { $$ = newBINOP(OP_AELEM, 0,
908 ref(newAVREF($1),OP_RV2AV),
911 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
912 { $$ = newBINOP(OP_AELEM, 0,
913 ref(newAVREF($1),OP_RV2AV),
916 | scalar '{' expr ';' '}' /* $foo{bar();} */
917 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
919 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
920 { $$ = newBINOP(OP_HELEM, 0,
921 ref(newHVREF($1),OP_RV2HV),
923 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
924 { $$ = newBINOP(OP_HELEM, 0,
925 ref(newHVREF($1),OP_RV2HV),
927 | term ARROW '(' ')' /* $subref->() */
928 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
929 newCVREF(0, scalar($1))); }
930 | term ARROW '(' expr ')' /* $subref->(@args) */
931 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
932 op_append_elem(OP_LIST, $4,
933 newCVREF(0, scalar($1)))); }
935 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
936 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
937 op_append_elem(OP_LIST, $3,
938 newCVREF(0, scalar($1)))); }
939 | subscripted '(' ')' /* $foo->{bar}->() */
940 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
941 newCVREF(0, scalar($1))); }
942 | '(' expr ')' '[' expr ']' /* list slice */
943 { $$ = newSLICEOP(0, $5, $2); }
944 | QWLIST '[' expr ']' /* list literal slice */
945 { $$ = newSLICEOP(0, $3, $1); }
946 | '(' ')' '[' expr ']' /* empty list slice! */
947 { $$ = newSLICEOP(0, $4, (OP*)NULL); }
950 /* Binary operators between terms */
951 termbinop: term ASSIGNOP term /* $x = $y */
952 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
953 | term POWOP term /* $x ** $y */
954 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
955 | term MULOP term /* $x * $y, $x x $y */
956 { if ($2 != OP_REPEAT)
958 $$ = newBINOP($2, 0, $1, scalar($3));
960 | term ADDOP term /* $x + $y */
961 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
962 | term SHIFTOP term /* $x >> $y, $x << $y */
963 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
964 | term RELOP term /* $x > $y, etc. */
965 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
966 | term EQOP term /* $x == $y, $x eq $y */
967 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
968 | term BITANDOP term /* $x & $y */
969 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
970 | term BITOROP term /* $x | $y */
971 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
972 | term DOTDOT term /* $x..$y, $x...$y */
973 { $$ = newRANGE($2, scalar($1), scalar($3)); }
974 | term ANDAND term /* $x && $y */
975 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
976 | term OROR term /* $x || $y */
977 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
978 | term DORDOR term /* $x // $y */
979 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
980 | term MATCHOP term /* $x =~ /$y/ */
981 { $$ = bind_match($2, $1, $3); }
984 /* Unary operators and terms */
985 termunop : '-' term %prec UMINUS /* -$x */
986 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
987 | '+' term %prec UMINUS /* +$x */
991 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
993 { $$ = newUNOP($1, 0, scalar($2)); }
994 | term POSTINC /* $x++ */
995 { $$ = newUNOP(OP_POSTINC, 0,
996 op_lvalue(scalar($1), OP_POSTINC)); }
997 | term POSTDEC /* $x-- */
998 { $$ = newUNOP(OP_POSTDEC, 0,
999 op_lvalue(scalar($1), OP_POSTDEC));}
1000 | term POSTJOIN /* implicit join after interpolated ->@ */
1001 { $$ = op_convert_list(OP_JOIN, 0,
1011 | PREINC term /* ++$x */
1012 { $$ = newUNOP(OP_PREINC, 0,
1013 op_lvalue(scalar($2), OP_PREINC)); }
1014 | PREDEC term /* --$x */
1015 { $$ = newUNOP(OP_PREDEC, 0,
1016 op_lvalue(scalar($2), OP_PREDEC)); }
1020 /* Constructors for anonymous data */
1021 anonymous: '[' expr ']'
1022 { $$ = newANONLIST($2); }
1024 { $$ = newANONLIST((OP*)NULL);}
1025 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1026 { $$ = newANONHASH($2); }
1027 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1028 { $$ = newANONHASH((OP*)NULL); }
1029 | ANONSUB startanonsub proto subattrlist block %prec '('
1030 { SvREFCNT_inc_simple_void(PL_compcv);
1031 $$ = newANONATTRSUB($2, $3, $4, $5); }
1032 | ANONSUB startanonsub remember subsignature subattrlist '{' stmtseq '}' %prec '('
1035 if (parser->copline > (line_t)$6)
1036 parser->copline = (line_t)$6;
1037 body = block_end($3,
1038 op_append_list(OP_LINESEQ, $4, $7));
1039 SvREFCNT_inc_simple_void(PL_compcv);
1040 $$ = newANONATTRSUB($2, NULL, $5, body);
1045 /* Things called with "do" */
1046 termdo : DO term %prec UNIOP /* do $filename */
1047 { $$ = dofile($2, $1);}
1048 | DO block %prec '(' /* do { code */
1049 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
1056 | term '?' term ':' term
1057 { $$ = newCONDOP(0, $1, $3, $5); }
1058 | REFGEN term /* \$x, \@y, \%z */
1059 { $$ = newUNOP(OP_REFGEN, 0, $2); }
1061 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
1062 | myattrterm %prec UNIOP
1064 | LOCAL term %prec UNIOP
1065 { $$ = localize($2,0); }
1067 { $$ = sawparens($2); }
1071 { $$ = sawparens(newNULLLIST()); }
1080 | arylen %prec '(' /* $#x, $#{ something } */
1081 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1084 | sliceme '[' expr ']' /* array slice */
1085 { $$ = op_prepend_elem(OP_ASLICE,
1086 newOP(OP_PUSHMARK, 0),
1087 newLISTOP(OP_ASLICE, 0,
1089 ref($1, OP_ASLICE)));
1092 $1->op_private & OPpSLICEWARNING;
1094 | kvslice '[' expr ']' /* array key/value slice */
1095 { $$ = op_prepend_elem(OP_KVASLICE,
1096 newOP(OP_PUSHMARK, 0),
1097 newLISTOP(OP_KVASLICE, 0,
1099 ref(oopsAV($1), OP_KVASLICE)));
1102 $1->op_private & OPpSLICEWARNING;
1104 | sliceme '{' expr ';' '}' /* @hash{@keys} */
1105 { $$ = op_prepend_elem(OP_HSLICE,
1106 newOP(OP_PUSHMARK, 0),
1107 newLISTOP(OP_HSLICE, 0,
1109 ref(oopsHV($1), OP_HSLICE)));
1112 $1->op_private & OPpSLICEWARNING;
1114 | kvslice '{' expr ';' '}' /* %hash{@keys} */
1115 { $$ = op_prepend_elem(OP_KVHSLICE,
1116 newOP(OP_PUSHMARK, 0),
1117 newLISTOP(OP_KVHSLICE, 0,
1119 ref($1, OP_KVHSLICE)));
1122 $1->op_private & OPpSLICEWARNING;
1127 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1128 | amper '(' ')' /* &foo() or foo() */
1129 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1131 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1133 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1134 op_append_elem(OP_LIST, $3, scalar($1)));
1136 | NOAMP subname optlistexpr /* foo @args (no parens) */
1137 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1138 op_append_elem(OP_LIST, $3, scalar($2)));
1140 | term ARROW '$' '*'
1141 { $$ = newSVREF($1); }
1142 | term ARROW '@' '*'
1143 { $$ = newAVREF($1); }
1144 | term ARROW '%' '*'
1145 { $$ = newHVREF($1); }
1146 | term ARROW '&' '*'
1147 { $$ = newUNOP(OP_ENTERSUB, 0,
1148 scalar(newCVREF($3,$1))); }
1149 | term ARROW '*' '*' %prec '('
1150 { $$ = newGVREF(0,$1); }
1151 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1152 { $$ = newOP($1, OPf_SPECIAL);
1153 PL_hints |= HINT_BLOCK_SCOPE; }
1155 { $$ = newLOOPEX($1,$2); }
1156 | NOTOP listexpr /* not $foo */
1157 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
1158 | UNIOP /* Unary op, $_ implied */
1159 { $$ = newOP($1, 0); }
1160 | UNIOP block /* eval { foo }* */
1161 { $$ = newUNOP($1, 0, $2); }
1162 | UNIOP term /* Unary op */
1163 { $$ = newUNOP($1, 0, $2); }
1164 | REQUIRE /* require, $_ implied */
1165 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
1166 | REQUIRE term /* require Foo */
1167 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
1169 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1170 | UNIOPSUB term /* Sub treated as unop */
1171 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1172 op_append_elem(OP_LIST, $2, scalar($1))); }
1173 | FUNC0 /* Nullary operator */
1174 { $$ = newOP($1, 0); }
1176 { $$ = newOP($1, 0);}
1177 | FUNC0OP /* Same as above, but op created in toke.c */
1181 | FUNC0SUB /* Sub treated as nullop */
1182 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1183 | FUNC1 '(' ')' /* not () */
1184 { $$ = ($1 == OP_NOT)
1185 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1186 : newOP($1, OPf_SPECIAL); }
1187 | FUNC1 '(' expr ')' /* not($foo) */
1188 { $$ = newUNOP($1, 0, $3); }
1189 | PMFUNC /* m//, s///, qr//, tr/// */
1191 if ( $1->op_type != OP_TRANS
1192 && $1->op_type != OP_TRANSR
1193 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1195 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1196 SAVEFREESV(PL_compcv);
1200 '(' listexpr optrepl ')'
1201 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1206 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1207 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1212 /* "my" declarations, with optional attributes */
1213 myattrterm: MY myterm myattrlist
1214 { $$ = my_attrs($2,$3); }
1216 { $$ = localize($2,1); }
1217 | MY REFGEN myterm myattrlist
1218 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
1221 /* Things that can be "my"'d */
1222 myterm : '(' expr ')'
1223 { $$ = sawparens($2); }
1225 { $$ = sawparens(newNULLLIST()); }
1235 /* Basic list expressions */
1236 optlistexpr: /* NULL */ %prec PREC_LOW
1238 | listexpr %prec PREC_LOW
1254 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1257 { parser->in_my = 0; $$ = my($1); }
1265 refgen_topic: my_var
1269 my_refgen: MY REFGEN
1274 { $$ = newCVREF($1,$2); }
1277 scalar : '$' indirob
1278 { $$ = newSVREF($2); }
1282 { $$ = newAVREF($2);
1283 if ($$) $$->op_private |= $1;
1288 { $$ = newHVREF($2);
1289 if ($$) $$->op_private |= $1;
1293 arylen : DOLSHARP indirob
1294 { $$ = newAVREF($2); }
1295 | term ARROW DOLSHARP '*'
1296 { $$ = newAVREF($1); }
1300 { $$ = newGVREF(0,$2); }
1305 { $$ = newAVREF($1); }
1310 { $$ = newHVREF($1); }
1315 { $$ = newGVREF(0,$1); }
1318 /* Indirect objects */
1320 { $$ = scalar($1); }
1321 | scalar %prec PREC_LOW
1322 { $$ = scalar($1); }
1324 { $$ = op_scope($1); }