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