-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.h
261 lines (224 loc) · 5.33 KB
/
Parser.h
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
#pragma once
#include "Common.h"
#include "InterCode.h"
#ifndef PARSER_H
#define PARSER_H
//符号
class Symbol {
public:
bool isVt;//是否为终结符
string content;//内容
friend bool operator ==(const Symbol&one, const Symbol&other);
friend bool operator < (const Symbol&one, const Symbol&other);
Symbol(const Symbol& sym);
Symbol(const bool &isVt, const string& content);
Symbol();
};
//声明类型(变量声明/函数声明)
enum DecType {
DEC_VAR,DEC_FUN
};
//数据类型(int/void)
enum DType { D_VOID, D_INT, D_INT_ARRAY };
struct Var {
string name;
DType type;
int level;
vector<int>size;
};
struct Func {
string name; // 函数名
DType returnType; // 返回类型
list<DType> paramTypes; // 形参列表
int enterPoint; // 中间代码标号(经m回填)
};
class Id :public Symbol {
public:
string name;
Id(const Symbol& sym, const string& name);
};
class Num :public Symbol {
public:
string number;
Num(const Symbol& sym,const string& number);
};
class FunctionDeclare :public Symbol {
public:
list<DType>plist;
FunctionDeclare(const Symbol& sym);
};
class Parameter :public Symbol {
public:
list<DType>plist;
Parameter(const Symbol& sym);
};
class ParameterList :public Symbol {
public:
list<DType>plist;
ParameterList(const Symbol& sym);
};
class ArrayDeclare :public Symbol {
public:
int size;
ArrayDeclare(const Symbol& sym);
};
class ArrayDeclareList :public Symbol {
public:
vector<int> size;
int total_size;
ArrayDeclareList(const Symbol& sym);
};
class Array :public Symbol {
public:
string name;
Array(const Symbol& sym);
};
class IndexList :public Symbol {
public:
vector<string> index;
IndexList(const Symbol& sym);
};
class SentenceBlock :public Symbol {
public:
list<int>nextList;
SentenceBlock(const Symbol& sym);
};
class SentenceList :public Symbol {
public:
list<int>nextList;
SentenceList(const Symbol& sym);
};
class Sentence :public Symbol {
public:
list<int>nextList;
Sentence(const Symbol& sym);
};
class WhileSentence :public Symbol {
public:
list<int>nextList;
WhileSentence(const Symbol& sym);
};
class IfSentence :public Symbol {
public:
list<int>nextList;
IfSentence(const Symbol& sym);
};
class Expression :public Symbol {
public:
string name;
list<int>falseList;
Expression(const Symbol& sym);
};
class M :public Symbol {
public:
int quad;
M(const Symbol& sym);
};
class N :public Symbol {
public:
list<int> nextList;
N(const Symbol& sym);
};
class AddExpression :public Symbol {
public:
string name;
AddExpression(const Symbol& sym);
};
class Nomial :public Symbol {
public:
string name;
Nomial(const Symbol& sym);
};
class Factor :public Symbol {
public:
string name;
Factor(const Symbol& sym);
};
class ArgumentList :public Symbol {
public:
list<string> alist;
ArgumentList(const Symbol& sym);
};
// 先把关键词保存了
const Symbol symbol[] = {
{true,"int"},{true,"void"},{true,"if"},{true,"else"},{true,"while"},{true,"return"},
{true,"+"},{true,"-"},{true,"*"},{true,"/"},{true,"="},
{true,"=="},{true,">"},{true,"<"},{true,"!="},{true,">="},{true,"<="},
{true,";"},{true,","},{true,"("},{true,")"},{true,"{"},{true,"}"},{true,"ID"},
{false,"ID"}
};
//产生式
struct Production {
int id;//产生式的标识id,方便比较
Symbol left;
vector<Symbol>right;
};
//项目
struct Item {
int pro;//产生式id
int pointPos;//.的位置
friend bool operator ==(const Item&one, const Item& other);
friend bool operator <(const Item&one, const Item& other);
};
//DFA状态
struct I {
set<Item> items;
};
typedef pair<int, Symbol> GOTO;
struct DFA {
list<I> stas;
map<GOTO, int> goTo;
};
enum Behave { reduct, shift, accept, error};
struct Behavior {
Behave behavior;
int nextStat;
};
class NewTemper {
private:
int now;
public:
NewTemper();
string newTemp();
};
class Parser {
private:
int lineCount;
int nowLevel;//当前分析行所在的语句块级次
vector<Production>productions;
DFA dfa;
map<GOTO,Behavior> SLR1_Table;//由product.txt构造出的SLR1表
map<Symbol,set<Symbol> >first;//由product.txt构造出的first集
map<Symbol, set<Symbol> >follow;//由product.txt构造出的follow集
stack<Symbol*> symStack;//符号栈
stack<int> staStack;//状态栈
vector<Var> varTable;//变量表
vector<Func> funcTable;//函数表
InterCode code;//生成的四元式
NewTemper nt;
void readProductions(const char*fileName);
I derive(Item item);
void createDFA();
void outputDFA(ostream& out);
void analyse(list<Token>&words,ostream& out);
void outputSymbolStack(ostream& out);
void outputStateStack(ostream& out);
void getFirst();
void getFollow();
Func* lookUpFunc(string ID);
Var* lookUpVar(string ID);
bool march(list<string>&argument_list,list<DType>¶meter_list);
Symbol* popSymbol();
void pushSymbol(Symbol* sym);
public:
Parser(const char*fileName);
void outputDFA();
void outputDFA(const char* fileName);
void outputInterCode();
void outputInterCode(const char* fileName);
void analyse(list<Token>&words,const char*fileName);
void analyse(list<Token>&words);
vector<pair<int, string> > getFuncEnter();
InterCode* getInterCode();
};
#endif // !PARSER_H