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