-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgrammar_outline.c
141 lines (117 loc) · 4.91 KB
/
grammar_outline.c
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
Program : TypeDefBlock GDeclBlock FDefBlock MainBlock { evaluate($4); }
;
TypeDefBlock : TYPE TypeDefList ENDTYPE
| {}
;
TypeDefList: TypeDefList TypeDef
|TypeDef
;
TypeDef: ID '{' FieldDeclList '}' { Tinstall($1,$3); }
;
FieldDeclList : FieldDeclList ';' FieldDecl
| FieldDecl
;
FieldDecl : TypeName ID ;
TypeName : INT
| STR
| ID
;
GDeclBlock : DECL GDeclList ENDDECL
|
;
GDeclList : GDecList GDecl
| GDecl
GDecl : TypeName Gidlist ';' ;
Gidlist : Gidlist ',' Gid
| Gid ;
Gid : ID { GInstall(varname,ttableptr, 1, NULL); }
| ID '(' ParamList ')' { GInstall(varname,ttableptr, 0,$3); }
| ID '[' NUM ']' { GInstall(varname,ttableptr, $3,NULL); }
;
ParamList : ParamList Arg { AppendArglist($1,$2);}
| Param
Param : TypeName ID { CreateArglist($1,$2); }
FDefBlock : TypeName ID '(' ArgList ')' '{' LdeclBlock Body '}'
{
GUpdate($2->name,$1,$4,$7,$8);
} }
;
MainBlock : INT MAIN '(' ')' '{' LdeclBlock Body '}'
{
type = TLookup("int");
gtemp = GInstall("MAIN",type,0,NULL);
gtemp->LocalVariables = $6;
gtemp->fbinding = $7;
$$ = TreeCreate(type, NODETYPE_MAIN, gtemp->name, (Constant){}, NULL, $7, NULL, NULL);
$$->Gentry = gtemp;
//reset LocalSymbolHead and other binding variables used
}
;
body : BEGIN slist retstmt END { $$ = TreeCreate($3->type, NODETYPE_BODY, NULL, (union Constant){}, NULL, $2, $3, NULL); }
;
slist : slist stmt { $$ = TreeCreate(TLookup("void"), NODETYPE_SLIST, NULL,(union Constant){}, NULL, $1, $2, NULL); }
| { $$ = NULL; }
;
stmt : ID ASGN expr ';' {
ltemp = LLookup($1->name);
if(ltemp == NULL){
gtemp = GLookup($1->name);
if(gtemp == NULL){
yyerror("Undeclared variable is used");exit(1);
}
$1->Gentry = gtemp;
$1->type = gtemp->type;
}
else{
$1->Lentry = ltemp;
$1->type = ltemp->type;
}
$$ = TreeCreate(TLookup("void"),NODETYPE_ASGN,NULL,(union Constant){},NULL,$1,$3,NULL);
}
| ....
.
.
| IF '(' expr ')' THEN slist ELSE slist ENDIF { $$ = TreeCreate(TLookup("void"),NODETYPE_IF,NULL,(union Constant){},NULL,$3,$6,$8); }
| ...
.
.
| ID ASGN ALLOC'(' ')' {}
| FIELD ASGN ALLOC'(' ')' {}
| FREE '(' ID ')' {}
| FREE '(' FIELD ')' {}
|...
;
FIELD : ID '.' ID { $$ = TreeCreate(TLookUp("void"), NODETYPE_FIELD, NULL, (Constant){}, NULL, $1, $3, NULL); }
| FIELD '.' ID { $$ = TreeCreate(TLookUp("void"), NODETYPE_FIELD, NULL, (Constant){}, NULL, $1, $3, NULL); }
;
expr : expr PLUS expr { $$ = TreeCreate(TLookup("int"),NODETYPE_PLUS,NULL,(union Constant){},NULL,$1,$3,NULL); }
| ....
| '(' expr ')' { $$ = $2; }
| NUM { $$ = $1; }
| ID {
ltemp = LLookup($1->name);
if(ltemp == NULL){
gtemp = GLookup($1->name);
if(gtemp == NULL){
yyerror("Undeclared variable is used");exit(1);
}
$1->Gentry = gtemp;
$1->type = gtemp->type;
}
else{
$1->Lentry = ltemp;
$1->type = ltemp->type;
}
$$ = $1;
}
| ID '[' expr ']' { $$ = TreeCreate(NULL, NODETYPE_ARR_ID, NULL, (Constant){}, NULL, $1, $3, NULL); }
| FIELD { $$ = $1; }
| ID '(' param ')' {
gtemp = GLookup($1->name);
if(gtemp == NULL){
yyerror("Yacc : Undefined function");exit(1);
}
$$ = TreeCreate(gtemp->type,NODETYPE_FUNCTION,$1->name,(Constant){},$3,NULL,NULL,NULL);
$$->Gentry = gtemp;
}
;