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