-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrcgram.y
1628 lines (1426 loc) · 40.2 KB
/
srcgram.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
%{
/*
======================================================================
CTREE Version 0.09
Written by Shaun Flisakowski (1995)
======================================================================
This program is provided free of charge on an "as is" basis without
warranty of any kind, either express or implied. Acceptance and use
of this program constitutes the user's understanding that (s)he will
have no recourse for any actual or consequential damages, including,
but not limited to, lost profits or savings, arising out of the use
of or inability to use this program.
======================================================================
*/
/* grammar File for C - Shaun Flisakowski */
/* Grammar was constructed with the assistance of:
"C - A Reference Manual" (Fourth Edition),
by Samuel P Harbison, and Guy L Steele Jr. */
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
#include <setjmp.h>
#include "lexer.h"
#include "tree.h"
#include "symtab.h"
#include "token.h"
#include "globals.h"
extern "C" void yyerror(char *);
extern "C" int errno;
extern "C" int err_cnt;
extern "C" int yylex(YYSTYPE *lvalp);
extern "C" int yyparse (void);
treenode *parse_include(char *filename);
static void insert_decl(leafnode *leaf, treenode *def, treenode*);
static void insert_typedef(leafnode *leaf, treenode *def, treenode*);
static void insert_component(leafnode *leaf, treenode *def,
treenode *container);
static void add_params_to_symtab(treenode *funcdecl);
/* Cause the `yydebug' variable to be defined. */
#define YYDEBUG 1
/* int yydebug = 1; */
/* ###################################################### */
%}
/* The next line makes the parser re-entrant. */
%pure_parser
%start program
%token <leaf> IDENT STRING FIELD_NAME TYPEDEF_NAME TAG
%token <leaf> CHAR_CONST
%token <leaf> INUM
%token <leaf> RNUM
%token <leaf> COMMENT
%token <leaf> PP_LINE PP_INCLUDE PP_DEFINE PP_UNDEF PP_ERROR
%token <leaf> PP_IF PP_IFDEF PP_IFNDEF PP_ELSE PP_ELIF PP_ENDIF
%token <leaf> PP_IDENT PP_PRAGMA
%token <tok> INVALID
/* the reserved words */
%token <node> AUTO BREAK CASE CHAR CONST CONT DEFLT DO DOUBLE ELSE ENUM EXTRN
%token <ifn> IF
%token <forn> FOR
%token <node> FLOAT GOTO INT LONG REGISTR RETURN SHORT SGNED
%token <node> STATIC STRUCT SWITCH TYPEDEF UNION UNSGNED VOID VOLATILE WHILE
%token <node> PLUS_EQ MINUS_EQ STAR_EQ DIV_EQ MOD_EQ
%token <node> B_NOT_EQ B_AND_EQ B_OR_EQ B_XOR_EQ
%token <node> L_SHIFT_EQ R_SHIFT_EQ
%token <node> EQUAL LESS_EQ GRTR_EQ NOT_EQ
%token <node> RPAREN RBRCKT LBRACE RBRACE
%token <node> SEMICOLON COMMA ELLIPSIS
%token <node> LB_SIGN DOUB_LB_SIGN
%token <node> BACKQUOTE AT DOLLAR
%token <node> CPP_INCLUDE CPP_DEFINE CPP_LINE
/* ParC extension */
%token <node> PARBLOCK PARFOR
/* Add precedence rules to solve dangling else s/r conflict */
%nonassoc IF
%nonassoc ELSE
/* Define the operator tokens and their precedences. */
%left COMMA_OP
%right <node> EQ ASSIGN
%right <node> QUESTMARK COLON COMMA_SEP
%left <node> OR
%left <node> AND
%left <node> B_OR
%left <node> B_XOR
%left <node> B_AND
%left <node> COMP_EQ
%left <node> COMP_ARITH LESS GRTR
%left <node> L_SHIFT R_SHIFT
%left <node> PLUS MINUS
%left <node> STAR DIV MOD
%right CAST
%right <node> UNARY NOT B_NOT SIZEOF INCR DECR
%left HYPERUNARY
%left <node> ARROW DOT LPAREN LBRCKT
%type <node> declaration decl_specs opt_decl_specs
%type <node> storage_class type_spec type_qual
%type <node> opt_init_decl_list init_decl_list init_decl
%type <node> declarator opt_declarator
%type <node> direct_declarator opt_comma
%type <node> pointer pointer_start
%type <node> simple_decl type_qual_list opt_type_qual_list
%type <node> decl_list opt_decl_list
%type <leaf> constant
%type <node> comp_decl_specs opt_comp_decl_specs
%type <node> param_list param_decl
%type <node> opt_param_type_list param_type_list
%type <node> ident_list
%type <leaf> ident
%type <node> field_ident
%type <node> abs_decl
%type <node> direct_abs_decl
%type <node> array_decl opt_const_expr const_expr expr
%type <node> comma_expr assign_expr
%type <node> prim_expr paren_expr postfix_expr
%type <node> subscript_expr comp_select_expr postinc_expr postdec_expr
%type <node> func_call opt_expr opt_expr_list expr_list
%type <node> top_level_decl func_def func_spec cmpnd_stemnt
%type <node> stemnt_list opt_stemnt_list stemnt
%type <node> expr_stemnt labeled_stemnt cond_stemnt
%type <node> opt_comment
%type <node> iter_stemnt switch_stemnt break_stemnt continue_stemnt
%type <node> return_stemnt goto_stemnt null_stemnt
%type <node> do_stemnt while_stemnt for_stemnt
%type <node> cond_expr if_stemnt if_else_stemnt
%type <node> log_or_expr log_and_expr log_neg_expr
%type <node> bitwise_or_expr bitwise_and_expr bitwise_neg_expr
%type <node> bitwise_xor_expr cast_expr equality_expr
%type <node> relational_expr shift_expr additive_expr mult_expr
%type <node> unary_expr unary_minus_expr unary_plus_expr
%type <node> sizeof_expr addr_expr indirection_expr
%type <node> preinc_expr predec_expr
%type <node> direct_comp_select indirect_comp_select
%type <node> add_op mult_op equality_op relation_op shift_op assign_op
%type <node> label named_label case_label deflt_label
%type <node> type_name typedef_name typename_as_ident
%type <node> enum_type_spec struct_type_spec union_type_spec
%type <node> tag opt_tag
%type <node> opt_trailing_comma
%type <node> enum_type_define enum_type_ref enum_def_list
%type <node> enum_const_def enum_constant
%type <node> struct_type_define struct_type_ref field_list
%type <node> union_type_define union_type_ref
%type <node> comp_decl comp_decl_list comp_declarator
%type <node> simple_comp bit_field width
%type <node> initializer_list initializer
%type <node> program trans_unit
%type <node> parblock_stemnt parblock_body parblock_block
%{
/* 1 if we explained undeclared var errors. */
/* static int undeclared_variable_notice = 0; */
%}
%%
program: /* emtpy source file */
{
if (err_cnt == 0)
fputs("Warning: ANSI/ISO C forbids an empty source file.\n",
stderr);
Parse_TOS->parse_tree= (treenode *) NULL;
$$ = (treenode *) NULL;
}
| trans_unit
{
if (err_cnt) {
fprintf(stderr,"%d Errors found.\n",err_cnt);
Parse_TOS->parse_tree = (treenode *) NULL;
} else {
fputs("No Errors encountered.\n",stderr);
Parse_TOS->parse_tree = $$;
}
}
| error
{
fputs("Errors - Aborting parse.\n",stderr);
Parse_TOS->parse_tree= (treenode *) NULL;
YYABORT;
}
trans_unit: top_level_decl
| trans_unit top_level_decl
{
treenode *tmp_node = make_node(TN_TRANS_LIST, ParseStack->contxt);
tmp_node->lnode = $1;
tmp_node->rnode = $2;
$$ = tmp_node;
}
top_level_decl: declaration
{
/* Safety precaution. */
exit_scopes(ParseStack->contxt, FILE_SCOPE);
}
| func_def
{
/* Safety precaution. */
exit_scopes(ParseStack->contxt, FILE_SCOPE);
}
| error SEMICOLON
{
free_tree($2);
$$ = (treenode *) NULL;
}
| error RBRACE
{
free_tree($2);
$$ = (treenode *) NULL;
}
func_def: func_spec cmpnd_stemnt
{
leafnode *lm, *rm;
for_node *tmpnode;
tmpnode = (for_node *) $1;
tmpnode->stemnt = $2;
if (ParseStack->contxt)
{
lm = leftmost($$);
rm = find_func_name($$);
if (rm)
{
if (lm && (lm->hdr.tok == STATIC))
{
if (! symtab_insert_at(ParseStack->contxt->syms,
mk_funcdef(rm->data.sval, $$), FILE_SCOPE))
yyerr("Duplicate function.");
}
else
{
if (! symtab_insert_at(ParseStack->contxt->syms,
mk_funcdef(rm->data.sval, $$), EXTERN_SCOPE))
yyerr("Duplicate function.");
}
}
}
/* This is the scope that starts in func_spec */
exit_scope(ParseStack->contxt);
}
enter_scope:
{
enter_scope(ParseStack->contxt);
}
func_spec: decl_specs declarator opt_decl_list
{
for_node *tmp_node = make_for(TN_FUNC_DEF, ParseStack->contxt);
tmp_node->init = $1;
tmp_node->test = $2;
tmp_node->incr = $3;
add_params_to_symtab($2);
$$ = (treenode *) tmp_node;
}
| declarator opt_decl_list
{
/* return type defaults to int */
for_node *tmp_node = make_for(TN_FUNC_DEF, ParseStack->contxt);
tmp_node->init = (treenode *) NULL;
tmp_node->test = $1;
tmp_node->incr = $2;
add_params_to_symtab($1);
$$ = (treenode *) tmp_node;
}
opt_decl_list: /* Nothing */
{
$$ = (treenode *) NULL;
}
| decl_list
;
decl_list: declaration
| decl_list declaration
{
treenode *tmp_node = make_node(TN_DECL_LIST, ParseStack->contxt);
tmp_node->lnode = $1;
tmp_node->rnode = $2;
$$ = tmp_node;
}
cmpnd_stemnt: LBRACE enter_scope
opt_decl_list opt_stemnt_list RBRACE
{
$1->hdr.type = TN_BLOCK;
$1->lnode = $3;
$1->rnode = $4;
free_tree($5);
exit_scope(ParseStack->contxt);
}
| error RBRACE
{
$$ = (treenode *) NULL;
free_tree($2);
}
opt_stemnt_list: /* Nothing */
{
$$ = (treenode *) NULL;
}
| stemnt_list
;
stemnt_list: stemnt
{
treenode *tmp_node = make_node(TN_STEMNT_LIST, ParseStack->contxt);
tmp_node->lnode = $1;
tmp_node->rnode = NULL;
$$ = tmp_node;
}
| stemnt_list stemnt
{
treenode *tmp_node = make_node(TN_STEMNT_LIST, ParseStack->contxt);
tmp_node->lnode = $1;
tmp_node->rnode = $2;
$$ = tmp_node;
}
stemnt: expr_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| labeled_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| cmpnd_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| cond_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| iter_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| switch_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
malloc(10);
$$ = tmp_node;
}
| break_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| continue_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| return_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| goto_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| parblock_stemnt
{
treenode *tmp_node = make_node(TN_PARBLOCK, ParseStack->contxt);
tmp_node->lnode = 0;
tmp_node->rnode = $1;
$$ = tmp_node;
}
| null_stemnt
{
treenode *tmp_node = make_node(TN_STEMNT, ParseStack->contxt);
tmp_node->rnode = $1;
$$ = tmp_node;
}
| error SEMICOLON
{
$$ = (treenode *) NULL;
free_tree($2);
}
parblock_stemnt : PARBLOCK parblock_body
{
$$ = $2;
free_tree($1);
}
;
parblock_body : parblock_block
{
$$ = make_node(TN_PARBLOCK_EMPTY, ParseStack->contxt);
$$->lnode = $1;
}
| parblock_body COLON parblock_block
{
free_tree($2);
$$ = make_node(TN_PARBLOCK_EMPTY, ParseStack->contxt);
$$->lnode = $3;
$$->rnode = $1;
}
;
parblock_block : cmpnd_stemnt
{
$$ = $1;
}
;
expr_stemnt: expr SEMICOLON opt_comment
{
free_tree($2);
}
labeled_stemnt: label COLON stemnt
{
$2->hdr.type = TN_LABEL;
$2->lnode = (treenode *) $1;
$2->rnode = $3;
$$ = $2;
}
cond_stemnt: if_stemnt
| if_else_stemnt
;
iter_stemnt: do_stemnt
| while_stemnt
| for_stemnt
;
switch_stemnt: SWITCH LPAREN expr RPAREN stemnt
{
$1->hdr.type = TN_SWITCH;
$1->lnode = (treenode *) $3;
$1->rnode = (treenode *) $5;
free_tree($2);
free_tree($4);
}
break_stemnt: BREAK SEMICOLON
{
$1->hdr.type = TN_JUMP;
free_tree($2);
}
continue_stemnt: CONT SEMICOLON
{
$1->hdr.type = TN_JUMP;
free_tree($2);
}
return_stemnt: RETURN opt_expr SEMICOLON
{
$1->hdr.type = TN_JUMP;
$1->lnode = $2;
free_tree($3);
}
goto_stemnt: GOTO ident SEMICOLON
{
$1->hdr.type = TN_JUMP;
$1->lnode = (treenode *) $2;
free_tree($3);
}
null_stemnt: SEMICOLON
{
$$ = (treenode *) NULL;
free_tree($1);
}
if_stemnt: IF LPAREN expr RPAREN stemnt %prec IF
{
$1->hdr.type = TN_IF;
$1->cond = $3;
$1->then_n = $5;
$$ = (treenode *) $1;
free_tree($2);
free_tree($4);
}
if_else_stemnt: IF LPAREN expr RPAREN stemnt ELSE stemnt
{
$1->hdr.type = TN_IF;
$1->cond = $3;
$1->then_n = $5;
$1->else_n = $7;
$$ = (treenode *) $1;
free_tree($2);
free_tree($4);
free_tree($6);
}
do_stemnt: DO stemnt WHILE LPAREN expr RPAREN SEMICOLON
{
$1->hdr.type = TN_DOWHILE;
$1->lnode = $5;
$1->rnode = $2;
free_tree($3);
free_tree($4);
free_tree($6);
free_tree($7);
}
while_stemnt: WHILE LPAREN expr RPAREN stemnt
{
$1->hdr.type = TN_WHILE;
$1->lnode = $3;
$1->rnode = $5;
free_tree($2);
free_tree($4);
}
for_stemnt: FOR LPAREN opt_expr SEMICOLON
opt_expr SEMICOLON opt_expr RPAREN stemnt
{
$1->hdr.type = TN_FOR;
$1->init = $3;
$1->test = $5;
$1->incr = $7;
$1->stemnt = $9;
free_tree($2);
free_tree($4);
free_tree($6);
free_tree($8);
}
label: named_label
| case_label
| deflt_label
;
cond_expr: log_or_expr
| log_or_expr QUESTMARK expr COLON cond_expr
{
if_node *tmpnode = make_if(TN_COND_EXPR, ParseStack->contxt);
tmpnode->cond = $1;
tmpnode->then_n = $3;
tmpnode->else_n = $5;
$$ = (treenode *) tmpnode;
free_tree($2);
free_tree($4);
}
log_or_expr: log_and_expr
| log_or_expr OR log_and_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
log_and_expr: bitwise_or_expr
| log_and_expr AND bitwise_or_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
log_neg_expr: NOT cast_expr
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
}
bitwise_or_expr: bitwise_xor_expr
| bitwise_or_expr B_OR bitwise_xor_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
bitwise_xor_expr: bitwise_and_expr
| bitwise_xor_expr B_XOR bitwise_and_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
bitwise_and_expr: equality_expr
| bitwise_and_expr B_AND equality_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
bitwise_neg_expr: B_NOT cast_expr
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
}
cast_expr: unary_expr
| LPAREN type_name RPAREN cast_expr %prec CAST
{
$1->hdr.type = TN_CAST;
$1->lnode = $2;
$1->rnode = $4;
free_tree($3);
}
equality_expr: relational_expr
| equality_expr equality_op relational_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
relational_expr: shift_expr
| relational_expr relation_op shift_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
shift_expr: additive_expr
| shift_expr shift_op additive_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
additive_expr: mult_expr
| additive_expr add_op mult_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
mult_expr: cast_expr
| mult_expr mult_op cast_expr
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
unary_expr: postfix_expr
| sizeof_expr
| unary_minus_expr
| unary_plus_expr
| log_neg_expr
| bitwise_neg_expr
| addr_expr
| indirection_expr
| preinc_expr
| predec_expr
;
sizeof_expr: SIZEOF LPAREN type_name RPAREN %prec HYPERUNARY
{
$1->hdr.type = TN_EXPR;
$1->rnode = $3;
free_tree($2);
free_tree($4);
}
| SIZEOF unary_expr
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
}
unary_minus_expr: MINUS cast_expr %prec UNARY
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
}
unary_plus_expr: PLUS cast_expr %prec UNARY
{
/* Unary plus is an ISO addition (for symmetry) - ignore it */
$$ = $2;
free_tree($1);
}
addr_expr: B_AND cast_expr %prec UNARY
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
}
indirection_expr: STAR cast_expr %prec UNARY
{
$1->hdr.type = TN_DEREF;
$1->rnode = $2;
}
preinc_expr: INCR unary_expr
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
}
predec_expr: DECR unary_expr
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
}
assign_expr: cond_expr
| unary_expr assign_op assign_expr
{
$2->hdr.type = TN_ASSIGN;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
opt_const_expr: /* Nothing */
{
$$ = (treenode *) NULL;
}
| const_expr
;
const_expr: expr
;
opt_expr: /* Nothing */
{
$$ = (treenode *) NULL;
}
| expr
;
expr: comma_expr
;
comma_expr: assign_expr
| comma_expr COMMA assign_expr %prec COMMA_OP
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
prim_expr: ident
{
$$ = (treenode *) $1;
}
| paren_expr
| constant
{
$$ = (treenode *) $1;
}
paren_expr: LPAREN expr RPAREN
{
$$ = $2;
free_tree($1);
free_tree($3);
}
| LPAREN error RPAREN
{
$$ = (treenode *) NULL;
free_tree($1);
free_tree($3);
}
postfix_expr: prim_expr
| subscript_expr
| comp_select_expr
| func_call
| postinc_expr
| postdec_expr
;
subscript_expr: postfix_expr LBRCKT expr RBRCKT
{
$2->hdr.type = TN_INDEX;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
free_tree($4);
}
comp_select_expr: direct_comp_select
| indirect_comp_select
;
postinc_expr: postfix_expr INCR
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$$ = $2;
}
postdec_expr: postfix_expr DECR
{
$2->hdr.type = TN_EXPR;
$2->lnode = $1;
$$ = $2;
}
opt_expr_list: /* Nothing */
{
$$ = (treenode *) NULL;
}
| expr_list
;
expr_list: assign_expr
| expr_list COMMA assign_expr %prec COMMA_SEP
{
$2->hdr.type = TN_EXPR_LIST;
$2->lnode = $1;
$2->rnode = $3;
$$ = $2;
}
named_label: IDENT
{
$$ = (treenode *) $1;
if (ParseStack->contxt)
{
if (! symtab_insert_at(ParseStack->contxt->labels,
mk_label($1->data.sval, $$), FUNCTION_SCOPE))
yyerr("Duplicate label.");
}
}
case_label: CASE const_expr
{
$1->hdr.type = TN_EXPR;
$1->rnode = $2;
$$ = (treenode *) $1;
}
deflt_label: DEFLT
;
add_op: PLUS
| MINUS
;
mult_op: STAR
| DIV
| MOD
;
equality_op: COMP_EQ
;
relation_op: COMP_ARITH
;
shift_op: L_SHIFT
| R_SHIFT
;
declaration: decl_specs opt_init_decl_list SEMICOLON
{
leafnode *lm;
$3->hdr.type = TN_DECL;
$3->lnode = $1;
$3->rnode = $2;
$$ = $3;
lm = leftmost($$);
if (lm)
{
if (lm->hdr.tok == TYPEDEF)
{
/* Decl is a typedef. Scan the subtree for the
ident naming the new type. Don't use rightmost()
since it doesn't give the ident for complex
types (like arrays). */
find_typedef_name($$,$$,insert_typedef);
} else {
/* Find the identifier for a normal declaration. */
find_ident_name($$,$$,NULL,insert_decl);
}
}
}
| COMMENT
{
$$ = (treenode *) $1;
}
opt_comment: /* Nothing */
{
$$ = (treenode *) NULL;
}
| COMMENT
{
$$ = (treenode *) $1;
}
opt_decl_specs: /* Nothing */
{
$$ = (treenode *) NULL;
}
| decl_specs
;
decl_specs: storage_class opt_decl_specs
{
treenode *tmpnode = make_node(TN_TYPE_LIST, ParseStack->contxt);
tmpnode->lnode = $1;
tmpnode->rnode = $2;
$$ = tmpnode;
}
| type_spec opt_decl_specs
{
treenode *tmpnode = make_node(TN_TYPE_LIST, ParseStack->contxt);
tmpnode->lnode = $1;
tmpnode->rnode = $2;
$$ = tmpnode;
}
| type_qual opt_decl_specs
{
treenode *tmpnode = make_node(TN_TYPE_LIST, ParseStack->contxt);
tmpnode->lnode = $1;
tmpnode->rnode = $2;
$$ = tmpnode;
}
;
comp_decl_specs: type_spec opt_comp_decl_specs
{
treenode *tmpnode = make_node(TN_TYPE_LIST, ParseStack->contxt);
tmpnode->lnode = $1;