This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / perly.y
1 /*    perly.y
2  *
3  *    Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
4  *    Copyright (c) 2007, 2008, 2009, 2010, 2011 by Larry Wall and others
5  *
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.
8  *
9  */
10
11 /*
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.'
14  *
15  *     [p.171 of _The Lord of the Rings_, I/x: "Strider"]
16  */
17
18 /*
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.
22  *
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.
26  *
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
29  * an action.
30  */
31
32 /*  Make the parser re-entrant. */
33
34 %define api.pure
35
36 %start grammar
37
38 %union {
39     I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
40                                 must always be 1st union member) */
41     char *pval;
42     OP *opval;
43     GV *gvval;
44 }
45
46 %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ GRAMSUBSIGNATURE
47
48 /* Tokens emitted by toke.c for simple punctiation characters - &, {, }, etc... */
49 %token <ival> PERLY_AMPERSAND
50 %token <ival> PERLY_BRACE_OPEN
51 %token <ival> PERLY_BRACE_CLOSE
52 %token <ival> PERLY_BRACKET_OPEN
53 %token <ival> PERLY_BRACKET_CLOSE
54 %token <ival> PERLY_COMMA
55 %token <ival> PERLY_DOLLAR
56 %token <ival> PERLY_DOT
57 %token <ival> PERLY_EQUAL_SIGN
58 %token <ival> PERLY_MINUS
59 %token <ival> PERLY_PERCENT_SIGN
60 %token <ival> PERLY_PLUS
61 %token <ival> PERLY_SEMICOLON
62 %token <ival> PERLY_SLASH
63 %token <ival> PERLY_SNAIL
64 %token <ival> PERLY_STAR
65
66 /* Tokens emitted by toke.c on simple keywords */
67 %token <ival> KW_FORMAT KW_PACKAGE 
68 %token <ival> KW_LOCAL KW_MY
69 %token <ival> KW_IF KW_ELSE KW_ELSIF KW_UNLESS
70 %token <ival> KW_FOR KW_UNTIL KW_WHILE KW_CONTINUE
71 %token <ival> KW_GIVEN KW_WHEN KW_DEFAULT
72 %token <ival> KW_TRY KW_CATCH KW_FINALLY KW_DEFER
73 %token <ival> KW_REQUIRE KW_DO
74
75 /* The 'use' and 'no' keywords both emit this */
76 %token <ival> KW_USE_or_NO
77
78 /* The 'sub' keyword is a bit special; four different tokens depending on
79  *   named-vs-anon, and whether signatures are in effect */
80 %token <ival> KW_SUB_named KW_SUB_named_sig KW_SUB_anon KW_SUB_anon_sig
81
82 /* Tokens emitted in other situations */
83 %token <opval> BAREWORD METHCALL0 METHCALL THING PMFUNC PRIVATEREF QWLIST
84 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
85 %token <opval> PLUGEXPR PLUGSTMT
86 %token <opval> LABEL
87 %token <ival> LOOPEX DOTDOT YADAYADA
88 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
89 %token <ival> MULOP ADDOP
90 %token <ival> DOLSHARP HASHBRACK NOAMP
91 %token <ival> COLONATTR FORMLBRACK FORMRBRACK
92 %token <ival> SUBLEXSTART SUBLEXEND
93
94 %type <ival> grammar remember mremember
95 %type <ival>  startsub startanonsub startformsub
96
97 %type <ival> mintro
98
99 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else finally
100 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
101 %type <opval> condition
102 %type <opval> catch_paren
103 %type <opval> empty
104 %type <opval> sliceme kvslice gelem
105 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
106 %type <opval> optlistexpr optexpr optrepl indirob listop methodname
107 %type <opval> formname subname proto cont my_scalar my_var
108 %type <opval> list_of_scalars my_list_of_scalars refgen_topic formblock
109 %type <opval> subattrlist myattrlist myattrterm myterm
110 %type <opval> termbinop termunop anonymous termdo
111 %type <opval> termrelop relopchain termeqop eqopchain
112 %type <ival>  sigslurpsigil
113 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
114 %type <opval> sigelem siglist optsiglist subsigguts subsignature optsubsignature
115 %type <opval> subbody optsubbody sigsubbody optsigsubbody
116 %type <opval> formstmtseq formline formarg
117
118 %nonassoc <ival> PREC_LOW
119 %nonassoc LOOPEX
120
121 %left <ival> OROP
122 %left <ival> ANDOP
123 %right <ival> NOTOP
124 %nonassoc LSTOP LSTOPSUB
125 %left PERLY_COMMA
126 %right <ival> ASSIGNOP
127 %right <ival> PERLY_QUESTION_MARK PERLY_COLON
128 %nonassoc DOTDOT
129 %left <ival> OROR DORDOR
130 %left <ival> ANDAND
131 %left <ival> BITOROP
132 %left <ival> BITANDOP
133 %left <ival> CHEQOP NCEQOP
134 %left <ival> CHRELOP NCRELOP
135 %nonassoc UNIOP UNIOPSUB
136 %nonassoc KW_REQUIRE
137 %left <ival> SHIFTOP
138 %left ADDOP
139 %left MULOP
140 %left <ival> MATCHOP
141 %right <ival> PERLY_EXCLAMATION_MARK PERLY_TILDE UMINUS REFGEN
142 %right <ival> POWOP
143 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
144 %left <ival> ARROW
145 %nonassoc <ival> PERLY_PAREN_CLOSE
146 %left <ival> PERLY_PAREN_OPEN
147 %left PERLY_BRACKET_OPEN PERLY_BRACE_OPEN
148
149 %% /* RULES */
150
151 /* Top-level choice of what kind of thing yyparse was called to parse */
152 grammar :       GRAMPROG
153                         {
154                           parser->expect = XSTATE;
155                           $<ival>$ = 0;
156                         }
157                 remember stmtseq
158                         {
159                           newPROG(block_end($remember,$stmtseq));
160                           PL_compiling.cop_seq = 0;
161                           $$ = 0;
162                         }
163         |       GRAMEXPR
164                         {
165                           parser->expect = XTERM;
166                           $<ival>$ = 0;
167                         }
168                 optexpr
169                         {
170                           PL_eval_root = $optexpr;
171                           $$ = 0;
172                         }
173         |       GRAMBLOCK
174                         {
175                           parser->expect = XBLOCK;
176                           $<ival>$ = 0;
177                         }
178                 block
179                         {
180                           PL_pad_reset_pending = TRUE;
181                           PL_eval_root = $block;
182                           $$ = 0;
183                           yyunlex();
184                           parser->yychar = yytoken = YYEOF;
185                         }
186         |       GRAMBARESTMT
187                         {
188                           parser->expect = XSTATE;
189                           $<ival>$ = 0;
190                         }
191                 barestmt
192                         {
193                           PL_pad_reset_pending = TRUE;
194                           PL_eval_root = $barestmt;
195                           $$ = 0;
196                           yyunlex();
197                           parser->yychar = yytoken = YYEOF;
198                         }
199         |       GRAMFULLSTMT
200                         {
201                           parser->expect = XSTATE;
202                           $<ival>$ = 0;
203                         }
204                 fullstmt
205                         {
206                           PL_pad_reset_pending = TRUE;
207                           PL_eval_root = $fullstmt;
208                           $$ = 0;
209                           yyunlex();
210                           parser->yychar = yytoken = YYEOF;
211                         }
212         |       GRAMSTMTSEQ
213                         {
214                           parser->expect = XSTATE;
215                           $<ival>$ = 0;
216                         }
217                 stmtseq
218                         {
219                           PL_eval_root = $stmtseq;
220                           $$ = 0;
221                         }
222         |       GRAMSUBSIGNATURE
223                         {
224                           parser->expect = XSTATE;
225                           $<ival>$ = 0;
226                         }
227                 subsigguts
228                         {
229                           PL_eval_root = $subsigguts;
230                           $$ = 0;
231                         }
232         ;
233
234 /* An ordinary block */
235 block   :       PERLY_BRACE_OPEN remember stmtseq PERLY_BRACE_CLOSE
236                         { if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
237                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
238                           $$ = block_end($remember, $stmtseq);
239                         }
240         ;
241
242 empty
243         :       %empty          { $$ = NULL; }
244         ;
245
246 /* format body */
247 formblock:      PERLY_EQUAL_SIGN remember PERLY_SEMICOLON FORMRBRACK formstmtseq PERLY_SEMICOLON PERLY_DOT
248                         { if (parser->copline > (line_t)$PERLY_EQUAL_SIGN)
249                               parser->copline = (line_t)$PERLY_EQUAL_SIGN;
250                           $$ = block_end($remember, $formstmtseq);
251                         }
252         ;
253
254 remember:       %empty  /* start a full lexical scope */
255                         { $$ = block_start(TRUE);
256                           parser->parsed_sub = 0; }
257         ;
258
259 mblock  :       PERLY_BRACE_OPEN mremember stmtseq PERLY_BRACE_CLOSE
260                         { if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
261                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
262                           $$ = block_end($mremember, $stmtseq);
263                         }
264         ;
265
266 mremember:      %empty  /* start a partial lexical scope */
267                         { $$ = block_start(FALSE);
268                           parser->parsed_sub = 0; }
269         ;
270
271 /* The parenthesized variable of a catch block */
272 catch_paren:    empty
273                         /* not really valid grammar but we detect it in the
274                          * action block to throw a nicer error message */
275         |       PERLY_PAREN_OPEN
276                         { parser->in_my = 1; }
277                 scalar
278                         { parser->in_my = 0; intro_my(); }
279                 PERLY_PAREN_CLOSE
280                         { $$ = $scalar; }
281         ;
282
283 /* A sequence of statements in the program */
284 stmtseq
285         :       empty
286         |       stmtseq[list] fullstmt
287                         {   $$ = op_append_list(OP_LINESEQ, $list, $fullstmt);
288                             PL_pad_reset_pending = TRUE;
289                             if ($list && $fullstmt)
290                                 PL_hints |= HINT_BLOCK_SCOPE;
291                         }
292         ;
293
294 /* A sequence of format lines */
295 formstmtseq
296         :       empty
297         |       formstmtseq[list] formline
298                         {   $$ = op_append_list(OP_LINESEQ, $list, $formline);
299                             PL_pad_reset_pending = TRUE;
300                             if ($list && $formline)
301                                 PL_hints |= HINT_BLOCK_SCOPE;
302                         }
303         ;
304
305 /* A statement in the program, including optional labels */
306 fullstmt:       barestmt
307                         {
308                           $$ = $barestmt ? newSTATEOP(0, NULL, $barestmt) : NULL;
309                         }
310         |       labfullstmt
311                         { $$ = $labfullstmt; }
312         ;
313
314 labfullstmt:    LABEL barestmt
315                         {
316                           SV *label = cSVOPx_sv($LABEL);
317                           $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
318                                             savepv(SvPVX_const(label)), $barestmt);
319                           op_free($LABEL);
320                         }
321         |       LABEL labfullstmt[list]
322                         {
323                           SV *label = cSVOPx_sv($LABEL);
324                           $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
325                                             savepv(SvPVX_const(label)), $list);
326                           op_free($LABEL);
327                         }
328         ;
329
330 /* A bare statement, lacking label and other aspects of state op */
331 barestmt:       PLUGSTMT
332                         { $$ = $PLUGSTMT; }
333         |       KW_FORMAT startformsub formname formblock
334                         {
335                           CV *fmtcv = PL_compcv;
336                           newFORM($startformsub, $formname, $formblock);
337                           $$ = NULL;
338                           if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
339                               pad_add_weakref(fmtcv);
340                           }
341                           parser->parsed_sub = 1;
342                         }
343         |       KW_SUB_named subname startsub
344                     /* sub declaration or definition not within scope
345                        of 'use feature "signatures"'*/
346                         {
347                           init_named_cv(PL_compcv, $subname);
348                           parser->in_my = 0;
349                           parser->in_my_stash = NULL;
350                         }
351                     proto subattrlist optsubbody
352                         {
353                           SvREFCNT_inc_simple_void(PL_compcv);
354                           $subname->op_type == OP_CONST
355                               ? newATTRSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
356                               : newMYSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
357                           ;
358                           $$ = NULL;
359                           intro_my();
360                           parser->parsed_sub = 1;
361                         }
362         |       KW_SUB_named_sig subname startsub
363                     /* sub declaration or definition under 'use feature
364                      * "signatures"'. (Note that a signature isn't
365                      * allowed in a declaration)
366                      */
367                         {
368                           init_named_cv(PL_compcv, $subname);
369                           parser->in_my = 0;
370                           parser->in_my_stash = NULL;
371                         }
372                     subattrlist optsigsubbody
373                         {
374                           SvREFCNT_inc_simple_void(PL_compcv);
375                           $subname->op_type == OP_CONST
376                               ? newATTRSUB($startsub, $subname, NULL, $subattrlist, $optsigsubbody)
377                               : newMYSUB(  $startsub, $subname, NULL, $subattrlist, $optsigsubbody)
378                           ;
379                           $$ = NULL;
380                           intro_my();
381                           parser->parsed_sub = 1;
382                         }
383         |       KW_PACKAGE BAREWORD[version] BAREWORD[package] PERLY_SEMICOLON
384                         {
385                           package($package);
386                           if ($version)
387                               package_version($version);
388                           $$ = NULL;
389                         }
390         |       KW_USE_or_NO startsub
391                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
392                 BAREWORD[version] BAREWORD[module] optlistexpr PERLY_SEMICOLON
393                         {
394                           SvREFCNT_inc_simple_void(PL_compcv);
395                           utilize($KW_USE_or_NO, $startsub, $version, $module, $optlistexpr);
396                           parser->parsed_sub = 1;
397                           $$ = NULL;
398                         }
399         |       KW_IF PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock else
400                         {
401                           $$ = block_end($remember,
402                               newCONDOP(0, $mexpr, op_scope($mblock), $else));
403                           parser->copline = (line_t)$KW_IF;
404                         }
405         |       KW_UNLESS PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock else
406                         {
407                           $$ = block_end($remember,
408                               newCONDOP(0, $mexpr, $else, op_scope($mblock)));
409                           parser->copline = (line_t)$KW_UNLESS;
410                         }
411         |       KW_GIVEN PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock
412                         {
413                           $$ = block_end($remember, newGIVENOP($mexpr, op_scope($mblock), 0));
414                           parser->copline = (line_t)$KW_GIVEN;
415                         }
416         |       KW_WHEN PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock
417                         { $$ = block_end($remember, newWHENOP($mexpr, op_scope($mblock))); }
418         |       KW_DEFAULT block
419                         { $$ = newWHENOP(0, op_scope($block)); }
420         |       KW_WHILE PERLY_PAREN_OPEN remember texpr PERLY_PAREN_CLOSE mintro mblock cont
421                         {
422                           $$ = block_end($remember,
423                                   newWHILEOP(0, 1, NULL,
424                                       $texpr, $mblock, $cont, $mintro));
425                           parser->copline = (line_t)$KW_WHILE;
426                         }
427         |       KW_UNTIL PERLY_PAREN_OPEN remember iexpr PERLY_PAREN_CLOSE mintro mblock cont
428                         {
429                           $$ = block_end($remember,
430                                   newWHILEOP(0, 1, NULL,
431                                       $iexpr, $mblock, $cont, $mintro));
432                           parser->copline = (line_t)$KW_UNTIL;
433                         }
434         |       KW_FOR PERLY_PAREN_OPEN remember mnexpr[init_mnexpr] PERLY_SEMICOLON
435                         { parser->expect = XTERM; }
436                 texpr PERLY_SEMICOLON
437                         { parser->expect = XTERM; }
438                 mintro mnexpr[iterate_mnexpr] PERLY_PAREN_CLOSE
439                 mblock
440                         {
441                           OP *initop = $init_mnexpr;
442                           OP *forop = newWHILEOP(0, 1, NULL,
443                                       scalar($texpr), $mblock, $iterate_mnexpr, $mintro);
444                           if (initop) {
445                               forop = op_prepend_elem(OP_LINESEQ, initop,
446                                   op_append_elem(OP_LINESEQ,
447                                       newOP(OP_UNSTACK, OPf_SPECIAL),
448                                       forop));
449                           }
450                           PL_hints |= HINT_BLOCK_SCOPE;
451                           $$ = block_end($remember, forop);
452                           parser->copline = (line_t)$KW_FOR;
453                         }
454         |       KW_FOR KW_MY remember my_scalar PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock cont
455                         {
456                           $$ = block_end($remember, newFOROP(0, $my_scalar, $mexpr, $mblock, $cont));
457                           parser->copline = (line_t)$KW_FOR;
458                         }
459         |       KW_FOR KW_MY remember PERLY_PAREN_OPEN my_list_of_scalars PERLY_PAREN_CLOSE PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock cont
460                         {
461                           if ($my_list_of_scalars->op_type == OP_PADSV)
462                             /* degenerate case of 1 var: for my ($x) ....
463                                Flag it so it can be special-cased in newFOROP */
464                                 $my_list_of_scalars->op_flags |= OPf_PARENS;
465                           $$ = block_end($remember, newFOROP(0, $my_list_of_scalars, $mexpr, $mblock, $cont));
466                           parser->copline = (line_t)$KW_FOR;
467                         }
468         |       KW_FOR scalar PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock cont
469                         {
470                           $$ = block_end($remember, newFOROP(0,
471                                       op_lvalue($scalar, OP_ENTERLOOP), $mexpr, $mblock, $cont));
472                           parser->copline = (line_t)$KW_FOR;
473                         }
474         |       KW_FOR my_refgen remember my_var
475                         { parser->in_my = 0; $<opval>$ = my($my_var); }[variable]
476                 PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock cont
477                         {
478                           $$ = block_end(
479                                 $remember,
480                                 newFOROP(0,
481                                          op_lvalue(
482                                             newUNOP(OP_REFGEN, 0,
483                                                     $<opval>variable),
484                                             OP_ENTERLOOP),
485                                          $mexpr, $mblock, $cont)
486                           );
487                           parser->copline = (line_t)$KW_FOR;
488                         }
489         |       KW_FOR REFGEN refgen_topic PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock cont
490                         {
491                           $$ = block_end($remember, newFOROP(
492                                 0, op_lvalue(newUNOP(OP_REFGEN, 0,
493                                                      $refgen_topic),
494                                              OP_ENTERLOOP), $mexpr, $mblock, $cont));
495                           parser->copline = (line_t)$KW_FOR;
496                         }
497         |       KW_FOR PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock cont
498                         {
499                           $$ = block_end($remember,
500                                   newFOROP(0, NULL, $mexpr, $mblock, $cont));
501                           parser->copline = (line_t)$KW_FOR;
502                         }
503         |       KW_TRY mblock[try] KW_CATCH remember catch_paren[scalar]
504                         {
505                           if(!$scalar) {
506                               yyerror("catch block requires a (VAR)");
507                               YYERROR;
508                           }
509                         }
510                 mblock[catch] finally
511                         {
512                           $$ = newTRYCATCHOP(0,
513                                   $try, $scalar, block_end($remember, op_scope($catch)));
514                           if($finally)
515                               $$ = op_wrap_finally($$, $finally);
516                           parser->copline = (line_t)$KW_TRY;
517                         }
518         |       block cont
519                         {
520                           /* a block is a loop that happens once */
521                           $$ = newWHILEOP(0, 1, NULL,
522                                   NULL, $block, $cont, 0);
523                         }
524         |       KW_PACKAGE BAREWORD[version] BAREWORD[package] PERLY_BRACE_OPEN remember
525                         {
526                           package($package);
527                           if ($version) {
528                               package_version($version);
529                           }
530                         }
531                 stmtseq PERLY_BRACE_CLOSE
532                         {
533                           /* a block is a loop that happens once */
534                           $$ = newWHILEOP(0, 1, NULL,
535                                   NULL, block_end($remember, $stmtseq), NULL, 0);
536                           if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
537                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
538                         }
539         |       sideff PERLY_SEMICOLON
540                         {
541                           $$ = $sideff;
542                         }
543         |       KW_DEFER mblock
544                         {
545                           $$ = newDEFEROP(0, op_scope($2));
546                         }
547         |       YADAYADA PERLY_SEMICOLON
548                         {
549                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
550                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
551                         }
552         |       PERLY_SEMICOLON
553                         {
554                           $$ = NULL;
555                           parser->copline = NOLINE;
556                         }
557         ;
558
559 /* Format line */
560 formline:       THING formarg
561                         { OP *list;
562                           if ($formarg) {
563                               OP *term = $formarg;
564                               list = op_append_elem(OP_LIST, $THING, term);
565                           }
566                           else {
567                               list = $THING;
568                           }
569                           if (parser->copline == NOLINE)
570                                parser->copline = CopLINE(PL_curcop)-1;
571                           else parser->copline--;
572                           $$ = newSTATEOP(0, NULL,
573                                           op_convert_list(OP_FORMLINE, 0, list));
574                         }
575         ;
576
577 formarg
578         :       empty
579         |       FORMLBRACK stmtseq FORMRBRACK
580                         { $$ = op_unscope($stmtseq); }
581         ;
582
583 condition: expr
584 ;
585
586 /* An expression which may have a side-effect */
587 sideff  :       error
588                         { $$ = NULL; }
589         |       expr[body]
590                         { $$ = $body; }
591         |       expr[body] KW_IF condition
592                         { $$ = newLOGOP(OP_AND, 0, $condition, $body); }
593         |       expr[body] KW_UNLESS condition
594                         { $$ = newLOGOP(OP_OR, 0, $condition, $body); }
595         |       expr[body] KW_WHILE condition
596                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($condition), $body); }
597         |       expr[body] KW_UNTIL iexpr
598                         { $$ = newLOOPOP(OPf_PARENS, 1, $iexpr, $body); }
599         |       expr[body] KW_FOR condition
600                         { $$ = newFOROP(0, NULL, $condition, $body, NULL);
601                           parser->copline = (line_t)$KW_FOR; }
602         |       expr[body] KW_WHEN condition
603                         { $$ = newWHENOP($condition, op_scope($body)); }
604         ;
605
606 /* else and elsif blocks */
607 else
608         :       empty
609         |       KW_ELSE mblock
610                         {
611                           ($mblock)->op_flags |= OPf_PARENS;
612                           $$ = op_scope($mblock);
613                         }
614         |       KW_ELSIF PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock else[else.recurse]
615                         { parser->copline = (line_t)$KW_ELSIF;
616                             $$ = newCONDOP(0,
617                                 newSTATEOP(OPf_SPECIAL,NULL,$mexpr),
618                                 op_scope($mblock), $[else.recurse]);
619                           PL_hints |= HINT_BLOCK_SCOPE;
620                         }
621         ;
622
623 /* Continue blocks */
624 cont
625         :       empty
626         |       KW_CONTINUE block
627                         { $$ = op_scope($block); }
628         ;
629
630 /* Finally blocks */
631 finally :       %empty
632                         { $$ = NULL; }
633         |       KW_FINALLY block
634                         { $$ = op_scope($block); }
635         ;
636
637 /* determine whether there are any new my declarations */
638 mintro  :       %empty
639                         { $$ = (PL_min_intro_pending &&
640                             PL_max_intro_pending >=  PL_min_intro_pending);
641                           intro_my(); }
642
643 /* Normal expression */
644 nexpr
645         :       empty
646         |       sideff
647         ;
648
649 /* Boolean expression */
650 texpr   :       %empty /* NULL means true */
651                         { YYSTYPE tmplval;
652                           (void)scan_num("1", &tmplval);
653                           $$ = tmplval.opval; }
654         |       expr
655         ;
656
657 /* Inverted boolean expression */
658 iexpr   :       expr
659                         { $$ = invert(scalar($expr)); }
660         ;
661
662 /* Expression with its own lexical scope */
663 mexpr   :       expr
664                         { $$ = $expr; intro_my(); }
665         ;
666
667 mnexpr  :       nexpr
668                         { $$ = $nexpr; intro_my(); }
669         ;
670
671 formname:       BAREWORD        { $$ = $BAREWORD; }
672         |       empty
673         ;
674
675 startsub:       %empty  /* start a regular subroutine scope */
676                         { $$ = start_subparse(FALSE, 0);
677                             SAVEFREESV(PL_compcv); }
678
679         ;
680
681 startanonsub:   %empty  /* start an anonymous subroutine scope */
682                         { $$ = start_subparse(FALSE, CVf_ANON);
683                             SAVEFREESV(PL_compcv); }
684         ;
685
686 startformsub:   %empty  /* start a format subroutine scope */
687                         { $$ = start_subparse(TRUE, 0);
688                             SAVEFREESV(PL_compcv); }
689         ;
690
691 /* Name of a subroutine - must be a bareword, could be special */
692 subname :       BAREWORD
693         |       PRIVATEREF
694         ;
695
696 /* Subroutine prototype */
697 proto
698         :       empty
699         |       THING
700         ;
701
702 /* Optional list of subroutine attributes */
703 subattrlist
704         :       empty
705         |       COLONATTR THING
706                         { $$ = $THING; }
707         |       COLONATTR
708                         { $$ = NULL; }
709         ;
710
711 /* List of attributes for a "my" variable declaration */
712 myattrlist:     COLONATTR THING
713                         { $$ = $THING; }
714         |       COLONATTR
715                         { $$ = NULL; }
716         ;
717
718
719
720 /* --------------------------------------
721  * subroutine signature parsing
722  */
723
724 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable  */
725 sigvarname:     %empty
726                         { parser->in_my = 0; $$ = NULL; }
727         |       PRIVATEREF
728                         { parser->in_my = 0; $$ = $PRIVATEREF; }
729         ;
730
731 sigslurpsigil:
732                 PERLY_SNAIL
733                         { $$ = '@'; }
734         |       PERLY_PERCENT_SIGN
735                         { $$ = '%'; }
736
737 /* @, %, @foo, %foo */
738 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */ 
739                         {
740                             I32 sigil   = $sigslurpsigil;
741                             OP *var     = $sigvarname;
742                             OP *defexpr = $sigdefault;
743
744                             if (parser->sig_slurpy)
745                                 yyerror("Multiple slurpy parameters not allowed");
746                             parser->sig_slurpy = (char)sigil;
747
748                             if (defexpr)
749                                 yyerror("A slurpy parameter may not have "
750                                         "a default value");
751
752                             $$ = var ? newSTATEOP(0, NULL, var) : NULL;
753                         }
754         ;
755
756 /* default part of sub signature scalar element: i.e. '= default_expr' */
757 sigdefault
758         :       empty
759         |       ASSIGNOP
760                         { $$ = newOP(OP_NULL, 0); }
761         |       ASSIGNOP term
762                         { $$ = $term; }
763
764
765 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
766 sigscalarelem:
767                 PERLY_DOLLAR sigvarname sigdefault
768                         {
769                             OP *var     = $sigvarname;
770                             OP *defexpr = $sigdefault;
771
772                             if (parser->sig_slurpy)
773                                 yyerror("Slurpy parameter not last");
774
775                             parser->sig_elems++;
776
777                             if (defexpr) {
778                                 parser->sig_optelems++;
779
780                                 if (   defexpr->op_type == OP_NULL
781                                     && !(defexpr->op_flags & OPf_KIDS))
782                                 {
783                                     /* handle '$=' special case */
784                                     if (var)
785                                         yyerror("Optional parameter "
786                                                     "lacks default expression");
787                                     op_free(defexpr);
788                                 }
789                                 else { 
790                                     /* a normal '=default' expression */ 
791                                     OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
792                                                         defexpr,
793                                                         LINKLIST(defexpr));
794                                     /* re-purpose op_targ to hold @_ index */
795                                     defop->op_targ =
796                                         (PADOFFSET)(parser->sig_elems - 1);
797
798                                     if (var) {
799                                         var->op_flags |= OPf_STACKED;
800                                         (void)op_sibling_splice(var,
801                                                         NULL, 0, defop);
802                                         scalar(defop);
803                                     }
804                                     else
805                                         var = newUNOP(OP_NULL, 0, defop);
806
807                                     LINKLIST(var);
808                                     /* NB: normally the first child of a
809                                      * logop is executed before the logop,
810                                      * and it pushes a boolean result
811                                      * ready for the logop. For ARGDEFELEM,
812                                      * the op itself does the boolean
813                                      * calculation, so set the first op to
814                                      * it instead.
815                                      */
816                                     var->op_next = defop;
817                                     defexpr->op_next = var;
818                                 }
819                             }
820                             else {
821                                 if (parser->sig_optelems)
822                                     yyerror("Mandatory parameter "
823                                             "follows optional parameter");
824                             }
825
826                             $$ = var ? newSTATEOP(0, NULL, var) : NULL;
827                         }
828         ;
829
830
831 /* subroutine signature element: e.g. '$x = $default' or '%h' */
832 sigelem:        sigscalarelem
833                         { parser->in_my = KEY_sigvar; $$ = $sigscalarelem; }
834         |       sigslurpelem
835                         { parser->in_my = KEY_sigvar; $$ = $sigslurpelem; }
836         ;
837
838 /* list of subroutine signature elements */
839 siglist:
840                 siglist[list] PERLY_COMMA
841                         { $$ = $list; }
842         |       siglist[list] PERLY_COMMA sigelem[element]
843                         {
844                           $$ = op_append_list(OP_LINESEQ, $list, $element);
845                         }
846         |       sigelem[element]  %prec PREC_LOW
847                         { $$ = $element; }
848         ;
849
850 /* () or (....) */
851 optsiglist
852         :       empty
853         |       siglist
854         ;
855
856 /* optional subroutine signature */
857 optsubsignature
858         :       empty
859         |       subsignature
860         ;
861
862 /* Subroutine signature */
863 subsignature:   PERLY_PAREN_OPEN subsigguts PERLY_PAREN_CLOSE
864                         { $$ = $subsigguts; }
865
866 subsigguts:
867                         {
868                             ENTER;
869                             SAVEIV(parser->sig_elems);
870                             SAVEIV(parser->sig_optelems);
871                             SAVEI8(parser->sig_slurpy);
872                             parser->sig_elems    = 0;
873                             parser->sig_optelems = 0;
874                             parser->sig_slurpy   = 0;
875                             parser->in_my        = KEY_sigvar;
876                         }
877                 optsiglist
878                         {
879                             OP            *sigops = $optsiglist;
880                             struct op_argcheck_aux *aux;
881                             OP            *check;
882
883                             if (!FEATURE_SIGNATURES_IS_ENABLED)
884                                 Perl_croak(aTHX_ "Experimental "
885                                     "subroutine signatures not enabled");
886
887                             /* We shouldn't get here otherwise */
888                             aux = (struct op_argcheck_aux*)
889                                     PerlMemShared_malloc(
890                                         sizeof(struct op_argcheck_aux));
891                             aux->params     = parser->sig_elems;
892                             aux->opt_params = parser->sig_optelems;
893                             aux->slurpy     = parser->sig_slurpy;
894                             check = newUNOP_AUX(OP_ARGCHECK, 0, NULL,
895                                             (UNOP_AUX_item *)aux);
896                             sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
897                             sigops = op_prepend_elem(OP_LINESEQ,
898                                                 newSTATEOP(0, NULL, NULL),
899                                                 sigops);
900                             /* a nextstate at the end handles context
901                              * correctly for an empty sub body */
902                             sigops = op_append_elem(OP_LINESEQ,
903                                                 sigops,
904                                                 newSTATEOP(0, NULL, NULL));
905                             /* wrap the list of arg ops in a NULL aux op.
906                               This serves two purposes. First, it makes
907                               the arg list a separate subtree from the
908                               body of the sub, and secondly the null op
909                               may in future be upgraded to an OP_SIGNATURE
910                               when implemented. For now leave it as
911                               ex-argcheck */
912                             $$ = newUNOP_AUX(OP_ARGCHECK, 0, sigops, NULL);
913                             op_null($$);
914
915                             CvSIGNATURE_on(PL_compcv);
916
917                             parser->in_my = 0;
918                             /* tell the toker that attrributes can follow
919                              * this sig, but only so that the toker
920                              * can skip through any (illegal) trailing
921                              * attribute text then give a useful error
922                              * message about "attributes before sig",
923                              * rather than falling over ina mess at
924                              * unrecognised syntax.
925                              */
926                             parser->expect = XATTRBLOCK;
927                             parser->sig_seen = TRUE;
928                             LEAVE;
929                         }
930         ;
931
932 /* Optional subroutine body (for named subroutine declaration) */
933 optsubbody
934         :       subbody
935         |       PERLY_SEMICOLON { $$ = NULL; }
936         ;
937
938
939 /* Subroutine body (without signature) */
940 subbody:        remember  PERLY_BRACE_OPEN stmtseq PERLY_BRACE_CLOSE
941                         {
942                           if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
943                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
944                           $$ = block_end($remember, $stmtseq);
945                         }
946         ;
947
948
949 /* optional [ Subroutine body with optional signature ] (for named
950  * subroutine declaration) */
951 optsigsubbody
952         :       sigsubbody
953         |       PERLY_SEMICOLON    { $$ = NULL; }
954         ;
955
956 /* Subroutine body with optional signature */
957 sigsubbody:     remember optsubsignature PERLY_BRACE_OPEN stmtseq PERLY_BRACE_CLOSE
958                         {
959                           if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
960                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
961                           $$ = block_end($remember,
962                                 op_append_list(OP_LINESEQ, $optsubsignature, $stmtseq));
963                         }
964         ;
965
966
967 /* Ordinary expressions; logical combinations */
968 expr    :       expr[lhs] ANDOP expr[rhs]
969                         { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
970         |       expr[lhs] OROP[operator] expr[rhs]
971                         { $$ = newLOGOP($operator, 0, $lhs, $rhs); }
972         |       listexpr %prec PREC_LOW
973         ;
974
975 /* Expressions are a list of terms joined by commas */
976 listexpr:       listexpr[list] PERLY_COMMA
977                         { $$ = $list; }
978         |       listexpr[list] PERLY_COMMA term
979                         {
980                           OP* term = $term;
981                           $$ = op_append_elem(OP_LIST, $list, term);
982                         }
983         |       term %prec PREC_LOW
984         ;
985
986 /* List operators */
987 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
988                         { $$ = op_convert_list($LSTOP, OPf_STACKED,
989                                 op_prepend_elem(OP_LIST, newGVREF($LSTOP,$indirob), $listexpr) );
990                         }
991         |       FUNC PERLY_PAREN_OPEN indirob expr PERLY_PAREN_CLOSE      /* print ($fh @args */
992                         { $$ = op_convert_list($FUNC, OPf_STACKED,
993                                 op_prepend_elem(OP_LIST, newGVREF($FUNC,$indirob), $expr) );
994                         }
995         |       term ARROW methodname PERLY_PAREN_OPEN optexpr PERLY_PAREN_CLOSE /* $foo->bar(list) */
996                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
997                                 op_append_elem(OP_LIST,
998                                     op_prepend_elem(OP_LIST, scalar($term), $optexpr),
999                                     newMETHOP(OP_METHOD, 0, $methodname)));
1000                         }
1001         |       term ARROW methodname                     /* $foo->bar */
1002                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
1003                                 op_append_elem(OP_LIST, scalar($term),
1004                                     newMETHOP(OP_METHOD, 0, $methodname)));
1005                         }
1006         |       METHCALL0 indirob optlistexpr           /* new Class @args */
1007                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
1008                                 op_append_elem(OP_LIST,
1009                                     op_prepend_elem(OP_LIST, $indirob, $optlistexpr),
1010                                     newMETHOP(OP_METHOD, 0, $METHCALL0)));
1011                         }
1012         |       METHCALL indirob PERLY_PAREN_OPEN optexpr PERLY_PAREN_CLOSE    /* method $object (@args) */
1013                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
1014                                 op_append_elem(OP_LIST,
1015                                     op_prepend_elem(OP_LIST, $indirob, $optexpr),
1016                                     newMETHOP(OP_METHOD, 0, $METHCALL)));
1017                         }
1018         |       LSTOP optlistexpr                    /* print @args */
1019                         { $$ = op_convert_list($LSTOP, 0, $optlistexpr); }
1020         |       FUNC PERLY_PAREN_OPEN optexpr PERLY_PAREN_CLOSE                 /* print (@args) */
1021                         { $$ = op_convert_list($FUNC, 0, $optexpr); }
1022         |       FUNC SUBLEXSTART optexpr SUBLEXEND          /* uc($arg) from "\U..." */
1023                         { $$ = op_convert_list($FUNC, 0, $optexpr); }
1024         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
1025                         { SvREFCNT_inc_simple_void(PL_compcv);
1026                           $<opval>$ = newANONATTRSUB($startanonsub, 0, NULL, $block); }[anonattrsub]
1027                     optlistexpr         %prec LSTOP  /* ... @bar */
1028                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1029                                  op_append_elem(OP_LIST,
1030                                    op_prepend_elem(OP_LIST, $<opval>anonattrsub, $optlistexpr), $LSTOPSUB));
1031                         }
1032         ;
1033
1034 /* Names of methods. May use $object->$methodname */
1035 methodname:     METHCALL0
1036         |       scalar
1037         ;
1038
1039 /* Some kind of subscripted expression */
1040 subscripted:    gelem PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE        /* *main::{something} */
1041                         /* In this and all the hash accessors, PERLY_SEMICOLON is
1042                          * provided by the tokeniser */
1043                         { $$ = newBINOP(OP_GELEM, 0, $gelem, scalar($expr)); }
1044         |       scalar[array] PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE          /* $array[$element] */
1045                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($array), scalar($expr));
1046                         }
1047         |       term[array_reference] ARROW PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE      /* somearef->[$element] */
1048                         { $$ = newBINOP(OP_AELEM, 0,
1049                                         ref(newAVREF($array_reference),OP_RV2AV),
1050                                         scalar($expr));
1051                         }
1052         |       subscripted[array_reference] PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE    /* $foo->[$bar]->[$baz] */
1053                         { $$ = newBINOP(OP_AELEM, 0,
1054                                         ref(newAVREF($array_reference),OP_RV2AV),
1055                                         scalar($expr));
1056                         }
1057         |       scalar[hash] PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE    /* $foo{bar();} */
1058                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($hash), jmaybe($expr));
1059                         }
1060         |       term[hash_reference] ARROW PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE /* somehref->{bar();} */
1061                         { $$ = newBINOP(OP_HELEM, 0,
1062                                         ref(newHVREF($hash_reference),OP_RV2HV),
1063                                         jmaybe($expr)); }
1064         |       subscripted[hash_reference] PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE /* $foo->[bar]->{baz;} */
1065                         { $$ = newBINOP(OP_HELEM, 0,
1066                                         ref(newHVREF($hash_reference),OP_RV2HV),
1067                                         jmaybe($expr)); }
1068         |       term[code_reference] ARROW PERLY_PAREN_OPEN PERLY_PAREN_CLOSE          /* $subref->() */
1069                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1070                                    newCVREF(0, scalar($code_reference)));
1071                           if (parser->expect == XBLOCK)
1072                               parser->expect = XOPERATOR;
1073                         }
1074         |       term[code_reference] ARROW PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE     /* $subref->(@args) */
1075                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1076                                    op_append_elem(OP_LIST, $expr,
1077                                        newCVREF(0, scalar($code_reference))));
1078                           if (parser->expect == XBLOCK)
1079                               parser->expect = XOPERATOR;
1080                         }
1081
1082         |       subscripted[code_reference] PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE   /* $foo->{bar}->(@args) */
1083                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1084                                    op_append_elem(OP_LIST, $expr,
1085                                                newCVREF(0, scalar($code_reference))));
1086                           if (parser->expect == XBLOCK)
1087                               parser->expect = XOPERATOR;
1088                         }
1089         |       subscripted[code_reference] PERLY_PAREN_OPEN PERLY_PAREN_CLOSE        /* $foo->{bar}->() */
1090                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1091                                    newCVREF(0, scalar($code_reference)));
1092                           if (parser->expect == XBLOCK)
1093                               parser->expect = XOPERATOR;
1094                         }
1095         |       PERLY_PAREN_OPEN expr[list] PERLY_PAREN_CLOSE PERLY_BRACKET_OPEN expr[slice] PERLY_BRACKET_CLOSE            /* list slice */
1096                         { $$ = newSLICEOP(0, $slice, $list); }
1097         |       QWLIST PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE            /* list literal slice */
1098                         { $$ = newSLICEOP(0, $expr, $QWLIST); }
1099         |       PERLY_PAREN_OPEN PERLY_PAREN_CLOSE PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE                 /* empty list slice! */
1100                         { $$ = newSLICEOP(0, $expr, NULL); }
1101     ;
1102
1103 /* Binary operators between terms */
1104 termbinop:      term[lhs] ASSIGNOP term[rhs]                     /* $x = $y, $x += $y */
1105                         { $$ = newASSIGNOP(OPf_STACKED, $lhs, $ASSIGNOP, $rhs); }
1106         |       term[lhs] POWOP term[rhs]                        /* $x ** $y */
1107                         { $$ = newBINOP($POWOP, 0, scalar($lhs), scalar($rhs)); }
1108         |       term[lhs] MULOP term[rhs]                        /* $x * $y, $x x $y */
1109                         {   if ($MULOP != OP_REPEAT)
1110                                 scalar($lhs);
1111                             $$ = newBINOP($MULOP, 0, $lhs, scalar($rhs));
1112                         }
1113         |       term[lhs] ADDOP term[rhs]                        /* $x + $y */
1114                         { $$ = newBINOP($ADDOP, 0, scalar($lhs), scalar($rhs)); }
1115         |       term[lhs] SHIFTOP term[rhs]                      /* $x >> $y, $x << $y */
1116                         { $$ = newBINOP($SHIFTOP, 0, scalar($lhs), scalar($rhs)); }
1117         |       termrelop %prec PREC_LOW               /* $x > $y, etc. */
1118                         { $$ = $termrelop; }
1119         |       termeqop %prec PREC_LOW                /* $x == $y, $x cmp $y */
1120                         { $$ = $termeqop; }
1121         |       term[lhs] BITANDOP term[rhs]                     /* $x & $y */
1122                         { $$ = newBINOP($BITANDOP, 0, scalar($lhs), scalar($rhs)); }
1123         |       term[lhs] BITOROP term[rhs]                      /* $x | $y */
1124                         { $$ = newBINOP($BITOROP, 0, scalar($lhs), scalar($rhs)); }
1125         |       term[lhs] DOTDOT term[rhs]                       /* $x..$y, $x...$y */
1126                         { $$ = newRANGE($DOTDOT, scalar($lhs), scalar($rhs)); }
1127         |       term[lhs] ANDAND term[rhs]                       /* $x && $y */
1128                         { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
1129         |       term[lhs] OROR term[rhs]                         /* $x || $y */
1130                         { $$ = newLOGOP(OP_OR, 0, $lhs, $rhs); }
1131         |       term[lhs] DORDOR term[rhs]                       /* $x // $y */
1132                         { $$ = newLOGOP(OP_DOR, 0, $lhs, $rhs); }
1133         |       term[lhs] MATCHOP term[rhs]                      /* $x =~ /$y/ */
1134                         { $$ = bind_match($MATCHOP, $lhs, $rhs); }
1135     ;
1136
1137 termrelop:      relopchain %prec PREC_LOW
1138                         { $$ = cmpchain_finish($relopchain); }
1139         |       term[lhs] NCRELOP term[rhs]
1140                         { $$ = newBINOP($NCRELOP, 0, scalar($lhs), scalar($rhs)); }
1141         |       termrelop NCRELOP
1142                         { yyerror("syntax error"); YYERROR; }
1143         |       termrelop CHRELOP
1144                         { yyerror("syntax error"); YYERROR; }
1145         ;
1146
1147 relopchain:     term[lhs] CHRELOP term[rhs]
1148                         { $$ = cmpchain_start($CHRELOP, $lhs, $rhs); }
1149         |       relopchain[lhs] CHRELOP term[rhs]
1150                         { $$ = cmpchain_extend($CHRELOP, $lhs, $rhs); }
1151         ;
1152
1153 termeqop:       eqopchain %prec PREC_LOW
1154                         { $$ = cmpchain_finish($eqopchain); }
1155         |       term[lhs] NCEQOP term[rhs]
1156                         { $$ = newBINOP($NCEQOP, 0, scalar($lhs), scalar($rhs)); }
1157         |       termeqop NCEQOP
1158                         { yyerror("syntax error"); YYERROR; }
1159         |       termeqop CHEQOP
1160                         { yyerror("syntax error"); YYERROR; }
1161         ;
1162
1163 eqopchain:      term[lhs] CHEQOP term[rhs]
1164                         { $$ = cmpchain_start($CHEQOP, $lhs, $rhs); }
1165         |       eqopchain[lhs] CHEQOP term[rhs]
1166                         { $$ = cmpchain_extend($CHEQOP, $lhs, $rhs); }
1167         ;
1168
1169 /* Unary operators and terms */
1170 termunop : PERLY_MINUS term %prec UMINUS                       /* -$x */
1171                         { $$ = newUNOP(OP_NEGATE, 0, scalar($term)); }
1172         |       PERLY_PLUS term %prec UMINUS                  /* +$x */
1173                         { $$ = $term; }
1174
1175         |       PERLY_EXCLAMATION_MARK term                               /* !$x */
1176                         { $$ = newUNOP(OP_NOT, 0, scalar($term)); }
1177         |       PERLY_TILDE term                               /* ~$x */
1178                         { $$ = newUNOP($PERLY_TILDE, 0, scalar($term)); }
1179         |       term POSTINC                           /* $x++ */
1180                         { $$ = newUNOP(OP_POSTINC, 0,
1181                                         op_lvalue(scalar($term), OP_POSTINC)); }
1182         |       term POSTDEC                           /* $x-- */
1183                         { $$ = newUNOP(OP_POSTDEC, 0,
1184                                         op_lvalue(scalar($term), OP_POSTDEC));}
1185         |       term POSTJOIN    /* implicit join after interpolated ->@ */
1186                         { $$ = op_convert_list(OP_JOIN, 0,
1187                                        op_append_elem(
1188                                         OP_LIST,
1189                                         newSVREF(scalar(
1190                                             newSVOP(OP_CONST,0,
1191                                                     newSVpvs("\""))
1192                                         )),
1193                                         $term
1194                                        ));
1195                         }
1196         |       PREINC term                            /* ++$x */
1197                         { $$ = newUNOP(OP_PREINC, 0,
1198                                         op_lvalue(scalar($term), OP_PREINC)); }
1199         |       PREDEC term                            /* --$x */
1200                         { $$ = newUNOP(OP_PREDEC, 0,
1201                                         op_lvalue(scalar($term), OP_PREDEC)); }
1202
1203     ;
1204
1205 /* Constructors for anonymous data */
1206 anonymous
1207         :       PERLY_BRACKET_OPEN optexpr PERLY_BRACKET_CLOSE
1208                         { $$ = newANONLIST($optexpr); }
1209         |       HASHBRACK optexpr PERLY_SEMICOLON PERLY_BRACE_CLOSE     %prec PERLY_PAREN_OPEN /* { foo => "Bar" } */
1210                         { $$ = newANONHASH($optexpr); }
1211         |       KW_SUB_anon     startanonsub proto subattrlist subbody    %prec PERLY_PAREN_OPEN
1212                         { SvREFCNT_inc_simple_void(PL_compcv);
1213                           $$ = newANONATTRSUB($startanonsub, $proto, $subattrlist, $subbody); }
1214         |       KW_SUB_anon_sig startanonsub subattrlist sigsubbody %prec PERLY_PAREN_OPEN
1215                         { SvREFCNT_inc_simple_void(PL_compcv);
1216                           $$ = newANONATTRSUB($startanonsub, NULL, $subattrlist, $sigsubbody); }
1217     ;
1218
1219 /* Things called with "do" */
1220 termdo  :       KW_DO term      %prec UNIOP                     /* do $filename */
1221                         { $$ = dofile($term, $KW_DO);}
1222         |       KW_DO block     %prec PERLY_PAREN_OPEN               /* do { code */
1223                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($block));}
1224         ;
1225
1226 term[product]   :       termbinop
1227         |       termunop
1228         |       anonymous
1229         |       termdo
1230         |       term[condition] PERLY_QUESTION_MARK term[then] PERLY_COLON term[else]
1231                         { $$ = newCONDOP(0, $condition, $then, $else); }
1232         |       REFGEN term[operand]                          /* \$x, \@y, \%z */
1233                         { $$ = newUNOP(OP_REFGEN, 0, $operand); }
1234         |       myattrterm      %prec UNIOP
1235                         { $$ = $myattrterm; }
1236         |       KW_LOCAL term[operand]  %prec UNIOP
1237                         { $$ = localize($operand,0); }
1238         |       PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE
1239                         { $$ = sawparens($expr); }
1240         |       QWLIST
1241                         { $$ = $QWLIST; }
1242         |       PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1243                         { $$ = sawparens(newNULLLIST()); }
1244         |       scalar  %prec PERLY_PAREN_OPEN
1245                         { $$ = $scalar; }
1246         |       star    %prec PERLY_PAREN_OPEN
1247                         { $$ = $star; }
1248         |       hsh     %prec PERLY_PAREN_OPEN
1249                         { $$ = $hsh; }
1250         |       ary     %prec PERLY_PAREN_OPEN
1251                         { $$ = $ary; }
1252         |       arylen  %prec PERLY_PAREN_OPEN                    /* $#x, $#{ something } */
1253                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($arylen, OP_AV2ARYLEN));}
1254         |       subscripted
1255                         { $$ = $subscripted; }
1256         |       sliceme PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE                     /* array slice */
1257                         { $$ = op_prepend_elem(OP_ASLICE,
1258                                 newOP(OP_PUSHMARK, 0),
1259                                     newLISTOP(OP_ASLICE, 0,
1260                                         list($expr),
1261                                         ref($sliceme, OP_ASLICE)));
1262                           if ($$ && $sliceme)
1263                               $$->op_private |=
1264                                   $sliceme->op_private & OPpSLICEWARNING;
1265                         }
1266         |       kvslice PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE                 /* array key/value slice */
1267                         { $$ = op_prepend_elem(OP_KVASLICE,
1268                                 newOP(OP_PUSHMARK, 0),
1269                                     newLISTOP(OP_KVASLICE, 0,
1270                                         list($expr),
1271                                         ref(oopsAV($kvslice), OP_KVASLICE)));
1272                           if ($$ && $kvslice)
1273                               $$->op_private |=
1274                                   $kvslice->op_private & OPpSLICEWARNING;
1275                         }
1276         |       sliceme PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE                 /* @hash{@keys} */
1277                         { $$ = op_prepend_elem(OP_HSLICE,
1278                                 newOP(OP_PUSHMARK, 0),
1279                                     newLISTOP(OP_HSLICE, 0,
1280                                         list($expr),
1281                                         ref(oopsHV($sliceme), OP_HSLICE)));
1282                           if ($$ && $sliceme)
1283                               $$->op_private |=
1284                                   $sliceme->op_private & OPpSLICEWARNING;
1285                         }
1286         |       kvslice PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE                 /* %hash{@keys} */
1287                         { $$ = op_prepend_elem(OP_KVHSLICE,
1288                                 newOP(OP_PUSHMARK, 0),
1289                                     newLISTOP(OP_KVHSLICE, 0,
1290                                         list($expr),
1291                                         ref($kvslice, OP_KVHSLICE)));
1292                           if ($$ && $kvslice)
1293                               $$->op_private |=
1294                                   $kvslice->op_private & OPpSLICEWARNING;
1295                         }
1296         |       THING   %prec PERLY_PAREN_OPEN
1297                         { $$ = $THING; }
1298         |       amper                                /* &foo; */
1299                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($amper)); }
1300         |       amper PERLY_PAREN_OPEN PERLY_PAREN_CLOSE                 /* &foo() or foo() */
1301                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($amper));
1302                         }
1303         |       amper PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE          /* &foo(@args) or foo(@args) */
1304                         {
1305                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1306                                 op_append_elem(OP_LIST, $expr, scalar($amper)));
1307                         }
1308         |       NOAMP subname optlistexpr       /* foo @args (no parens) */
1309                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1310                             op_append_elem(OP_LIST, $optlistexpr, scalar($subname)));
1311                         }
1312         |       term[operand] ARROW PERLY_DOLLAR PERLY_STAR
1313                         { $$ = newSVREF($operand); }
1314         |       term[operand] ARROW PERLY_SNAIL PERLY_STAR
1315                         { $$ = newAVREF($operand); }
1316         |       term[operand] ARROW PERLY_PERCENT_SIGN PERLY_STAR
1317                         { $$ = newHVREF($operand); }
1318         |       term[operand] ARROW PERLY_AMPERSAND PERLY_STAR
1319                         { $$ = newUNOP(OP_ENTERSUB, 0,
1320                                        scalar(newCVREF($PERLY_AMPERSAND,$operand))); }
1321         |       term[operand] ARROW PERLY_STAR PERLY_STAR       %prec PERLY_PAREN_OPEN
1322                         { $$ = newGVREF(0,$operand); }
1323         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1324                         { $$ = newOP($LOOPEX, OPf_SPECIAL);
1325                             PL_hints |= HINT_BLOCK_SCOPE; }
1326         |       LOOPEX term[operand]
1327                         { $$ = newLOOPEX($LOOPEX,$operand); }
1328         |       NOTOP listexpr                       /* not $foo */
1329                         { $$ = newUNOP(OP_NOT, 0, scalar($listexpr)); }
1330         |       UNIOP                                /* Unary op, $_ implied */
1331                         { $$ = newOP($UNIOP, 0); }
1332         |       UNIOP block                          /* eval { foo }* */
1333                         { $$ = newUNOP($UNIOP, 0, $block); }
1334         |       UNIOP term[operand]                           /* Unary op */
1335                         { $$ = newUNOP($UNIOP, 0, $operand); }
1336         |       KW_REQUIRE                              /* require, $_ implied */
1337                         { $$ = newOP(OP_REQUIRE, $KW_REQUIRE ? OPf_SPECIAL : 0); }
1338         |       KW_REQUIRE term[operand]                         /* require Foo */
1339                         { $$ = newUNOP(OP_REQUIRE, $KW_REQUIRE ? OPf_SPECIAL : 0, $operand); }
1340         |       UNIOPSUB
1341                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($UNIOPSUB)); }
1342         |       UNIOPSUB term[operand]                        /* Sub treated as unop */
1343                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1344                             op_append_elem(OP_LIST, $operand, scalar($UNIOPSUB))); }
1345         |       FUNC0                                /* Nullary operator */
1346                         { $$ = newOP($FUNC0, 0); }
1347         |       FUNC0 PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1348                         { $$ = newOP($FUNC0, 0);}
1349         |       FUNC0OP       /* Same as above, but op created in toke.c */
1350                         { $$ = $FUNC0OP; }
1351         |       FUNC0OP PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1352                         { $$ = $FUNC0OP; }
1353         |       FUNC0SUB                             /* Sub treated as nullop */
1354                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($FUNC0SUB)); }
1355         |       FUNC1 PERLY_PAREN_OPEN PERLY_PAREN_CLOSE                        /* not () */
1356                         { $$ = ($FUNC1 == OP_NOT)
1357                           ? newUNOP($FUNC1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1358                           : newOP($FUNC1, OPf_SPECIAL); }
1359         |       FUNC1 PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE                   /* not($foo) */
1360                         { $$ = newUNOP($FUNC1, 0, $expr); }
1361         |       PMFUNC /* m//, s///, qr//, tr/// */
1362                         {
1363                             if (   $PMFUNC->op_type != OP_TRANS
1364                                 && $PMFUNC->op_type != OP_TRANSR
1365                                 && (((PMOP*)$PMFUNC)->op_pmflags & PMf_HAS_CV))
1366                             {
1367                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1368                                 SAVEFREESV(PL_compcv);
1369                             } else
1370                                 $<ival>$ = 0;
1371                         }
1372                     SUBLEXSTART listexpr optrepl SUBLEXEND
1373                         { $$ = pmruntime($PMFUNC, $listexpr, $optrepl, 1, $<ival>2); }
1374         |       BAREWORD
1375         |       listop
1376         |       PLUGEXPR
1377         ;
1378
1379 /* "my" declarations, with optional attributes */
1380 myattrterm
1381         :       KW_MY myterm myattrlist
1382                         { $$ = my_attrs($myterm,$myattrlist); }
1383         |       KW_MY myterm
1384                         { $$ = localize($myterm,1); }
1385         |       KW_MY REFGEN myterm myattrlist
1386                         { $$ = newUNOP(OP_REFGEN, 0, my_attrs($myterm,$myattrlist)); }
1387         |       KW_MY REFGEN term[operand]
1388                         { $$ = newUNOP(OP_REFGEN, 0, localize($operand,1)); }
1389         ;
1390
1391 /* Things that can be "my"'d */
1392 myterm  :       PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE
1393                         { $$ = sawparens($expr); }
1394         |       PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1395                         { $$ = sawparens(newNULLLIST()); }
1396
1397         |       scalar  %prec PERLY_PAREN_OPEN
1398                         { $$ = $scalar; }
1399         |       hsh     %prec PERLY_PAREN_OPEN
1400                         { $$ = $hsh; }
1401         |       ary     %prec PERLY_PAREN_OPEN
1402                         { $$ = $ary; }
1403         ;
1404
1405 /* Basic list expressions */
1406 optlistexpr
1407         :       empty                   %prec PREC_LOW
1408         |       listexpr                %prec PREC_LOW
1409         ;
1410
1411 optexpr
1412         :       empty
1413         |       expr
1414         ;
1415
1416 optrepl
1417         :       empty
1418         |       PERLY_SLASH expr        { $$ = $expr; }
1419         ;
1420
1421 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1422    lexical */
1423 my_scalar:      scalar
1424                         { parser->in_my = 0; $$ = my($scalar); }
1425         ;
1426
1427 /* A list of scalars for "for my ($foo, $bar) (@baz)"  */
1428 list_of_scalars:        list_of_scalars[list] PERLY_COMMA
1429                         { $$ = $list; }
1430         |               list_of_scalars[list] PERLY_COMMA scalar
1431                         {
1432                           $$ = op_append_elem(OP_LIST, $list, $scalar);
1433                         }
1434         |               scalar %prec PREC_LOW
1435         ;
1436
1437 my_list_of_scalars:     list_of_scalars
1438                         { parser->in_my = 0; $$ = $list_of_scalars; }
1439         ;
1440
1441 my_var  :       scalar
1442         |       ary
1443         |       hsh
1444         ;
1445
1446 refgen_topic:   my_var
1447         |       amper
1448         ;
1449
1450 my_refgen:      KW_MY REFGEN
1451         |       REFGEN KW_MY
1452         ;
1453
1454 amper   :       PERLY_AMPERSAND indirob
1455                         { $$ = newCVREF($PERLY_AMPERSAND,$indirob); }
1456         ;
1457
1458 scalar  :       PERLY_DOLLAR indirob
1459                         { $$ = newSVREF($indirob); }
1460         ;
1461
1462 ary     :       PERLY_SNAIL indirob
1463                         { $$ = newAVREF($indirob);
1464                           if ($$) $$->op_private |= $PERLY_SNAIL;
1465                         }
1466         ;
1467
1468 hsh     :       PERLY_PERCENT_SIGN indirob
1469                         { $$ = newHVREF($indirob);
1470                           if ($$) $$->op_private |= $PERLY_PERCENT_SIGN;
1471                         }
1472         ;
1473
1474 arylen  :       DOLSHARP indirob
1475                         { $$ = newAVREF($indirob); }
1476         |       term ARROW DOLSHARP PERLY_STAR
1477                         { $$ = newAVREF($term); }
1478         ;
1479
1480 star    :       PERLY_STAR indirob
1481                         { $$ = newGVREF(0,$indirob); }
1482         ;
1483
1484 sliceme :       ary
1485         |       term ARROW PERLY_SNAIL
1486                         { $$ = newAVREF($term); }
1487         ;
1488
1489 kvslice :       hsh
1490         |       term ARROW PERLY_PERCENT_SIGN
1491                         { $$ = newHVREF($term); }
1492         ;
1493
1494 gelem   :       star
1495         |       term ARROW PERLY_STAR
1496                         { $$ = newGVREF(0,$term); }
1497         ;
1498
1499 /* Indirect objects */
1500 indirob :       BAREWORD
1501                         { $$ = scalar($BAREWORD); }
1502         |       scalar %prec PREC_LOW
1503                         { $$ = scalar($scalar); }
1504         |       block
1505                         { $$ = op_scope($block); }
1506
1507         |       PRIVATEREF
1508                         { $$ = $PRIVATEREF; }
1509         ;