-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParseLexFile.cpp
278 lines (228 loc) · 5.11 KB
/
ParseLexFile.cpp
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
#include "stdafx.h"
#include"structs.h"
using namespace std;
static vector<string> split(const string& str, const string& delim);
static string& trim(string &s);
static void remove_comment(string& s);
//对lex.l文件解析
int read_and_parse_lex(string filePath,map<string,string>& m,vector<Rules>& vRules,vector<string>&P1,vector<string>&P4)
{
//读入文件
ifstream in;
in.open(filePath.c_str(), ios::in);
if (!in)
{
cout << "Open lex.l failed!" << endl;
return 1;
}
string line; //用来存储每行的字符串
enum State{beforePart1,Part1,Part2,Part3,Part4}; //用来指示当前读写指针所处部分
State st = beforePart1;
vector<string> vBuf; //用来暂存字符串分割的结果;
int lineCount=0; //保存行号,用于错误提示
bool hasError=false; //标识是否有错误,有错误立即终止解析
bool findUpperPar=false; //是否发现上括弧
bool findUpperMarks = false; //是否发现上引号
bool findUPPerBrackets = false;//是否发现上中括号
string lhd; //存储左边的字符串
string rhd; //存储右边的字符串
vector<string>action;//存储动作
while (!in.eof()&&!hasError)
{
getline(in,line);
++lineCount;
//若某行为空,直接跳过
if (line.empty())
{
continue;
}
//根据状态执行相应操作
switch (st)
{
case beforePart1:
if (line.compare("%{")==0)
{
st = Part1;
}
else
{
cout << "ERROR: No entry sign %{ " << endl;
hasError = true;
}
break;
case Part1:
if (line.compare("%}")==0)
{
st = Part2;
}
else
{
P1.push_back(line);
}
break;
case Part2:
if (line.compare("%%")==0)
{
st = Part3;
}
else
{
//去掉句尾注释
// line = split(line, "/*").at(0);
//分割成两部分
vBuf = split(line, " ");
if (vBuf.size() == 1)
{
vBuf = split(line, "\t");
}
if (vBuf.size() == 2)
{
m.insert(pair<string, string>(vBuf[0], vBuf[1]));
}
}
break;
case Part3:
if (line.compare("%%")==0)
{
st = Part4;
}
//在P3部分,需要注意空白字符
else
{
//去掉句尾注释
//removeComment(line);
//去掉首尾空格
trim(line);
//如果处在{}之间且此行不为}
if (findUpperPar && line.compare("}")!=0)
{
action.push_back(line);
findUpperPar = true;
break;
}
//若当前行为}
if (findUpperPar && line.compare("}")==0)
{
vRules.push_back(Rules{ lhd, action });
action.clear();
findUpperPar = false;
break;
}
//若不处于{}之间
if (!findUpperPar)
{
//对[和"开头的作特殊处理
if (line[0] == '[')
{
findUPPerBrackets = true;
int i = 1;
for (i = 1; i < line.size(); i++)
{
if (line[i] == ']')
{
findUPPerBrackets=!findUPPerBrackets;
}
if ((line[i + 1] == ' '|| line[i + 1] == '\t') &&findUPPerBrackets == false)
{
break;
}
}
lhd = line.substr(0, i+1);
rhd = line.substr(i+1);
trim(rhd);
}
if (line[0] == '\"')
{
findUpperMarks = true;
int i = 1;
for (i = 1; i < line.size(); i++)
{
if (line[i] == '\"')
{
findUpperMarks=!findUpperMarks;
}
if ((line[i+1] == ' '||line[i+1]=='\t')&&findUpperMarks == false)
{
break;
}
}
lhd = line.substr(0, i+1);
rhd = line.substr(i + 1);
trim(rhd);
}
if (line[0] != '\''&&line[0] != '[')
{
int i = 0;
for (i = 0; i < line.size(); i++)
{
if (line[i] == ' '||line[i]=='\t')
break;
}
lhd = line.substr(0, i );
rhd = line.substr(i, line.size());
trim(rhd);
}
if (rhd.compare("{")!=0)
{
action.push_back(rhd);
vRules.push_back(Rules{ lhd, action });
action.clear();
}
if (rhd.compare("{")==0)
{
findUpperPar = true;
}
}
}
break;
case Part4:
P4.push_back(line);
break;
}
}
in.close();
return 0;
}
//字符串分割函数
vector<string> split(const string& str, const string& delim) {
vector<string> res;
if ("" == str) return res;
//先将要切割的字符串从string类型转换为char*类型
char * strs = new char[str.length() + 1]; //不要忘了
strcpy(strs, str.c_str());
char * d = new char[delim.length() + 1];
strcpy(d,delim.c_str());
char *p = strtok(strs, d);
while (p) {
string s = p; //分割得到的字符串转换为string类型
res.push_back(s); //存入结果数组
p = strtok(NULL, d);
}
delete strs;
delete d;
return res;
}
//去除字符串的首尾空格
string& trim(string &s)
{
if (s.empty())
{
return s;
}
s.erase(0, s.find_first_not_of(' '));
s.erase(0, s.find_first_not_of('\t'));
s.erase(s.find_last_not_of(' ') + 1);
s.erase(s.find_last_not_of('\t') + 1);
return s;
}
//去除注释
void remove_comment(string& s)
{
char* p = new char[100];
strcpy(p,s.c_str());
char* p1=strstr(p, "/*");
if (p1 == NULL)return;
int k = p1 - p;
s = s.substr(0, k);
delete p;
}