-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rpq2NFAConvertor.cpp
191 lines (175 loc) · 5.79 KB
/
Rpq2NFAConvertor.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* @file Rpq2NFAConvertor.cpp
* @author Yue Pang
* @brief Implements methods in Rpq2NFAConvertor.h
* @date 2022-08-24
*/
#include "Rpq2NFAConvertor.h"
using namespace std;
void Rpq2NFAConvertor::getClauses(const std::string &query, std::vector<std::string> &v) {
istringstream ifs(query);
RpqErrorListener lstnr;
antlr4::ANTLRInputStream input(ifs);
rpqLexer lexer(&input);
lexer.removeErrorListeners();
lexer.addErrorListener(&lstnr);
antlr4::CommonTokenStream tokens(&lexer);
rpqParser parser(&tokens);
parser.removeErrorListeners();
parser.addErrorListener(&lstnr);
rpqParser::PathContext *path = parser.path();
if (path->pathSequence().size() > 1)
return;
v.clear();
getClausesSinglePath(path, v);
}
void Rpq2NFAConvertor::getClausesSinglePath(rpqParser::PathContext *path, std::vector<std::string> &v) {
std::vector<rpqParser::PathEltContext *> cVec = path->pathSequence()[0]->pathElt();
for (const auto &c : cVec) {
if (!c->pathMod() && c->pathPrimary()->path())
getClausesSinglePath(c->pathPrimary()->path(), v);
else
v.emplace_back(c->getText());
}
}
/**
* @brief Overall driver function, returns pointer to the NFA corresponding to the query.
*
* @param query the query string
* @return shared_ptr<NFA> pointer to the NFA corresponding to the query
*/
shared_ptr<NFA> Rpq2NFAConvertor::convert(const string &query)
{
shared_ptr<NFA> nfa_ptr = make_shared<NFA>();
try
{
istringstream ifs(query);
RpqErrorListener lstnr;
antlr4::ANTLRInputStream input(ifs);
rpqLexer lexer(&input);
lexer.removeErrorListeners();
lexer.addErrorListener(&lstnr);
antlr4::CommonTokenStream tokens(&lexer);
rpqParser parser(&tokens);
parser.removeErrorListeners();
parser.addErrorListener(&lstnr);
rpqParser::PathContext *path = parser.path();
visitPath(path, nfa_ptr);
}
catch(const runtime_error& e1)
{
throw runtime_error(e1.what());
}
return nfa_ptr;
}
/**
* @brief path : pathAlternative ;
*
* @param ctx pointer to path's context
* @param nfa_ptr pointer to the NFA to be constructed
* @return antlrcpp::Any a dummy antlrcpp::Any object
*/
antlrcpp::Any
Rpq2NFAConvertor::visitPath(rpqParser::PathContext *ctx, shared_ptr<NFA> nfa_ptr)
{
if ((ctx->pathSequence()).size() == 1)
visitPathSequence(ctx->pathSequence(0), nfa_ptr);
else
{
shared_ptr<NFA> currNfa_ptr;
nfa_ptr->unsetAccept();
for (auto pathSequence : ctx->pathSequence())
{
currNfa_ptr = make_shared<NFA>();
visitPathSequence(pathSequence, currNfa_ptr);
// Attach transition from current initial to sub-initial (label eps)
nfa_ptr->initial->addTransition(-1, true, currNfa_ptr->initial);
for (auto acceptState : currNfa_ptr->accepts)
nfa_ptr->setAccept(acceptState);
nfa_ptr->addStates(currNfa_ptr->states);
}
}
return antlrcpp::Any();
}
/**
* @brief pathSequence : pathEltOrInverse ( '/' pathEltOrInverse )*
*
* @param ctx pointer to pathSequence's context
* @param nfa_ptr pointer to the NFA to be constructed
* @return antlrcpp::Any a dummy antlrcpp::Any object
*/
antlrcpp::Any
Rpq2NFAConvertor::visitPathSequence(rpqParser::PathSequenceContext *ctx, std::shared_ptr<NFA> nfa_ptr)
{
if ((ctx->pathElt()).size() == 1)
visitPathElt(ctx->pathElt(0), nfa_ptr);
else
{
shared_ptr<NFA> currNfa_ptr;
for (auto pathEltOrInverse : ctx->pathElt())
{
currNfa_ptr = make_shared<NFA>();
visitPathElt(pathEltOrInverse, currNfa_ptr);
// Attach transition from current accepts to sub-initial (label eps)
for (auto acceptState : nfa_ptr->accepts)
acceptState->addTransition(-1, true, currNfa_ptr->initial);
nfa_ptr->unsetAccept();
nfa_ptr->setAccept(currNfa_ptr->accepts);
nfa_ptr->addStates(currNfa_ptr->states);
}
}
return antlrcpp::Any();
}
/**
* @brief pathElt : pathPrimary pathMod? ;
*
* @param ctx pointer to pathElt's context
* @param nfa_ptr pointer to the NFA to be constructed
* @return antlrcpp::Any a dummy antlrcpp::Any object
*/
antlrcpp::Any
Rpq2NFAConvertor::visitPathElt(rpqParser::PathEltContext *ctx,
std::shared_ptr<NFA> nfa_ptr)
{
visitPathPrimary(ctx->pathPrimary(), nfa_ptr);
if (ctx->pathMod())
{
string elt = ctx->pathMod()->getText();
if (elt == "*" || elt == "+") {
// Backward edge of Kleene star
for (auto acceptState : nfa_ptr->accepts)
acceptState->addTransition(-1, true, nfa_ptr->initial);
}
if (elt != "+")
nfa_ptr->setAccept(nfa_ptr->initial); // Both ? and *
}
return antlrcpp::Any();
}
/**
* @brief pathPrimary : iri | '(' path ')' ;
* iri : '[' num_integer '-'? ']' ;
*
* @param ctx pointer to pathPrimary's context
* @param nfa_ptr pointer to the NFA to be constructed
* @return antlrcpp::Any a dummy antlrcpp::Any object
*/
antlrcpp::Any
Rpq2NFAConvertor::visitPathPrimary(rpqParser::PathPrimaryContext *ctx,
std::shared_ptr<NFA> nfa_ptr)
{
if (ctx->path())
visitPath(ctx->path(), nfa_ptr);
else if (ctx->iri())
{
int lbl_ = stoi(ctx->iri()->num_integer()->getText());
bool forward_ = true;
string iriStr = ctx->iri()->getText();
if (iriStr.length() > 2 && iriStr[iriStr.length() - 2] == '-')
forward_ = false;
nfa_ptr->unsetAccept();
shared_ptr<State> newAccept = nfa_ptr->addState(true); // TODO: addState
nfa_ptr->initial->addTransition(lbl_, forward_, newAccept);
nfa_ptr->setAccept(newAccept);
}
return antlrcpp::Any();
}