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 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> condition
73 %type <opval> sliceme kvslice gelem
74 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
75 %type <opval> optlistexpr optexpr optrepl indirob listop method
76 %type <opval> formname subname proto cont my_scalar my_var
77 %type <opval> refgen_topic formblock
78 %type <opval> subattrlist myattrlist myattrterm myterm
79 %type <opval> termbinop termunop anonymous termdo
80 %type <opval> termrelop relopchain termeqop eqopchain
81 %type <ival> sigslurpsigil
82 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
83 %type <opval> sigelem siglist siglistornull subsigguts subsignature optsubsignature
84 %type <opval> subbody optsubbody sigsubbody optsigsubbody
85 %type <opval> formstmtseq formline formarg
87 %nonassoc <ival> PREC_LOW
90 %left <ival> OROP DOROP
93 %nonassoc LSTOP LSTOPSUB
95 %right <ival> ASSIGNOP
98 %left <ival> OROR DORDOR
101 %left <ival> BITANDOP
102 %left <ival> CHEQOP NCEQOP
103 %left <ival> CHRELOP NCRELOP
104 %nonassoc UNIOP UNIOPSUB
110 %right <ival> '!' '~' UMINUS REFGEN
112 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
120 /* Top-level choice of what kind of thing yyparse was called to parse */
123 parser->expect = XSTATE;
128 newPROG(block_end($remember,$stmtseq));
129 PL_compiling.cop_seq = 0;
134 parser->expect = XTERM;
139 PL_eval_root = $optexpr;
144 parser->expect = XBLOCK;
149 PL_pad_reset_pending = TRUE;
150 PL_eval_root = $block;
153 parser->yychar = yytoken = YYEOF;
157 parser->expect = XSTATE;
162 PL_pad_reset_pending = TRUE;
163 PL_eval_root = $barestmt;
166 parser->yychar = yytoken = YYEOF;
170 parser->expect = XSTATE;
175 PL_pad_reset_pending = TRUE;
176 PL_eval_root = $fullstmt;
179 parser->yychar = yytoken = YYEOF;
183 parser->expect = XSTATE;
188 PL_eval_root = $stmtseq;
193 parser->expect = XSTATE;
198 PL_eval_root = $subsigguts;
203 /* An ordinary block */
204 block : '{' remember stmtseq '}'
205 { if (parser->copline > (line_t)$1)
206 parser->copline = (line_t)$1;
207 $$ = block_end($remember, $stmtseq);
212 formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
213 { if (parser->copline > (line_t)$1)
214 parser->copline = (line_t)$1;
215 $$ = block_end($remember, $formstmtseq);
219 remember: /* NULL */ /* start a full lexical scope */
220 { $$ = block_start(TRUE);
221 parser->parsed_sub = 0; }
224 mblock : '{' mremember stmtseq '}'
225 { if (parser->copline > (line_t)$1)
226 parser->copline = (line_t)$1;
227 $$ = block_end($mremember, $stmtseq);
231 mremember: /* NULL */ /* start a partial lexical scope */
232 { $$ = block_start(FALSE);
233 parser->parsed_sub = 0; }
236 /* A sequence of statements in the program */
239 | stmtseq[list] fullstmt
240 { $$ = op_append_list(OP_LINESEQ, $list, $fullstmt);
241 PL_pad_reset_pending = TRUE;
242 if ($list && $fullstmt)
243 PL_hints |= HINT_BLOCK_SCOPE;
247 /* A sequence of format lines */
248 formstmtseq: /* NULL */
250 | formstmtseq[list] formline
251 { $$ = op_append_list(OP_LINESEQ, $list, $formline);
252 PL_pad_reset_pending = TRUE;
253 if ($list && $formline)
254 PL_hints |= HINT_BLOCK_SCOPE;
258 /* A statement in the program, including optional labels */
261 $$ = $barestmt ? newSTATEOP(0, NULL, $barestmt) : NULL;
264 { $$ = $labfullstmt; }
267 labfullstmt: LABEL barestmt
269 SV *label = cSVOPx_sv($LABEL);
270 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
271 savepv(SvPVX_const(label)), $barestmt);
274 | LABEL labfullstmt[list]
276 SV *label = cSVOPx_sv($LABEL);
277 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
278 savepv(SvPVX_const(label)), $list);
283 /* A bare statement, lacking label and other aspects of state op */
286 | FORMAT startformsub formname formblock
288 CV *fmtcv = PL_compcv;
289 newFORM($startformsub, $formname, $formblock);
291 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
292 pad_add_weakref(fmtcv);
294 parser->parsed_sub = 1;
296 | SUB subname startsub
297 /* sub declaration or definition not within scope
298 of 'use feature "signatures"'*/
300 init_named_cv(PL_compcv, $subname);
302 parser->in_my_stash = NULL;
304 proto subattrlist optsubbody
306 SvREFCNT_inc_simple_void(PL_compcv);
307 $subname->op_type == OP_CONST
308 ? newATTRSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
309 : newMYSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
313 parser->parsed_sub = 1;
315 | SIGSUB subname startsub
316 /* sub declaration or definition under 'use feature
317 * "signatures"'. (Note that a signature isn't
318 * allowed in a declaration)
321 init_named_cv(PL_compcv, $subname);
323 parser->in_my_stash = NULL;
325 subattrlist optsigsubbody
327 SvREFCNT_inc_simple_void(PL_compcv);
328 $subname->op_type == OP_CONST
329 ? newATTRSUB($startsub, $subname, NULL, $subattrlist, $optsigsubbody)
330 : newMYSUB( $startsub, $subname, NULL, $subattrlist, $optsigsubbody)
334 parser->parsed_sub = 1;
336 | PACKAGE BAREWORD[version] BAREWORD[package] ';'
340 package_version($version);
344 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
345 BAREWORD[version] BAREWORD[module] optlistexpr ';'
347 SvREFCNT_inc_simple_void(PL_compcv);
348 utilize($USE, $startsub, $version, $module, $optlistexpr);
349 parser->parsed_sub = 1;
352 | IF '(' remember mexpr ')' mblock else
354 $$ = block_end($remember,
355 newCONDOP(0, $mexpr, op_scope($mblock), $else));
356 parser->copline = (line_t)$IF;
358 | UNLESS '(' remember mexpr ')' mblock else
360 $$ = block_end($remember,
361 newCONDOP(0, $mexpr, $else, op_scope($mblock)));
362 parser->copline = (line_t)$UNLESS;
364 | GIVEN '(' remember mexpr ')' mblock
366 $$ = block_end($remember, newGIVENOP($mexpr, op_scope($mblock), 0));
367 parser->copline = (line_t)$GIVEN;
369 | WHEN '(' remember mexpr ')' mblock
370 { $$ = block_end($remember, newWHENOP($mexpr, op_scope($mblock))); }
372 { $$ = newWHENOP(0, op_scope($block)); }
373 | WHILE '(' remember texpr ')' mintro mblock cont
375 $$ = block_end($remember,
376 newWHILEOP(0, 1, NULL,
377 $texpr, $mblock, $cont, $mintro));
378 parser->copline = (line_t)$WHILE;
380 | UNTIL '(' remember iexpr ')' mintro mblock cont
382 $$ = block_end($remember,
383 newWHILEOP(0, 1, NULL,
384 $iexpr, $mblock, $cont, $mintro));
385 parser->copline = (line_t)$UNTIL;
387 | FOR '(' remember mnexpr[init_mnexpr] ';'
388 { parser->expect = XTERM; }
390 { parser->expect = XTERM; }
391 mintro mnexpr[iterate_mnexpr] ')'
394 OP *initop = $init_mnexpr;
395 OP *forop = newWHILEOP(0, 1, NULL,
396 scalar($texpr), $mblock, $iterate_mnexpr, $mintro);
398 forop = op_prepend_elem(OP_LINESEQ, initop,
399 op_append_elem(OP_LINESEQ,
400 newOP(OP_UNSTACK, OPf_SPECIAL),
403 PL_hints |= HINT_BLOCK_SCOPE;
404 $$ = block_end($remember, forop);
405 parser->copline = (line_t)$FOR;
407 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
409 $$ = block_end($remember, newFOROP(0, $my_scalar, $mexpr, $mblock, $cont));
410 parser->copline = (line_t)$FOR;
412 | FOR scalar '(' remember mexpr ')' mblock cont
414 $$ = block_end($remember, newFOROP(0,
415 op_lvalue($scalar, OP_ENTERLOOP), $mexpr, $mblock, $cont));
416 parser->copline = (line_t)$FOR;
418 | FOR my_refgen remember my_var
419 { parser->in_my = 0; $<opval>$ = my($my_var); }[variable]
420 '(' mexpr ')' mblock cont
426 newUNOP(OP_REFGEN, 0,
429 $mexpr, $mblock, $cont)
431 parser->copline = (line_t)$FOR;
433 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
435 $$ = block_end($remember, newFOROP(
436 0, op_lvalue(newUNOP(OP_REFGEN, 0,
438 OP_ENTERLOOP), $mexpr, $mblock, $cont));
439 parser->copline = (line_t)$FOR;
441 | FOR '(' remember mexpr ')' mblock cont
443 $$ = block_end($remember,
444 newFOROP(0, NULL, $mexpr, $mblock, $cont));
445 parser->copline = (line_t)$FOR;
449 /* a block is a loop that happens once */
450 $$ = newWHILEOP(0, 1, NULL,
451 NULL, $block, $cont, 0);
453 | PACKAGE BAREWORD[version] BAREWORD[package] '{' remember
457 package_version($version);
462 /* a block is a loop that happens once */
463 $$ = newWHILEOP(0, 1, NULL,
464 NULL, block_end($remember, $stmtseq), NULL, 0);
465 if (parser->copline > (line_t)$4)
466 parser->copline = (line_t)$4;
474 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
475 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
480 parser->copline = NOLINE;
485 formline: THING formarg
489 list = op_append_elem(OP_LIST, $THING, term);
494 if (parser->copline == NOLINE)
495 parser->copline = CopLINE(PL_curcop)-1;
496 else parser->copline--;
497 $$ = newSTATEOP(0, NULL,
498 op_convert_list(OP_FORMLINE, 0, list));
504 | FORMLBRACK stmtseq FORMRBRACK
505 { $$ = op_unscope($stmtseq); }
511 /* An expression which may have a side-effect */
516 | expr[body] IF condition
517 { $$ = newLOGOP(OP_AND, 0, $condition, $body); }
518 | expr[body] UNLESS condition
519 { $$ = newLOGOP(OP_OR, 0, $condition, $body); }
520 | expr[body] WHILE condition
521 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($condition), $body); }
522 | expr[body] UNTIL iexpr
523 { $$ = newLOOPOP(OPf_PARENS, 1, $iexpr, $body); }
524 | expr[body] FOR condition
525 { $$ = newFOROP(0, NULL, $condition, $body, NULL);
526 parser->copline = (line_t)$FOR; }
527 | expr[body] WHEN condition
528 { $$ = newWHENOP($condition, op_scope($body)); }
531 /* else and elsif blocks */
536 ($mblock)->op_flags |= OPf_PARENS;
537 $$ = op_scope($mblock);
539 | ELSIF '(' mexpr ')' mblock else[else.recurse]
540 { parser->copline = (line_t)$ELSIF;
542 newSTATEOP(OPf_SPECIAL,NULL,$mexpr),
543 op_scope($mblock), $[else.recurse]);
544 PL_hints |= HINT_BLOCK_SCOPE;
548 /* Continue blocks */
552 { $$ = op_scope($block); }
555 /* determine whether there are any new my declarations */
557 { $$ = (PL_min_intro_pending &&
558 PL_max_intro_pending >= PL_min_intro_pending);
561 /* Normal expression */
567 /* Boolean expression */
568 texpr : /* NULL means true */
570 (void)scan_num("1", &tmplval);
571 $$ = tmplval.opval; }
575 /* Inverted boolean expression */
577 { $$ = invert(scalar($expr)); }
580 /* Expression with its own lexical scope */
582 { $$ = $expr; intro_my(); }
586 { $$ = $nexpr; intro_my(); }
589 formname: BAREWORD { $$ = $BAREWORD; }
590 | /* NULL */ { $$ = NULL; }
593 startsub: /* NULL */ /* start a regular subroutine scope */
594 { $$ = start_subparse(FALSE, 0);
595 SAVEFREESV(PL_compcv); }
599 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
600 { $$ = start_subparse(FALSE, CVf_ANON);
601 SAVEFREESV(PL_compcv); }
604 startformsub: /* NULL */ /* start a format subroutine scope */
605 { $$ = start_subparse(TRUE, 0);
606 SAVEFREESV(PL_compcv); }
609 /* Name of a subroutine - must be a bareword, could be special */
614 /* Subroutine prototype */
620 /* Optional list of subroutine attributes */
621 subattrlist: /* NULL */
629 /* List of attributes for a "my" variable declaration */
630 myattrlist: COLONATTR THING
638 /* --------------------------------------
639 * subroutine signature parsing
642 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
643 sigvarname: /* NULL */
644 { parser->in_my = 0; $$ = NULL; }
646 { parser->in_my = 0; $$ = $PRIVATEREF; }
655 /* @, %, @foo, %foo */
656 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
658 I32 sigil = $sigslurpsigil;
659 OP *var = $sigvarname;
660 OP *defexpr = $sigdefault;
662 if (parser->sig_slurpy)
663 yyerror("Multiple slurpy parameters not allowed");
664 parser->sig_slurpy = (char)sigil;
667 yyerror("A slurpy parameter may not have "
670 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
674 /* default part of sub signature scalar element: i.e. '= default_expr' */
675 sigdefault: /* NULL */
678 { $$ = newOP(OP_NULL, 0); }
683 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
685 '$' sigvarname sigdefault
687 OP *var = $sigvarname;
688 OP *defexpr = $sigdefault;
690 if (parser->sig_slurpy)
691 yyerror("Slurpy parameter not last");
696 parser->sig_optelems++;
698 if ( defexpr->op_type == OP_NULL
699 && !(defexpr->op_flags & OPf_KIDS))
701 /* handle '$=' special case */
703 yyerror("Optional parameter "
704 "lacks default expression");
708 /* a normal '=default' expression */
709 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
712 /* re-purpose op_targ to hold @_ index */
714 (PADOFFSET)(parser->sig_elems - 1);
717 var->op_flags |= OPf_STACKED;
718 (void)op_sibling_splice(var,
723 var = newUNOP(OP_NULL, 0, defop);
726 /* NB: normally the first child of a
727 * logop is executed before the logop,
728 * and it pushes a boolean result
729 * ready for the logop. For ARGDEFELEM,
730 * the op itself does the boolean
731 * calculation, so set the first op to
734 var->op_next = defop;
735 defexpr->op_next = var;
739 if (parser->sig_optelems)
740 yyerror("Mandatory parameter "
741 "follows optional parameter");
744 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
749 /* subroutine signature element: e.g. '$x = $default' or '%h' */
750 sigelem: sigscalarelem
751 { parser->in_my = KEY_sigvar; $$ = $sigscalarelem; }
753 { parser->in_my = KEY_sigvar; $$ = $sigslurpelem; }
756 /* list of subroutine signature elements */
760 | siglist[list] ',' sigelem[element]
762 $$ = op_append_list(OP_LINESEQ, $list, $element);
764 | sigelem[element] %prec PREC_LOW
769 siglistornull: /* NULL */
774 /* optional subroutine signature */
775 optsubsignature: /* NULL */
778 { $$ = $subsignature; }
780 /* Subroutine signature */
781 subsignature: '(' subsigguts ')'
782 { $$ = $subsigguts; }
787 SAVEIV(parser->sig_elems);
788 SAVEIV(parser->sig_optelems);
789 SAVEI8(parser->sig_slurpy);
790 parser->sig_elems = 0;
791 parser->sig_optelems = 0;
792 parser->sig_slurpy = 0;
793 parser->in_my = KEY_sigvar;
797 OP *sigops = $siglistornull;
798 struct op_argcheck_aux *aux;
801 if (!FEATURE_SIGNATURES_IS_ENABLED)
802 Perl_croak(aTHX_ "Experimental "
803 "subroutine signatures not enabled");
805 /* We shouldn't get here otherwise */
806 Perl_ck_warner_d(aTHX_
807 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
808 "The signatures feature is experimental");
810 aux = (struct op_argcheck_aux*)
811 PerlMemShared_malloc(
812 sizeof(struct op_argcheck_aux));
813 aux->params = parser->sig_elems;
814 aux->opt_params = parser->sig_optelems;
815 aux->slurpy = parser->sig_slurpy;
816 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL,
817 (UNOP_AUX_item *)aux);
818 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
819 sigops = op_prepend_elem(OP_LINESEQ,
820 newSTATEOP(0, NULL, NULL),
822 /* a nextstate at the end handles context
823 * correctly for an empty sub body */
824 sigops = op_append_elem(OP_LINESEQ,
826 newSTATEOP(0, NULL, NULL));
827 /* wrap the list of arg ops in a NULL aux op.
828 This serves two purposes. First, it makes
829 the arg list a separate subtree from the
830 body of the sub, and secondly the null op
831 may in future be upgraded to an OP_SIGNATURE
832 when implemented. For now leave it as
834 $$ = newUNOP_AUX(OP_ARGCHECK, 0, sigops, NULL);
838 /* tell the toker that attrributes can follow
839 * this sig, but only so that the toker
840 * can skip through any (illegal) trailing
841 * attribute text then give a useful error
842 * message about "attributes before sig",
843 * rather than falling over ina mess at
844 * unrecognised syntax.
846 parser->expect = XATTRBLOCK;
847 parser->sig_seen = TRUE;
852 /* Optional subroutine body (for named subroutine declaration) */
853 optsubbody: subbody { $$ = $subbody; }
858 /* Subroutine body (without signature) */
859 subbody: remember '{' stmtseq '}'
861 if (parser->copline > (line_t)$2)
862 parser->copline = (line_t)$2;
863 $$ = block_end($remember, $stmtseq);
868 /* optional [ Subroutine body with optional signature ] (for named
869 * subroutine declaration) */
870 optsigsubbody: sigsubbody { $$ = $sigsubbody; }
873 /* Subroutine body with optional signature */
874 sigsubbody: remember optsubsignature '{' stmtseq '}'
876 if (parser->copline > (line_t)$3)
877 parser->copline = (line_t)$3;
878 $$ = block_end($remember,
879 op_append_list(OP_LINESEQ, $optsubsignature, $stmtseq));
884 /* Ordinary expressions; logical combinations */
885 expr : expr[lhs] ANDOP expr[rhs]
886 { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
887 | expr[lhs] OROP[operator] expr[rhs]
888 { $$ = newLOGOP($operator, 0, $lhs, $rhs); }
889 | expr[lhs] DOROP expr[rhs]
890 { $$ = newLOGOP(OP_DOR, 0, $lhs, $rhs); }
891 | listexpr %prec PREC_LOW
894 /* Expressions are a list of terms joined by commas */
895 listexpr: listexpr[list] ','
897 | listexpr[list] ',' term
900 $$ = op_append_elem(OP_LIST, $list, term);
902 | term %prec PREC_LOW
906 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
907 { $$ = op_convert_list($LSTOP, OPf_STACKED,
908 op_prepend_elem(OP_LIST, newGVREF($LSTOP,$indirob), $listexpr) );
910 | FUNC '(' indirob expr ')' /* print ($fh @args */
911 { $$ = op_convert_list($FUNC, OPf_STACKED,
912 op_prepend_elem(OP_LIST, newGVREF($FUNC,$indirob), $expr) );
914 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
915 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
916 op_append_elem(OP_LIST,
917 op_prepend_elem(OP_LIST, scalar($term), $optexpr),
918 newMETHOP(OP_METHOD, 0, $method)));
920 | term ARROW method /* $foo->bar */
921 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
922 op_append_elem(OP_LIST, scalar($term),
923 newMETHOP(OP_METHOD, 0, $method)));
925 | METHOD indirob optlistexpr /* new Class @args */
926 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
927 op_append_elem(OP_LIST,
928 op_prepend_elem(OP_LIST, $indirob, $optlistexpr),
929 newMETHOP(OP_METHOD, 0, $METHOD)));
931 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
932 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
933 op_append_elem(OP_LIST,
934 op_prepend_elem(OP_LIST, $indirob, $optexpr),
935 newMETHOP(OP_METHOD, 0, $FUNCMETH)));
937 | LSTOP optlistexpr /* print @args */
938 { $$ = op_convert_list($LSTOP, 0, $optlistexpr); }
939 | FUNC '(' optexpr ')' /* print (@args) */
940 { $$ = op_convert_list($FUNC, 0, $optexpr); }
941 | FUNC SUBLEXSTART optexpr SUBLEXEND /* uc($arg) from "\U..." */
942 { $$ = op_convert_list($FUNC, 0, $optexpr); }
943 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
944 { SvREFCNT_inc_simple_void(PL_compcv);
945 $<opval>$ = newANONATTRSUB($startanonsub, 0, NULL, $block); }[anonattrsub]
946 optlistexpr %prec LSTOP /* ... @bar */
947 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
948 op_append_elem(OP_LIST,
949 op_prepend_elem(OP_LIST, $<opval>anonattrsub, $optlistexpr), $LSTOPSUB));
953 /* Names of methods. May use $object->$methodname */
958 /* Some kind of subscripted expression */
959 subscripted: gelem '{' expr ';' '}' /* *main::{something} */
960 /* In this and all the hash accessors, ';' is
961 * provided by the tokeniser */
962 { $$ = newBINOP(OP_GELEM, 0, $gelem, scalar($expr)); }
963 | scalar[array] '[' expr ']' /* $array[$element] */
964 { $$ = newBINOP(OP_AELEM, 0, oopsAV($array), scalar($expr));
966 | term[array_reference] ARROW '[' expr ']' /* somearef->[$element] */
967 { $$ = newBINOP(OP_AELEM, 0,
968 ref(newAVREF($array_reference),OP_RV2AV),
971 | subscripted[array_reference] '[' expr ']' /* $foo->[$bar]->[$baz] */
972 { $$ = newBINOP(OP_AELEM, 0,
973 ref(newAVREF($array_reference),OP_RV2AV),
976 | scalar[hash] '{' expr ';' '}' /* $foo{bar();} */
977 { $$ = newBINOP(OP_HELEM, 0, oopsHV($hash), jmaybe($expr));
979 | term[hash_reference] ARROW '{' expr ';' '}' /* somehref->{bar();} */
980 { $$ = newBINOP(OP_HELEM, 0,
981 ref(newHVREF($hash_reference),OP_RV2HV),
983 | subscripted[hash_reference] '{' expr ';' '}' /* $foo->[bar]->{baz;} */
984 { $$ = newBINOP(OP_HELEM, 0,
985 ref(newHVREF($hash_reference),OP_RV2HV),
987 | term[code_reference] ARROW '(' ')' /* $subref->() */
988 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
989 newCVREF(0, scalar($code_reference)));
990 if (parser->expect == XBLOCK)
991 parser->expect = XOPERATOR;
993 | term[code_reference] ARROW '(' expr ')' /* $subref->(@args) */
994 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
995 op_append_elem(OP_LIST, $expr,
996 newCVREF(0, scalar($code_reference))));
997 if (parser->expect == XBLOCK)
998 parser->expect = XOPERATOR;
1001 | subscripted[code_reference] '(' expr ')' /* $foo->{bar}->(@args) */
1002 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1003 op_append_elem(OP_LIST, $expr,
1004 newCVREF(0, scalar($code_reference))));
1005 if (parser->expect == XBLOCK)
1006 parser->expect = XOPERATOR;
1008 | subscripted[code_reference] '(' ')' /* $foo->{bar}->() */
1009 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1010 newCVREF(0, scalar($code_reference)));
1011 if (parser->expect == XBLOCK)
1012 parser->expect = XOPERATOR;
1014 | '(' expr[list] ')' '[' expr[slice] ']' /* list slice */
1015 { $$ = newSLICEOP(0, $slice, $list); }
1016 | QWLIST '[' expr ']' /* list literal slice */
1017 { $$ = newSLICEOP(0, $expr, $QWLIST); }
1018 | '(' ')' '[' expr ']' /* empty list slice! */
1019 { $$ = newSLICEOP(0, $expr, NULL); }
1022 /* Binary operators between terms */
1023 termbinop: term[lhs] ASSIGNOP term[rhs] /* $x = $y, $x += $y */
1024 { $$ = newASSIGNOP(OPf_STACKED, $lhs, $ASSIGNOP, $rhs); }
1025 | term[lhs] POWOP term[rhs] /* $x ** $y */
1026 { $$ = newBINOP($POWOP, 0, scalar($lhs), scalar($rhs)); }
1027 | term[lhs] MULOP term[rhs] /* $x * $y, $x x $y */
1028 { if ($MULOP != OP_REPEAT)
1030 $$ = newBINOP($MULOP, 0, $lhs, scalar($rhs));
1032 | term[lhs] ADDOP term[rhs] /* $x + $y */
1033 { $$ = newBINOP($ADDOP, 0, scalar($lhs), scalar($rhs)); }
1034 | term[lhs] SHIFTOP term[rhs] /* $x >> $y, $x << $y */
1035 { $$ = newBINOP($SHIFTOP, 0, scalar($lhs), scalar($rhs)); }
1036 | termrelop %prec PREC_LOW /* $x > $y, etc. */
1037 { $$ = $termrelop; }
1038 | termeqop %prec PREC_LOW /* $x == $y, $x cmp $y */
1040 | term[lhs] BITANDOP term[rhs] /* $x & $y */
1041 { $$ = newBINOP($BITANDOP, 0, scalar($lhs), scalar($rhs)); }
1042 | term[lhs] BITOROP term[rhs] /* $x | $y */
1043 { $$ = newBINOP($BITOROP, 0, scalar($lhs), scalar($rhs)); }
1044 | term[lhs] DOTDOT term[rhs] /* $x..$y, $x...$y */
1045 { $$ = newRANGE($DOTDOT, scalar($lhs), scalar($rhs)); }
1046 | term[lhs] ANDAND term[rhs] /* $x && $y */
1047 { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
1048 | term[lhs] OROR term[rhs] /* $x || $y */
1049 { $$ = newLOGOP(OP_OR, 0, $lhs, $rhs); }
1050 | term[lhs] DORDOR term[rhs] /* $x // $y */
1051 { $$ = newLOGOP(OP_DOR, 0, $lhs, $rhs); }
1052 | term[lhs] MATCHOP term[rhs] /* $x =~ /$y/ */
1053 { $$ = bind_match($MATCHOP, $lhs, $rhs); }
1056 termrelop: relopchain %prec PREC_LOW
1057 { $$ = cmpchain_finish($relopchain); }
1058 | term[lhs] NCRELOP term[rhs]
1059 { $$ = newBINOP($NCRELOP, 0, scalar($lhs), scalar($rhs)); }
1061 { yyerror("syntax error"); YYERROR; }
1063 { yyerror("syntax error"); YYERROR; }
1066 relopchain: term[lhs] CHRELOP term[rhs]
1067 { $$ = cmpchain_start($CHRELOP, $lhs, $rhs); }
1068 | relopchain[lhs] CHRELOP term[rhs]
1069 { $$ = cmpchain_extend($CHRELOP, $lhs, $rhs); }
1072 termeqop: eqopchain %prec PREC_LOW
1073 { $$ = cmpchain_finish($eqopchain); }
1074 | term[lhs] NCEQOP term[rhs]
1075 { $$ = newBINOP($NCEQOP, 0, scalar($lhs), scalar($rhs)); }
1077 { yyerror("syntax error"); YYERROR; }
1079 { yyerror("syntax error"); YYERROR; }
1082 eqopchain: term[lhs] CHEQOP term[rhs]
1083 { $$ = cmpchain_start($CHEQOP, $lhs, $rhs); }
1084 | eqopchain[lhs] CHEQOP term[rhs]
1085 { $$ = cmpchain_extend($CHEQOP, $lhs, $rhs); }
1088 /* Unary operators and terms */
1089 termunop : '-' term %prec UMINUS /* -$x */
1090 { $$ = newUNOP(OP_NEGATE, 0, scalar($term)); }
1091 | '+' term %prec UMINUS /* +$x */
1094 | '!' term /* !$x */
1095 { $$ = newUNOP(OP_NOT, 0, scalar($term)); }
1096 | '~' term /* ~$x */
1097 { $$ = newUNOP($1, 0, scalar($term)); }
1098 | term POSTINC /* $x++ */
1099 { $$ = newUNOP(OP_POSTINC, 0,
1100 op_lvalue(scalar($term), OP_POSTINC)); }
1101 | term POSTDEC /* $x-- */
1102 { $$ = newUNOP(OP_POSTDEC, 0,
1103 op_lvalue(scalar($term), OP_POSTDEC));}
1104 | term POSTJOIN /* implicit join after interpolated ->@ */
1105 { $$ = op_convert_list(OP_JOIN, 0,
1115 | PREINC term /* ++$x */
1116 { $$ = newUNOP(OP_PREINC, 0,
1117 op_lvalue(scalar($term), OP_PREINC)); }
1118 | PREDEC term /* --$x */
1119 { $$ = newUNOP(OP_PREDEC, 0,
1120 op_lvalue(scalar($term), OP_PREDEC)); }
1124 /* Constructors for anonymous data */
1125 anonymous: '[' expr ']'
1126 { $$ = newANONLIST($expr); }
1128 { $$ = newANONLIST(NULL);}
1129 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
1130 { $$ = newANONHASH($expr); }
1131 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
1132 { $$ = newANONHASH(NULL); }
1133 | ANONSUB startanonsub proto subattrlist subbody %prec '('
1134 { SvREFCNT_inc_simple_void(PL_compcv);
1135 $$ = newANONATTRSUB($startanonsub, $proto, $subattrlist, $subbody); }
1136 | ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '('
1137 { SvREFCNT_inc_simple_void(PL_compcv);
1138 $$ = newANONATTRSUB($startanonsub, NULL, $subattrlist, $sigsubbody); }
1141 /* Things called with "do" */
1142 termdo : DO term %prec UNIOP /* do $filename */
1143 { $$ = dofile($term, $DO);}
1144 | DO block %prec '(' /* do { code */
1145 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($block));}
1148 term[product] : termbinop
1152 | term[condition] '?' term[then] ':' term[else]
1153 { $$ = newCONDOP(0, $condition, $then, $else); }
1154 | REFGEN term[operand] /* \$x, \@y, \%z */
1155 { $$ = newUNOP(OP_REFGEN, 0, $operand); }
1156 | MY REFGEN term[operand]
1157 { $$ = newUNOP(OP_REFGEN, 0, localize($operand,1)); }
1158 | myattrterm %prec UNIOP
1159 { $$ = $myattrterm; }
1160 | LOCAL term[operand] %prec UNIOP
1161 { $$ = localize($operand,0); }
1163 { $$ = sawparens($expr); }
1167 { $$ = sawparens(newNULLLIST()); }
1176 | arylen %prec '(' /* $#x, $#{ something } */
1177 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($arylen, OP_AV2ARYLEN));}
1179 { $$ = $subscripted; }
1180 | sliceme '[' expr ']' /* array slice */
1181 { $$ = op_prepend_elem(OP_ASLICE,
1182 newOP(OP_PUSHMARK, 0),
1183 newLISTOP(OP_ASLICE, 0,
1185 ref($sliceme, OP_ASLICE)));
1188 $sliceme->op_private & OPpSLICEWARNING;
1190 | kvslice '[' expr ']' /* array key/value slice */
1191 { $$ = op_prepend_elem(OP_KVASLICE,
1192 newOP(OP_PUSHMARK, 0),
1193 newLISTOP(OP_KVASLICE, 0,
1195 ref(oopsAV($kvslice), OP_KVASLICE)));
1198 $kvslice->op_private & OPpSLICEWARNING;
1200 | sliceme '{' expr ';' '}' /* @hash{@keys} */
1201 { $$ = op_prepend_elem(OP_HSLICE,
1202 newOP(OP_PUSHMARK, 0),
1203 newLISTOP(OP_HSLICE, 0,
1205 ref(oopsHV($sliceme), OP_HSLICE)));
1208 $sliceme->op_private & OPpSLICEWARNING;
1210 | kvslice '{' expr ';' '}' /* %hash{@keys} */
1211 { $$ = op_prepend_elem(OP_KVHSLICE,
1212 newOP(OP_PUSHMARK, 0),
1213 newLISTOP(OP_KVHSLICE, 0,
1215 ref($kvslice, OP_KVHSLICE)));
1218 $kvslice->op_private & OPpSLICEWARNING;
1223 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($amper)); }
1224 | amper '(' ')' /* &foo() or foo() */
1225 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($amper));
1227 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1229 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1230 op_append_elem(OP_LIST, $expr, scalar($amper)));
1232 | NOAMP subname optlistexpr /* foo @args (no parens) */
1233 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1234 op_append_elem(OP_LIST, $optlistexpr, scalar($subname)));
1236 | term[operand] ARROW '$' '*'
1237 { $$ = newSVREF($operand); }
1238 | term[operand] ARROW '@' '*'
1239 { $$ = newAVREF($operand); }
1240 | term[operand] ARROW '%' '*'
1241 { $$ = newHVREF($operand); }
1242 | term[operand] ARROW '&' '*'
1243 { $$ = newUNOP(OP_ENTERSUB, 0,
1244 scalar(newCVREF($3,$operand))); }
1245 | term[operand] ARROW '*' '*' %prec '('
1246 { $$ = newGVREF(0,$operand); }
1247 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1248 { $$ = newOP($LOOPEX, OPf_SPECIAL);
1249 PL_hints |= HINT_BLOCK_SCOPE; }
1250 | LOOPEX term[operand]
1251 { $$ = newLOOPEX($LOOPEX,$operand); }
1252 | NOTOP listexpr /* not $foo */
1253 { $$ = newUNOP(OP_NOT, 0, scalar($listexpr)); }
1254 | UNIOP /* Unary op, $_ implied */
1255 { $$ = newOP($UNIOP, 0); }
1256 | UNIOP block /* eval { foo }* */
1257 { $$ = newUNOP($UNIOP, 0, $block); }
1258 | UNIOP term[operand] /* Unary op */
1259 { $$ = newUNOP($UNIOP, 0, $operand); }
1260 | REQUIRE /* require, $_ implied */
1261 { $$ = newOP(OP_REQUIRE, $REQUIRE ? OPf_SPECIAL : 0); }
1262 | REQUIRE term[operand] /* require Foo */
1263 { $$ = newUNOP(OP_REQUIRE, $REQUIRE ? OPf_SPECIAL : 0, $operand); }
1265 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($UNIOPSUB)); }
1266 | UNIOPSUB term[operand] /* Sub treated as unop */
1267 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1268 op_append_elem(OP_LIST, $operand, scalar($UNIOPSUB))); }
1269 | FUNC0 /* Nullary operator */
1270 { $$ = newOP($FUNC0, 0); }
1272 { $$ = newOP($FUNC0, 0);}
1273 | FUNC0OP /* Same as above, but op created in toke.c */
1277 | FUNC0SUB /* Sub treated as nullop */
1278 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($FUNC0SUB)); }
1279 | FUNC1 '(' ')' /* not () */
1280 { $$ = ($FUNC1 == OP_NOT)
1281 ? newUNOP($FUNC1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1282 : newOP($FUNC1, OPf_SPECIAL); }
1283 | FUNC1 '(' expr ')' /* not($foo) */
1284 { $$ = newUNOP($FUNC1, 0, $expr); }
1285 | PMFUNC /* m//, s///, qr//, tr/// */
1287 if ( $PMFUNC->op_type != OP_TRANS
1288 && $PMFUNC->op_type != OP_TRANSR
1289 && (((PMOP*)$PMFUNC)->op_pmflags & PMf_HAS_CV))
1291 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1292 SAVEFREESV(PL_compcv);
1296 SUBLEXSTART listexpr optrepl SUBLEXEND
1297 { $$ = pmruntime($PMFUNC, $listexpr, $optrepl, 1, $<ival>2); }
1303 /* "my" declarations, with optional attributes */
1304 myattrterm: MY myterm myattrlist
1305 { $$ = my_attrs($myterm,$myattrlist); }
1307 { $$ = localize($myterm,1); }
1308 | MY REFGEN myterm myattrlist
1309 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($myterm,$myattrlist)); }
1312 /* Things that can be "my"'d */
1313 myterm : '(' expr ')'
1314 { $$ = sawparens($expr); }
1316 { $$ = sawparens(newNULLLIST()); }
1326 /* Basic list expressions */
1327 optlistexpr: /* NULL */ %prec PREC_LOW
1329 | listexpr %prec PREC_LOW
1345 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1348 { parser->in_my = 0; $$ = my($scalar); }
1356 refgen_topic: my_var
1360 my_refgen: MY REFGEN
1365 { $$ = newCVREF($1,$indirob); }
1368 scalar : '$' indirob
1369 { $$ = newSVREF($indirob); }
1373 { $$ = newAVREF($indirob);
1374 if ($$) $$->op_private |= $1;
1379 { $$ = newHVREF($indirob);
1380 if ($$) $$->op_private |= $1;
1384 arylen : DOLSHARP indirob
1385 { $$ = newAVREF($indirob); }
1386 | term ARROW DOLSHARP '*'
1387 { $$ = newAVREF($term); }
1391 { $$ = newGVREF(0,$indirob); }
1396 { $$ = newAVREF($term); }
1401 { $$ = newHVREF($term); }
1406 { $$ = newGVREF(0,$term); }
1409 /* Indirect objects */
1411 { $$ = scalar($BAREWORD); }
1412 | scalar %prec PREC_LOW
1413 { $$ = scalar($scalar); }
1415 { $$ = op_scope($block); }
1418 { $$ = $PRIVATEREF; }