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