3 * Copyright (c) 1991-2002, 2003, 2004 Larry Wall
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.
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.'
15 /* Make the parser re-entrant. */
30 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
31 %token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
33 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
34 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
35 %token <ival> LOOPEX DOTDOT
36 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
37 %token <ival> RELOP EQOP MULOP ADDOP
38 %token <ival> DOLSHARP DO HASHBRACK NOAMP
39 %token <ival> LOCAL MY MYSUB
42 %type <ival> prog decl format startsub startanonsub startformsub
43 %type <ival> progstart remember mremember '&'
44 %type <opval> block mblock lineseq line loop cond else
45 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
46 %type <opval> argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr
47 %type <opval> listexpr listexprcom indirob listop method
48 %type <opval> formname subname proto subbody cont my_scalar
49 %type <opval> subattrlist myattrlist mysubrout myattrterm myterm
50 %type <opval> termbinop termunop anonymous termdo
56 %left <ival> OROP DOROP
59 %nonassoc LSTOP LSTOPSUB
61 %right <ival> ASSIGNOP
70 %nonassoc UNIOP UNIOPSUB
75 %right '!' '~' UMINUS REFGEN
77 %nonassoc PREINC PREDEC POSTINC POSTDEC
85 /* The whole program */
88 { $$ = $1; newPROG(block_end($1,$2)); }
91 /* An ordinary block */
92 block : '{' remember lineseq '}'
93 { if (PL_copline > (line_t)$1)
94 PL_copline = (line_t)$1;
95 $$ = block_end($2, $3); }
98 remember: /* NULL */ /* start a full lexical scope */
99 { $$ = block_start(TRUE); }
104 PL_expect = XSTATE; $$ = block_start(TRUE);
109 mblock : '{' mremember lineseq '}'
110 { if (PL_copline > (line_t)$1)
111 PL_copline = (line_t)$1;
112 $$ = block_end($2, $3); }
115 mremember: /* NULL */ /* start a partial lexical scope */
116 { $$ = block_start(FALSE); }
119 /* A collection of "lines" in the program */
125 { $$ = append_list(OP_LINESEQ,
126 (LISTOP*)$1, (LISTOP*)$2);
127 PL_pad_reset_pending = TRUE;
128 if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; }
131 /* A "line" in the program */
133 { $$ = newSTATEOP(0, $1, $2); }
134 | loop /* loops add their own labels */
136 { if ($1 != Nullch) {
137 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
143 PL_expect = XSTATE; }
145 { $$ = newSTATEOP(0, $1, $2);
146 PL_expect = XSTATE; }
149 /* An expression which may have a side-effect */
155 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
157 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
159 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
161 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
163 { $$ = newFOROP(0, Nullch, (line_t)$2,
164 Nullop, $3, $1, Nullop); }
167 /* else and elsif blocks */
171 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
172 | ELSIF '(' mexpr ')' mblock else
173 { PL_copline = (line_t)$1;
174 $$ = newCONDOP(0, $3, scope($5), $6);
175 PL_hints |= HINT_BLOCK_SCOPE; }
178 /* Real conditional expressions */
179 cond : IF '(' remember mexpr ')' mblock else
180 { PL_copline = (line_t)$1;
182 newCONDOP(0, $4, scope($6), $7)); }
183 | UNLESS '(' remember miexpr ')' mblock else
184 { PL_copline = (line_t)$1;
186 newCONDOP(0, $4, scope($6), $7)); }
189 /* Continue blocks */
196 /* Loops: while, until, for, and a bare block */
197 loop : label WHILE '(' remember mtexpr ')' mblock cont
198 { PL_copline = (line_t)$2;
201 newWHILEOP(0, 1, (LOOP*)Nullop,
203 | label UNTIL '(' remember miexpr ')' mblock cont
204 { PL_copline = (line_t)$2;
207 newWHILEOP(0, 1, (LOOP*)Nullop,
209 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
211 newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); }
212 | label FOR scalar '(' remember mexpr ')' mblock cont
214 newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP),
216 | label FOR '(' remember mexpr ')' mblock cont
218 newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); }
219 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
220 /* basically fake up an initialize-while lineseq */
222 PL_copline = (line_t)$2;
223 forop = newSTATEOP(0, $1,
224 newWHILEOP(0, 1, (LOOP*)Nullop,
228 forop = append_elem(OP_LINESEQ,
229 newSTATEOP(0, ($1?savepv($1):Nullch),
234 $$ = block_end($4, forop); }
235 | label block cont /* a block is a loop that happens once */
236 { $$ = newSTATEOP(0, $1,
237 newWHILEOP(0, 1, (LOOP*)Nullop,
238 NOLINE, Nullop, $2, $3)); }
241 /* Normal expression */
247 /* Boolean expression */
248 texpr : /* NULL means true */
249 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
253 /* Inverted boolean expression */
255 { $$ = invert(scalar($1)); }
258 /* Expression with its own lexical scope */
260 { $$ = $1; intro_my(); }
264 { $$ = $1; intro_my(); }
268 { $$ = $1; intro_my(); }
272 { $$ = $1; intro_my(); }
275 /* Optional "MAIN:"-style loop labels */
281 /* Some kind of declaration - does not take part in the parse tree */
294 format : FORMAT startformsub formname block
295 { newFORM($2, $3, $4); }
298 formname: WORD { $$ = $1; }
299 | /* NULL */ { $$ = Nullop; }
302 /* Unimplemented "my sub foo { }" */
303 mysubrout: MYSUB startsub subname proto subattrlist subbody
304 { newMYSUB($2, $3, $4, $5, $6); }
307 /* Subroutine definition */
308 subrout : SUB startsub subname proto subattrlist subbody
309 { newATTRSUB($2, $3, $4, $5, $6); }
312 startsub: /* NULL */ /* start a regular subroutine scope */
313 { $$ = start_subparse(FALSE, 0); }
316 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
317 { $$ = start_subparse(FALSE, CVf_ANON); }
320 startformsub: /* NULL */ /* start a format subroutine scope */
321 { $$ = start_subparse(TRUE, 0); }
324 /* Name of a subroutine - must be a bareword, could be special */
325 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
326 if (strEQ(name, "BEGIN") || strEQ(name, "END")
327 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
328 CvSPECIAL_on(PL_compcv);
332 /* Subroutine prototype */
338 /* Optional list of subroutine attributes */
339 subattrlist: /* NULL */
347 /* List of attributes for a "my" variable declaration */
348 myattrlist: COLONATTR THING
354 /* Subroutine body - either null or a block */
355 subbody : block { $$ = $1; }
356 | ';' { $$ = Nullop; PL_expect = XSTATE; }
359 package : PACKAGE WORD ';'
364 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
365 WORD WORD listexpr ';'
366 { utilize($1, $2, $4, $5, $6); }
369 /* Ordinary expressions; logical combinations */
370 expr : expr ANDOP expr
371 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
373 { $$ = newLOGOP($2, 0, $1, $3); }
375 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
376 | argexpr %prec PREC_LOW
379 /* Expressions are a list of terms joined by commas */
380 argexpr : argexpr ','
383 { $$ = append_elem(OP_LIST, $1, $3); }
384 | term %prec PREC_LOW
388 listop : LSTOP indirob argexpr /* print $fh @args */
389 { $$ = convert($1, OPf_STACKED,
390 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
391 | FUNC '(' indirob expr ')' /* print ($fh @args */
392 { $$ = convert($1, OPf_STACKED,
393 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
394 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
395 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
397 prepend_elem(OP_LIST, scalar($1), $5),
398 newUNOP(OP_METHOD, 0, $3))); }
399 | term ARROW method /* $foo->bar */
400 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
401 append_elem(OP_LIST, scalar($1),
402 newUNOP(OP_METHOD, 0, $3))); }
403 | METHOD indirob listexpr /* new Class @args */
404 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
406 prepend_elem(OP_LIST, $2, $3),
407 newUNOP(OP_METHOD, 0, $1))); }
408 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
409 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
411 prepend_elem(OP_LIST, $2, $4),
412 newUNOP(OP_METHOD, 0, $1))); }
413 | LSTOP listexpr /* print @args */
414 { $$ = convert($1, 0, $2); }
415 | FUNC '(' listexprcom ')' /* print (@args) */
416 { $$ = convert($1, 0, $3); }
417 | LSTOPSUB startanonsub block /* map { foo } ... */
418 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
419 listexpr %prec LSTOP /* ... @bar */
420 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
422 prepend_elem(OP_LIST, $3, $5), $1)); }
425 /* Names of methods. May use $object->$methodname */
430 /* Some kind of subscripted expression */
431 subscripted: star '{' expr ';' '}' /* *main::{something} */
432 /* In this and all the hash accessors, ';' is
433 * provided by the tokeniser */
434 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
435 PL_expect = XOPERATOR; }
436 | scalar '[' expr ']' /* $array[$element] */
437 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
438 | term ARROW '[' expr ']' /* somearef->[$element] */
439 { $$ = newBINOP(OP_AELEM, 0,
440 ref(newAVREF($1),OP_RV2AV),
442 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
443 { $$ = newBINOP(OP_AELEM, 0,
444 ref(newAVREF($1),OP_RV2AV),
446 | scalar '{' expr ';' '}' /* $foo->{bar();} */
447 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
448 PL_expect = XOPERATOR; }
449 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
450 { $$ = newBINOP(OP_HELEM, 0,
451 ref(newHVREF($1),OP_RV2HV),
453 PL_expect = XOPERATOR; }
454 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
455 { $$ = newBINOP(OP_HELEM, 0,
456 ref(newHVREF($1),OP_RV2HV),
458 PL_expect = XOPERATOR; }
459 | term ARROW '(' ')' /* $subref->() */
460 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
461 newCVREF(0, scalar($1))); }
462 | term ARROW '(' expr ')' /* $subref->(@args) */
463 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
464 append_elem(OP_LIST, $4,
465 newCVREF(0, scalar($1)))); }
467 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
468 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
469 append_elem(OP_LIST, $3,
470 newCVREF(0, scalar($1)))); }
471 | subscripted '(' ')' /* $foo->{bar}->() */
472 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
473 newCVREF(0, scalar($1))); }
476 /* Binary operators between terms */
477 termbinop : term ASSIGNOP term /* $x = $y */
478 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
479 | term POWOP term /* $x ** $y */
480 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
481 | term MULOP term /* $x * $y, $x x $y */
482 { if ($2 != OP_REPEAT)
484 $$ = newBINOP($2, 0, $1, scalar($3)); }
485 | term ADDOP term /* $x + $y */
486 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
487 | term SHIFTOP term /* $x >> $y, $x << $y */
488 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
489 | term RELOP term /* $x > $y, etc. */
490 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
491 | term EQOP term /* $x == $y, $x eq $y */
492 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
493 | term BITANDOP term /* $x & $y */
494 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
495 | term BITOROP term /* $x | $y */
496 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
497 | term DOTDOT term /* $x..$y, $x...$y */
498 { $$ = newRANGE($2, scalar($1), scalar($3));}
499 | term ANDAND term /* $x && $y */
500 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
501 | term OROR term /* $x || $y */
502 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
503 | term DORDOR term /* $x // $y */
504 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
505 | term MATCHOP term /* $x =~ /$y/ */
506 { $$ = bind_match($2, $1, $3); }
509 /* Unary operators and terms */
510 termunop : '-' term %prec UMINUS /* -$x */
511 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
512 | '+' term %prec UMINUS /* +$x */
515 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
517 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
518 | term POSTINC /* $x++ */
519 { $$ = newUNOP(OP_POSTINC, 0,
520 mod(scalar($1), OP_POSTINC)); }
521 | term POSTDEC /* $x-- */
522 { $$ = newUNOP(OP_POSTDEC, 0,
523 mod(scalar($1), OP_POSTDEC)); }
524 | PREINC term /* ++$x */
525 { $$ = newUNOP(OP_PREINC, 0,
526 mod(scalar($2), OP_PREINC)); }
527 | PREDEC term /* --$x */
528 { $$ = newUNOP(OP_PREDEC, 0,
529 mod(scalar($2), OP_PREDEC)); }
533 /* Constructors for anonymous data */
534 anonymous: '[' expr ']'
535 { $$ = newANONLIST($2); }
537 { $$ = newANONLIST(Nullop); }
538 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
539 { $$ = newANONHASH($2); }
540 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
541 { $$ = newANONHASH(Nullop); }
542 | ANONSUB startanonsub proto subattrlist block %prec '('
543 { $$ = newANONATTRSUB($2, $3, $4, $5); }
547 /* Things called with "do" */
548 termdo : DO term %prec UNIOP /* do $filename */
550 | DO block %prec '(' /* do { code */
551 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
552 | DO WORD '(' ')' /* do somesub() */
553 { $$ = newUNOP(OP_ENTERSUB,
554 OPf_SPECIAL|OPf_STACKED,
555 prepend_elem(OP_LIST,
557 (OPpENTERSUB_AMPER<<8),
560 | DO WORD '(' expr ')' /* do somesub(@args) */
561 { $$ = newUNOP(OP_ENTERSUB,
562 OPf_SPECIAL|OPf_STACKED,
566 (OPpENTERSUB_AMPER<<8),
569 | DO scalar '(' ')' /* do $subref () */
570 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
571 prepend_elem(OP_LIST,
572 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
573 | DO scalar '(' expr ')' /* do $subref (@args) */
574 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
575 prepend_elem(OP_LIST,
577 scalar(newCVREF(0,scalar($2))))); dep();}
585 | term '?' term ':' term
586 { $$ = newCONDOP(0, $1, $3, $5); }
587 | REFGEN term /* \$x, \@y, \%z */
588 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
589 | myattrterm %prec UNIOP
591 | LOCAL term %prec UNIOP
592 { $$ = localize($2,$1); }
594 { $$ = sawparens($2); }
596 { $$ = sawparens(newNULLLIST()); }
605 | arylen %prec '(' /* $#x, $#{ something } */
606 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
609 | '(' expr ')' '[' expr ']' /* list slice */
610 { $$ = newSLICEOP(0, $5, $2); }
611 | '(' ')' '[' expr ']' /* empty list slice! */
612 { $$ = newSLICEOP(0, $4, Nullop); }
613 | ary '[' expr ']' /* array slice */
614 { $$ = prepend_elem(OP_ASLICE,
615 newOP(OP_PUSHMARK, 0),
616 newLISTOP(OP_ASLICE, 0,
618 ref($1, OP_ASLICE))); }
619 | ary '{' expr ';' '}' /* @hash{@keys} */
620 { $$ = prepend_elem(OP_HSLICE,
621 newOP(OP_PUSHMARK, 0),
622 newLISTOP(OP_HSLICE, 0,
624 ref(oopsHV($1), OP_HSLICE)));
625 PL_expect = XOPERATOR; }
629 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
630 | amper '(' ')' /* &foo() */
631 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
632 | amper '(' expr ')' /* &foo(@args) */
633 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
634 append_elem(OP_LIST, $3, scalar($1))); }
635 | NOAMP WORD listexpr /* foo(@args) */
636 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
637 append_elem(OP_LIST, $3, scalar($2))); }
638 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
639 { $$ = newOP($1, OPf_SPECIAL);
640 PL_hints |= HINT_BLOCK_SCOPE; }
642 { $$ = newLOOPEX($1,$2); }
643 | NOTOP argexpr /* not $foo */
644 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
645 | UNIOP /* Unary op, $_ implied */
646 { $$ = newOP($1, 0); }
647 | UNIOP block /* eval { foo }, I *think* */
648 { $$ = newUNOP($1, 0, $2); }
649 | UNIOP term /* Unary op */
650 { $$ = newUNOP($1, 0, $2); }
651 | UNIOPSUB term /* Sub treated as unop */
652 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
653 append_elem(OP_LIST, $2, scalar($1))); }
654 | FUNC0 /* Nullary operator */
655 { $$ = newOP($1, 0); }
657 { $$ = newOP($1, 0); }
658 | FUNC0SUB /* Sub treated as nullop */
659 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
661 | FUNC1 '(' ')' /* not () */
662 { $$ = newOP($1, OPf_SPECIAL); }
663 | FUNC1 '(' expr ')' /* not($foo) */
664 { $$ = newUNOP($1, 0, $3); }
665 | PMFUNC '(' term ')' /* split (/foo/) */
666 { $$ = pmruntime($1, $3, Nullop); }
667 | PMFUNC '(' term ',' term ')' /* split (/foo/,$bar) */
668 { $$ = pmruntime($1, $3, $5); }
673 /* "my" declarations, with optional attributes */
674 myattrterm: MY myterm myattrlist
675 { $$ = my_attrs($2,$3); }
677 { $$ = localize($2,$1); }
680 /* Things that can be "my"'d */
681 myterm : '(' expr ')'
682 { $$ = sawparens($2); }
684 { $$ = sawparens(newNULLLIST()); }
693 /* Basic list expressions */
694 listexpr: /* NULL */ %prec PREC_LOW
696 | argexpr %prec PREC_LOW
700 listexprcom: /* NULL */
708 /* A little bit of trickery to make "for my $foo (@bar)" actually be
711 { PL_in_my = 0; $$ = my($1); }
715 { $$ = newCVREF($1,$2); }
719 { $$ = newSVREF($2); }
723 { $$ = newAVREF($2); }
727 { $$ = newHVREF($2); }
730 arylen : DOLSHARP indirob
731 { $$ = newAVREF($2); }
735 { $$ = newGVREF(0,$2); }
738 /* Indirect objects */
741 | scalar %prec PREC_LOW