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