-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5.9 Regular expression pattern matching
- Loading branch information
1 parent
4317564
commit 304b8d0
Showing
27 changed files
with
273 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
|
||
#include <vector> | ||
#include <string> | ||
#include "StringSorting.h" | ||
|
||
class LSD : public StringSorting { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "NFA.h" | ||
#include <list> | ||
#include "DirectedDFS.h" | ||
|
||
NFA::NFA(std::string_view regexp) : re(regexp.begin(), regexp.end()), M(re.size()), G(M + 1) { | ||
std::list<int> ops; | ||
for (int i = 0; i < M; ++i) { | ||
int lp = i; // left position | ||
if (re[i] == '(' || re[i] == '|') ops.push_front(i); | ||
else if (re[i] == ')') { | ||
int orPos = ops.front(); | ||
ops.pop_front(); | ||
if (re[orPos] == '|') { | ||
lp = ops.front(); | ||
ops.pop_front(); | ||
G.addEdge(lp, orPos + 1); | ||
G.addEdge(orPos, i); | ||
} else lp = orPos; | ||
} | ||
if (i < M - 1 && re[i + 1] == '*') { | ||
// 查看下一个字符 | ||
G.addEdge(lp, i + 1); | ||
G.addEdge(i + 1, lp); | ||
} | ||
if (re[i] == '(' || re[i] == '*' || re[i] == ')') G.addEdge(i, i + 1); | ||
} | ||
} | ||
|
||
bool NFA::recognizes(std::string_view txt) const { | ||
std::list<int> pc; | ||
DirectedDFS dfs(G, 0); | ||
for (int v = 0; v < G.V(); ++v) | ||
if (dfs.marked(v)) pc.push_front(v); | ||
|
||
for (int i = 0; i < txt.length(); ++i) { | ||
// 计算txt[i+1]可能到达的所有状态 | ||
std::list<int> match; | ||
for (int v: pc) { | ||
if (v < M) { | ||
if (re[v] == txt[i] || re[v] == '.') match.push_front(v + 1); | ||
} | ||
} | ||
pc = std::list<int>(); | ||
dfs = DirectedDFS(G, match); | ||
for (int v = 0; v < G.V(); ++v) | ||
if (dfs.marked(v)) pc.push_front(v); | ||
} | ||
for (int v: pc) | ||
if (v == M) return true; | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef NFA_H | ||
#define NFA_H | ||
|
||
|
||
#include "Digraph.h" | ||
#include <vector> | ||
#include <string_view> | ||
|
||
class NFA { | ||
private: | ||
std::vector<char> re; // 匹配转换 | ||
int M; // 状态数量 | ||
Digraph G; // epsilon转换 | ||
|
||
public: | ||
explicit NFA(std::string_view regexp); | ||
|
||
bool recognizes(std::string_view txt) const; | ||
}; | ||
|
||
|
||
#endif //NFA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.