-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_parser.h
More file actions
54 lines (45 loc) · 933 Bytes
/
regex_parser.h
File metadata and controls
54 lines (45 loc) · 933 Bytes
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
#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;
enum TokenType
{
CHAR,
ALTERNATION,
STAR,
LPAREN,
RPAREN,
END
};
struct Token
{
TokenType type;
char value;
explicit Token(TokenType t, char v = '\0') : type(t), value(v) {};
};
struct RegexNode
{
char type;
char value;
RegexNode *left;
RegexNode *right;
explicit RegexNode(char t, char v = '\0', RegexNode *l = nullptr, RegexNode *r = nullptr) : type(t), value(v), left(l), right(r) {};
};
class RegexParser
{
private:
vector<Token> tokens;
int pos;
RegexNode *parseAlternation();
RegexNode *parseConcatenation();
RegexNode *parseKleene();
RegexNode *parsePrimary();
Token currentToken();
void advance();
void error(string message);
vector<Token> tokenize(string regex);
public:
RegexParser();
RegexNode *parse(string regex);
};