This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #25824] Segmentation fault with
[perl5.git] / perly.y
1 /*    perly.y
2  *
3  *    Copyright (c) 1991-2002, 2003, 2004 Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 /*
11  * 'I see,' laughed Strider.  'I look foul and feel fair.  Is that it?
12  * All that is gold does not glitter, not all those who wander are lost.'
13  */
14
15 /*  Make the parser re-entrant. */
16
17 %pure_parser
18
19 %start prog
20
21 %union {
22     I32 ival;
23     char *pval;
24     OP *opval;
25     GV *gvval;
26 }
27
28 %token <ival> '{'
29
30 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
31 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
32 %token <pval> LABEL
33 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
34 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
35 %token <ival> LOOPEX DOTDOT
36 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
37 %token <ival> RELOP EQOP MULOP ADDOP
38 %token <ival> DOLSHARP DO HASHBRACK NOAMP
39 %token <ival> LOCAL MY MYSUB
40 %token COLONATTR
41
42 %type <ival> prog decl format startsub startanonsub startformsub
43 %type <ival> progstart remember mremember '&' savescope
44 %type <opval> block mblock lineseq line loop cond else
45 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
46 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
47 %type <opval> listexpr listexprcom indirob listop method
48 %type <opval> formname subname proto subbody cont my_scalar
49 %type <opval> subattrlist myattrlist mysubrout myattrterm myterm
50 %type <opval> termbinop termunop anonymous termdo
51 %type <pval> label
52
53 %nonassoc PREC_LOW
54 %nonassoc LOOPEX
55
56 %left <ival> OROP DOROP
57 %left ANDOP
58 %right NOTOP
59 %nonassoc LSTOP LSTOPSUB
60 %left ','
61 %right <ival> ASSIGNOP
62 %right '?' ':'
63 %nonassoc DOTDOT
64 %left OROR DORDOR
65 %left ANDAND
66 %left <ival> BITOROP
67 %left <ival> BITANDOP
68 %nonassoc EQOP
69 %nonassoc RELOP
70 %nonassoc UNIOP UNIOPSUB
71 %left <ival> SHIFTOP
72 %left ADDOP
73 %left MULOP
74 %left <ival> MATCHOP
75 %right '!' '~' UMINUS REFGEN
76 %right <ival> POWOP
77 %nonassoc PREINC PREDEC POSTINC POSTDEC
78 %left ARROW
79 %nonassoc <ival> ')'
80 %left '('
81 %left '[' '{'
82
83 %% /* RULES */
84
85 /* The whole program */
86 prog    :       progstart
87         /*CONTINUED*/   lineseq
88                         { $$ = $1; newPROG(block_end($1,$2)); }
89         ;
90
91 /* An ordinary block */
92 block   :       '{' remember lineseq '}'
93                         { if (PL_copline > (line_t)$1)
94                               PL_copline = (line_t)$1;
95                           $$ = block_end($2, $3); }
96         ;
97
98 remember:       /* NULL */      /* start a full lexical scope */
99                         { $$ = block_start(TRUE); }
100         ;
101
102 progstart:
103                 {
104                     PL_expect = XSTATE; $$ = block_start(TRUE);
105                 }
106         ;
107
108
109 mblock  :       '{' mremember lineseq '}'
110                         { if (PL_copline > (line_t)$1)
111                               PL_copline = (line_t)$1;
112                           $$ = block_end($2, $3); }
113         ;
114
115 mremember:      /* NULL */      /* start a partial lexical scope */
116                         { $$ = block_start(FALSE); }
117         ;
118
119 savescope:      /* NULL */      /* remember stack pos in case of error */
120                 { $$ = PL_savestack_ix; }
121
122 /* A collection of "lines" in the program */
123 lineseq :       /* NULL */
124                         { $$ = Nullop; }
125         |       lineseq decl
126                         { $$ = $1; }
127         |       lineseq savescope line
128                         {   LEAVE_SCOPE($2);
129                             $$ = append_list(OP_LINESEQ,
130                                 (LISTOP*)$1, (LISTOP*)$3);
131                             PL_pad_reset_pending = TRUE;
132                             if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
133         ;
134
135 /* A "line" in the program */
136 line    :       label cond
137                         { $$ = newSTATEOP(0, $1, $2); }
138         |       loop    /* loops add their own labels */
139         |       label ';'
140                         { if ($1 != Nullch) {
141                               $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
142                             }
143                             else {
144                               $$ = Nullop;
145                               PL_copline = NOLINE;
146                             }
147                             PL_expect = XSTATE; }
148         |       label sideff ';'
149                         { $$ = newSTATEOP(0, $1, $2);
150                           PL_expect = XSTATE; }
151         ;
152
153 /* An expression which may have a side-effect */
154 sideff  :       error
155                         { $$ = Nullop; }
156         |       expr
157                         { $$ = $1; }
158         |       expr IF expr
159                         { $$ = newLOGOP(OP_AND, 0, $3, $1); }
160         |       expr UNLESS expr
161                         { $$ = newLOGOP(OP_OR, 0, $3, $1); }
162         |       expr WHILE expr
163                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
164         |       expr UNTIL iexpr
165                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
166         |       expr FOR expr
167                         { $$ = newFOROP(0, Nullch, (line_t)$2,
168                                         Nullop, $3, $1, Nullop); }
169         ;
170
171 /* else and elsif blocks */
172 else    :       /* NULL */
173                         { $$ = Nullop; }
174         |       ELSE mblock
175                         { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
176         |       ELSIF '(' mexpr ')' mblock else
177                         { PL_copline = (line_t)$1;
178                             $$ = newCONDOP(0, $3, scope($5), $6);
179                             PL_hints |= HINT_BLOCK_SCOPE; }
180         ;
181
182 /* Real conditional expressions */
183 cond    :       IF '(' remember mexpr ')' mblock else
184                         { PL_copline = (line_t)$1;
185                             $$ = block_end($3,
186                                    newCONDOP(0, $4, scope($6), $7)); }
187         |       UNLESS '(' remember miexpr ')' mblock else
188                         { PL_copline = (line_t)$1;
189                             $$ = block_end($3,
190                                    newCONDOP(0, $4, scope($6), $7)); }
191         ;
192
193 /* Continue blocks */
194 cont    :       /* NULL */
195                         { $$ = Nullop; }
196         |       CONTINUE block
197                         { $$ = scope($2); }
198         ;
199
200 /* Loops: while, until, for, and a bare block */
201 loop    :       label WHILE '(' remember mtexpr ')' mblock cont
202                         { PL_copline = (line_t)$2;
203                             $$ = block_end($4,
204                                    newSTATEOP(0, $1,
205                                      newWHILEOP(0, 1, (LOOP*)Nullop,
206                                                 $2, $5, $7, $8))); }
207         |       label UNTIL '(' remember miexpr ')' mblock cont
208                         { PL_copline = (line_t)$2;
209                             $$ = block_end($4,
210                                    newSTATEOP(0, $1,
211                                      newWHILEOP(0, 1, (LOOP*)Nullop,
212                                                 $2, $5, $7, $8))); }
213         |       label FOR MY remember my_scalar '(' mexpr ')' mblock cont
214                         { $$ = block_end($4,
215                                  newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); }
216         |       label FOR scalar '(' remember mexpr ')' mblock cont
217                         { $$ = block_end($5,
218                                  newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP),
219                                           $6, $8, $9)); }
220         |       label FOR '(' remember mexpr ')' mblock cont
221                         { $$ = block_end($4,
222                                  newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); }
223         |       label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
224                         /* basically fake up an initialize-while lineseq */
225                         { OP *forop;
226                           PL_copline = (line_t)$2;
227                           forop = newSTATEOP(0, $1,
228                                             newWHILEOP(0, 1, (LOOP*)Nullop,
229                                                 $2, scalar($7),
230                                                 $11, $9));
231                           if ($5) {
232                                 forop = append_elem(OP_LINESEQ,
233                                         newSTATEOP(0, ($1?savepv($1):Nullch),
234                                                    $5),
235                                         forop);
236                           }
237
238                           $$ = block_end($4, forop); }
239         |       label block cont  /* a block is a loop that happens once */
240                         { $$ = newSTATEOP(0, $1,
241                                  newWHILEOP(0, 1, (LOOP*)Nullop,
242                                             NOLINE, Nullop, $2, $3)); }
243         ;
244
245 /* Normal expression */
246 nexpr   :       /* NULL */
247                         { $$ = Nullop; }
248         |       sideff
249         ;
250
251 /* Boolean expression */
252 texpr   :       /* NULL means true */
253                         { (void)scan_num("1", &yylval); $$ = yylval.opval; }
254         |       expr
255         ;
256
257 /* Inverted boolean expression */
258 iexpr   :       expr
259                         { $$ = invert(scalar($1)); }
260         ;
261
262 /* Expression with its own lexical scope */
263 mexpr   :       expr
264                         { $$ = $1; intro_my(); }
265         ;
266
267 mnexpr  :       nexpr
268                         { $$ = $1; intro_my(); }
269         ;
270
271 mtexpr  :       texpr
272                         { $$ = $1; intro_my(); }
273         ;
274
275 miexpr  :       iexpr
276                         { $$ = $1; intro_my(); }
277         ;
278
279 /* Optional "MAIN:"-style loop labels */
280 label   :       /* empty */
281                         { $$ = Nullch; }
282         |       LABEL
283         ;
284
285 /* Some kind of declaration - does not take part in the parse tree */
286 decl    :       format
287                         { $$ = 0; }
288         |       subrout
289                         { $$ = 0; }
290         |       mysubrout
291                         { $$ = 0; }
292         |       package
293                         { $$ = 0; }
294         |       use
295                         { $$ = 0; }
296         ;
297
298 format  :       FORMAT startformsub formname block
299                         { newFORM($2, $3, $4); }
300         ;
301
302 formname:       WORD            { $$ = $1; }
303         |       /* NULL */      { $$ = Nullop; }
304         ;
305
306 /* Unimplemented "my sub foo { }" */
307 mysubrout:      MYSUB startsub subname proto subattrlist subbody
308                         { newMYSUB($2, $3, $4, $5, $6); }
309         ;
310
311 /* Subroutine definition */
312 subrout :       SUB startsub subname proto subattrlist subbody
313                         { newATTRSUB($2, $3, $4, $5, $6); }
314         ;
315
316 startsub:       /* NULL */      /* start a regular subroutine scope */
317                         { $$ = start_subparse(FALSE, 0); }
318         ;
319
320 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
321                         { $$ = start_subparse(FALSE, CVf_ANON); }
322         ;
323
324 startformsub:   /* NULL */      /* start a format subroutine scope */
325                         { $$ = start_subparse(TRUE, 0); }
326         ;
327
328 /* Name of a subroutine - must be a bareword, could be special */
329 subname :       WORD    { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
330                           if (strEQ(name, "BEGIN") || strEQ(name, "END")
331                               || strEQ(name, "INIT") || strEQ(name, "CHECK"))
332                               CvSPECIAL_on(PL_compcv);
333                           $$ = $1; }
334         ;
335
336 /* Subroutine prototype */
337 proto   :       /* NULL */
338                         { $$ = Nullop; }
339         |       THING
340         ;
341
342 /* Optional list of subroutine attributes */
343 subattrlist:    /* NULL */
344                         { $$ = Nullop; }
345         |       COLONATTR THING
346                         { $$ = $2; }
347         |       COLONATTR
348                         { $$ = Nullop; }
349         ;
350
351 /* List of attributes for a "my" variable declaration */
352 myattrlist:     COLONATTR THING
353                         { $$ = $2; }
354         |       COLONATTR
355                         { $$ = Nullop; }
356         ;
357
358 /* Subroutine body - either null or a block */
359 subbody :       block   { $$ = $1; }
360         |       ';'     { $$ = Nullop; PL_expect = XSTATE; }
361         ;
362
363 package :       PACKAGE WORD ';'
364                         { package($2); }
365         ;
366
367 use     :       USE startsub
368                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
369                     WORD WORD listexpr ';'
370                         { utilize($1, $2, $4, $5, $6); }
371         ;
372
373 /* Ordinary expressions; logical combinations */
374 expr    :       expr ANDOP expr
375                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
376         |       expr OROP expr
377                         { $$ = newLOGOP($2, 0, $1, $3); }
378         |       expr DOROP expr
379                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
380         |       argexpr %prec PREC_LOW
381         ;
382
383 /* Expressions are a list of terms joined by commas */
384 argexpr :       argexpr ','
385                         { $$ = $1; }
386         |       argexpr ',' term
387                         { $$ = append_elem(OP_LIST, $1, $3); }
388         |       term %prec PREC_LOW
389         ;
390
391 /* List operators */
392 listop  :       LSTOP indirob argexpr          /* print $fh @args */
393                         { $$ = convert($1, OPf_STACKED,
394                                 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
395         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
396                         { $$ = convert($1, OPf_STACKED,
397                                 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
398         |       term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
399                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
400                                 append_elem(OP_LIST,
401                                     prepend_elem(OP_LIST, scalar($1), $5),
402                                     newUNOP(OP_METHOD, 0, $3))); }
403         |       term ARROW method                     /* $foo->bar */
404                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
405                                 append_elem(OP_LIST, scalar($1),
406                                     newUNOP(OP_METHOD, 0, $3))); }
407         |       METHOD indirob listexpr              /* new Class @args */
408                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
409                                 append_elem(OP_LIST,
410                                     prepend_elem(OP_LIST, $2, $3),
411                                     newUNOP(OP_METHOD, 0, $1))); }
412         |       FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
413                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
414                                 append_elem(OP_LIST,
415                                     prepend_elem(OP_LIST, $2, $4),
416                                     newUNOP(OP_METHOD, 0, $1))); }
417         |       LSTOP listexpr                       /* print @args */
418                         { $$ = convert($1, 0, $2); }
419         |       FUNC '(' listexprcom ')'             /* print (@args) */
420                         { $$ = convert($1, 0, $3); }
421         |       LSTOPSUB startanonsub block          /* map { foo } ... */
422                         { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
423                     listexpr            %prec LSTOP  /* ... @bar */
424                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
425                                  append_elem(OP_LIST,
426                                    prepend_elem(OP_LIST, $3, $5), $1)); }
427         ;
428
429 /* Names of methods. May use $object->$methodname */
430 method  :       METHOD
431         |       scalar
432         ;
433
434 /* Some kind of subscripted expression */
435 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
436                         /* In this and all the hash accessors, ';' is
437                          * provided by the tokeniser */
438                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
439                             PL_expect = XOPERATOR; }
440         |       scalar '[' expr ']'          /* $array[$element] */
441                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
442         |       term ARROW '[' expr ']'      /* somearef->[$element] */
443                         { $$ = newBINOP(OP_AELEM, 0,
444                                         ref(newAVREF($1),OP_RV2AV),
445                                         scalar($4));}
446         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
447                         { $$ = newBINOP(OP_AELEM, 0,
448                                         ref(newAVREF($1),OP_RV2AV),
449                                         scalar($3));}
450         |       scalar '{' expr ';' '}'    /* $foo->{bar();} */
451                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
452                             PL_expect = XOPERATOR; }
453         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
454                         { $$ = newBINOP(OP_HELEM, 0,
455                                         ref(newHVREF($1),OP_RV2HV),
456                                         jmaybe($4));
457                             PL_expect = XOPERATOR; }
458         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
459                         { $$ = newBINOP(OP_HELEM, 0,
460                                         ref(newHVREF($1),OP_RV2HV),
461                                         jmaybe($3));
462                             PL_expect = XOPERATOR; }
463         |       term ARROW '(' ')'          /* $subref->() */
464                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
465                                    newCVREF(0, scalar($1))); }
466         |       term ARROW '(' expr ')'     /* $subref->(@args) */
467                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
468                                    append_elem(OP_LIST, $4,
469                                        newCVREF(0, scalar($1)))); }
470
471         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
472                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
473                                    append_elem(OP_LIST, $3,
474                                                newCVREF(0, scalar($1)))); }
475         |       subscripted '(' ')'        /* $foo->{bar}->() */
476                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
477                                    newCVREF(0, scalar($1))); }
478     ;
479
480 /* Binary operators between terms */
481 termbinop       :       term ASSIGNOP term             /* $x = $y */
482                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
483         |       term POWOP term                        /* $x ** $y */
484                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
485         |       term MULOP term                        /* $x * $y, $x x $y */
486                         {   if ($2 != OP_REPEAT)
487                                 scalar($1);
488                             $$ = newBINOP($2, 0, $1, scalar($3)); }
489         |       term ADDOP term                        /* $x + $y */
490                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
491         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
492                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
493         |       term RELOP term                        /* $x > $y, etc. */
494                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
495         |       term EQOP term                         /* $x == $y, $x eq $y */
496                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
497         |       term BITANDOP term                     /* $x & $y */
498                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
499         |       term BITOROP term                      /* $x | $y */
500                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
501         |       term DOTDOT term                       /* $x..$y, $x...$y */
502                         { $$ = newRANGE($2, scalar($1), scalar($3));}
503         |       term ANDAND term                       /* $x && $y */
504                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
505         |       term OROR term                         /* $x || $y */
506                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
507         |       term DORDOR term                       /* $x // $y */
508                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
509         |       term MATCHOP term                      /* $x =~ /$y/ */
510                         { $$ = bind_match($2, $1, $3); }
511     ;
512
513 /* Unary operators and terms */
514 termunop : '-' term %prec UMINUS                       /* -$x */
515                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
516         |       '+' term %prec UMINUS                  /* +$x */
517                         { $$ = $2; }
518         |       '!' term                               /* !$x */
519                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
520         |       '~' term                               /* ~$x */
521                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
522         |       term POSTINC                           /* $x++ */
523                         { $$ = newUNOP(OP_POSTINC, 0,
524                                         mod(scalar($1), OP_POSTINC)); }
525         |       term POSTDEC                           /* $x-- */
526                         { $$ = newUNOP(OP_POSTDEC, 0,
527                                         mod(scalar($1), OP_POSTDEC)); }
528         |       PREINC term                            /* ++$x */
529                         { $$ = newUNOP(OP_PREINC, 0,
530                                         mod(scalar($2), OP_PREINC)); }
531         |       PREDEC term                            /* --$x */
532                         { $$ = newUNOP(OP_PREDEC, 0,
533                                         mod(scalar($2), OP_PREDEC)); }
534
535     ;
536
537 /* Constructors for anonymous data */
538 anonymous:      '[' expr ']'
539                         { $$ = newANONLIST($2); }
540         |       '[' ']'
541                         { $$ = newANONLIST(Nullop); }
542         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
543                         { $$ = newANONHASH($2); }
544         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
545                         { $$ = newANONHASH(Nullop); }
546         |       ANONSUB startanonsub proto subattrlist block    %prec '('
547                         { $$ = newANONATTRSUB($2, $3, $4, $5); }
548
549     ;
550
551 /* Things called with "do" */
552 termdo  :       DO term %prec UNIOP                     /* do $filename */
553                         { $$ = dofile($2); }
554         |       DO block        %prec '('               /* do { code */
555                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
556         |       DO WORD '(' ')'                         /* do somesub() */
557                         { $$ = newUNOP(OP_ENTERSUB,
558                             OPf_SPECIAL|OPf_STACKED,
559                             prepend_elem(OP_LIST,
560                                 scalar(newCVREF(
561                                     (OPpENTERSUB_AMPER<<8),
562                                     scalar($2)
563                                 )),Nullop)); dep();}
564         |       DO WORD '(' expr ')'                    /* do somesub(@args) */
565                         { $$ = newUNOP(OP_ENTERSUB,
566                             OPf_SPECIAL|OPf_STACKED,
567                             append_elem(OP_LIST,
568                                 $4,
569                                 scalar(newCVREF(
570                                     (OPpENTERSUB_AMPER<<8),
571                                     scalar($2)
572                                 )))); dep();}
573         |       DO scalar '(' ')'                      /* do $subref () */
574                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
575                             prepend_elem(OP_LIST,
576                                 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
577         |       DO scalar '(' expr ')'                 /* do $subref (@args) */
578                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
579                             prepend_elem(OP_LIST,
580                                 $4,
581                                 scalar(newCVREF(0,scalar($2))))); dep();}
582
583         ;
584
585 term    :       termbinop
586         |       termunop
587         |       anonymous
588         |       termdo
589         |       term '?' term ':' term
590                         { $$ = newCONDOP(0, $1, $3, $5); }
591         |       REFGEN term                          /* \$x, \@y, \%z */
592                         { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
593         |       myattrterm      %prec UNIOP
594                         { $$ = $1; }
595         |       LOCAL term      %prec UNIOP
596                         { $$ = localize($2,$1); }
597         |       '(' expr ')'
598                         { $$ = sawparens($2); }
599         |       '(' ')'
600                         { $$ = sawparens(newNULLLIST()); }
601         |       scalar  %prec '('
602                         { $$ = $1; }
603         |       star    %prec '('
604                         { $$ = $1; }
605         |       hsh     %prec '('
606                         { $$ = $1; }
607         |       ary     %prec '('
608                         { $$ = $1; }
609         |       arylen  %prec '('                    /* $#x, $#{ something } */
610                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
611         |       subscripted
612                         { $$ = $1; }
613         |       '(' expr ')' '[' expr ']'            /* list slice */
614                         { $$ = newSLICEOP(0, $5, $2); }
615         |       '(' ')' '[' expr ']'                 /* empty list slice! */
616                         { $$ = newSLICEOP(0, $4, Nullop); }
617         |       ary '[' expr ']'                     /* array slice */
618                         { $$ = prepend_elem(OP_ASLICE,
619                                 newOP(OP_PUSHMARK, 0),
620                                     newLISTOP(OP_ASLICE, 0,
621                                         list($3),
622                                         ref($1, OP_ASLICE))); }
623         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
624                         { $$ = prepend_elem(OP_HSLICE,
625                                 newOP(OP_PUSHMARK, 0),
626                                     newLISTOP(OP_HSLICE, 0,
627                                         list($3),
628                                         ref(oopsHV($1), OP_HSLICE)));
629                             PL_expect = XOPERATOR; }
630         |       THING   %prec '('
631                         { $$ = $1; }
632         |       amper                                /* &foo; */
633                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
634         |       amper '(' ')'                        /* &foo() */
635                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
636         |       amper '(' expr ')'                   /* &foo(@args) */
637                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
638                             append_elem(OP_LIST, $3, scalar($1))); }
639         |       NOAMP WORD listexpr                  /* foo(@args) */
640                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
641                             append_elem(OP_LIST, $3, scalar($2))); }
642         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
643                         { $$ = newOP($1, OPf_SPECIAL);
644                             PL_hints |= HINT_BLOCK_SCOPE; }
645         |       LOOPEX term
646                         { $$ = newLOOPEX($1,$2); }
647         |       NOTOP argexpr                        /* not $foo */
648                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
649         |       UNIOP                                /* Unary op, $_ implied */
650                         { $$ = newOP($1, 0); }
651         |       UNIOP block                          /* eval { foo }, I *think* */
652                         { $$ = newUNOP($1, 0, $2); }
653         |       UNIOP term                           /* Unary op */
654                         { $$ = newUNOP($1, 0, $2); }
655         |       UNIOPSUB term                        /* Sub treated as unop */
656                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
657                             append_elem(OP_LIST, $2, scalar($1))); }
658         |       FUNC0                                /* Nullary operator */
659                         { $$ = newOP($1, 0); }
660         |       FUNC0 '(' ')'
661                         { $$ = newOP($1, 0); }
662         |       FUNC0SUB                             /* Sub treated as nullop */
663                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
664                                 scalar($1)); }
665         |       FUNC1 '(' ')'                        /* not () */
666                         { $$ = newOP($1, OPf_SPECIAL); }
667         |       FUNC1 '(' expr ')'                   /* not($foo) */
668                         { $$ = newUNOP($1, 0, $3); }
669         |       PMFUNC '(' term ')'                  /* split (/foo/) */
670                         { $$ = pmruntime($1, $3, Nullop); }
671         |       PMFUNC '(' term ',' term ')'         /* split (/foo/,$bar) */
672                         { $$ = pmruntime($1, $3, $5); }
673         |       WORD
674         |       listop
675         ;
676
677 /* "my" declarations, with optional attributes */
678 myattrterm:     MY myterm myattrlist
679                         { $$ = my_attrs($2,$3); }
680         |       MY myterm
681                         { $$ = localize($2,$1); }
682         ;
683
684 /* Things that can be "my"'d */
685 myterm  :       '(' expr ')'
686                         { $$ = sawparens($2); }
687         |       '(' ')'
688                         { $$ = sawparens(newNULLLIST()); }
689         |       scalar  %prec '('
690                         { $$ = $1; }
691         |       hsh     %prec '('
692                         { $$ = $1; }
693         |       ary     %prec '('
694                         { $$ = $1; }
695         ;
696
697 /* Basic list expressions */
698 listexpr:       /* NULL */ %prec PREC_LOW
699                         { $$ = Nullop; }
700         |       argexpr    %prec PREC_LOW
701                         { $$ = $1; }
702         ;
703
704 listexprcom:    /* NULL */
705                         { $$ = Nullop; }
706         |       expr
707                         { $$ = $1; }
708         |       expr ','
709                         { $$ = $1; }
710         ;
711
712 /* A little bit of trickery to make "for my $foo (@bar)" actually be
713    lexical */
714 my_scalar:      scalar
715                         { PL_in_my = 0; $$ = my($1); }
716         ;
717
718 amper   :       '&' indirob
719                         { $$ = newCVREF($1,$2); }
720         ;
721
722 scalar  :       '$' indirob
723                         { $$ = newSVREF($2); }
724         ;
725
726 ary     :       '@' indirob
727                         { $$ = newAVREF($2); }
728         ;
729
730 hsh     :       '%' indirob
731                         { $$ = newHVREF($2); }
732         ;
733
734 arylen  :       DOLSHARP indirob
735                         { $$ = newAVREF($2); }
736         ;
737
738 star    :       '*' indirob
739                         { $$ = newGVREF(0,$2); }
740         ;
741
742 /* Indirect objects */
743 indirob :       WORD
744                         { $$ = scalar($1); }
745         |       scalar %prec PREC_LOW
746                         { $$ = scalar($1);  }
747         |       block
748                         { $$ = scope($1); }
749
750         |       PRIVATEREF
751                         { $$ = $1; }
752         ;