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