-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.h
73 lines (60 loc) · 1.36 KB
/
Lexer.h
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
#ifndef TOPCODER_LEXER_H_
#define TOPCODER_LEXER_H_
#include <iostream>
#include <list>
#include <string>
#include <queue>
#include <vector>
using namespace std;
struct compareString
{
bool operator()(string left, string right)
{
return left.size() < right.size();
}
};
class Lexer
{
public:
vector<string> tokenize(vector<string> tokens, string input);
};
vector<string> Lexer::tokenize(vector<string> tokens, string input)
{
vector<string> result;
while (!input.empty())
{
priority_queue<string, vector<string>, compareString> Q;
for (string token : tokens)
{
if (input[0] == token[0])
{
Q.push(token);
}
}
if (Q.empty())
{
input.erase(0, 1);
}
else
{
bool found = false;
while (!Q.empty() && !found)
{
string token = Q.top();
Q.pop();
if (0 == token.compare(input.substr(0, token.size())))
{
result.push_back(token);
input.erase(0, token.size());
found = true;
}
}
if (!found)
{
input.erase(0, 1);
}
}
}
return result;
}
#endif TOPCODER_LEXER_H_