Update checkcfgvars.pl: config.ce is in win32/ in 5.9.x
[perl.git] / perly.y
1 /*    perly.y
2  *
3  *    Copyright (c) 1991-2002, 2003, 2004 Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 /*
11  * 'I see,' laughed Strider.  'I look foul and feel fair.  Is that it?
12  * All that is gold does not glitter, not all those who wander are lost.'
13  */
14
15 /* This file holds the grammar for the Perl language. If edited, you need
16  * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
17  * and perly.act which are derived from this.
18  *
19  * The main job of of this grammar is to call the various newFOO()
20  * functions in op.c to build a syntax tree of OP structs.
21  * It relies on the lexer in toke.c to do the tokenizing.
22  */
23
24 /*  Make the parser re-entrant. */
25
26 %pure_parser
27
28 %start prog
29
30 %union {
31     I32 ival;
32     char *pval;
33 #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                         { SvREFCNT_inc(PL_compcv);
345                           newFORM($2, $3, $4); }
346         ;
347
348 formname:       WORD            { $$ = $1; }
349         |       /* NULL */      { $$ = Nullop; }
350         ;
351
352 /* Unimplemented "my sub foo { }" */
353 mysubrout:      MYSUB startsub subname proto subattrlist subbody
354                         { SvREFCNT_inc(PL_compcv);
355                           newMYSUB($2, $3, $4, $5, $6); }
356         ;
357
358 /* Subroutine definition */
359 subrout :       SUB startsub subname proto subattrlist subbody
360                         { SvREFCNT_inc(PL_compcv);
361                           newATTRSUB($2, $3, $4, $5, $6); }
362         ;
363
364 startsub:       /* NULL */      /* start a regular subroutine scope */
365                         { $$ = start_subparse(FALSE, 0);
366                             SAVEFREESV(PL_compcv); }
367         ;
368
369 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
370                         { $$ = start_subparse(FALSE, CVf_ANON);
371                             SAVEFREESV(PL_compcv); }
372         ;
373
374 startformsub:   /* NULL */      /* start a format subroutine scope */
375                         { $$ = start_subparse(TRUE, 0);
376                             SAVEFREESV(PL_compcv); }
377         ;
378
379 /* Name of a subroutine - must be a bareword, could be special */
380 subname :       WORD    { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
381                           if (strEQ(name, "BEGIN") || strEQ(name, "END")
382                               || strEQ(name, "INIT") || strEQ(name, "CHECK")
383                               || strEQ(name, "UNITCHECK"))
384                               CvSPECIAL_on(PL_compcv);
385                           $$ = $1; }
386         ;
387
388 /* Subroutine prototype */
389 proto   :       /* NULL */
390                         { $$ = Nullop; }
391         |       THING
392         ;
393
394 /* Optional list of subroutine attributes */
395 subattrlist:    /* NULL */
396                         { $$ = Nullop; }
397         |       COLONATTR THING
398                         { $$ = $2; }
399         |       COLONATTR
400                         { $$ = Nullop; }
401         ;
402
403 /* List of attributes for a "my" variable declaration */
404 myattrlist:     COLONATTR THING
405                         { $$ = $2; }
406         |       COLONATTR
407                         { $$ = Nullop; }
408         ;
409
410 /* Subroutine body - either null or a block */
411 subbody :       block   { $$ = $1; }
412         |       ';'     { $$ = Nullop; PL_expect = XSTATE; }
413         ;
414
415 package :       PACKAGE WORD ';'
416                         { package($2); }
417         ;
418
419 use     :       USE startsub
420                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
421                     WORD WORD listexpr ';'
422                          { SvREFCNT_inc(PL_compcv);
423                            utilize($1, $2, $4, $5, $6); }
424         ;
425
426 /* Ordinary expressions; logical combinations */
427 expr    :       expr ANDOP expr
428                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
429         |       expr OROP expr
430                         { $$ = newLOGOP($2, 0, $1, $3); }
431         |       expr DOROP expr
432                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
433         |       argexpr %prec PREC_LOW
434         ;
435
436 /* Expressions are a list of terms joined by commas */
437 argexpr :       argexpr ','
438                         { $$ = $1; }
439         |       argexpr ',' term
440                         { $$ = append_elem(OP_LIST, $1, $3); }
441         |       term %prec PREC_LOW
442         ;
443
444 /* List operators */
445 listop  :       LSTOP indirob argexpr /* map {...} @args or print $fh @args */
446                         { $$ = convert($1, OPf_STACKED,
447                                 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
448         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
449                         { $$ = convert($1, OPf_STACKED,
450                                 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
451         |       term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
452                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
453                                 append_elem(OP_LIST,
454                                     prepend_elem(OP_LIST, scalar($1), $5),
455                                     newUNOP(OP_METHOD, 0, $3))); }
456         |       term ARROW method                     /* $foo->bar */
457                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
458                                 append_elem(OP_LIST, scalar($1),
459                                     newUNOP(OP_METHOD, 0, $3))); }
460         |       METHOD indirob listexpr              /* new Class @args */
461                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
462                                 append_elem(OP_LIST,
463                                     prepend_elem(OP_LIST, $2, $3),
464                                     newUNOP(OP_METHOD, 0, $1))); }
465         |       FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
466                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
467                                 append_elem(OP_LIST,
468                                     prepend_elem(OP_LIST, $2, $4),
469                                     newUNOP(OP_METHOD, 0, $1))); }
470         |       LSTOP listexpr                       /* print @args */
471                         { $$ = convert($1, 0, $2); }
472         |       FUNC '(' listexprcom ')'             /* print (@args) */
473                         { $$ = convert($1, 0, $3); }
474         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
475                         { SvREFCNT_inc(PL_compcv);
476                           $3 = newANONATTRSUB($2, 0, Nullop, $3); }
477                     listexpr            %prec LSTOP  /* ... @bar */
478                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
479                                  append_elem(OP_LIST,
480                                    prepend_elem(OP_LIST, $3, $5), $1)); }
481         ;
482
483 /* Names of methods. May use $object->$methodname */
484 method  :       METHOD
485         |       scalar
486         ;
487
488 /* Some kind of subscripted expression */
489 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
490                         /* In this and all the hash accessors, ';' is
491                          * provided by the tokeniser */
492                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
493                             PL_expect = XOPERATOR; }
494         |       scalar '[' expr ']'          /* $array[$element] */
495                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
496         |       term ARROW '[' expr ']'      /* somearef->[$element] */
497                         { $$ = newBINOP(OP_AELEM, 0,
498                                         ref(newAVREF($1),OP_RV2AV),
499                                         scalar($4));}
500         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
501                         { $$ = newBINOP(OP_AELEM, 0,
502                                         ref(newAVREF($1),OP_RV2AV),
503                                         scalar($3));}
504         |       scalar '{' expr ';' '}'    /* $foo->{bar();} */
505                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
506                             PL_expect = XOPERATOR; }
507         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
508                         { $$ = newBINOP(OP_HELEM, 0,
509                                         ref(newHVREF($1),OP_RV2HV),
510                                         jmaybe($4));
511                             PL_expect = XOPERATOR; }
512         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
513                         { $$ = newBINOP(OP_HELEM, 0,
514                                         ref(newHVREF($1),OP_RV2HV),
515                                         jmaybe($3));
516                             PL_expect = XOPERATOR; }
517         |       term ARROW '(' ')'          /* $subref->() */
518                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
519                                    newCVREF(0, scalar($1))); }
520         |       term ARROW '(' expr ')'     /* $subref->(@args) */
521                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
522                                    append_elem(OP_LIST, $4,
523                                        newCVREF(0, scalar($1)))); }
524
525         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
526                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
527                                    append_elem(OP_LIST, $3,
528                                                newCVREF(0, scalar($1)))); }
529         |       subscripted '(' ')'        /* $foo->{bar}->() */
530                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
531                                    newCVREF(0, scalar($1))); }
532         |       '(' expr ')' '[' expr ']'            /* list slice */
533                         { $$ = newSLICEOP(0, $5, $2); }
534         |       '(' ')' '[' expr ']'                 /* empty list slice! */
535                         { $$ = newSLICEOP(0, $4, Nullop); }
536     ;
537
538 /* Binary operators between terms */
539 termbinop       :       term ASSIGNOP term             /* $x = $y */
540                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
541         |       term POWOP term                        /* $x ** $y */
542                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
543         |       term MULOP term                        /* $x * $y, $x x $y */
544                         {   if ($2 != OP_REPEAT)
545                                 scalar($1);
546                             $$ = newBINOP($2, 0, $1, scalar($3)); }
547         |       term ADDOP term                        /* $x + $y */
548                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
549         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
550                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
551         |       term RELOP term                        /* $x > $y, etc. */
552                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
553         |       term EQOP term                         /* $x == $y, $x eq $y */
554                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
555         |       term BITANDOP term                     /* $x & $y */
556                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
557         |       term BITOROP term                      /* $x | $y */
558                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
559         |       term DOTDOT term                       /* $x..$y, $x...$y */
560                         { $$ = newRANGE($2, scalar($1), scalar($3));}
561         |       term ANDAND term                       /* $x && $y */
562                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
563         |       term OROR term                         /* $x || $y */
564                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
565         |       term DORDOR term                       /* $x // $y */
566                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
567         |       term MATCHOP term                      /* $x =~ /$y/ */
568                         { $$ = bind_match($2, $1, $3); }
569     ;
570
571 /* Unary operators and terms */
572 termunop : '-' term %prec UMINUS                       /* -$x */
573                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
574         |       '+' term %prec UMINUS                  /* +$x */
575                         { $$ = $2; }
576         |       '!' term                               /* !$x */
577                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
578         |       '~' term                               /* ~$x */
579                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
580         |       term POSTINC                           /* $x++ */
581                         { $$ = newUNOP(OP_POSTINC, 0,
582                                         mod(scalar($1), OP_POSTINC)); }
583         |       term POSTDEC                           /* $x-- */
584                         { $$ = newUNOP(OP_POSTDEC, 0,
585                                         mod(scalar($1), OP_POSTDEC)); }
586         |       PREINC term                            /* ++$x */
587                         { $$ = newUNOP(OP_PREINC, 0,
588                                         mod(scalar($2), OP_PREINC)); }
589         |       PREDEC term                            /* --$x */
590                         { $$ = newUNOP(OP_PREDEC, 0,
591                                         mod(scalar($2), OP_PREDEC)); }
592
593     ;
594
595 /* Constructors for anonymous data */
596 anonymous:      '[' expr ']'
597                         { $$ = newANONLIST($2); }
598         |       '[' ']'
599                         { $$ = newANONLIST(Nullop); }
600         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
601                         { $$ = newANONHASH($2); }
602         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
603                         { $$ = newANONHASH(Nullop); }
604         |       ANONSUB startanonsub proto subattrlist block    %prec '('
605                         { SvREFCNT_inc(PL_compcv);
606                           $$ = newANONATTRSUB($2, $3, $4, $5); }
607
608     ;
609
610 /* Things called with "do" */
611 termdo  :       DO term %prec UNIOP                     /* do $filename */
612                         { $$ = dofile($2, $1); }
613         |       DO block        %prec '('               /* do { code */
614                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
615         |       DO WORD '(' ')'                         /* do somesub() */
616                         { $$ = newUNOP(OP_ENTERSUB,
617                             OPf_SPECIAL|OPf_STACKED,
618                             prepend_elem(OP_LIST,
619                                 scalar(newCVREF(
620                                     (OPpENTERSUB_AMPER<<8),
621                                     scalar($2)
622                                 )),Nullop)); dep();}
623         |       DO WORD '(' expr ')'                    /* do somesub(@args) */
624                         { $$ = newUNOP(OP_ENTERSUB,
625                             OPf_SPECIAL|OPf_STACKED,
626                             append_elem(OP_LIST,
627                                 $4,
628                                 scalar(newCVREF(
629                                     (OPpENTERSUB_AMPER<<8),
630                                     scalar($2)
631                                 )))); dep();}
632         |       DO scalar '(' ')'                      /* do $subref () */
633                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
634                             prepend_elem(OP_LIST,
635                                 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
636         |       DO scalar '(' expr ')'                 /* do $subref (@args) */
637                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
638                             prepend_elem(OP_LIST,
639                                 $4,
640                                 scalar(newCVREF(0,scalar($2))))); dep();}
641
642         ;
643
644 term    :       termbinop
645         |       termunop
646         |       anonymous
647         |       termdo
648         |       term '?' term ':' term
649                         { $$ = newCONDOP(0, $1, $3, $5); }
650         |       REFGEN term                          /* \$x, \@y, \%z */
651                         { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
652         |       myattrterm      %prec UNIOP
653                         { $$ = $1; }
654         |       LOCAL term      %prec UNIOP
655                         { $$ = localize($2,$1); }
656         |       '(' expr ')'
657                         { $$ = sawparens($2); }
658         |       '(' ')'
659                         { $$ = sawparens(newNULLLIST()); }
660         |       scalar  %prec '('
661                         { $$ = $1; }
662         |       star    %prec '('
663                         { $$ = $1; }
664         |       hsh     %prec '('
665                         { $$ = $1; }
666         |       ary     %prec '('
667                         { $$ = $1; }
668         |       arylen  %prec '('                    /* $#x, $#{ something } */
669                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
670         |       subscripted
671                         { $$ = $1; }
672         |       ary '[' expr ']'                     /* array slice */
673                         { $$ = prepend_elem(OP_ASLICE,
674                                 newOP(OP_PUSHMARK, 0),
675                                     newLISTOP(OP_ASLICE, 0,
676                                         list($3),
677                                         ref($1, OP_ASLICE))); }
678         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
679                         { $$ = prepend_elem(OP_HSLICE,
680                                 newOP(OP_PUSHMARK, 0),
681                                     newLISTOP(OP_HSLICE, 0,
682                                         list($3),
683                                         ref(oopsHV($1), OP_HSLICE)));
684                             PL_expect = XOPERATOR; }
685         |       THING   %prec '('
686                         { $$ = $1; }
687         |       amper                                /* &foo; */
688                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
689         |       amper '(' ')'                        /* &foo() */
690                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
691         |       amper '(' expr ')'                   /* &foo(@args) */
692                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
693                             append_elem(OP_LIST, $3, scalar($1))); }
694         |       NOAMP WORD listexpr                  /* foo(@args) */
695                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
696                             append_elem(OP_LIST, $3, scalar($2))); }
697         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
698                         { $$ = newOP($1, OPf_SPECIAL);
699                             PL_hints |= HINT_BLOCK_SCOPE; }
700         |       LOOPEX term
701                         { $$ = newLOOPEX($1,$2); }
702         |       NOTOP argexpr                        /* not $foo */
703                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
704         |       UNIOP                                /* Unary op, $_ implied */
705                         { $$ = newOP($1, 0); }
706         |       UNIOP block                          /* eval { foo } */
707                         { $$ = newUNOP($1, 0, $2); }
708         |       UNIOP term                           /* Unary op */
709                         { $$ = newUNOP($1, 0, $2); }
710         |       REQUIRE                              /* require, $_ implied */
711                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
712         |       REQUIRE term                         /* require Foo */
713                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
714         |       UNIOPSUB
715                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
716         |       UNIOPSUB term                        /* Sub treated as unop */
717                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
718                             append_elem(OP_LIST, $2, scalar($1))); }
719         |       FUNC0                                /* Nullary operator */
720                         { $$ = newOP($1, 0); }
721         |       FUNC0 '(' ')'
722                         { $$ = newOP($1, 0); }
723         |       FUNC0SUB                             /* Sub treated as nullop */
724                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
725                                 scalar($1)); }
726         |       FUNC1 '(' ')'                        /* not () */
727                         { $$ = $1 == OP_NOT ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
728                                             : newOP($1, OPf_SPECIAL); }
729         |       FUNC1 '(' expr ')'                   /* not($foo) */
730                         { $$ = newUNOP($1, 0, $3); }
731         |       PMFUNC '(' argexpr ')'           /* m//, s///, tr/// */
732                         { $$ = pmruntime($1, $3, 1); }
733         |       WORD
734         |       listop
735         ;
736
737 /* "my" declarations, with optional attributes */
738 myattrterm:     MY myterm myattrlist
739                         { $$ = my_attrs($2,$3); }
740         |       MY myterm
741                         { $$ = localize($2,$1); }
742         ;
743
744 /* Things that can be "my"'d */
745 myterm  :       '(' expr ')'
746                         { $$ = sawparens($2); }
747         |       '(' ')'
748                         { $$ = sawparens(newNULLLIST()); }
749         |       scalar  %prec '('
750                         { $$ = $1; }
751         |       hsh     %prec '('
752                         { $$ = $1; }
753         |       ary     %prec '('
754                         { $$ = $1; }
755         ;
756
757 /* Basic list expressions */
758 listexpr:       /* NULL */ %prec PREC_LOW
759                         { $$ = Nullop; }
760         |       argexpr    %prec PREC_LOW
761                         { $$ = $1; }
762         ;
763
764 listexprcom:    /* NULL */
765                         { $$ = Nullop; }
766         |       expr
767                         { $$ = $1; }
768         |       expr ','
769                         { $$ = $1; }
770         ;
771
772 /* A little bit of trickery to make "for my $foo (@bar)" actually be
773    lexical */
774 my_scalar:      scalar
775                         { PL_in_my = 0; $$ = my($1); }
776         ;
777
778 amper   :       '&' indirob
779                         { $$ = newCVREF($1,$2); }
780         ;
781
782 scalar  :       '$' indirob
783                         { $$ = newSVREF($2); }
784         ;
785
786 ary     :       '@' indirob
787                         { $$ = newAVREF($2); }
788         ;
789
790 hsh     :       '%' indirob
791                         { $$ = newHVREF($2); }
792         ;
793
794 arylen  :       DOLSHARP indirob
795                         { $$ = newAVREF($2); }
796         ;
797
798 star    :       '*' indirob
799                         { $$ = newGVREF(0,$2); }
800         ;
801
802 /* Indirect objects */
803 indirob :       WORD
804                         { $$ = scalar($1); }
805         |       scalar %prec PREC_LOW
806                         { $$ = scalar($1);  }
807         |       block
808                         { $$ = scope($1); }
809
810         |       PRIVATEREF
811                         { $$ = $1; }
812         ;