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> '[' ']' '-' '+' '@' '%' '&' '=' '.'
49 %token <ival> PERLY_BRACE_OPEN
50 %token <ival> PERLY_BRACE_CLOSE
52 %token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
53 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
54 %token <opval> PLUGEXPR PLUGSTMT
56 %token <ival> FORMAT SUB SIGSUB ANONSUB ANON_SIGSUB PACKAGE USE
57 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
58 %token <ival> GIVEN WHEN DEFAULT
59 %token <ival> LOOPEX DOTDOT YADAYADA
60 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
61 %token <ival> MULOP ADDOP
62 %token <ival> DOLSHARP DO HASHBRACK NOAMP
63 %token <ival> LOCAL MY REQUIRE
64 %token <ival> COLONATTR FORMLBRACK FORMRBRACK
65 %token <ival> SUBLEXSTART SUBLEXEND
67 %type <ival> grammar remember mremember
68 %type <ival> startsub startanonsub startformsub
72 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
73 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
74 %type <opval> condition
75 %type <opval> sliceme kvslice gelem
76 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
77 %type <opval> optlistexpr optexpr optrepl indirob listop method
78 %type <opval> formname subname proto cont my_scalar my_var
79 %type <opval> refgen_topic formblock
80 %type <opval> subattrlist myattrlist myattrterm myterm
81 %type <opval> termbinop termunop anonymous termdo
82 %type <opval> termrelop relopchain termeqop eqopchain
83 %type <ival> sigslurpsigil
84 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
85 %type <opval> sigelem siglist siglistornull subsigguts subsignature optsubsignature
86 %type <opval> subbody optsubbody sigsubbody optsigsubbody
87 %type <opval> formstmtseq formline formarg
89 %nonassoc <ival> PREC_LOW
92 %left <ival> OROP DOROP
95 %nonassoc LSTOP LSTOPSUB
97 %right <ival> ASSIGNOP
100 %left <ival> OROR DORDOR
103 %left <ival> BITANDOP
104 %left <ival> CHEQOP NCEQOP
105 %left <ival> CHRELOP NCRELOP
106 %nonassoc UNIOP UNIOPSUB
112 %right <ival> '!' '~' UMINUS REFGEN
114 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
118 %left '[' PERLY_BRACE_OPEN
122 /* Top-level choice of what kind of thing yyparse was called to parse */
125 parser->expect = XSTATE;
130 newPROG(block_end($remember,$stmtseq));
131 PL_compiling.cop_seq = 0;
136 parser->expect = XTERM;
141 PL_eval_root = $optexpr;
146 parser->expect = XBLOCK;
151 PL_pad_reset_pending = TRUE;
152 PL_eval_root = $block;
155 parser->yychar = yytoken = YYEOF;
159 parser->expect = XSTATE;
164 PL_pad_reset_pending = TRUE;
165 PL_eval_root = $barestmt;
168 parser->yychar = yytoken = YYEOF;
172 parser->expect = XSTATE;
177 PL_pad_reset_pending = TRUE;
178 PL_eval_root = $fullstmt;
181 parser->yychar = yytoken = YYEOF;
185 parser->expect = XSTATE;
190 PL_eval_root = $stmtseq;
195 parser->expect = XSTATE;
200 PL_eval_root = $subsigguts;
205 /* An ordinary block */
206 block : PERLY_BRACE_OPEN remember stmtseq PERLY_BRACE_CLOSE
207 { if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
208 parser->copline = (line_t)$PERLY_BRACE_OPEN;
209 $$ = block_end($remember, $stmtseq);
214 formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
215 { if (parser->copline > (line_t)$1)
216 parser->copline = (line_t)$1;
217 $$ = block_end($remember, $formstmtseq);
221 remember: /* NULL */ /* start a full lexical scope */
222 { $$ = block_start(TRUE);
223 parser->parsed_sub = 0; }
226 mblock : PERLY_BRACE_OPEN mremember stmtseq PERLY_BRACE_CLOSE
227 { if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
228 parser->copline = (line_t)$PERLY_BRACE_OPEN;
229 $$ = block_end($mremember, $stmtseq);
233 mremember: /* NULL */ /* start a partial lexical scope */
234 { $$ = block_start(FALSE);
235 parser->parsed_sub = 0; }
238 /* A sequence of statements in the program */
241 | stmtseq[list] fullstmt
242 { $$ = op_append_list(OP_LINESEQ, $list, $fullstmt);
243 PL_pad_reset_pending = TRUE;
244 if ($list && $fullstmt)
245 PL_hints |= HINT_BLOCK_SCOPE;
249 /* A sequence of format lines */
250 formstmtseq: /* NULL */
252 | formstmtseq[list] formline
253 { $$ = op_append_list(OP_LINESEQ, $list, $formline);
254 PL_pad_reset_pending = TRUE;
255 if ($list && $formline)
256 PL_hints |= HINT_BLOCK_SCOPE;
260 /* A statement in the program, including optional labels */
263 $$ = $barestmt ? newSTATEOP(0, NULL, $barestmt) : NULL;
266 { $$ = $labfullstmt; }
269 labfullstmt: LABEL barestmt
271 SV *label = cSVOPx_sv($LABEL);
272 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
273 savepv(SvPVX_const(label)), $barestmt);
276 | LABEL labfullstmt[list]
278 SV *label = cSVOPx_sv($LABEL);
279 $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
280 savepv(SvPVX_const(label)), $list);
285 /* A bare statement, lacking label and other aspects of state op */
288 | FORMAT startformsub formname formblock
290 CV *fmtcv = PL_compcv;
291 newFORM($startformsub, $formname, $formblock);
293 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
294 pad_add_weakref(fmtcv);
296 parser->parsed_sub = 1;
298 | SUB subname startsub
299 /* sub declaration or definition not within scope
300 of 'use feature "signatures"'*/
302 init_named_cv(PL_compcv, $subname);
304 parser->in_my_stash = NULL;
306 proto subattrlist optsubbody
308 SvREFCNT_inc_simple_void(PL_compcv);
309 $subname->op_type == OP_CONST
310 ? newATTRSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
311 : newMYSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
315 parser->parsed_sub = 1;
317 | SIGSUB subname startsub
318 /* sub declaration or definition under 'use feature
319 * "signatures"'. (Note that a signature isn't
320 * allowed in a declaration)
323 init_named_cv(PL_compcv, $subname);
325 parser->in_my_stash = NULL;
327 subattrlist optsigsubbody
329 SvREFCNT_inc_simple_void(PL_compcv);
330 $subname->op_type == OP_CONST
331 ? newATTRSUB($startsub, $subname, NULL, $subattrlist, $optsigsubbody)
332 : newMYSUB( $startsub, $subname, NULL, $subattrlist, $optsigsubbody)
336 parser->parsed_sub = 1;
338 | PACKAGE BAREWORD[version] BAREWORD[package] ';'
342 package_version($version);
346 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
347 BAREWORD[version] BAREWORD[module] optlistexpr ';'
349 SvREFCNT_inc_simple_void(PL_compcv);
350 utilize($USE, $startsub, $version, $module, $optlistexpr);
351 parser->parsed_sub = 1;
354 | IF '(' remember mexpr ')' mblock else
356 $$ = block_end($remember,
357 newCONDOP(0, $mexpr, op_scope($mblock), $else));
358 parser->copline = (line_t)$IF;
360 | UNLESS '(' remember mexpr ')' mblock else
362 $$ = block_end($remember,
363 newCONDOP(0, $mexpr, $else, op_scope($mblock)));
364 parser->copline = (line_t)$UNLESS;
366 | GIVEN '(' remember mexpr ')' mblock
368 $$ = block_end($remember, newGIVENOP($mexpr, op_scope($mblock), 0));
369 parser->copline = (line_t)$GIVEN;
371 | WHEN '(' remember mexpr ')' mblock
372 { $$ = block_end($remember, newWHENOP($mexpr, op_scope($mblock))); }
374 { $$ = newWHENOP(0, op_scope($block)); }
375 | WHILE '(' remember texpr ')' mintro mblock cont
377 $$ = block_end($remember,
378 newWHILEOP(0, 1, NULL,
379 $texpr, $mblock, $cont, $mintro));
380 parser->copline = (line_t)$WHILE;
382 | UNTIL '(' remember iexpr ')' mintro mblock cont
384 $$ = block_end($remember,
385 newWHILEOP(0, 1, NULL,
386 $iexpr, $mblock, $cont, $mintro));
387 parser->copline = (line_t)$UNTIL;
389 | FOR '(' remember mnexpr[init_mnexpr] ';'
390 { parser->expect = XTERM; }
392 { parser->expect = XTERM; }
393 mintro mnexpr[iterate_mnexpr] ')'
396 OP *initop = $init_mnexpr;
397 OP *forop = newWHILEOP(0, 1, NULL,
398 scalar($texpr), $mblock, $iterate_mnexpr, $mintro);
400 forop = op_prepend_elem(OP_LINESEQ, initop,
401 op_append_elem(OP_LINESEQ,
402 newOP(OP_UNSTACK, OPf_SPECIAL),
405 PL_hints |= HINT_BLOCK_SCOPE;
406 $$ = block_end($remember, forop);
407 parser->copline = (line_t)$FOR;
409 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
411 $$ = block_end($remember, newFOROP(0, $my_scalar, $mexpr, $mblock, $cont));
412 parser->copline = (line_t)$FOR;
414 | FOR scalar '(' remember mexpr ')' mblock cont
416 $$ = block_end($remember, newFOROP(0,
417 op_lvalue($scalar, OP_ENTERLOOP), $mexpr, $mblock, $cont));
418 parser->copline = (line_t)$FOR;
420 | FOR my_refgen remember my_var
421 { parser->in_my = 0; $<opval>$ = my($my_var); }[variable]
422 '(' mexpr ')' mblock cont
428 newUNOP(OP_REFGEN, 0,
431 $mexpr, $mblock, $cont)
433 parser->copline = (line_t)$FOR;
435 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
437 $$ = block_end($remember, newFOROP(
438 0, op_lvalue(newUNOP(OP_REFGEN, 0,
440 OP_ENTERLOOP), $mexpr, $mblock, $cont));
441 parser->copline = (line_t)$FOR;
443 | FOR '(' remember mexpr ')' mblock cont
445 $$ = block_end($remember,
446 newFOROP(0, NULL, $mexpr, $mblock, $cont));
447 parser->copline = (line_t)$FOR;
451 /* a block is a loop that happens once */
452 $$ = newWHILEOP(0, 1, NULL,
453 NULL, $block, $cont, 0);
455 | PACKAGE BAREWORD[version] BAREWORD[package] PERLY_BRACE_OPEN remember
459 package_version($version);
462 stmtseq PERLY_BRACE_CLOSE
464 /* a block is a loop that happens once */
465 $$ = newWHILEOP(0, 1, NULL,
466 NULL, block_end($remember, $stmtseq), NULL, 0);
467 if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
468 parser->copline = (line_t)$PERLY_BRACE_OPEN;
476 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
477 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
482 parser->copline = NOLINE;
487 formline: THING formarg
491 list = op_append_elem(OP_LIST, $THING, term);
496 if (parser->copline == NOLINE)
497 parser->copline = CopLINE(PL_curcop)-1;
498 else parser->copline--;
499 $$ = newSTATEOP(0, NULL,
500 op_convert_list(OP_FORMLINE, 0, list));
506 | FORMLBRACK stmtseq FORMRBRACK
507 { $$ = op_unscope($stmtseq); }
513 /* An expression which may have a side-effect */
518 | expr[body] IF condition
519 { $$ = newLOGOP(OP_AND, 0, $condition, $body); }
520 | expr[body] UNLESS condition
521 { $$ = newLOGOP(OP_OR, 0, $condition, $body); }
522 | expr[body] WHILE condition
523 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($condition), $body); }
524 | expr[body] UNTIL iexpr
525 { $$ = newLOOPOP(OPf_PARENS, 1, $iexpr, $body); }
526 | expr[body] FOR condition
527 { $$ = newFOROP(0, NULL, $condition, $body, NULL);
528 parser->copline = (line_t)$FOR; }
529 | expr[body] WHEN condition
530 { $$ = newWHENOP($condition, op_scope($body)); }
533 /* else and elsif blocks */
538 ($mblock)->op_flags |= OPf_PARENS;
539 $$ = op_scope($mblock);
541 | ELSIF '(' mexpr ')' mblock else[else.recurse]
542 { parser->copline = (line_t)$ELSIF;
544 newSTATEOP(OPf_SPECIAL,NULL,$mexpr),
545 op_scope($mblock), $[else.recurse]);
546 PL_hints |= HINT_BLOCK_SCOPE;
550 /* Continue blocks */
554 { $$ = op_scope($block); }
557 /* determine whether there are any new my declarations */
559 { $$ = (PL_min_intro_pending &&
560 PL_max_intro_pending >= PL_min_intro_pending);
563 /* Normal expression */
569 /* Boolean expression */
570 texpr : /* NULL means true */
572 (void)scan_num("1", &tmplval);
573 $$ = tmplval.opval; }
577 /* Inverted boolean expression */
579 { $$ = invert(scalar($expr)); }
582 /* Expression with its own lexical scope */
584 { $$ = $expr; intro_my(); }
588 { $$ = $nexpr; intro_my(); }
591 formname: BAREWORD { $$ = $BAREWORD; }
592 | /* NULL */ { $$ = NULL; }
595 startsub: /* NULL */ /* start a regular subroutine scope */
596 { $$ = start_subparse(FALSE, 0);
597 SAVEFREESV(PL_compcv); }
601 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
602 { $$ = start_subparse(FALSE, CVf_ANON);
603 SAVEFREESV(PL_compcv); }
606 startformsub: /* NULL */ /* start a format subroutine scope */
607 { $$ = start_subparse(TRUE, 0);
608 SAVEFREESV(PL_compcv); }
611 /* Name of a subroutine - must be a bareword, could be special */
616 /* Subroutine prototype */
622 /* Optional list of subroutine attributes */
623 subattrlist: /* NULL */
631 /* List of attributes for a "my" variable declaration */
632 myattrlist: COLONATTR THING
640 /* --------------------------------------
641 * subroutine signature parsing
644 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
645 sigvarname: /* NULL */
646 { parser->in_my = 0; $$ = NULL; }
648 { parser->in_my = 0; $$ = $PRIVATEREF; }
657 /* @, %, @foo, %foo */
658 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
660 I32 sigil = $sigslurpsigil;
661 OP *var = $sigvarname;
662 OP *defexpr = $sigdefault;
664 if (parser->sig_slurpy)
665 yyerror("Multiple slurpy parameters not allowed");
666 parser->sig_slurpy = (char)sigil;
669 yyerror("A slurpy parameter may not have "
672 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
676 /* default part of sub signature scalar element: i.e. '= default_expr' */
677 sigdefault: /* NULL */
680 { $$ = newOP(OP_NULL, 0); }
685 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
687 '$' sigvarname sigdefault
689 OP *var = $sigvarname;
690 OP *defexpr = $sigdefault;
692 if (parser->sig_slurpy)
693 yyerror("Slurpy parameter not last");
698 parser->sig_optelems++;
700 if ( defexpr->op_type == OP_NULL
701 && !(defexpr->op_flags & OPf_KIDS))
703 /* handle '$=' special case */
705 yyerror("Optional parameter "
706 "lacks default expression");
710 /* a normal '=default' expression */
711 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
714 /* re-purpose op_targ to hold @_ index */
716 (PADOFFSET)(parser->sig_elems - 1);
719 var->op_flags |= OPf_STACKED;
720 (void)op_sibling_splice(var,
725 var = newUNOP(OP_NULL, 0, defop);
728 /* NB: normally the first child of a
729 * logop is executed before the logop,
730 * and it pushes a boolean result
731 * ready for the logop. For ARGDEFELEM,
732 * the op itself does the boolean
733 * calculation, so set the first op to
736 var->op_next = defop;
737 defexpr->op_next = var;
741 if (parser->sig_optelems)
742 yyerror("Mandatory parameter "
743 "follows optional parameter");
746 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
751 /* subroutine signature element: e.g. '$x = $default' or '%h' */
752 sigelem: sigscalarelem
753 { parser->in_my = KEY_sigvar; $$ = $sigscalarelem; }
755 { parser->in_my = KEY_sigvar; $$ = $sigslurpelem; }
758 /* list of subroutine signature elements */
762 | siglist[list] ',' sigelem[element]
764 $$ = op_append_list(OP_LINESEQ, $list, $element);
766 | sigelem[element] %prec PREC_LOW
771 siglistornull: /* NULL */
776 /* optional subroutine signature */
777 optsubsignature: /* NULL */
780 { $$ = $subsignature; }
782 /* Subroutine signature */
783 subsignature: '(' subsigguts ')'
784 { $$ = $subsigguts; }
789 SAVEIV(parser->sig_elems);
790 SAVEIV(parser->sig_optelems);
791 SAVEI8(parser->sig_slurpy);
792 parser->sig_elems = 0;
793 parser->sig_optelems = 0;
794 parser->sig_slurpy = 0;
795 parser->in_my = KEY_sigvar;
799 OP *sigops = $siglistornull;
800 struct op_argcheck_aux *aux;
803 if (!FEATURE_SIGNATURES_IS_ENABLED)
804 Perl_croak(aTHX_ "Experimental "
805 "subroutine signatures not enabled");
807 /* We shouldn't get here otherwise */
808 Perl_ck_warner_d(aTHX_
809 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
810 "The signatures feature is experimental");
812 aux = (struct op_argcheck_aux*)
813 PerlMemShared_malloc(
814 sizeof(struct op_argcheck_aux));
815 aux->params = parser->sig_elems;
816 aux->opt_params = parser->sig_optelems;
817 aux->slurpy = parser->sig_slurpy;
818 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL,
819 (UNOP_AUX_item *)aux);
820 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
821 sigops = op_prepend_elem(OP_LINESEQ,
822 newSTATEOP(0, NULL, NULL),
824 /* a nextstate at the end handles context
825 * correctly for an empty sub body */
826 sigops = op_append_elem(OP_LINESEQ,
828 newSTATEOP(0, NULL, NULL));
829 /* wrap the list of arg ops in a NULL aux op.
830 This serves two purposes. First, it makes
831 the arg list a separate subtree from the
832 body of the sub, and secondly the null op
833 may in future be upgraded to an OP_SIGNATURE
834 when implemented. For now leave it as
836 $$ = newUNOP_AUX(OP_ARGCHECK, 0, sigops, NULL);
840 /* tell the toker that attrributes can follow
841 * this sig, but only so that the toker
842 * can skip through any (illegal) trailing
843 * attribute text then give a useful error
844 * message about "attributes before sig",
845 * rather than falling over ina mess at
846 * unrecognised syntax.
848 parser->expect = XATTRBLOCK;
849 parser->sig_seen = TRUE;
854 /* Optional subroutine body (for named subroutine declaration) */
855 optsubbody: subbody { $$ = $subbody; }
860 /* Subroutine body (without signature) */
861 subbody: remember PERLY_BRACE_OPEN stmtseq PERLY_BRACE_CLOSE
863 if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
864 parser->copline = (line_t)$PERLY_BRACE_OPEN;
865 $$ = block_end($remember, $stmtseq);
870 /* optional [ Subroutine body with optional signature ] (for named
871 * subroutine declaration) */
872 optsigsubbody: sigsubbody { $$ = $sigsubbody; }
875 /* Subroutine body with optional signature */
876 sigsubbody: remember optsubsignature PERLY_BRACE_OPEN stmtseq PERLY_BRACE_CLOSE
878 if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
879 parser->copline = (line_t)$PERLY_BRACE_OPEN;
880 $$ = block_end($remember,
881 op_append_list(OP_LINESEQ, $optsubsignature, $stmtseq));
886 /* Ordinary expressions; logical combinations */
887 expr : expr[lhs] ANDOP expr[rhs]
888 { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
889 | expr[lhs] OROP[operator] expr[rhs]
890 { $$ = newLOGOP($operator, 0, $lhs, $rhs); }
891 | expr[lhs] DOROP expr[rhs]
892 { $$ = newLOGOP(OP_DOR, 0, $lhs, $rhs); }
893 | listexpr %prec PREC_LOW
896 /* Expressions are a list of terms joined by commas */
897 listexpr: listexpr[list] ','
899 | listexpr[list] ',' term
902 $$ = op_append_elem(OP_LIST, $list, term);
904 | term %prec PREC_LOW
908 listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
909 { $$ = op_convert_list($LSTOP, OPf_STACKED,
910 op_prepend_elem(OP_LIST, newGVREF($LSTOP,$indirob), $listexpr) );
912 | FUNC '(' indirob expr ')' /* print ($fh @args */
913 { $$ = op_convert_list($FUNC, OPf_STACKED,
914 op_prepend_elem(OP_LIST, newGVREF($FUNC,$indirob), $expr) );
916 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
917 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
918 op_append_elem(OP_LIST,
919 op_prepend_elem(OP_LIST, scalar($term), $optexpr),
920 newMETHOP(OP_METHOD, 0, $method)));
922 | term ARROW method /* $foo->bar */
923 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
924 op_append_elem(OP_LIST, scalar($term),
925 newMETHOP(OP_METHOD, 0, $method)));
927 | METHOD indirob optlistexpr /* new Class @args */
928 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
929 op_append_elem(OP_LIST,
930 op_prepend_elem(OP_LIST, $indirob, $optlistexpr),
931 newMETHOP(OP_METHOD, 0, $METHOD)));
933 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
934 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
935 op_append_elem(OP_LIST,
936 op_prepend_elem(OP_LIST, $indirob, $optexpr),
937 newMETHOP(OP_METHOD, 0, $FUNCMETH)));
939 | LSTOP optlistexpr /* print @args */
940 { $$ = op_convert_list($LSTOP, 0, $optlistexpr); }
941 | FUNC '(' optexpr ')' /* print (@args) */
942 { $$ = op_convert_list($FUNC, 0, $optexpr); }
943 | FUNC SUBLEXSTART optexpr SUBLEXEND /* uc($arg) from "\U..." */
944 { $$ = op_convert_list($FUNC, 0, $optexpr); }
945 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
946 { SvREFCNT_inc_simple_void(PL_compcv);
947 $<opval>$ = newANONATTRSUB($startanonsub, 0, NULL, $block); }[anonattrsub]
948 optlistexpr %prec LSTOP /* ... @bar */
949 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
950 op_append_elem(OP_LIST,
951 op_prepend_elem(OP_LIST, $<opval>anonattrsub, $optlistexpr), $LSTOPSUB));
955 /* Names of methods. May use $object->$methodname */
960 /* Some kind of subscripted expression */
961 subscripted: gelem PERLY_BRACE_OPEN expr ';' PERLY_BRACE_CLOSE /* *main::{something} */
962 /* In this and all the hash accessors, ';' is
963 * provided by the tokeniser */
964 { $$ = newBINOP(OP_GELEM, 0, $gelem, scalar($expr)); }
965 | scalar[array] '[' expr ']' /* $array[$element] */
966 { $$ = newBINOP(OP_AELEM, 0, oopsAV($array), scalar($expr));
968 | term[array_reference] ARROW '[' expr ']' /* somearef->[$element] */
969 { $$ = newBINOP(OP_AELEM, 0,
970 ref(newAVREF($array_reference),OP_RV2AV),
973 | subscripted[array_reference] '[' expr ']' /* $foo->[$bar]->[$baz] */
974 { $$ = newBINOP(OP_AELEM, 0,
975 ref(newAVREF($array_reference),OP_RV2AV),
978 | scalar[hash] PERLY_BRACE_OPEN expr ';' PERLY_BRACE_CLOSE /* $foo{bar();} */
979 { $$ = newBINOP(OP_HELEM, 0, oopsHV($hash), jmaybe($expr));
981 | term[hash_reference] ARROW PERLY_BRACE_OPEN expr ';' PERLY_BRACE_CLOSE /* somehref->{bar();} */
982 { $$ = newBINOP(OP_HELEM, 0,
983 ref(newHVREF($hash_reference),OP_RV2HV),
985 | subscripted[hash_reference] PERLY_BRACE_OPEN expr ';' PERLY_BRACE_CLOSE /* $foo->[bar]->{baz;} */
986 { $$ = newBINOP(OP_HELEM, 0,
987 ref(newHVREF($hash_reference),OP_RV2HV),
989 | term[code_reference] ARROW '(' ')' /* $subref->() */
990 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
991 newCVREF(0, scalar($code_reference)));
992 if (parser->expect == XBLOCK)
993 parser->expect = XOPERATOR;
995 | term[code_reference] ARROW '(' expr ')' /* $subref->(@args) */
996 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
997 op_append_elem(OP_LIST, $expr,
998 newCVREF(0, scalar($code_reference))));
999 if (parser->expect == XBLOCK)
1000 parser->expect = XOPERATOR;
1003 | subscripted[code_reference] '(' expr ')' /* $foo->{bar}->(@args) */
1004 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1005 op_append_elem(OP_LIST, $expr,
1006 newCVREF(0, scalar($code_reference))));
1007 if (parser->expect == XBLOCK)
1008 parser->expect = XOPERATOR;
1010 | subscripted[code_reference] '(' ')' /* $foo->{bar}->() */
1011 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1012 newCVREF(0, scalar($code_reference)));
1013 if (parser->expect == XBLOCK)
1014 parser->expect = XOPERATOR;
1016 | '(' expr[list] ')' '[' expr[slice] ']' /* list slice */
1017 { $$ = newSLICEOP(0, $slice, $list); }
1018 | QWLIST '[' expr ']' /* list literal slice */
1019 { $$ = newSLICEOP(0, $expr, $QWLIST); }
1020 | '(' ')' '[' expr ']' /* empty list slice! */
1021 { $$ = newSLICEOP(0, $expr, NULL); }
1024 /* Binary operators between terms */
1025 termbinop: term[lhs] ASSIGNOP term[rhs] /* $x = $y, $x += $y */
1026 { $$ = newASSIGNOP(OPf_STACKED, $lhs, $ASSIGNOP, $rhs); }
1027 | term[lhs] POWOP term[rhs] /* $x ** $y */
1028 { $$ = newBINOP($POWOP, 0, scalar($lhs), scalar($rhs)); }
1029 | term[lhs] MULOP term[rhs] /* $x * $y, $x x $y */
1030 { if ($MULOP != OP_REPEAT)
1032 $$ = newBINOP($MULOP, 0, $lhs, scalar($rhs));
1034 | term[lhs] ADDOP term[rhs] /* $x + $y */
1035 { $$ = newBINOP($ADDOP, 0, scalar($lhs), scalar($rhs)); }
1036 | term[lhs] SHIFTOP term[rhs] /* $x >> $y, $x << $y */
1037 { $$ = newBINOP($SHIFTOP, 0, scalar($lhs), scalar($rhs)); }
1038 | termrelop %prec PREC_LOW /* $x > $y, etc. */
1039 { $$ = $termrelop; }
1040 | termeqop %prec PREC_LOW /* $x == $y, $x cmp $y */
1042 | term[lhs] BITANDOP term[rhs] /* $x & $y */
1043 { $$ = newBINOP($BITANDOP, 0, scalar($lhs), scalar($rhs)); }
1044 | term[lhs] BITOROP term[rhs] /* $x | $y */
1045 { $$ = newBINOP($BITOROP, 0, scalar($lhs), scalar($rhs)); }
1046 | term[lhs] DOTDOT term[rhs] /* $x..$y, $x...$y */
1047 { $$ = newRANGE($DOTDOT, scalar($lhs), scalar($rhs)); }
1048 | term[lhs] ANDAND term[rhs] /* $x && $y */
1049 { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
1050 | term[lhs] OROR term[rhs] /* $x || $y */
1051 { $$ = newLOGOP(OP_OR, 0, $lhs, $rhs); }
1052 | term[lhs] DORDOR term[rhs] /* $x // $y */
1053 { $$ = newLOGOP(OP_DOR, 0, $lhs, $rhs); }
1054 | term[lhs] MATCHOP term[rhs] /* $x =~ /$y/ */
1055 { $$ = bind_match($MATCHOP, $lhs, $rhs); }
1058 termrelop: relopchain %prec PREC_LOW
1059 { $$ = cmpchain_finish($relopchain); }
1060 | term[lhs] NCRELOP term[rhs]
1061 { $$ = newBINOP($NCRELOP, 0, scalar($lhs), scalar($rhs)); }
1063 { yyerror("syntax error"); YYERROR; }
1065 { yyerror("syntax error"); YYERROR; }
1068 relopchain: term[lhs] CHRELOP term[rhs]
1069 { $$ = cmpchain_start($CHRELOP, $lhs, $rhs); }
1070 | relopchain[lhs] CHRELOP term[rhs]
1071 { $$ = cmpchain_extend($CHRELOP, $lhs, $rhs); }
1074 termeqop: eqopchain %prec PREC_LOW
1075 { $$ = cmpchain_finish($eqopchain); }
1076 | term[lhs] NCEQOP term[rhs]
1077 { $$ = newBINOP($NCEQOP, 0, scalar($lhs), scalar($rhs)); }
1079 { yyerror("syntax error"); YYERROR; }
1081 { yyerror("syntax error"); YYERROR; }
1084 eqopchain: term[lhs] CHEQOP term[rhs]
1085 { $$ = cmpchain_start($CHEQOP, $lhs, $rhs); }
1086 | eqopchain[lhs] CHEQOP term[rhs]
1087 { $$ = cmpchain_extend($CHEQOP, $lhs, $rhs); }
1090 /* Unary operators and terms */
1091 termunop : '-' term %prec UMINUS /* -$x */
1092 { $$ = newUNOP(OP_NEGATE, 0, scalar($term)); }
1093 | '+' term %prec UMINUS /* +$x */
1096 | '!' term /* !$x */
1097 { $$ = newUNOP(OP_NOT, 0, scalar($term)); }
1098 | '~' term /* ~$x */
1099 { $$ = newUNOP($1, 0, scalar($term)); }
1100 | term POSTINC /* $x++ */
1101 { $$ = newUNOP(OP_POSTINC, 0,
1102 op_lvalue(scalar($term), OP_POSTINC)); }
1103 | term POSTDEC /* $x-- */
1104 { $$ = newUNOP(OP_POSTDEC, 0,
1105 op_lvalue(scalar($term), OP_POSTDEC));}
1106 | term POSTJOIN /* implicit join after interpolated ->@ */
1107 { $$ = op_convert_list(OP_JOIN, 0,
1117 | PREINC term /* ++$x */
1118 { $$ = newUNOP(OP_PREINC, 0,
1119 op_lvalue(scalar($term), OP_PREINC)); }
1120 | PREDEC term /* --$x */
1121 { $$ = newUNOP(OP_PREDEC, 0,
1122 op_lvalue(scalar($term), OP_PREDEC)); }
1126 /* Constructors for anonymous data */
1127 anonymous: '[' expr ']'
1128 { $$ = newANONLIST($expr); }
1130 { $$ = newANONLIST(NULL);}
1131 | HASHBRACK expr ';' PERLY_BRACE_CLOSE %prec '(' /* { foo => "Bar" } */
1132 { $$ = newANONHASH($expr); }
1133 | HASHBRACK ';' PERLY_BRACE_CLOSE %prec '(' /* { } (';' by tokener) */
1134 { $$ = newANONHASH(NULL); }
1135 | ANONSUB startanonsub proto subattrlist subbody %prec '('
1136 { SvREFCNT_inc_simple_void(PL_compcv);
1137 $$ = newANONATTRSUB($startanonsub, $proto, $subattrlist, $subbody); }
1138 | ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '('
1139 { SvREFCNT_inc_simple_void(PL_compcv);
1140 $$ = newANONATTRSUB($startanonsub, NULL, $subattrlist, $sigsubbody); }
1143 /* Things called with "do" */
1144 termdo : DO term %prec UNIOP /* do $filename */
1145 { $$ = dofile($term, $DO);}
1146 | DO block %prec '(' /* do { code */
1147 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($block));}
1150 term[product] : termbinop
1154 | term[condition] '?' term[then] ':' term[else]
1155 { $$ = newCONDOP(0, $condition, $then, $else); }
1156 | REFGEN term[operand] /* \$x, \@y, \%z */
1157 { $$ = newUNOP(OP_REFGEN, 0, $operand); }
1158 | MY REFGEN term[operand]
1159 { $$ = newUNOP(OP_REFGEN, 0, localize($operand,1)); }
1160 | myattrterm %prec UNIOP
1161 { $$ = $myattrterm; }
1162 | LOCAL term[operand] %prec UNIOP
1163 { $$ = localize($operand,0); }
1165 { $$ = sawparens($expr); }
1169 { $$ = sawparens(newNULLLIST()); }
1178 | arylen %prec '(' /* $#x, $#{ something } */
1179 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($arylen, OP_AV2ARYLEN));}
1181 { $$ = $subscripted; }
1182 | sliceme '[' expr ']' /* array slice */
1183 { $$ = op_prepend_elem(OP_ASLICE,
1184 newOP(OP_PUSHMARK, 0),
1185 newLISTOP(OP_ASLICE, 0,
1187 ref($sliceme, OP_ASLICE)));
1190 $sliceme->op_private & OPpSLICEWARNING;
1192 | kvslice '[' expr ']' /* array key/value slice */
1193 { $$ = op_prepend_elem(OP_KVASLICE,
1194 newOP(OP_PUSHMARK, 0),
1195 newLISTOP(OP_KVASLICE, 0,
1197 ref(oopsAV($kvslice), OP_KVASLICE)));
1200 $kvslice->op_private & OPpSLICEWARNING;
1202 | sliceme PERLY_BRACE_OPEN expr ';' PERLY_BRACE_CLOSE /* @hash{@keys} */
1203 { $$ = op_prepend_elem(OP_HSLICE,
1204 newOP(OP_PUSHMARK, 0),
1205 newLISTOP(OP_HSLICE, 0,
1207 ref(oopsHV($sliceme), OP_HSLICE)));
1210 $sliceme->op_private & OPpSLICEWARNING;
1212 | kvslice PERLY_BRACE_OPEN expr ';' PERLY_BRACE_CLOSE /* %hash{@keys} */
1213 { $$ = op_prepend_elem(OP_KVHSLICE,
1214 newOP(OP_PUSHMARK, 0),
1215 newLISTOP(OP_KVHSLICE, 0,
1217 ref($kvslice, OP_KVHSLICE)));
1220 $kvslice->op_private & OPpSLICEWARNING;
1225 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($amper)); }
1226 | amper '(' ')' /* &foo() or foo() */
1227 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($amper));
1229 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
1231 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1232 op_append_elem(OP_LIST, $expr, scalar($amper)));
1234 | NOAMP subname optlistexpr /* foo @args (no parens) */
1235 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1236 op_append_elem(OP_LIST, $optlistexpr, scalar($subname)));
1238 | term[operand] ARROW '$' '*'
1239 { $$ = newSVREF($operand); }
1240 | term[operand] ARROW '@' '*'
1241 { $$ = newAVREF($operand); }
1242 | term[operand] ARROW '%' '*'
1243 { $$ = newHVREF($operand); }
1244 | term[operand] ARROW '&' '*'
1245 { $$ = newUNOP(OP_ENTERSUB, 0,
1246 scalar(newCVREF($3,$operand))); }
1247 | term[operand] ARROW '*' '*' %prec '('
1248 { $$ = newGVREF(0,$operand); }
1249 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
1250 { $$ = newOP($LOOPEX, OPf_SPECIAL);
1251 PL_hints |= HINT_BLOCK_SCOPE; }
1252 | LOOPEX term[operand]
1253 { $$ = newLOOPEX($LOOPEX,$operand); }
1254 | NOTOP listexpr /* not $foo */
1255 { $$ = newUNOP(OP_NOT, 0, scalar($listexpr)); }
1256 | UNIOP /* Unary op, $_ implied */
1257 { $$ = newOP($UNIOP, 0); }
1258 | UNIOP block /* eval { foo }* */
1259 { $$ = newUNOP($UNIOP, 0, $block); }
1260 | UNIOP term[operand] /* Unary op */
1261 { $$ = newUNOP($UNIOP, 0, $operand); }
1262 | REQUIRE /* require, $_ implied */
1263 { $$ = newOP(OP_REQUIRE, $REQUIRE ? OPf_SPECIAL : 0); }
1264 | REQUIRE term[operand] /* require Foo */
1265 { $$ = newUNOP(OP_REQUIRE, $REQUIRE ? OPf_SPECIAL : 0, $operand); }
1267 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($UNIOPSUB)); }
1268 | UNIOPSUB term[operand] /* Sub treated as unop */
1269 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1270 op_append_elem(OP_LIST, $operand, scalar($UNIOPSUB))); }
1271 | FUNC0 /* Nullary operator */
1272 { $$ = newOP($FUNC0, 0); }
1274 { $$ = newOP($FUNC0, 0);}
1275 | FUNC0OP /* Same as above, but op created in toke.c */
1279 | FUNC0SUB /* Sub treated as nullop */
1280 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($FUNC0SUB)); }
1281 | FUNC1 '(' ')' /* not () */
1282 { $$ = ($FUNC1 == OP_NOT)
1283 ? newUNOP($FUNC1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1284 : newOP($FUNC1, OPf_SPECIAL); }
1285 | FUNC1 '(' expr ')' /* not($foo) */
1286 { $$ = newUNOP($FUNC1, 0, $expr); }
1287 | PMFUNC /* m//, s///, qr//, tr/// */
1289 if ( $PMFUNC->op_type != OP_TRANS
1290 && $PMFUNC->op_type != OP_TRANSR
1291 && (((PMOP*)$PMFUNC)->op_pmflags & PMf_HAS_CV))
1293 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1294 SAVEFREESV(PL_compcv);
1298 SUBLEXSTART listexpr optrepl SUBLEXEND
1299 { $$ = pmruntime($PMFUNC, $listexpr, $optrepl, 1, $<ival>2); }
1305 /* "my" declarations, with optional attributes */
1306 myattrterm: MY myterm myattrlist
1307 { $$ = my_attrs($myterm,$myattrlist); }
1309 { $$ = localize($myterm,1); }
1310 | MY REFGEN myterm myattrlist
1311 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($myterm,$myattrlist)); }
1314 /* Things that can be "my"'d */
1315 myterm : '(' expr ')'
1316 { $$ = sawparens($expr); }
1318 { $$ = sawparens(newNULLLIST()); }
1328 /* Basic list expressions */
1329 optlistexpr: /* NULL */ %prec PREC_LOW
1331 | listexpr %prec PREC_LOW
1347 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1350 { parser->in_my = 0; $$ = my($scalar); }
1358 refgen_topic: my_var
1362 my_refgen: MY REFGEN
1367 { $$ = newCVREF($1,$indirob); }
1370 scalar : '$' indirob
1371 { $$ = newSVREF($indirob); }
1375 { $$ = newAVREF($indirob);
1376 if ($$) $$->op_private |= $1;
1381 { $$ = newHVREF($indirob);
1382 if ($$) $$->op_private |= $1;
1386 arylen : DOLSHARP indirob
1387 { $$ = newAVREF($indirob); }
1388 | term ARROW DOLSHARP '*'
1389 { $$ = newAVREF($term); }
1393 { $$ = newGVREF(0,$indirob); }
1398 { $$ = newAVREF($term); }
1403 { $$ = newHVREF($term); }
1408 { $$ = newGVREF(0,$term); }
1411 /* Indirect objects */
1413 { $$ = scalar($BAREWORD); }
1414 | scalar %prec PREC_LOW
1415 { $$ = scalar($scalar); }
1417 { $$ = op_scope($block); }
1420 { $$ = $PRIVATEREF; }