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