📄 re.h
字号:
#ifndef _REGULAR_EXP_H_
#define _REGULAR_EXP_H_
#include <memory.h>
#include <set>
#include <string>
#include <algorithm>
#include "NFA.h"
#include "DFA.h"
#define MAP_SIZE ('z' - '0' + 1)
using namespace std;
class Scanner
{
private:
// Regular expression string
string re;
int off;
// Analyse the string, then we can know how many characters are used.
void analyze();
public:
// token type
enum {CHARS, SYMBOL, SCAN_ERR, ENDSTRING};
// Map character to sequence number
int rawMap[MAP_SIZE];
// Map charset sequence number to character
int *map;
int mapSize;
int token;
int value;
Scanner()
{
off = 0;
mapSize = 0;
map = NULL;
memset(rawMap, 0, MAP_SIZE * sizeof(int));
}
~Scanner()
{
delete []map; map = NULL;
}
void setRe(const char *_re)
{
re = _re;
analyze();
}
// String scanner
void get();
};
class RegularExp
{
private:
Scanner scanner;
// Recursive expression parser
NFA *unionSet();
NFA *intersectionSet();
NFA *join();
NFA *complementSet();
NFA *closure();
NFA *element();
protected:
int error(const char *msg);
public:
NFA *nfa;
DFA *dfa;
// Constructions
RegularExp()
{
nfa = NULL;
dfa = NULL;
}
~RegularExp()
{
delete nfa;
delete dfa;
}
// Parse the regular expression into NFA
void parse(const char *_re)
{
scanner.setRe(_re);
parse();
}
void parse(void);
void buildDFA()
{
if(dfa != NULL)
{
delete dfa;
}
dfa = new DFA(nfa, scanner.rawMap);
}
int identify(const char *str)
{
return dfa->identify(str);
}
};
#endif //_REGULAR_EXP_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -