-
Notifications
You must be signed in to change notification settings - Fork 0
/
awkgram.y
5560 lines (4922 loc) · 131 KB
/
awkgram.y
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* awkgram.y --- yacc/bison parser
*/
/*
* Copyright (C) 1986, 1988, 1989, 1991-2012 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GAWK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
%{
#ifdef GAWKDEBUG
#define YYDEBUG 12
#endif
#include "awk.h"
#if defined(__STDC__) && __STDC__ < 1 /* VMS weirdness, maybe elsewhere */
#define signed /**/
#endif
static void yyerror(const char *m, ...) ATTRIBUTE_PRINTF_1;
static void error_ln(int line, const char *m, ...) ATTRIBUTE_PRINTF_2;
static void lintwarn_ln(int line, const char *m, ...) ATTRIBUTE_PRINTF_2;
static void warning_ln(int line, const char *m, ...) ATTRIBUTE_PRINTF_2;
static char *get_src_buf(void);
static int yylex(void);
int yyparse(void);
static INSTRUCTION *snode(INSTRUCTION *subn, INSTRUCTION *op);
static char **check_params(char *fname, int pcount, INSTRUCTION *list);
static int install_function(char *fname, INSTRUCTION *fi, INSTRUCTION *plist);
static NODE *mk_rexp(INSTRUCTION *exp);
static void param_sanity(INSTRUCTION *arglist);
static int parms_shadow(INSTRUCTION *pc, int *shadow);
static int isnoeffect(OPCODE type);
static INSTRUCTION *make_assignable(INSTRUCTION *ip);
static void dumpintlstr(const char *str, size_t len);
static void dumpintlstr2(const char *str1, size_t len1, const char *str2, size_t len2);
static int include_source(INSTRUCTION *file);
static int load_library(INSTRUCTION *file);
static void next_sourcefile(void);
static char *tokexpand(void);
#define instruction(t) bcalloc(t, 1, 0)
static INSTRUCTION *mk_program(void);
static INSTRUCTION *append_rule(INSTRUCTION *pattern, INSTRUCTION *action);
static INSTRUCTION *mk_function(INSTRUCTION *fi, INSTRUCTION *def);
static INSTRUCTION *mk_condition(INSTRUCTION *cond, INSTRUCTION *ifp, INSTRUCTION *true_branch,
INSTRUCTION *elsep, INSTRUCTION *false_branch);
static INSTRUCTION *mk_expression_list(INSTRUCTION *list, INSTRUCTION *s1);
static INSTRUCTION *mk_for_loop(INSTRUCTION *forp, INSTRUCTION *init, INSTRUCTION *cond,
INSTRUCTION *incr, INSTRUCTION *body);
static void fix_break_continue(INSTRUCTION *list, INSTRUCTION *b_target, INSTRUCTION *c_target);
static INSTRUCTION *mk_binary(INSTRUCTION *s1, INSTRUCTION *s2, INSTRUCTION *op);
static INSTRUCTION *mk_boolean(INSTRUCTION *left, INSTRUCTION *right, INSTRUCTION *op);
static INSTRUCTION *mk_assignment(INSTRUCTION *lhs, INSTRUCTION *rhs, INSTRUCTION *op);
static INSTRUCTION *mk_getline(INSTRUCTION *op, INSTRUCTION *opt_var, INSTRUCTION *redir, int redirtype);
static NODE *make_regnode(int type, NODE *exp);
static int count_expressions(INSTRUCTION **list, bool isarg);
static INSTRUCTION *optimize_assignment(INSTRUCTION *exp);
static void add_lint(INSTRUCTION *list, LINTTYPE linttype);
enum defref { FUNC_DEFINE, FUNC_USE, FUNC_EXT };
static void func_use(const char *name, enum defref how);
static void check_funcs(void);
static ssize_t read_one_line(int fd, void *buffer, size_t count);
static int one_line_close(int fd);
static bool want_source = false;
static bool want_regexp; /* lexical scanning kludge */
static char *in_function; /* parsing kludge */
static int rule = 0;
const char *const ruletab[] = {
"?",
"BEGIN",
"Rule",
"END",
"BEGINFILE",
"ENDFILE",
};
static bool in_print = false; /* lexical scanning kludge for print */
static int in_parens = 0; /* lexical scanning kludge for print */
static int sub_counter = 0; /* array dimension counter for use in delete */
static char *lexptr = NULL; /* pointer to next char during parsing */
static char *lexend;
static char *lexptr_begin; /* keep track of where we were for error msgs */
static char *lexeme; /* beginning of lexeme for debugging */
static bool lexeof; /* seen EOF for current source? */
static char *thisline = NULL;
static int in_braces = 0; /* count braces for firstline, lastline in an 'action' */
static int lastline = 0;
static int firstline = 0;
static SRCFILE *sourcefile = NULL; /* current program source */
static int lasttok = 0;
static bool eof_warned = false; /* GLOBAL: want warning for each file */
static int break_allowed; /* kludge for break */
static int continue_allowed; /* kludge for continue */
#define END_FILE -1000
#define END_SRC -2000
#define YYDEBUG_LEXER_TEXT (lexeme)
static char *tokstart = NULL;
static char *tok = NULL;
static char *tokend;
static int errcount = 0;
extern char *source;
extern int sourceline;
extern SRCFILE *srcfiles;
extern INSTRUCTION *rule_list;
extern int max_args;
extern NODE **args_array;
static INSTRUCTION *rule_block[sizeof(ruletab)];
static INSTRUCTION *ip_rec;
static INSTRUCTION *ip_newfile;
static INSTRUCTION *ip_atexit = NULL;
static INSTRUCTION *ip_end;
static INSTRUCTION *ip_endfile;
static INSTRUCTION *ip_beginfile;
static inline INSTRUCTION *list_create(INSTRUCTION *x);
static inline INSTRUCTION *list_append(INSTRUCTION *l, INSTRUCTION *x);
static inline INSTRUCTION *list_prepend(INSTRUCTION *l, INSTRUCTION *x);
static inline INSTRUCTION *list_merge(INSTRUCTION *l1, INSTRUCTION *l2);
extern double fmod(double x, double y);
#define YYSTYPE INSTRUCTION *
%}
%token FUNC_CALL NAME REGEXP FILENAME
%token YNUMBER YSTRING
%token RELOP IO_OUT IO_IN
%token ASSIGNOP ASSIGN MATCHOP CONCAT_OP
%token SUBSCRIPT
%token LEX_BEGIN LEX_END LEX_IF LEX_ELSE LEX_RETURN LEX_DELETE
%token LEX_SWITCH LEX_CASE LEX_DEFAULT LEX_WHILE LEX_DO LEX_FOR LEX_BREAK LEX_CONTINUE
%token LEX_PRINT LEX_PRINTF LEX_NEXT LEX_EXIT LEX_FUNCTION
%token LEX_BEGINFILE LEX_ENDFILE
%token LEX_GETLINE LEX_NEXTFILE
%token LEX_IN
%token LEX_AND LEX_OR INCREMENT DECREMENT
%token LEX_BUILTIN LEX_LENGTH
%token LEX_EOF
%token LEX_INCLUDE LEX_EVAL LEX_LOAD
%token NEWLINE
/* Lowest to highest */
%right ASSIGNOP ASSIGN SLASH_BEFORE_EQUAL
%right '?' ':'
%left LEX_OR
%left LEX_AND
%left LEX_GETLINE
%nonassoc LEX_IN
%left FUNC_CALL LEX_BUILTIN LEX_LENGTH
%nonassoc ','
%left MATCHOP
%nonassoc RELOP '<' '>' IO_IN IO_OUT
%left CONCAT_OP
%left YSTRING YNUMBER
%left '+' '-'
%left '*' '/' '%'
%right '!' UNARY
%right '^'
%left INCREMENT DECREMENT
%left '$'
%left '(' ')'
%%
program
: /* empty */
| program rule
{
rule = 0;
yyerrok;
}
| program nls
| program LEX_EOF
{
next_sourcefile();
}
| program error
{
rule = 0;
/*
* If errors, give up, don't produce an infinite
* stream of syntax error messages.
*/
/* yyerrok; */
}
;
rule
: pattern action
{
(void) append_rule($1, $2);
}
| pattern statement_term
{
if (rule != Rule) {
msg(_("%s blocks must have an action part"), ruletab[rule]);
errcount++;
} else if ($1 == NULL) {
msg(_("each rule must have a pattern or an action part"));
errcount++;
} else /* pattern rule with non-empty pattern */
(void) append_rule($1, NULL);
}
| function_prologue action
{
in_function = NULL;
(void) mk_function($1, $2);
yyerrok;
}
| '@' LEX_INCLUDE source statement_term
{
want_source = false;
yyerrok;
}
| '@' LEX_LOAD library statement_term
{
want_source = false;
yyerrok;
}
;
source
: FILENAME
{
if (include_source($1) < 0)
YYABORT;
efree($1->lextok);
bcfree($1);
$$ = NULL;
}
| FILENAME error
{ $$ = NULL; }
| error
{ $$ = NULL; }
;
library
: FILENAME
{
if (load_library($1) < 0)
YYABORT;
efree($1->lextok);
bcfree($1);
$$ = NULL;
}
| FILENAME error
{ $$ = NULL; }
| error
{ $$ = NULL; }
;
pattern
: /* empty */
{ $$ = NULL; rule = Rule; }
| exp
{ $$ = $1; rule = Rule; }
| exp ',' opt_nls exp
{
INSTRUCTION *tp;
add_lint($1, LINT_assign_in_cond);
add_lint($4, LINT_assign_in_cond);
tp = instruction(Op_no_op);
list_prepend($1, bcalloc(Op_line_range, !!do_pretty_print + 1, 0));
$1->nexti->triggered = false;
$1->nexti->target_jmp = $4->nexti;
list_append($1, instruction(Op_cond_pair));
$1->lasti->line_range = $1->nexti;
$1->lasti->target_jmp = tp;
list_append($4, instruction(Op_cond_pair));
$4->lasti->line_range = $1->nexti;
$4->lasti->target_jmp = tp;
if (do_pretty_print) {
($1->nexti + 1)->condpair_left = $1->lasti;
($1->nexti + 1)->condpair_right = $4->lasti;
}
$$ = list_append(list_merge($1, $4), tp);
rule = Rule;
}
| LEX_BEGIN
{
static int begin_seen = 0;
if (do_lint_old && ++begin_seen == 2)
warning_ln($1->source_line,
_("old awk does not support multiple `BEGIN' or `END' rules"));
$1->in_rule = rule = BEGIN;
$1->source_file = source;
$$ = $1;
}
| LEX_END
{
static int end_seen = 0;
if (do_lint_old && ++end_seen == 2)
warning_ln($1->source_line,
_("old awk does not support multiple `BEGIN' or `END' rules"));
$1->in_rule = rule = END;
$1->source_file = source;
$$ = $1;
}
| LEX_BEGINFILE
{
$1->in_rule = rule = BEGINFILE;
$1->source_file = source;
$$ = $1;
}
| LEX_ENDFILE
{
$1->in_rule = rule = ENDFILE;
$1->source_file = source;
$$ = $1;
}
;
action
: l_brace statements r_brace opt_semi opt_nls
{
if ($2 == NULL)
$$ = list_create(instruction(Op_no_op));
else
$$ = $2;
}
;
func_name
: NAME
{ $$ = $1; }
| FUNC_CALL
{ $$ = $1; }
| lex_builtin
{
yyerror(_("`%s' is a built-in function, it cannot be redefined"),
tokstart);
YYABORT;
}
| '@' LEX_EVAL
{ $$ = $2; }
;
lex_builtin
: LEX_BUILTIN
| LEX_LENGTH
;
function_prologue
: LEX_FUNCTION func_name '(' opt_param_list r_paren opt_nls
{
$1->source_file = source;
if (install_function($2->lextok, $1, $4) < 0)
YYABORT;
in_function = $2->lextok;
$2->lextok = NULL;
bcfree($2);
/* $4 already free'd in install_function */
$$ = $1;
}
;
regexp
/*
* In this rule, want_regexp tells yylex that the next thing
* is a regexp so it should read up to the closing slash.
*/
: a_slash
{ ++want_regexp; }
REGEXP /* The terminating '/' is consumed by yylex(). */
{
NODE *n, *exp;
char *re;
size_t len;
re = $3->lextok;
$3->lextok = NULL;
len = strlen(re);
if (do_lint) {
if (len == 0)
lintwarn_ln($3->source_line,
_("regexp constant `//' looks like a C++ comment, but is not"));
else if (re[0] == '*' && re[len-1] == '*')
/* possible C comment */
lintwarn_ln($3->source_line,
_("regexp constant `/%s/' looks like a C comment, but is not"), re);
}
exp = make_str_node(re, len, ALREADY_MALLOCED);
n = make_regnode(Node_regex, exp);
if (n == NULL) {
unref(exp);
YYABORT;
}
$$ = $3;
$$->opcode = Op_match_rec;
$$->memory = n;
}
;
a_slash
: '/'
{ bcfree($1); }
| SLASH_BEFORE_EQUAL
;
statements
: /* empty */
{ $$ = NULL; }
| statements statement
{
if ($2 == NULL)
$$ = $1;
else {
add_lint($2, LINT_no_effect);
if ($1 == NULL)
$$ = $2;
else
$$ = list_merge($1, $2);
}
yyerrok;
}
| statements error
{ $$ = NULL; }
;
statement_term
: nls
| semi opt_nls
;
statement
: semi opt_nls
{ $$ = NULL; }
| l_brace statements r_brace
{ $$ = $2; }
| if_statement
{
if (do_pretty_print)
$$ = list_prepend($1, instruction(Op_exec_count));
else
$$ = $1;
}
| LEX_SWITCH '(' exp r_paren opt_nls l_brace case_statements opt_nls r_brace
{
INSTRUCTION *dflt, *curr = NULL, *cexp, *cstmt;
INSTRUCTION *ip, *nextc, *tbreak;
const char **case_values = NULL;
int maxcount = 128;
int case_count = 0;
int i;
tbreak = instruction(Op_no_op);
cstmt = list_create(tbreak);
cexp = list_create(instruction(Op_pop));
dflt = instruction(Op_jmp);
dflt->target_jmp = tbreak; /* if no case match and no explicit default */
if ($7 != NULL) {
curr = $7->nexti;
bcfree($7); /* Op_list */
} /* else
curr = NULL; */
for(; curr != NULL; curr = nextc) {
INSTRUCTION *caseexp = curr->case_exp;
INSTRUCTION *casestmt = curr->case_stmt;
nextc = curr->nexti;
if (curr->opcode == Op_K_case) {
if (caseexp->opcode == Op_push_i) {
/* a constant scalar */
char *caseval;
caseval = force_string(caseexp->memory)->stptr;
for (i = 0; i < case_count; i++) {
if (strcmp(caseval, case_values[i]) == 0)
error_ln(curr->source_line,
_("duplicate case values in switch body: %s"), caseval);
}
if (case_values == NULL)
emalloc(case_values, const char **, sizeof(char *) * maxcount, "statement");
else if (case_count >= maxcount) {
maxcount += 128;
erealloc(case_values, const char **, sizeof(char*) * maxcount, "statement");
}
case_values[case_count++] = caseval;
} else {
/* match a constant regex against switch expression. */
(curr + 1)->match_exp = true;
}
curr->stmt_start = casestmt->nexti;
curr->stmt_end = casestmt->lasti;
(void) list_prepend(cexp, curr);
(void) list_prepend(cexp, caseexp);
} else {
if (dflt->target_jmp != tbreak)
error_ln(curr->source_line,
_("duplicate `default' detected in switch body"));
else
dflt->target_jmp = casestmt->nexti;
if (do_pretty_print) {
curr->stmt_start = casestmt->nexti;
curr->stmt_end = casestmt->lasti;
(void) list_prepend(cexp, curr);
} else
bcfree(curr);
}
cstmt = list_merge(casestmt, cstmt);
}
if (case_values != NULL)
efree(case_values);
ip = $3;
if (do_pretty_print) {
(void) list_prepend(ip, $1);
(void) list_prepend(ip, instruction(Op_exec_count));
$1->target_break = tbreak;
($1 + 1)->switch_start = cexp->nexti;
($1 + 1)->switch_end = cexp->lasti;
}/* else
$1 is NULL */
(void) list_append(cexp, dflt);
(void) list_merge(ip, cexp);
$$ = list_merge(ip, cstmt);
break_allowed--;
fix_break_continue(ip, tbreak, NULL);
}
| LEX_WHILE '(' exp r_paren opt_nls statement
{
/*
* -----------------
* tc:
* cond
* -----------------
* [Op_jmp_false tb ]
* -----------------
* body
* -----------------
* [Op_jmp tc ]
* tb:[Op_no_op ]
*/
INSTRUCTION *ip, *tbreak, *tcont;
tbreak = instruction(Op_no_op);
add_lint($3, LINT_assign_in_cond);
tcont = $3->nexti;
ip = list_append($3, instruction(Op_jmp_false));
ip->lasti->target_jmp = tbreak;
if (do_pretty_print) {
(void) list_append(ip, instruction(Op_exec_count));
$1->target_break = tbreak;
$1->target_continue = tcont;
($1 + 1)->while_body = ip->lasti;
(void) list_prepend(ip, $1);
}/* else
$1 is NULL */
if ($6 != NULL)
(void) list_merge(ip, $6);
(void) list_append(ip, instruction(Op_jmp));
ip->lasti->target_jmp = tcont;
$$ = list_append(ip, tbreak);
break_allowed--;
continue_allowed--;
fix_break_continue(ip, tbreak, tcont);
}
| LEX_DO opt_nls statement LEX_WHILE '(' exp r_paren opt_nls
{
/*
* -----------------
* z:
* body
* -----------------
* tc:
* cond
* -----------------
* [Op_jmp_true | z ]
* tb:[Op_no_op ]
*/
INSTRUCTION *ip, *tbreak, *tcont;
tbreak = instruction(Op_no_op);
tcont = $6->nexti;
add_lint($6, LINT_assign_in_cond);
if ($3 != NULL)
ip = list_merge($3, $6);
else
ip = list_prepend($6, instruction(Op_no_op));
if (do_pretty_print)
(void) list_prepend(ip, instruction(Op_exec_count));
(void) list_append(ip, instruction(Op_jmp_true));
ip->lasti->target_jmp = ip->nexti;
$$ = list_append(ip, tbreak);
break_allowed--;
continue_allowed--;
fix_break_continue(ip, tbreak, tcont);
if (do_pretty_print) {
$1->target_break = tbreak;
$1->target_continue = tcont;
($1 + 1)->doloop_cond = tcont;
$$ = list_prepend(ip, $1);
bcfree($4);
} /* else
$1 and $4 are NULLs */
}
| LEX_FOR '(' NAME LEX_IN simple_variable r_paren opt_nls statement
{
INSTRUCTION *ip;
char *var_name = $3->lextok;
if ($8 != NULL
&& $8->lasti->opcode == Op_K_delete
&& $8->lasti->expr_count == 1
&& $8->nexti->opcode == Op_push
&& ($8->nexti->memory->type != Node_var || !($8->nexti->memory->var_update))
&& strcmp($8->nexti->memory->vname, var_name) == 0
) {
/* Efficiency hack. Recognize the special case of
*
* for (iggy in foo)
* delete foo[iggy]
*
* and treat it as if it were
*
* delete foo
*
* Check that the body is a `delete a[i]' statement,
* and that both the loop var and array names match.
*/
NODE *arr = NULL;
ip = $8->nexti->nexti;
if ($5->nexti->opcode == Op_push && $5->lasti == $5->nexti)
arr = $5->nexti->memory;
if (arr != NULL
&& ip->opcode == Op_no_op
&& ip->nexti->opcode == Op_push_array
&& strcmp(ip->nexti->memory->vname, arr->vname) == 0
&& ip->nexti->nexti == $8->lasti
) {
(void) make_assignable($8->nexti);
$8->lasti->opcode = Op_K_delete_loop;
$8->lasti->expr_count = 0;
if ($1 != NULL)
bcfree($1);
efree(var_name);
bcfree($3);
bcfree($4);
bcfree($5);
$$ = $8;
} else
goto regular_loop;
} else {
INSTRUCTION *tbreak, *tcont;
/* [ Op_push_array a ]
* [ Op_arrayfor_init | ib ]
* ic:[ Op_arrayfor_incr | ib ]
* [ Op_var_assign if any ]
*
* body
*
* [Op_jmp | ic ]
* ib:[Op_arrayfor_final ]
*/
regular_loop:
ip = $5;
ip->nexti->opcode = Op_push_array;
tbreak = instruction(Op_arrayfor_final);
$4->opcode = Op_arrayfor_incr;
$4->array_var = variable($3->source_line, var_name, Node_var);
$4->target_jmp = tbreak;
tcont = $4;
$3->opcode = Op_arrayfor_init;
$3->target_jmp = tbreak;
(void) list_append(ip, $3);
if (do_pretty_print) {
$1->opcode = Op_K_arrayfor;
$1->target_continue = tcont;
$1->target_break = tbreak;
(void) list_append(ip, $1);
} /* else
$1 is NULL */
/* add update_FOO instruction if necessary */
if ($4->array_var->type == Node_var && $4->array_var->var_update) {
(void) list_append(ip, instruction(Op_var_update));
ip->lasti->update_var = $4->array_var->var_update;
}
(void) list_append(ip, $4);
/* add set_FOO instruction if necessary */
if ($4->array_var->type == Node_var && $4->array_var->var_assign) {
(void) list_append(ip, instruction(Op_var_assign));
ip->lasti->assign_var = $4->array_var->var_assign;
}
if (do_pretty_print) {
(void) list_append(ip, instruction(Op_exec_count));
($1 + 1)->forloop_cond = $4;
($1 + 1)->forloop_body = ip->lasti;
}
if ($8 != NULL)
(void) list_merge(ip, $8);
(void) list_append(ip, instruction(Op_jmp));
ip->lasti->target_jmp = $4;
$$ = list_append(ip, tbreak);
fix_break_continue(ip, tbreak, tcont);
}
break_allowed--;
continue_allowed--;
}
| LEX_FOR '(' opt_simple_stmt semi opt_nls exp semi opt_nls opt_simple_stmt r_paren opt_nls statement
{
$$ = mk_for_loop($1, $3, $6, $9, $12);
break_allowed--;
continue_allowed--;
}
| LEX_FOR '(' opt_simple_stmt semi opt_nls semi opt_nls opt_simple_stmt r_paren opt_nls statement
{
$$ = mk_for_loop($1, $3, (INSTRUCTION *) NULL, $8, $11);
break_allowed--;
continue_allowed--;
}
| non_compound_stmt
{
if (do_pretty_print)
$$ = list_prepend($1, instruction(Op_exec_count));
else
$$ = $1;
}
;
non_compound_stmt
: LEX_BREAK statement_term
{
if (! break_allowed)
error_ln($1->source_line,
_("`break' is not allowed outside a loop or switch"));
$1->target_jmp = NULL;
$$ = list_create($1);
}
| LEX_CONTINUE statement_term
{
if (! continue_allowed)
error_ln($1->source_line,
_("`continue' is not allowed outside a loop"));
$1->target_jmp = NULL;
$$ = list_create($1);
}
| LEX_NEXT statement_term
{
/* if inside function (rule = 0), resolve context at run-time */
if (rule && rule != Rule)
error_ln($1->source_line,
_("`next' used in %s action"), ruletab[rule]);
$1->target_jmp = ip_rec;
$$ = list_create($1);
}
| LEX_NEXTFILE statement_term
{
/* if inside function (rule = 0), resolve context at run-time */
if (rule == BEGIN || rule == END || rule == ENDFILE)
error_ln($1->source_line,
_("`nextfile' used in %s action"), ruletab[rule]);
$1->target_newfile = ip_newfile;
$1->target_endfile = ip_endfile;
$$ = list_create($1);
}
| LEX_EXIT opt_exp statement_term
{
/* Initialize the two possible jump targets, the actual target
* is resolved at run-time.
*/
$1->target_end = ip_end; /* first instruction in end_block */
$1->target_atexit = ip_atexit; /* cleanup and go home */
if ($2 == NULL) {
$$ = list_create($1);
(void) list_prepend($$, instruction(Op_push_i));
$$->nexti->memory = dupnode(Nnull_string);
} else
$$ = list_append($2, $1);
}
| LEX_RETURN
{
if (! in_function)
yyerror(_("`return' used outside function context"));
} opt_exp statement_term {
if ($3 == NULL) {
$$ = list_create($1);
(void) list_prepend($$, instruction(Op_push_i));
$$->nexti->memory = dupnode(Nnull_string);
} else {
if (do_optimize > 1
&& $3->lasti->opcode == Op_func_call
&& strcmp($3->lasti->func_name, in_function) == 0
) {
/* Do tail recursion optimization. Tail
* call without a return value is recognized
* in mk_function().
*/
($3->lasti + 1)->tail_call = true;
}
$$ = list_append($3, $1);
}
}
| simple_stmt statement_term
;
/*
* A simple_stmt exists to satisfy a constraint in the POSIX
* grammar allowing them to occur as the 1st and 3rd parts
* in a `for (...;...;...)' loop. This is a historical oddity
* inherited from Unix awk, not at all documented in the AK&W
* awk book. We support it, as this was reported as a bug.
* We don't bother to document it though. So there.
*/
simple_stmt
: print { in_print = true; in_parens = 0; } print_expression_list output_redir
{
/*
* Optimization: plain `print' has no expression list, so $3 is null.
* If $3 is NULL or is a bytecode list for $0 use Op_K_print_rec,
* which is faster for these two cases.
*/
if ($1->opcode == Op_K_print &&
($3 == NULL
|| ($3->lasti->opcode == Op_field_spec
&& $3->nexti->nexti->nexti == $3->lasti
&& $3->nexti->nexti->opcode == Op_push_i
&& $3->nexti->nexti->memory->type == Node_val)
)
) {
static bool warned = false;
/* -----------------
* output_redir
* [ redirect exp ]
* -----------------
* expression_list
* ------------------
* [Op_K_print_rec | NULL | redir_type | expr_count]
*/
if ($3 != NULL) {
NODE *n = $3->nexti->nexti->memory;
if (! iszero(n))
goto regular_print;
bcfree($3->lasti); /* Op_field_spec */
unref(n); /* Node_val */
bcfree($3->nexti->nexti); /* Op_push_i */
bcfree($3->nexti); /* Op_list */
bcfree($3); /* Op_list */
} else {
if (do_lint && (rule == BEGIN || rule == END) && ! warned) {
warned = true;
lintwarn_ln($1->source_line,
_("plain `print' in BEGIN or END rule should probably be `print \"\"'"));
}
}
$1->expr_count = 0;
$1->opcode = Op_K_print_rec;
if ($4 == NULL) { /* no redircetion */
$1->redir_type = redirect_none;
$$ = list_create($1);
} else {
INSTRUCTION *ip;
ip = $4->nexti;
$1->redir_type = ip->redir_type;
$4->nexti = ip->nexti;
bcfree(ip);
$$ = list_append($4, $1);
}
} else {
/* -----------------
* [ output_redir ]
* [ redirect exp ]
* -----------------
* [ expression_list ]
* ------------------
* [$1 | NULL | redir_type | expr_count]
*
*/
regular_print:
if ($4 == NULL) { /* no redirection */
if ($3 == NULL) { /* printf without arg */
$1->expr_count = 0;
$1->redir_type = redirect_none;
$$ = list_create($1);
} else {
INSTRUCTION *t = $3;
$1->expr_count = count_expressions(&t, false);
$1->redir_type = redirect_none;
$$ = list_append(t, $1);
}
} else {
INSTRUCTION *ip;
ip = $4->nexti;
$1->redir_type = ip->redir_type;
$4->nexti = ip->nexti;
bcfree(ip);
if ($3 == NULL) {
$1->expr_count = 0;
$$ = list_append($4, $1);
} else {
INSTRUCTION *t = $3;
$1->expr_count = count_expressions(&t, false);
$$ = list_append(list_merge($4, t), $1);
}
}
}
}
| LEX_DELETE NAME { sub_counter = 0; } delete_subscript_list
{
char *arr = $2->lextok;
$2->opcode = Op_push_array;
$2->memory = variable($2->source_line, arr, Node_var_new);
if (! do_posix && ! do_traditional) {
if ($2->memory == symbol_table)
fatal(_("`delete' is not allowed with SYMTAB"));
else if ($2->memory == func_table)
fatal(_("`delete' is not allowed with FUNCTAB"));
}
if ($4 == NULL) {
/*
* As of September 2012, POSIX has added support
* for `delete array'. See:
* http://austingroupbugs.net/view.php?id=544
*
* Thanks to Nathan Weeks for the initiative.
*
* Thus we no longer warn or check do_posix.
* Also, since BWK awk supports it, we don't have to
* check do_traditional either.
*/
$1->expr_count = 0;
$$ = list_append(list_create($2), $1);
} else {
$1->expr_count = sub_counter;
$$ = list_append(list_append($4, $2), $1);
}