-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_regex_matching.c
120 lines (89 loc) · 2.05 KB
/
_regex_matching.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
// https://leetcode.com/problems/regular-expression-matching/description/
// Passes only 90% of tests for now
// fails:
// s: aaa
// r: ab*a*c*a
// because final `a` is not matched properly because `a` consumes `a`, `a*` consumes `aa`
static char* str;
static char* reg;
bool parse_star();
bool parse_dott();
bool parse_char();
bool parse_token();
bool parse_all();
static dotcount = 0;
bool isMatch(char* s, char* r) {
str = s;
reg = r;
dotcount = 0;
return parse_all();
}
bool parse_all() {
while(*reg)
if(!parse_token())
return false;
if(*str != 0 || *reg != 0)
return false;
return true;
}
bool parse_token() {
switch(*reg) {
case 0 : return false;
case '.': return parse_dott();
case '*': return parse_star();
default :
return parse_char();
}
}
bool parse_star() {
printf("ERR\n");
return false;
}
bool parse_dott() {
if(*(reg+1) == '*') {
printf("PARSE . *; ");
while(*(reg+2+dotcount) == '.')
dotcount++;
while(*(str+dotcount) && *str != *(reg+2+dotcount)) {
printf("%c", *str);
str++;
}
printf("\n");
dotcount = 0;
reg+=2;
if(*str == 0 && *reg != 0) {
printf("PARSE .* 0;\n");
return false;
}
return true;
} else {
if(*str == 0) {
printf("PARSE . 0;\n");
return false;
}
printf("PARSE . %c;\n", *str);
str++; reg++;
return true;
}
return false;
}
bool parse_char() {
if(*(reg+1) == '*') {
printf("PARSE * %c; ", *reg);
while(*reg == *(reg+2+dotcount))
dotcount++;
while(*str && *str == *reg) {
printf("%c", *str);
str++;
}
str -= dotcount;
dotcount = 0;
printf("\n");
reg+=2;
return true;
} else {
printf("PARSE %c %c;\n", *reg, *str);
return (*str++ == *reg++);
}
return false;
}