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