1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
/* perly.y
*
* Copyright (c) 1991-1994, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
* All that is gold does not glitter, not all those that wander are lost.'
*/
%{
#include "EXTERN.h"
#include "perl.h"
static void
dep()
{
deprecate("\"do\" to call subroutines");
}
%}
%start prog
%union {
I32 ival;
char *pval;
OP *opval;
GV *gvval;
}
%token <ival> '{' ')'
%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
%token <pval> LABEL
%token <ival> FORMAT SUB ANONSUB PACKAGE USE
%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
%token <ival> LOOPEX DOTDOT
%token <ival> FUNC0 FUNC1 FUNC
%token <ival> RELOP EQOP MULOP ADDOP
%token <ival> DOLSHARP DO HASHBRACK NOAMP
%token LOCAL MY
%type <ival> prog decl local format startsub remember mremember '&'
%type <opval> block mblock mintro lineseq line loop cond else
%type <opval> expr term scalar ary hsh arylen star amper sideff
%type <opval> argexpr nexpr texpr iexpr mexpr mtexpr miexpr
%type <opval> listexpr listexprcom indirob
%type <opval> listop method proto cont my_scalar
%type <pval> label
%left <ival> OROP
%left ANDOP
%right NOTOP
%nonassoc <ival> LSTOP
%left ','
%right <ival> ASSIGNOP
%right '?' ':'
%nonassoc DOTDOT
%left OROR
%left ANDAND
%left <ival> BITOROP
%left <ival> BITANDOP
%nonassoc EQOP
%nonassoc RELOP
%nonassoc <ival> UNIOP
%left <ival> SHIFTOP
%left ADDOP
%left MULOP
%left <ival> MATCHOP
%right '!' '~' UMINUS REFGEN
%right <ival> POWOP
%nonassoc PREINC PREDEC POSTINC POSTDEC
%left ARROW
%left '('
%% /* RULES */
prog : /* NULL */
{
#if defined(YYDEBUG) && defined(DEBUGGING)
yydebug = (debug & 1);
#endif
expect = XSTATE;
}
/*CONTINUED*/ lineseq
{ newPROG($2); }
;
block : '{' remember lineseq '}'
{ $$ = block_end($1,$2,$3); }
;
remember: /* NULL */ /* start a full lexical scope */
{ $$ = block_start(TRUE); }
;
mblock : '{' mintro mremember lineseq '}'
{ if ($2)
$4 = $4 ? append_list(OP_LINESEQ,
(LISTOP*)$2, (LISTOP*)$4) : $2;
$$ = block_end($1, $3, $4); }
;
mintro : /* NULL */ /* introduce pending lexicals */
{ $$ = min_intro_pending
? newSTATEOP(0, Nullch, newOP(OP_NULL, 0))
: NULL; }
;
mremember: /* NULL */ /* start a partial lexical scope */
{ $$ = block_start(FALSE); }
;
lineseq : /* NULL */
{ $$ = Nullop; }
| lineseq decl
{ $$ = $1; }
| lineseq line
{ $$ = append_list(OP_LINESEQ,
(LISTOP*)$1, (LISTOP*)$2);
pad_reset_pending = TRUE;
if ($1 && $2) hints |= HINT_BLOCK_SCOPE; }
;
line : label cond
{ $$ = newSTATEOP(0, $1, $2); }
| loop /* loops add their own labels */
| label ';'
{ if ($1 != Nullch) {
$$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
}
else {
$$ = Nullop;
copline = NOLINE;
}
expect = XSTATE; }
| label sideff ';'
{ $$ = newSTATEOP(0, $1, $2);
expect = XSTATE; }
;
sideff : error
{ $$ = Nullop; }
| expr
{ $$ = $1; }
| expr IF expr
{ $$ = newLOGOP(OP_AND, 0, $3, $1); }
| expr UNLESS expr
{ $$ = newLOGOP(OP_OR, 0, $3, $1); }
| expr WHILE expr
{ $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
| expr UNTIL iexpr
{ $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
;
else : /* NULL */
{ $$ = Nullop; }
| ELSE mblock
{ $$ = scope($2); }
| ELSIF '(' mexpr ')' mblock else
{ copline = $1;
$$ = newCONDOP(0, $3, scope($5), $6);
hints |= HINT_BLOCK_SCOPE; }
;
cond : IF '(' remember mexpr ')' mblock else
{ copline = $1;
$$ = block_end($1, $3,
newCONDOP(0, $4, scope($6), $7)); }
| UNLESS '(' remember miexpr ')' mblock else
{ copline = $1;
$$ = block_end($1, $3,
newCONDOP(0, $4, scope($6), $7)); }
| IF block block else
{ copline = $1;
deprecate("if BLOCK BLOCK");
$$ = newCONDOP(0, scope($2), scope($3), $4); }
| UNLESS block block else
{ copline = $1;
deprecate("unless BLOCK BLOCK");
$$ = newCONDOP(0, invert(scalar(scope($2))),
scope($3), $4); }
;
cont : /* NULL */
{ $$ = Nullop; }
| CONTINUE block
{ $$ = scope($2); }
;
loop : label WHILE '(' remember mtexpr ')' mblock cont
{ copline = $2;
$$ = block_end($2, $4,
newSTATEOP(0, $1,
newWHILEOP(0, 1, (LOOP*)Nullop,
$5, $7, $8) )); }
| label UNTIL '(' remember miexpr ')' mblock cont
{ copline = $2;
$$ = block_end($2, $4,
newSTATEOP(0, $1,
newWHILEOP(0, 1, (LOOP*)Nullop,
$5, $7, $8) )); }
| label WHILE block block cont
{ copline = $2;
$$ = newSTATEOP(0, $1,
newWHILEOP(0, 1, (LOOP*)Nullop,
scope($3), $4, $5) ); }
| label UNTIL block block cont
{ copline = $2;
$$ = newSTATEOP(0, $1,
newWHILEOP(0, 1, (LOOP*)Nullop,
invert(scalar(scope($3))), $4, $5)); }
| label FOR MY remember my_scalar '(' expr ')' mblock cont
{ $$ = block_end($2, $4,
newFOROP(0, $1, $2, $5, $7, $9, $10)); }
| label FOR scalar '(' expr ')' block cont
{ $$ = newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP),
$5, $7, $8); }
| label FOR '(' remember expr ')' mblock cont
{ $$ = block_end($2, $4,
newFOROP(0, $1, $2, Nullop, $5, $7, $8)); }
| label FOR '(' remember nexpr ';'
{ if ($5) {
$5 = scalar($5);
if (min_intro_pending)
$5 = newSTATEOP(0, Nullch, $5); } }
texpr ';'
{ $8 = scalar($8);
if (min_intro_pending)
$8 = newSTATEOP(0, Nullch, $8); }
nexpr ')' mblock
/* basically fake up an initialize-while lineseq */
{ copline = $2;
$$ = block_end($2, $4,
append_elem(OP_LINESEQ, $5,
newSTATEOP(0, $1,
newWHILEOP(0, 1, (LOOP*)Nullop,
$8, $13, scalar($11))))); }
| label block cont /* a block is a loop that happens once */
{ $$ = newSTATEOP(0,
$1, newWHILEOP(0, 1, (LOOP*)Nullop,
Nullop, $2, $3)); }
;
nexpr : /* NULL */
{ $$ = Nullop; }
| sideff
;
texpr : /* NULL means true */
{ (void)scan_num("1"); $$ = yylval.opval; }
| expr
;
iexpr : expr
{ $$ = invert(scalar($1)); }
;
mexpr : expr
{ $$ = min_intro_pending
? newSTATEOP(0, Nullch, $1) : $1; }
;
mtexpr : texpr
{ $$ = min_intro_pending
? newSTATEOP(0, Nullch, $1) : $1; }
;
miexpr : iexpr
{ $$ = min_intro_pending
? newSTATEOP(0, Nullch, $1) : $1; }
;
label : /* empty */
{ $$ = Nullch; }
| LABEL
;
decl : format
{ $$ = 0; }
| subrout
{ $$ = 0; }
| package
{ $$ = 0; }
| use
{ $$ = 0; }
;
format : FORMAT startsub WORD block
{ newFORM($2, $3, $4); }
| FORMAT startsub block
{ newFORM($2, Nullop, $3); }
;
subrout : SUB startsub WORD proto block
{ newSUB($2, $3, $4, $5); }
| SUB startsub WORD proto ';'
{ newSUB($2, $3, $4, Nullop); expect = XSTATE; }
;
proto : /* NULL */
{ $$ = Nullop; }
| THING
;
startsub: /* NULL */ /* start a subroutine scope */
{ $$ = start_subparse(); }
;
package : PACKAGE WORD ';'
{ package($2); }
| PACKAGE ';'
{ package(Nullop); }
;
use : USE startsub WORD WORD listexpr ';'
{ utilize($1, $2, $3, $4, $5); }
;
expr : expr ANDOP expr
{ $$ = newLOGOP(OP_AND, 0, $1, $3); }
| expr OROP expr
{ $$ = newLOGOP($2, 0, $1, $3); }
| argexpr
;
argexpr : argexpr ','
{ $$ = $1; }
| argexpr ',' term
{ $$ = append_elem(OP_LIST, $1, $3); }
| term
;
listop : LSTOP indirob argexpr
{ $$ = convert($1, OPf_STACKED,
prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
| FUNC '(' indirob expr ')'
{ $$ = convert($1, OPf_STACKED,
prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
| term ARROW method '(' listexprcom ')'
{ $$ = convert(OP_ENTERSUB, OPf_STACKED,
append_elem(OP_LIST,
prepend_elem(OP_LIST, scalar($1), $5),
newUNOP(OP_METHOD, 0, $3))); }
| METHOD indirob listexpr
{ $$ = convert(OP_ENTERSUB, OPf_STACKED,
append_elem(OP_LIST,
prepend_elem(OP_LIST, $2, $3),
newUNOP(OP_METHOD, 0, $1))); }
| FUNCMETH indirob '(' listexprcom ')'
{ $$ = convert(OP_ENTERSUB, OPf_STACKED,
append_elem(OP_LIST,
prepend_elem(OP_LIST, $2, $4),
newUNOP(OP_METHOD, 0, $1))); }
| LSTOP listexpr
{ $$ = convert($1, 0, $2); }
| FUNC '(' listexprcom ')'
{ $$ = convert($1, 0, $3); }
| LSTOPSUB startsub block listexpr %prec LSTOP
{ $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
append_elem(OP_LIST,
prepend_elem(OP_LIST, newANONSUB($2, 0, $3), $4),
$1)); }
;
method : METHOD
| scalar
;
term : term ASSIGNOP term
{ $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
| term POWOP term
{ $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
| term MULOP term
{ if ($2 != OP_REPEAT)
scalar($1);
$$ = newBINOP($2, 0, $1, scalar($3)); }
| term ADDOP term
{ $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
| term SHIFTOP term
{ $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
| term RELOP term
{ $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
| term EQOP term
{ $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
| term BITANDOP term
{ $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
| term BITOROP term
{ $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
| term DOTDOT term
{ $$ = newRANGE($2, scalar($1), scalar($3));}
| term ANDAND term
{ $$ = newLOGOP(OP_AND, 0, $1, $3); }
| term OROR term
{ $$ = newLOGOP(OP_OR, 0, $1, $3); }
| term '?' term ':' term
{ $$ = newCONDOP(0, $1, $3, $5); }
| term MATCHOP term
{ $$ = bind_match($2, $1, $3); }
| '-' term %prec UMINUS
{ $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
| '+' term %prec UMINUS
{ $$ = $2; }
| '!' term
{ $$ = newUNOP(OP_NOT, 0, scalar($2)); }
| '~' term
{ $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
| REFGEN term
{ $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
| term POSTINC
{ $$ = newUNOP(OP_POSTINC, 0,
mod(scalar($1), OP_POSTINC)); }
| term POSTDEC
{ $$ = newUNOP(OP_POSTDEC, 0,
mod(scalar($1), OP_POSTDEC)); }
| PREINC term
{ $$ = newUNOP(OP_PREINC, 0,
mod(scalar($2), OP_PREINC)); }
| PREDEC term
{ $$ = newUNOP(OP_PREDEC, 0,
mod(scalar($2), OP_PREDEC)); }
| local term %prec UNIOP
{ $$ = localize($2,$1); }
| '(' expr ')'
{ $$ = sawparens($2); }
| '(' ')'
{ $$ = sawparens(newNULLLIST()); }
| '[' expr ']' %prec '('
{ $$ = newANONLIST($2); }
| '[' ']' %prec '('
{ $$ = newANONLIST(Nullop); }
| HASHBRACK expr ';' '}' %prec '('
{ $$ = newANONHASH($2); }
| HASHBRACK ';' '}' %prec '('
{ $$ = newANONHASH(Nullop); }
| ANONSUB startsub proto block %prec '('
{ $$ = newANONSUB($2, $3, $4); }
| scalar %prec '('
{ $$ = $1; }
| star '{' expr ';' '}'
{ $$ = newBINOP(OP_GELEM, 0, newGVREF(0,$1), $3); }
| star %prec '('
{ $$ = $1; }
| scalar '[' expr ']' %prec '('
{ $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
| term ARROW '[' expr ']' %prec '('
{ $$ = newBINOP(OP_AELEM, 0,
ref(newAVREF($1),OP_RV2AV),
scalar($4));}
| term '[' expr ']' %prec '('
{ assertref($1); $$ = newBINOP(OP_AELEM, 0,
ref(newAVREF($1),OP_RV2AV),
scalar($3));}
| hsh %prec '('
{ $$ = $1; }
| ary %prec '('
{ $$ = $1; }
| arylen %prec '('
{ $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
| scalar '{' expr ';' '}' %prec '('
{ $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
expect = XOPERATOR; }
| term ARROW '{' expr ';' '}' %prec '('
{ $$ = newBINOP(OP_HELEM, 0,
ref(newHVREF($1),OP_RV2HV),
jmaybe($4));
expect = XOPERATOR; }
| term '{' expr ';' '}' %prec '('
{ assertref($1); $$ = newBINOP(OP_HELEM, 0,
ref(newHVREF($1),OP_RV2HV),
jmaybe($3));
expect = XOPERATOR; }
| '(' expr ')' '[' expr ']' %prec '('
{ $$ = newSLICEOP(0, $5, $2); }
| '(' ')' '[' expr ']' %prec '('
{ $$ = newSLICEOP(0, $4, Nullop); }
| ary '[' expr ']' %prec '('
{ $$ = prepend_elem(OP_ASLICE,
newOP(OP_PUSHMARK, 0),
newLISTOP(OP_ASLICE, 0,
list($3),
ref($1, OP_ASLICE))); }
| ary '{' expr ';' '}' %prec '('
{ $$ = prepend_elem(OP_HSLICE,
newOP(OP_PUSHMARK, 0),
newLISTOP(OP_HSLICE, 0,
list($3),
ref(oopsHV($1), OP_HSLICE)));
expect = XOPERATOR; }
| THING %prec '('
{ $$ = $1; }
| amper
{ $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
| amper '(' ')'
{ $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
| amper '(' expr ')'
{ $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
append_elem(OP_LIST, $3, scalar($1))); }
| NOAMP WORD listexpr
{ $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
append_elem(OP_LIST, $3, scalar($2))); }
| DO term %prec UNIOP
{ $$ = newUNOP(OP_DOFILE, 0, scalar($2)); }
| DO block %prec '('
{ $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
| DO WORD '(' ')'
{ $$ = newUNOP(OP_ENTERSUB,
OPf_SPECIAL|OPf_STACKED,
prepend_elem(OP_LIST,
scalar(newCVREF(
(OPpENTERSUB_AMPER<<8),
scalar($2)
)),Nullop)); dep();}
| DO WORD '(' expr ')'
{ $$ = newUNOP(OP_ENTERSUB,
OPf_SPECIAL|OPf_STACKED,
append_elem(OP_LIST,
$4,
scalar(newCVREF(
(OPpENTERSUB_AMPER<<8),
scalar($2)
)))); dep();}
| DO scalar '(' ')'
{ $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
prepend_elem(OP_LIST,
scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
| DO scalar '(' expr ')'
{ $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
prepend_elem(OP_LIST,
$4,
scalar(newCVREF(0,scalar($2))))); dep();}
| LOOPEX
{ $$ = newOP($1, OPf_SPECIAL);
hints |= HINT_BLOCK_SCOPE; }
| LOOPEX term
{ $$ = newLOOPEX($1,$2); }
| NOTOP argexpr
{ $$ = newUNOP(OP_NOT, 0, scalar($2)); }
| UNIOP
{ $$ = newOP($1, 0); }
| UNIOP block
{ $$ = newUNOP($1, 0, $2); }
| UNIOP term
{ $$ = newUNOP($1, 0, $2); }
| UNIOPSUB term
{ $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
append_elem(OP_LIST, $2, scalar($1))); }
| FUNC0
{ $$ = newOP($1, 0); }
| FUNC0 '(' ')'
{ $$ = newOP($1, 0); }
| FUNC0SUB
{ $$ = newUNOP(OP_ENTERSUB, 0,
scalar($1)); }
| FUNC1 '(' ')'
{ $$ = newOP($1, OPf_SPECIAL); }
| FUNC1 '(' expr ')'
{ $$ = newUNOP($1, 0, $3); }
| PMFUNC '(' term ')'
{ $$ = pmruntime($1, $3, Nullop); }
| PMFUNC '(' term ',' term ')'
{ $$ = pmruntime($1, $3, $5); }
| WORD
| listop
;
listexpr: /* NULL */
{ $$ = Nullop; }
| argexpr
{ $$ = $1; }
;
listexprcom: /* NULL */
{ $$ = Nullop; }
| expr
{ $$ = $1; }
| expr ','
{ $$ = $1; }
;
local : LOCAL { $$ = 0; }
| MY { $$ = 1; }
;
my_scalar: scalar
{ in_my = 0; $$ = my($1); }
;
amper : '&' indirob
{ $$ = newCVREF($1,$2); }
;
scalar : '$' indirob
{ $$ = newSVREF($2); }
;
ary : '@' indirob
{ $$ = newAVREF($2); }
;
hsh : '%' indirob
{ $$ = newHVREF($2); }
;
arylen : DOLSHARP indirob
{ $$ = newAVREF($2); }
;
star : '*' indirob
{ $$ = newGVREF(0,$2); }
;
indirob : WORD
{ $$ = scalar($1); }
| scalar
{ $$ = scalar($1); }
| block
{ $$ = scope($1); }
| PRIVATEREF
{ $$ = $1; }
;
%% /* PROGRAM */
|