-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalParser.cpp
More file actions
58 lines (50 loc) · 1.74 KB
/
LexicalParser.cpp
File metadata and controls
58 lines (50 loc) · 1.74 KB
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
#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
#include "MinimizedDFA.h"
#include "LexicalParser.h"
using namespace std;
LexicalParser::LexicalParser(Entity* head):entity(head),index(-1){}
string LexicalParser::next_token(string& path, vector<string>& keywords, vector<char>& puncuations){
stack<Entity*> entities_path;
Entity* cur = entity;
string cur_token_val = "";
while(++index < path.size()+1){
if(index < path.size() && cur->next_states.find(path[index]) != cur->next_states.end()){
cur = cur->next_states[path[index]];
cur_token_val.push_back(path[index]);
entities_path.push(cur);
}else{
if(cur_token_val == "" && find(puncuations.begin(),puncuations.end(),path[index]) != puncuations.end()){
return string(1,path[index]);
continue;
}
index--;
while(entities_path.size() && !is_accepted(entities_path.top())){
index--;
entities_path.pop();
}
if(find(keywords.begin(),keywords.end(),cur_token_val) != keywords.end()){
return cur_token_val;
}
else if(entities_path.size()) return find_token(entities_path.top());
else { // error
index++;
return "";
}
// entities_path = stack<Entity*>();
// cur = entity;
// cur_token_val = "";
// if(index == path.size()-1) break;
// if(path[index+1] == ' ') index++;
}
}
return "";
}
bool LexicalParser::has_next(string& path){
return (index < (int)path.size());
}
void LexicalParser::resetIndex(){
index = -1;
}