forked from juyaoliu/C-minus-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sematic.txt
185 lines (135 loc) · 4.99 KB
/
sematic.txt
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
0:P ::= N declare_list
back_patch(N.nextList,find(main).enter)
1:declare_list ::= declare declare_list
2:declare_list ::= declare
3:declare ::= int ID M A function_declare
func_table.insert(ID.name,int,function_declare.plist,M.quad)
4:declare ::= int ID var_declare
var_table.insert(ID.name,int,nowLevel)
5:declare ::= void ID M A function_declare
func_table.insert(ID.name,void,function_declare.plist,M.quad)
6:A ::=
nowLevel++
7:var_declare ::= ;
8:function_declare ::= ( parameter ) sentence_block
function_declare.plist = parameter.plist
9:parameter ::= parameter_list
parameter.plist = parameter_list.plist
10:parameter ::= void
parameter.plist = list()
11:parameter_list ::= param
parameter_list.plist = list().push_back(int)
12:parameter_list1 ::= param , parameter_list2
parameter_list.plist = parameter_list2.plist.push_front(int)
13:param ::= int ID
var_table.insert(ID.name,int,now_level)
14:sentence_block ::= { inner_declare sentence_list }
sentence_block.nextList = sentence_list.nextList
nowLevel--
15:inner_declare ::=
16:inner_declare ::= inner_var_declare ; inner_declare
17:inner_var_declare ::= int ID
var_table.insert(ID.name,int,nowLevel)
18:sentence_list ::= sentence M sentence_list
sentence_list.nextList = sentence_list.nextList
backpatch(sentence.nextList,M.quad)
19:sentence_list ::= sentence
sentence_list.nextList = sentence.nextList
20:sentence ::= if_sentence
sentence.nextList = if_sentence.nextList
21:sentence ::= while_sentence
sentence.nextList = while_sentence.nextList
22:sentence ::= return_sentence
sentence.nextList = list()
23:sentence ::= assign_sentence
sentence.nextList = list()
24:assign_sentence ::= ID = expression ;
emit (=,expression.name,,ID)
25:return_sentence ::= return ;
emit (return,_,_,_)
26:return_sentence ::= return expression ;
emit (return,expression.name,_,_)
27:while_sentence ::= while M ( expression ) A sentence_block
backpatch(sentence_block.next,M.quad)
while_sentence.nextList = expression.falseList
emit(j,_,_,M.quad)
28:if_sentence ::= if ( expression ) A sentence_block
if_sentence.nextList = merge(expression.falseList,sentence_block.nextList)
29:if_sentence ::= if ( expression ) A sentence_block1 N else M A sentence_block2
backpatch(expression.falseList,M.quad)
if_sentence.nextList = merge(sentence_block1.nextList,sentence_block2.nextList,N.nextList)
30:N ::=
N.nextList = code.size()
emit(j,_,_,-1)
31:M ::=
M.quad = code.size()
32:expression ::= add_expression
expression.name = add_expression.name
expression.falseList = -1
33:expression ::= add_expression > add_expression
expression.name = NULL
expression.falseList = code.size()
emit (j<=,add_expression1.name,add_expression2.name,-1)
34:expression ::= add_expression < add_expression
expression.name = NULL
expression.falseList = code.size()
emit (j>=,add_expression1.name,add_expression2.name,-1)
35:expression ::= add_expression == add_expression
expression.name = NULL
expression.falseList = code.size()
emit (j!=,add_expression1.name,add_expression2.name,-1)
36:expression ::= add_expression >= add_expression
expression.name = NULL
expression.falseList = code.size()
emit (j<,add_expression1.name,add_expression2.name,-1)
37:expression ::= add_expression <= add_expression
expression.name = NULL
expression.falseList = code.size()
emit (j>,add_expression1.name,add_expression2.name,-1)
38:expression ::= add_expression != add_expression
expression.name = NULL
expression.falseList = code.size()
emit (j==,add_expression1.name,add_expression2.name,add_expression.name,-1)
39:add_expression ::= item
add_expression.name = item.name
40:add_expression ::= item + add_expression
add_expression.name = newTemp()
emit(+,item.name,add_expression.name,add_expression.name)
41:add_expression ::= item - add_expression
add_expression.name = newTemp()
emit(-,item.name,add_expression.name,add_expression.name)
42:item ::= factor
item.name = factor.name
43:item1 ::= factor * item2
item.name = newTemp()
emit (*,factor.name,item2.name,item1.name)
44:item1 ::= factor / item2
item.name = newTemp()
emit (/,factor.name,item2.name,item1.name)
45:factor ::= NUM
factor.name = NUM
46:factor ::= ( expression )
factor.name = expression.name
47:factor ::= ID ( argument_list )
f = func_find(ID.name)
if f == NULL
error
else if !march(argument_list,f.parameter_list)
error
else
push argument_list
emit jal find(ID.name).enterPoint
if(f.returnType ==int)
factor.name = newTemp()
emit (=,factor.name,_,RETURN_PLACE)
48:factor ::= ID
if func_find(ID.name) == NULL
error
else
factor.name = ID.name
50:argument_list ::=
argument_list.plist = list()
51:argument_list ::= expression
argument_list.plist = expression.name
52:argument_list ::= expression , argument_list
argument_list.plist = argument_list.plist + expression.name