-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexicalAnalysis.c
More file actions
325 lines (290 loc) · 7.62 KB
/
lexicalAnalysis.c
File metadata and controls
325 lines (290 loc) · 7.62 KB
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
#include "header.h"
extern int charType(const char *);
/*
* 函数名称:charToInt
* 函数功能:将字符串读为整数 返回
* 输入参数:输入位数和字符串指针
* 返回值:整数
*/
static int charToInt(int i,char* pchar)
{
int rate = 0;
for (;pchar[i] <= '9' && pchar[i] >= '0';i++)
rate = rate*10 + (pchar[i] - '0');
return rate;
}
/*
* 函数名称:setRate
* 函数功能:将pchar指向的字符串(可能的重复次数)编码为3位数字 保存到rate数组。
* 输入参数:指向重复次数的指针 用于保存结构的rate数组
* 返回值:无
*/
/*重复次数和3位数字的编码关系如下
第一位0表示出现次数是rate[1]到rate[2]的闭区间
第一位1表示出现次数是 rate[1]次及以上
例如
* 编码为 1 0
+ 编码为 1 1
? 编码为 0 0 1
{n} 编码为 0 n n
{n,} 编码为 1 n
{n,m} 编码为 0 n m*/
static void setRate(char* pchar, int rate[3])
{
int a=0,b=0,n=0;
rate[2] = 0;
if (pchar[n]=='\0') rate[0] = 0,rate[1] = 1,rate[2] = 1;
if (pchar[n]=='*' && pchar[1]=='\0') rate[0] = 1,rate[1] = 0; //*
if (pchar[n]=='+' && pchar[1]=='\0') rate[0] = 1,rate[1] = 1; //+
if (pchar[n]=='?' && pchar[1]=='\0') rate[0] = 0,rate[1] = 0,rate[2] = 1; //?
if (pchar[n]=='{')
{
n++;
for (;pchar[n] <= '9' && pchar[n] >= '0';)
{
n++,a++;
}
if (pchar[n]=='}') rate[0] = 0,rate[1]=charToInt(n-a,pchar),rate[2] = rate[1];
if (pchar[n]==',')
{
n++;
if (pchar[n]=='}') rate[0] = 1,rate[1]=charToInt(n-a-1,pchar);
else
{
for (;pchar[n] <= '9' && pchar[n] >= '0';)
{
n++,b++;
}
if (pchar[n]=='}') rate[0] = 0,rate[1]=charToInt(n-a-b-1,pchar),rate[2] = charToInt(n-b,pchar);
}
}
}
}
/*
* 函数名称:saveToList
* 函数功能:将一个正则单元的所有信息保存到链表结点 供词法分析函数调用
* 输入参数:
* struct regex_list * head 链表头结点
* struct regex_list * p 保存当前正则单元的结点
* char * arr 正则表达式数组
* int low 该单元在数组中起始偏移量
* int high 该单元在数组中结尾偏移量
* int posi_start 将元字符和重复次数信息分开的偏移量
* int * group 用于处理分组的栈数组
* int top 栈数组的栈顶位置
* 返回值:返回保存这些单元正则的结点指针
*/
static struct regex_list * saveToList(struct regex_list * head, struct regex_list * p, char * arr, int low, int high, int posi_start, int * group, int top)
{
struct regex_list * q;
struct regex_list * p_to_find_bracket; //用来寻找和右括号配对的左括号
int i;
if ((q = (struct regex_list *)malloc(sizeof(struct regex_list))) == NULL)
exit(OVERFLOW);
memset(q, 0, sizeof(struct regex_list));
//构成双向链表
q->next = p->next;
p->next->pred = q;
p->next = q;
q->pred = p;
//初始化
q->branch_num = 0;
for (i = 0; i < MAX_GROUP; ++i) //
{
q->group[i] = 0;
}
q->is_posi = 0;
q->is_back_ref = 0;
q->pbranch[0] = q;
q->matched_num = 0;
//设置char unit[UNIT_LEN]; //保存单位正则字符
strncpy(q->unit, arr + low, high - low + 1);
q->unit[high - low + 1] = '\0';
//设置int posi_start; //分割元字符和出现次数的下标
q->posi_start = posi_start - low;
//设置group
for (i = 0; i <= top; ++i)
{
q->group[(group[i])] = 1;
}
//设置rate[3]记录重复次数
setRate(q->unit + q->posi_start, q->rate);
//设置是否是位置字符
if (q->unit[0] == '^' || q->unit[0] == '$')
{
q->is_posi = 1;
}
if ((q->unit[0] == '\\') && ((q->unit[1] == 'b') || (q->unit[1] == 'B')))
{
q->is_posi = 1;
}
//设置是否是后向引用
if (q->unit[0] == '\\' && ((q->unit[1] >= '1') && (q->unit[1] <= '9')))
{
q->is_back_ref = 1;
}
//设置group_id为所在的分组中的最大者 右括号另行处理
q->group_id = group[top];
//处理括号
if (q->unit[0] == ')')
{
q->group_id = group[top + 1];
//设置括号对之间互相指向
p_to_find_bracket = head->next;
while (p_to_find_bracket->unit[0] != '(' || p_to_find_bracket->group_id != q->group_id)
{
p_to_find_bracket = p_to_find_bracket->next;
}
//现在p_to_find_bracket指向配对的左括号
p_to_find_bracket->pbracket = q;
q->pbracket = p_to_find_bracket;
//将左括号的pbranch最后一个元素指向右括号
p_to_find_bracket->pbranch[p_to_find_bracket->branch_num + 1] = q;
}
//处理分支符号
if (q->unit[0] == '|')
{
if (q->group_id == 0)
{
p_to_find_bracket = head;
p_to_find_bracket->pbranch[p_to_find_bracket->branch_num + 2] = q->next;
}
else
{
p_to_find_bracket = head->next;
while (p_to_find_bracket->unit[0] != '(' || p_to_find_bracket->group_id != q->group_id)
{
p_to_find_bracket = p_to_find_bracket->next;
}
}
//现在p_to_find_bracket指向|所在分组的左括号
++p_to_find_bracket->branch_num;
p_to_find_bracket->pbranch[p_to_find_bracket->branch_num] = q;
}
return q;
}
/*
* 函数名称:lexicalAnalysis
* 函数功能:分词。将结果放进链表
* 输入参数:指向正则表达式字符串的指针 和 保存分词后信息的链表头结点
* 返回值:可能的错误码(不包括0) 正确返回1 。
*/
int lexicalAnalysis(char * arr, struct regex_list * head)
{
int low = 0;
int high;
struct regex_list * p;
//struct regex_list * q;
int group[10] = { 0 };
int top = 0; //group的栈顶指针
int group_id = 0; //当前左括号出现次数
int posi_start = 0; //分割元字符和重复次数
int tmp;
p = head;
//q = p;
high = low;
while (arr[high] != '\0')
{
tmp = charType(&arr[low]);
switch (tmp)
{
case 15: //low和high指向[abc]中的[
{
for (high = high + 2; arr[high] != ']'; ++high)
{
;
}
//现在high指向]
--high; //继续执行下去case 14中会将high增加1 因此这里先减去1
//没有break
}
case 43:; // \1 \2 \3 \4 \5 \6 \7 \8 \9
case 14: //high指向\w \s \d \W \S \D中的\ 。
high = high + 1;
//没有break
case 1:;
case 11: //处理匹配文本的元字符后面可能有的重复次数
{
posi_start = high + 1;
if (arr[high + 1] != '\0' && charType(&arr[high + 1]) == 21)
{
high = high + 1;
}
else if (arr[high + 1] != '\0' && arr[high + 1] == '{')
{
for (high = high + 2; arr[high] != '}'; ++high)
{
;
}
}
else
{
high = high;
}
break;
}
case 31:;
case 12: //^和$
{
posi_start = high + 1;
high = low;
break;
}
case 13: //\b \B
{
posi_start = high + 2;
high = low + 1;
break;
}
case 41: // (
{
//++group;
++group_id;
++top;
group[top] = group_id; //左括号进栈
posi_start = high + 1;
high = low;
break;
}
case 42: //)
{
//--group;
--top; //遇见右括号 出栈
posi_start = high + 1;
if (arr[high + 1] != '\0' && charType(&arr[high + 1]) == 21)
{
high = high + 1;
}
else if (arr[high + 1] != '\0' && arr[high + 1] == '{')
{
for (high = high + 2; arr[high] != '}'; ++high)
{
;
}
}
else
{
;
}
break;
}
default:
{
break;
}
}
if (top == -1)
{
break;
}
//这个时候区间[low, high]是一个匹配单元
p = saveToList(head, p, arr, low, high, posi_start, group, top);
low = high + 1;
high = low;
}
if (top != 0)
{
return UNMATCHED_BRACKET;
}
return TRUE;
}