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