本文整理汇总了C++中NFA类的典型用法代码示例。如果您正苦于以下问题:C++ NFA类的具体用法?C++ NFA怎么用?C++ NFA使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NFA类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getNFA_AND
NFA getNFA_AND(NFA n1, NFA n2) {
int offset = n1.states.size();
vector<State> states;
NFA ret = n1;
for(int i = 0; i < n1.states.size(); i++) {
states.push_back(State(false));
if(n1.states[i].isAccepted())
ret.trans_func[i][NFA::lambda].push_back(n2.start_state + offset);
}
for(int i = 0; i < n2.states.size(); i++) {
ret.setTransFunc(i + offset, n2.trans_func[i]);
for(map<char, vector<int> >::iterator it = ret.trans_func[i+offset].begin();
it != ret.trans_func[i+offset].end(); it++) {
if(find(ret.alphabet.begin(), ret.alphabet.end(), it->first) == ret.alphabet.end())
ret.alphabet.push_back(it->first);
for(vector<int>::iterator jt = it->second.begin();
jt != it->second.end(); jt++) {
*jt = *jt + offset;
}
}
if(n2.states[i].isAccepted())
states.push_back(State(true));
else
states.push_back(State(false));
}
if(find(ret.alphabet.begin(), ret.alphabet.end(), NFA::lambda) == ret.alphabet.end())
ret.alphabet.push_back(NFA::lambda);
ret.setStateSet(states);
return ret;
}
示例2: parseAtom
// item: ATOM('+'|'*'|'?')
void Grammar::parseItem(string &ruleName, NFA **start, NFA **end)
{
parseAtom(ruleName, start, end);
assert(*start != NULL);
assert(*end != NULL);
// check to see wether repeator exist?
if (isMatch(TT_OP, "+")) {
(*end)->arc(*start);
advanceToken();
}
else if (isMatch(TT_OP, "*")) {
NFA *startState = new NFA();
NFA *endState = new NFA();
startState->arc(endState);
startState->arc(*start);
(*end)->arc(*start);
(*end)->arc(endState);
*start = startState;
*end = endState;
advanceToken();
}
else if (isMatch(TT_OP, "?")) {
NFA *endState = new NFA();
(*end)->arc(endState);
(*start)->arc(endState);
*end = endState;
advanceToken();
}
}
示例3: parseAtom
NFA RE2NFA::parsePiece()
{
NFA atom = parseAtom();
if (atom.isEmpty() || !hasNext())
return atom;
return parseMaybeQuantifier(atom);
}
示例4: assert
/// parse the alternative, such as alternative : items (| items)*
void Grammar::parseAlternative(string &ruleName, NFA **start, NFA **end)
{
assert(start != NULL);
assert(end != NULL);
// parse items
parseItems(ruleName, start, end);
if (isMatch(TT_OP, "|")) {
// make a closing state
NFA *closingStartState = new NFA();
NFA *closingEndState = new NFA;
closingStartState->arc(*start);
(*end)->arc(closingEndState);
while (isMatch(TT_OP, "|")) {
advanceToken();
NFA *startState = NULL;
NFA *endState = NULL;
parseItems(ruleName, &startState, &endState);
closingStartState->arc(startState);
endState->arc(closingEndState);
}
*start = closingStartState;
*end = closingEndState;
}
}
示例5: createSingleInputNFA
NFA NFA::createSingleInputNFA(InputType input)
{
NFA result;
result.initialize(2);
result.addTransition(result.initialState, input, result.finalState);
return result;
}
示例6: compileRuleList
Ref<CompiledContentExtension> compileRuleList(const String& ruleList)
{
auto parsedRuleList = parseRuleList(ruleList);
#if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
double nfaBuildTimeStart = monotonicallyIncreasingTime();
#endif
Vector<SerializedActionByte> actions;
Vector<unsigned> actionLocations = serializeActions(parsedRuleList, actions);
NFA nfa;
URLFilterParser urlFilterParser(nfa);
for (unsigned ruleIndex = 0; ruleIndex < parsedRuleList.size(); ++ruleIndex) {
const ContentExtensionRule& contentExtensionRule = parsedRuleList[ruleIndex];
const Trigger& trigger = contentExtensionRule.trigger();
ASSERT(trigger.urlFilter.length());
String error = urlFilterParser.addPattern(trigger.urlFilter, trigger.urlFilterIsCaseSensitive, actionLocations[ruleIndex]);
if (!error.isNull()) {
dataLogF("Error while parsing %s: %s\n", trigger.urlFilter.utf8().data(), error.utf8().data());
continue;
}
}
#if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
double nfaBuildTimeEnd = monotonicallyIncreasingTime();
dataLogF(" Time spent building the NFA: %f\n", (nfaBuildTimeEnd - nfaBuildTimeStart));
#endif
#if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
nfa.debugPrintDot();
#endif
#if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
double dfaBuildTimeStart = monotonicallyIncreasingTime();
#endif
const DFA dfa = NFAToDFA::convert(nfa);
#if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
double dfaBuildTimeEnd = monotonicallyIncreasingTime();
dataLogF(" Time spent building the DFA: %f\n", (dfaBuildTimeEnd - dfaBuildTimeStart));
#endif
// FIXME: never add a DFA that only matches the empty set.
#if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
dfa.debugPrintDot();
#endif
Vector<DFABytecode> bytecode;
DFABytecodeCompiler compiler(dfa, bytecode);
compiler.compile();
return CompiledContentExtension::create(WTF::move(bytecode), WTF::move(actions));
}
示例7: print
void CombinedURLFilters::processNFAs(size_t maxNFASize, std::function<void(NFA&&)> handler)
{
#if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
print();
#endif
while (true) {
// Traverse out to a leaf.
Vector<PrefixTreeVertex*, 128> stack;
PrefixTreeVertex* vertex = m_prefixTreeRoot.get();
while (true) {
ASSERT(vertex);
stack.append(vertex);
if (vertex->edges.isEmpty())
break;
vertex = vertex->edges.last().child.get();
}
if (stack.size() == 1)
break; // We're done once we have processed and removed all the edges in the prefix tree.
// Find the prefix root for this NFA. This is the vertex after the last term with a quantifier if there is one,
// or the root if there are no quantifiers left.
while (stack.size() > 1) {
if (!stack[stack.size() - 2]->edges.last().term.hasFixedLength())
break;
stack.removeLast();
}
ASSERT_WITH_MESSAGE(!stack.isEmpty(), "At least the root should be in the stack");
// Make an NFA with the subtrees for whom this is also the last quantifier (or who also have no quantifier).
NFA nfa;
// Put the prefix into the NFA.
unsigned prefixEnd = nfa.root();
for (unsigned i = 0; i < stack.size() - 1; ++i) {
ASSERT(!stack[i]->edges.isEmpty());
const PrefixTreeEdge& edge = stack[i]->edges.last();
prefixEnd = edge.term.generateGraph(nfa, prefixEnd, edge.child->finalActions);
}
// Put the non-quantified vertices in the subtree into the NFA and delete them.
ASSERT(stack.last());
generateNFAForSubtree(nfa, prefixEnd, *stack.last(), maxNFASize);
handler(WTF::move(nfa));
// Clean up any processed leaf nodes.
while (true) {
if (stack.size() > 1) {
if (stack[stack.size() - 1]->edges.isEmpty()) {
stack[stack.size() - 2]->edges.removeLast();
stack.removeLast();
} else
break; // Vertex is not a leaf.
} else
break; // Leave the empty root.
}
}
}
示例8: main
int main()
{
NFA a = NFA('a');
NFA b = NFA('b');
NFA x = NFA('b');
x = x + b + a + *(b|a);
x.show();
return 0;
}
示例9: foreach
NFA NFA::createStringNFA(const QByteArray &str)
{
NFA result;
foreach (char c, str) {
NFA ch = NFA::createSingleInputNFA(c);
if (result.isEmpty())
result = ch;
else
result = NFA::createConcatenatingNFA(result, ch);
}
示例10: NFA
unique_ptr<NFA> ConcatExp::buildNFA()
{
NFA* nfa = new NFA();
for (auto& child : m_childExps)
{
unique_ptr<NFA> cnfa = child->buildNFA();
nfa->merge(*cnfa, std::make_pair(nfa->endPoint(), 0));
}
return unique_ptr<NFA>(nfa);
}
示例11: parsePiece
NFA RE2NFA::parseBranch()
{
NFA value = parsePiece();
if (!hasNext())
return value;
NFA next;
do {
next = parsePiece();
if (!next.isEmpty())
value = NFA::createConcatenatingNFA(value, next);
} while (!next.isEmpty() && hasNext());
return value;
}
示例12: main
int main() {
NFA test1 = getNFAbyString("a");
NFA test2 = getNFA_Star(test1);
test2.print();
char s[1024], postfix[1024];
while(scanf("%s", s) == 1) {
trans(s, postfix);
puts(postfix);
NFA ret = calcPostfix(postfix);
ret.print();
}
return 0;
}
示例13: createConcatenatingNFA
NFA NFA::createConcatenatingNFA(const NFA &a, const NFA &b)
{
NFA result;
int initialA, finalA,
initialB, finalB;
result.initializeFromPair(a, b, &initialA, &finalA, &initialB, &finalB);
result.addTransition(result.initialState, Epsilon, initialA);
result.addTransition(finalA, Epsilon, initialB);
result.addTransition(finalB, Epsilon, result.finalState);
return result;
}
示例14: group_regex
unsigned long regex_parser::parse_regex_group(FILE *file, int group[]){
unsigned long size = _INFINITY;
do {
NFA *nfa = group_regex(file, group);
nfa->remove_epsilon();
nfa->reduce();
DFA *dfa = nfa->nfa2dfa();
delete nfa;
size = dfa->size();
delete dfa;
} while (0);
return size;
}
示例15: or_selection
NFA or_selection(vector<NFA> selections, int no_of_selections) {
NFA result;
int vertex_count = 2;
int i, j;
NFA med;
trans new_trans;
for(i = 0; i < no_of_selections; i++) {
vertex_count += selections.at(i).get_vertex_count();
}
result.set_vertex(vertex_count);
int adder_track = 1;
for(i = 0; i < no_of_selections; i++) {
result.set_transition(0, adder_track, '^');
med = selections.at(i);
for(j = 0; j < med.transitions.size(); j++) {
new_trans = med.transitions.at(j);
result.set_transition(new_trans.vertex_from + adder_track, new_trans.vertex_to + adder_track, new_trans.trans_symbol);
}
adder_track += med.get_vertex_count();
result.set_transition(adder_track - 1, vertex_count - 1, '^');
}
result.set_final_state(vertex_count - 1);
return result;
}