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 '&' savescope
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 savescope: /* NULL */ /* remember stack pos in case of error */
120 { $$ = PL_savestack_ix; }
122 /* A collection of "lines" in the program */
127 | lineseq savescope line
129 $$ = append_list(OP_LINESEQ,
130 (LISTOP*)$1, (LISTOP*)$3);
131 PL_pad_reset_pending = TRUE;
132 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
135 /* A "line" in the program */
137 { $$ = newSTATEOP(0, $1, $2); }
138 | loop /* loops add their own labels */
140 { if ($1 != Nullch) {
141 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
147 PL_expect = XSTATE; }
149 { $$ = newSTATEOP(0, $1, $2);
150 PL_expect = XSTATE; }
153 /* An expression which may have a side-effect */
159 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
161 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
163 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
165 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
167 { $$ = newFOROP(0, Nullch, (line_t)$2,
168 Nullop, $3, $1, Nullop); }
171 /* else and elsif blocks */
175 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
176 | ELSIF '(' mexpr ')' mblock else
177 { PL_copline = (line_t)$1;
178 $$ = newCONDOP(0, $3, scope($5), $6);
179 PL_hints |= HINT_BLOCK_SCOPE; }
182 /* Real conditional expressions */
183 cond : IF '(' remember mexpr ')' mblock else
184 { PL_copline = (line_t)$1;
186 newCONDOP(0, $4, scope($6), $7)); }
187 | UNLESS '(' remember miexpr ')' mblock else
188 { PL_copline = (line_t)$1;
190 newCONDOP(0, $4, scope($6), $7)); }
193 /* Continue blocks */
200 /* Loops: while, until, for, and a bare block */
201 loop : label WHILE '(' remember mtexpr ')' mblock cont
202 { PL_copline = (line_t)$2;
205 newWHILEOP(0, 1, (LOOP*)Nullop,
207 | label UNTIL '(' remember miexpr ')' mblock cont
208 { PL_copline = (line_t)$2;
211 newWHILEOP(0, 1, (LOOP*)Nullop,
213 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
215 newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); }
216 | label FOR scalar '(' remember mexpr ')' mblock cont
218 newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP),
220 | label FOR '(' remember mexpr ')' mblock cont
222 newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); }
223 | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock
224 /* basically fake up an initialize-while lineseq */
226 PL_copline = (line_t)$2;
227 forop = newSTATEOP(0, $1,
228 newWHILEOP(0, 1, (LOOP*)Nullop,
232 forop = append_elem(OP_LINESEQ,
233 newSTATEOP(0, ($1?savepv($1):Nullch),
238 $$ = block_end($4, forop); }
239 | label block cont /* a block is a loop that happens once */
240 { $$ = newSTATEOP(0, $1,
241 newWHILEOP(0, 1, (LOOP*)Nullop,
242 NOLINE, Nullop, $2, $3)); }
245 /* Normal expression */
251 /* Boolean expression */
252 texpr : /* NULL means true */
253 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
257 /* Inverted boolean expression */
259 { $$ = invert(scalar($1)); }
262 /* Expression with its own lexical scope */
264 { $$ = $1; intro_my(); }
268 { $$ = $1; intro_my(); }
272 { $$ = $1; intro_my(); }
276 { $$ = $1; intro_my(); }
279 /* Optional "MAIN:"-style loop labels */
285 /* Some kind of declaration - does not take part in the parse tree */
298 format : FORMAT startformsub formname block
299 { newFORM($2, $3, $4); }
302 formname: WORD { $$ = $1; }
303 | /* NULL */ { $$ = Nullop; }
306 /* Unimplemented "my sub foo { }" */
307 mysubrout: MYSUB startsub subname proto subattrlist subbody
308 { newMYSUB($2, $3, $4, $5, $6); }
311 /* Subroutine definition */
312 subrout : SUB startsub subname proto subattrlist subbody
313 { newATTRSUB($2, $3, $4, $5, $6); }
316 startsub: /* NULL */ /* start a regular subroutine scope */
317 { $$ = start_subparse(FALSE, 0); }
320 startanonsub: /* NULL */ /* start an anonymous subroutine scope */
321 { $$ = start_subparse(FALSE, CVf_ANON); }
324 startformsub: /* NULL */ /* start a format subroutine scope */
325 { $$ = start_subparse(TRUE, 0); }
328 /* Name of a subroutine - must be a bareword, could be special */
329 subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a);
330 if (strEQ(name, "BEGIN") || strEQ(name, "END")
331 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
332 CvSPECIAL_on(PL_compcv);
336 /* Subroutine prototype */
342 /* Optional list of subroutine attributes */
343 subattrlist: /* NULL */
351 /* List of attributes for a "my" variable declaration */
352 myattrlist: COLONATTR THING
358 /* Subroutine body - either null or a block */
359 subbody : block { $$ = $1; }
360 | ';' { $$ = Nullop; PL_expect = XSTATE; }
363 package : PACKAGE WORD ';'
368 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
369 WORD WORD listexpr ';'
370 { utilize($1, $2, $4, $5, $6); }
373 /* Ordinary expressions; logical combinations */
374 expr : expr ANDOP expr
375 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
377 { $$ = newLOGOP($2, 0, $1, $3); }
379 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
380 | argexpr %prec PREC_LOW
383 /* Expressions are a list of terms joined by commas */
384 argexpr : argexpr ','
387 { $$ = append_elem(OP_LIST, $1, $3); }
388 | term %prec PREC_LOW
392 listop : LSTOP indirob argexpr /* print $fh @args */
393 { $$ = convert($1, OPf_STACKED,
394 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
395 | FUNC '(' indirob expr ')' /* print ($fh @args */
396 { $$ = convert($1, OPf_STACKED,
397 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
398 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
399 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
401 prepend_elem(OP_LIST, scalar($1), $5),
402 newUNOP(OP_METHOD, 0, $3))); }
403 | term ARROW method /* $foo->bar */
404 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
405 append_elem(OP_LIST, scalar($1),
406 newUNOP(OP_METHOD, 0, $3))); }
407 | METHOD indirob listexpr /* new Class @args */
408 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
410 prepend_elem(OP_LIST, $2, $3),
411 newUNOP(OP_METHOD, 0, $1))); }
412 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
413 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
415 prepend_elem(OP_LIST, $2, $4),
416 newUNOP(OP_METHOD, 0, $1))); }
417 | LSTOP listexpr /* print @args */
418 { $$ = convert($1, 0, $2); }
419 | FUNC '(' listexprcom ')' /* print (@args) */
420 { $$ = convert($1, 0, $3); }
421 | LSTOPSUB startanonsub block /* map { foo } ... */
422 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
423 listexpr %prec LSTOP /* ... @bar */
424 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
426 prepend_elem(OP_LIST, $3, $5), $1)); }
429 /* Names of methods. May use $object->$methodname */
434 /* Some kind of subscripted expression */
435 subscripted: star '{' expr ';' '}' /* *main::{something} */
436 /* In this and all the hash accessors, ';' is
437 * provided by the tokeniser */
438 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
439 PL_expect = XOPERATOR; }
440 | scalar '[' expr ']' /* $array[$element] */
441 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
442 | term ARROW '[' expr ']' /* somearef->[$element] */
443 { $$ = newBINOP(OP_AELEM, 0,
444 ref(newAVREF($1),OP_RV2AV),
446 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
447 { $$ = newBINOP(OP_AELEM, 0,
448 ref(newAVREF($1),OP_RV2AV),
450 | scalar '{' expr ';' '}' /* $foo->{bar();} */
451 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
452 PL_expect = XOPERATOR; }
453 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
454 { $$ = newBINOP(OP_HELEM, 0,
455 ref(newHVREF($1),OP_RV2HV),
457 PL_expect = XOPERATOR; }
458 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
459 { $$ = newBINOP(OP_HELEM, 0,
460 ref(newHVREF($1),OP_RV2HV),
462 PL_expect = XOPERATOR; }
463 | term ARROW '(' ')' /* $subref->() */
464 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
465 newCVREF(0, scalar($1))); }
466 | term ARROW '(' expr ')' /* $subref->(@args) */
467 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
468 append_elem(OP_LIST, $4,
469 newCVREF(0, scalar($1)))); }
471 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
472 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
473 append_elem(OP_LIST, $3,
474 newCVREF(0, scalar($1)))); }
475 | subscripted '(' ')' /* $foo->{bar}->() */
476 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
477 newCVREF(0, scalar($1))); }
480 /* Binary operators between terms */
481 termbinop : term ASSIGNOP term /* $x = $y */
482 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
483 | term POWOP term /* $x ** $y */
484 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
485 | term MULOP term /* $x * $y, $x x $y */
486 { if ($2 != OP_REPEAT)
488 $$ = newBINOP($2, 0, $1, scalar($3)); }
489 | term ADDOP term /* $x + $y */
490 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
491 | term SHIFTOP term /* $x >> $y, $x << $y */
492 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
493 | term RELOP term /* $x > $y, etc. */
494 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
495 | term EQOP term /* $x == $y, $x eq $y */
496 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
497 | term BITANDOP term /* $x & $y */
498 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
499 | term BITOROP term /* $x | $y */
500 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
501 | term DOTDOT term /* $x..$y, $x...$y */
502 { $$ = newRANGE($2, scalar($1), scalar($3));}
503 | term ANDAND term /* $x && $y */
504 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
505 | term OROR term /* $x || $y */
506 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
507 | term DORDOR term /* $x // $y */
508 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
509 | term MATCHOP term /* $x =~ /$y/ */
510 { $$ = bind_match($2, $1, $3); }
513 /* Unary operators and terms */
514 termunop : '-' term %prec UMINUS /* -$x */
515 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
516 | '+' term %prec UMINUS /* +$x */
519 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
521 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
522 | term POSTINC /* $x++ */
523 { $$ = newUNOP(OP_POSTINC, 0,
524 mod(scalar($1), OP_POSTINC)); }
525 | term POSTDEC /* $x-- */
526 { $$ = newUNOP(OP_POSTDEC, 0,
527 mod(scalar($1), OP_POSTDEC)); }
528 | PREINC term /* ++$x */
529 { $$ = newUNOP(OP_PREINC, 0,
530 mod(scalar($2), OP_PREINC)); }
531 | PREDEC term /* --$x */
532 { $$ = newUNOP(OP_PREDEC, 0,
533 mod(scalar($2), OP_PREDEC)); }
537 /* Constructors for anonymous data */
538 anonymous: '[' expr ']'
539 { $$ = newANONLIST($2); }
541 { $$ = newANONLIST(Nullop); }
542 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
543 { $$ = newANONHASH($2); }
544 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
545 { $$ = newANONHASH(Nullop); }
546 | ANONSUB startanonsub proto subattrlist block %prec '('
547 { $$ = newANONATTRSUB($2, $3, $4, $5); }
551 /* Things called with "do" */
552 termdo : DO term %prec UNIOP /* do $filename */
554 | DO block %prec '(' /* do { code */
555 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
556 | DO WORD '(' ')' /* do somesub() */
557 { $$ = newUNOP(OP_ENTERSUB,
558 OPf_SPECIAL|OPf_STACKED,
559 prepend_elem(OP_LIST,
561 (OPpENTERSUB_AMPER<<8),
564 | DO WORD '(' expr ')' /* do somesub(@args) */
565 { $$ = newUNOP(OP_ENTERSUB,
566 OPf_SPECIAL|OPf_STACKED,
570 (OPpENTERSUB_AMPER<<8),
573 | DO scalar '(' ')' /* do $subref () */
574 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
575 prepend_elem(OP_LIST,
576 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
577 | DO scalar '(' expr ')' /* do $subref (@args) */
578 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
579 prepend_elem(OP_LIST,
581 scalar(newCVREF(0,scalar($2))))); dep();}
589 | term '?' term ':' term
590 { $$ = newCONDOP(0, $1, $3, $5); }
591 | REFGEN term /* \$x, \@y, \%z */
592 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
593 | myattrterm %prec UNIOP
595 | LOCAL term %prec UNIOP
596 { $$ = localize($2,$1); }
598 { $$ = sawparens($2); }
600 { $$ = sawparens(newNULLLIST()); }
609 | arylen %prec '(' /* $#x, $#{ something } */
610 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
613 | '(' expr ')' '[' expr ']' /* list slice */
614 { $$ = newSLICEOP(0, $5, $2); }
615 | '(' ')' '[' expr ']' /* empty list slice! */
616 { $$ = newSLICEOP(0, $4, Nullop); }
617 | ary '[' expr ']' /* array slice */
618 { $$ = prepend_elem(OP_ASLICE,
619 newOP(OP_PUSHMARK, 0),
620 newLISTOP(OP_ASLICE, 0,
622 ref($1, OP_ASLICE))); }
623 | ary '{' expr ';' '}' /* @hash{@keys} */
624 { $$ = prepend_elem(OP_HSLICE,
625 newOP(OP_PUSHMARK, 0),
626 newLISTOP(OP_HSLICE, 0,
628 ref(oopsHV($1), OP_HSLICE)));
629 PL_expect = XOPERATOR; }
633 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
634 | amper '(' ')' /* &foo() */
635 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
636 | amper '(' expr ')' /* &foo(@args) */
637 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
638 append_elem(OP_LIST, $3, scalar($1))); }
639 | NOAMP WORD listexpr /* foo(@args) */
640 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
641 append_elem(OP_LIST, $3, scalar($2))); }
642 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
643 { $$ = newOP($1, OPf_SPECIAL);
644 PL_hints |= HINT_BLOCK_SCOPE; }
646 { $$ = newLOOPEX($1,$2); }
647 | NOTOP argexpr /* not $foo */
648 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
649 | UNIOP /* Unary op, $_ implied */
650 { $$ = newOP($1, 0); }
651 | UNIOP block /* eval { foo } */
652 { $$ = newUNOP($1, 0, $2); }
653 | UNIOP term /* Unary op */
654 { $$ = newUNOP($1, 0, $2); }
655 | UNIOPSUB term /* Sub treated as unop */
656 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
657 append_elem(OP_LIST, $2, scalar($1))); }
658 | FUNC0 /* Nullary operator */
659 { $$ = newOP($1, 0); }
661 { $$ = newOP($1, 0); }
662 | FUNC0SUB /* Sub treated as nullop */
663 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
665 | FUNC1 '(' ')' /* not () */
666 { $$ = newOP($1, OPf_SPECIAL); }
667 | FUNC1 '(' expr ')' /* not($foo) */
668 { $$ = newUNOP($1, 0, $3); }
669 | PMFUNC '(' term ')' /* /foo/ */
670 { $$ = pmruntime($1, $3, Nullop); }
671 | PMFUNC '(' term ',' term ')' /* s/foo/bar/ (or tr) */
672 { $$ = pmruntime($1, $3, $5); }
677 /* "my" declarations, with optional attributes */
678 myattrterm: MY myterm myattrlist
679 { $$ = my_attrs($2,$3); }
681 { $$ = localize($2,$1); }
684 /* Things that can be "my"'d */
685 myterm : '(' expr ')'
686 { $$ = sawparens($2); }
688 { $$ = sawparens(newNULLLIST()); }
697 /* Basic list expressions */
698 listexpr: /* NULL */ %prec PREC_LOW
700 | argexpr %prec PREC_LOW
704 listexprcom: /* NULL */
712 /* A little bit of trickery to make "for my $foo (@bar)" actually be
715 { PL_in_my = 0; $$ = my($1); }
719 { $$ = newCVREF($1,$2); }
723 { $$ = newSVREF($2); }
727 { $$ = newAVREF($2); }
731 { $$ = newHVREF($2); }
734 arylen : DOLSHARP indirob
735 { $$ = newAVREF($2); }
739 { $$ = newGVREF(0,$2); }
742 /* Indirect objects */
745 | scalar %prec PREC_LOW