Compare commits
No commits in common. "e2942af12371aef66a1b0bb275a6f06f65333e9d" and "1d585ec51ebad22adfd01405e51af09ba7c1f033" have entirely different histories.
e2942af123
...
1d585ec51e
13 changed files with 0 additions and 3418 deletions
|
|
@ -1 +0,0 @@
|
||||||
g++ -std=c++20 regex dfa.cpp dfa_node.cpp main.cpp match.cpp nfa.cpp nfa_node.cpp regex.cpp
|
|
||||||
900
dfa.cpp
900
dfa.cpp
|
|
@ -1,900 +0,0 @@
|
||||||
//
|
|
||||||
// dfa.cpp
|
|
||||||
// lab2
|
|
||||||
//
|
|
||||||
// Created by Владислав Агибалов on 19.07.2025.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "dfa.hpp"
|
|
||||||
|
|
||||||
regex::dfa::dfa(): start_(nullptr){}
|
|
||||||
|
|
||||||
void regex::dfa::dfaGenerate(nfa const& nfa_){
|
|
||||||
|
|
||||||
std::map<std::set<std::shared_ptr<nfa_node>>, std::shared_ptr<dfa_node>> new_dfa_nodes_;
|
|
||||||
|
|
||||||
std::queue<std::set<std::shared_ptr<nfa_node>>> queue_;
|
|
||||||
|
|
||||||
std::set<std::set<std::shared_ptr<nfa_node>>> set_;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<nfa_node>> start_set_;
|
|
||||||
|
|
||||||
start_set_.emplace(nfa_.getStart());
|
|
||||||
|
|
||||||
start_ = std::make_shared<dfa_node>(dfa_node());
|
|
||||||
|
|
||||||
new_dfa_nodes_.emplace(start_set_, start_);
|
|
||||||
|
|
||||||
queue_.push(start_set_);
|
|
||||||
|
|
||||||
while(!queue_.empty()){
|
|
||||||
|
|
||||||
size_t old_size = set_.size();
|
|
||||||
|
|
||||||
set_.emplace(queue_.front());
|
|
||||||
|
|
||||||
size_t new_size = set_.size();
|
|
||||||
|
|
||||||
if(old_size == new_size){
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> current_dfa_node_ = new_dfa_nodes_.find(queue_.front())->second;
|
|
||||||
|
|
||||||
std::queue<std::shared_ptr<nfa_node>> future_nfa_;
|
|
||||||
|
|
||||||
for(auto i: new_dfa_nodes_.find(queue_.front())->first){
|
|
||||||
future_nfa_.push(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<nfa_node>> passed_nfa_;
|
|
||||||
|
|
||||||
std::map<size_t, std::set<std::shared_ptr<nfa_node>>> new_sets_;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<nfa_node>> passed_starts_;
|
|
||||||
|
|
||||||
while(!future_nfa_.empty()){
|
|
||||||
|
|
||||||
old_size = passed_nfa_.size();
|
|
||||||
|
|
||||||
passed_nfa_.emplace(future_nfa_.front());
|
|
||||||
|
|
||||||
new_size = passed_nfa_.size();
|
|
||||||
|
|
||||||
if(old_size == new_size){
|
|
||||||
|
|
||||||
future_nfa_.pop();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> current_nfa_ = future_nfa_.front();
|
|
||||||
|
|
||||||
if(current_nfa_->isEps()){
|
|
||||||
|
|
||||||
current_dfa_node_->setFinish(current_dfa_node_->getFinish() + current_nfa_->getFinish());
|
|
||||||
|
|
||||||
for(size_t i = 0; i < current_nfa_->size(); ++i){
|
|
||||||
|
|
||||||
future_nfa_.push((*current_nfa_)[i].lock());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
size_t symbol_ = current_nfa_->getSymbol();
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> next_ = (*current_nfa_)[0].lock();
|
|
||||||
|
|
||||||
if(new_sets_.find(symbol_) == new_sets_.end()){
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<nfa_node>> new_set_;
|
|
||||||
|
|
||||||
new_set_.emplace(next_);
|
|
||||||
|
|
||||||
new_sets_.emplace(symbol_, new_set_);
|
|
||||||
|
|
||||||
passed_starts_.emplace(current_nfa_);
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
new_sets_.find(symbol_)->second.emplace(next_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
future_nfa_.pop();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto i: new_sets_){
|
|
||||||
|
|
||||||
if(new_dfa_nodes_.find(i.second) == new_dfa_nodes_.end())
|
|
||||||
|
|
||||||
new_dfa_nodes_.emplace(i.second, new dfa_node);
|
|
||||||
|
|
||||||
current_dfa_node_->addEdge(i.first, new_dfa_nodes_.find(i.second)->second);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(current_dfa_node_->getFinish()){
|
|
||||||
|
|
||||||
finish_nodes_.emplace(current_dfa_node_);
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
common_nodes_.emplace(current_dfa_node_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
all_nodes_.emplace(current_dfa_node_);
|
|
||||||
|
|
||||||
for(auto i: new_sets_){
|
|
||||||
|
|
||||||
queue_.push(i.second);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::dfa::minimization(dfa const& dfa_){
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<dfa_node>, std::set<std::shared_ptr<dfa_node>>> included_in_set_ = generateSets(dfa_);
|
|
||||||
|
|
||||||
std::set<std::set<std::shared_ptr<dfa_node>>> passed_sets_;
|
|
||||||
|
|
||||||
std::queue<std::set<std::shared_ptr<dfa_node>>> sets_queue_;
|
|
||||||
|
|
||||||
std::map<std::set<std::shared_ptr<dfa_node>>, std::shared_ptr<dfa_node>> new_nodes_;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> start_set_ = included_in_set_.find(dfa_.start_)->second;
|
|
||||||
|
|
||||||
sets_queue_.push(start_set_);
|
|
||||||
|
|
||||||
start_ = std::make_shared<dfa_node>(dfa_node());
|
|
||||||
|
|
||||||
new_nodes_.emplace(start_set_, start_);
|
|
||||||
|
|
||||||
while(!sets_queue_.empty()){
|
|
||||||
|
|
||||||
size_t old_size_ = passed_sets_.size();
|
|
||||||
|
|
||||||
passed_sets_.emplace(sets_queue_.front());
|
|
||||||
|
|
||||||
size_t new_size_ = passed_sets_.size();
|
|
||||||
|
|
||||||
if(old_size_ != new_size_){
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> current_set_ = sets_queue_.front();
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> current_dfa_ = new_nodes_.find(current_set_)->second;
|
|
||||||
|
|
||||||
for(auto i: current_set_){
|
|
||||||
|
|
||||||
current_dfa_->setFinish(current_dfa_->getFinish() + i->getFinish());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> current_node_ = *current_set_.begin();
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> next_nodes_ = current_node_->getnextstates();
|
|
||||||
|
|
||||||
for(auto i: next_nodes_){
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> next_set_ = included_in_set_.find(i.second.lock())->second;
|
|
||||||
|
|
||||||
if(new_nodes_.find(next_set_) == new_nodes_.end()){
|
|
||||||
|
|
||||||
new_nodes_.emplace(next_set_, new dfa_node);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
current_dfa_->addEdge(i.first, new_nodes_.find(next_set_)->second);
|
|
||||||
|
|
||||||
|
|
||||||
if(next_set_ != sets_queue_.front())
|
|
||||||
|
|
||||||
sets_queue_.push(next_set_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(current_dfa_->getFinish())
|
|
||||||
|
|
||||||
finish_nodes_.emplace(current_dfa_);
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
common_nodes_.emplace(current_dfa_);
|
|
||||||
|
|
||||||
all_nodes_.emplace(current_dfa_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
sets_queue_.pop();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<regex::dfa_node>, std::set<std::shared_ptr<regex::dfa_node>>> regex::dfa::generateSets(dfa const& dfa_) const{
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<dfa_node>, std::set<std::shared_ptr<dfa_node>>> included_in_set_;
|
|
||||||
|
|
||||||
std::set<std::set<std::shared_ptr<dfa_node>>> set_of_set_;
|
|
||||||
|
|
||||||
std::queue<std::set<std::shared_ptr<dfa_node>>> queue_;
|
|
||||||
|
|
||||||
if(dfa_.common_nodes_.size())
|
|
||||||
|
|
||||||
set_of_set_.emplace(dfa_.common_nodes_);
|
|
||||||
|
|
||||||
set_of_set_.emplace(dfa_.finish_nodes_);
|
|
||||||
|
|
||||||
for(auto i: dfa_.common_nodes_){
|
|
||||||
|
|
||||||
included_in_set_.emplace(i, dfa_.common_nodes_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto i: dfa_.finish_nodes_){
|
|
||||||
|
|
||||||
included_in_set_.emplace(i, dfa_.finish_nodes_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t old_size_ = (bool)(dfa_.common_nodes_.size()) + (bool)(dfa_.finish_nodes_.size()), new_size_ = 0;
|
|
||||||
|
|
||||||
while (old_size_ != new_size_) {
|
|
||||||
|
|
||||||
old_size_ = set_of_set_.size();
|
|
||||||
|
|
||||||
for(auto i: set_of_set_){
|
|
||||||
|
|
||||||
queue_.push(i);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
while(!queue_.empty()){
|
|
||||||
|
|
||||||
for(auto current_letter_: getAlphabet(queue_.front())){
|
|
||||||
|
|
||||||
std::map<std::set<std::shared_ptr<dfa_node>>, std::set<std::shared_ptr<dfa_node>>> new_sets_;
|
|
||||||
|
|
||||||
for(auto current_node_: queue_.front()){
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> const& next_nodes_ = current_node_->getnextstates();
|
|
||||||
|
|
||||||
if(next_nodes_.find(current_letter_) == next_nodes_.end()){
|
|
||||||
|
|
||||||
if(new_sets_.find(std::set<std::shared_ptr<dfa_node>>()) == new_sets_.end()){
|
|
||||||
|
|
||||||
new_sets_.emplace(std::set<std::shared_ptr<dfa_node>>(), std::set<std::shared_ptr<dfa_node>>());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
new_sets_.find(std::set<std::shared_ptr<dfa_node>>())->second.emplace(current_node_);
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> next_set_ = included_in_set_.find(next_nodes_.find(current_letter_)->second.lock())->second;
|
|
||||||
|
|
||||||
if(new_sets_.find(next_set_) == new_sets_.end()){
|
|
||||||
|
|
||||||
new_sets_.emplace(next_set_, std::set<std::shared_ptr<dfa_node>>());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
new_sets_.find(next_set_)->second.emplace(current_node_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(new_sets_.size() > 1){
|
|
||||||
|
|
||||||
set_of_set_.erase(queue_.front());
|
|
||||||
|
|
||||||
for(auto i: new_sets_){
|
|
||||||
|
|
||||||
for(auto j: i.second){
|
|
||||||
|
|
||||||
included_in_set_.find(j)->second = i.second;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_.push(i.second);
|
|
||||||
|
|
||||||
set_of_set_.emplace(i.second);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
new_size_ = set_of_set_.size();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return included_in_set_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::set<size_t> regex::dfa::getAlphabet(std::set<std::shared_ptr<dfa_node>> const& nodes_set_){
|
|
||||||
|
|
||||||
std::set<size_t> result_;
|
|
||||||
|
|
||||||
for(auto currnet_node_: nodes_set_){
|
|
||||||
|
|
||||||
for(auto current_edge_: currnet_node_->getnextstates()){
|
|
||||||
|
|
||||||
result_.emplace(current_edge_.first);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return result_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::dfa::print(std::string const & file_name_) const{
|
|
||||||
|
|
||||||
std::ofstream out_;
|
|
||||||
|
|
||||||
out_.open(file_name_ + ".dot");
|
|
||||||
|
|
||||||
out_<< "digraph NFA{" << std::endl;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> set_;
|
|
||||||
|
|
||||||
std::queue<std::shared_ptr<dfa_node>> queue_;
|
|
||||||
|
|
||||||
queue_.push(start_);
|
|
||||||
|
|
||||||
while (!queue_.empty() ) {
|
|
||||||
|
|
||||||
size_t old_size_ = set_.size();
|
|
||||||
|
|
||||||
set_.insert(queue_.front());
|
|
||||||
|
|
||||||
size_t new_size_ = set_.size();
|
|
||||||
|
|
||||||
if(old_size_ == new_size_){
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
out_ << " \"node" << queue_.front() << "\"";
|
|
||||||
|
|
||||||
out_ << "[";
|
|
||||||
|
|
||||||
if(queue_.front()->getFinish()){
|
|
||||||
|
|
||||||
out_ << "shape = box, ";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
out_ << "label=\"" << set_.size() << "\"]\n";
|
|
||||||
|
|
||||||
for(auto i: queue_.front()->getnextstates()){
|
|
||||||
|
|
||||||
out_ << " \"node" << queue_.front() << "\" -> \"node" << i.second.lock() << "\"";
|
|
||||||
|
|
||||||
out_ << "[label=\"" << (char)i.first << "\"]\n";
|
|
||||||
|
|
||||||
queue_.push(i.second.lock());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
out_<< "}";
|
|
||||||
|
|
||||||
out_.close();
|
|
||||||
|
|
||||||
std::string siscall("/opt/homebrew/bin/dot -Tpng " + file_name_ + ".dot -o " + file_name_ + ".png");
|
|
||||||
|
|
||||||
system(siscall.data());
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t regex::dfa::find(std::string const &recognazing_text_, match& match_) const{
|
|
||||||
|
|
||||||
if(start_->getFinish() && recognazing_text_ == ""){
|
|
||||||
|
|
||||||
match_.setMatched(std::string(""));
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* text_ = recognazing_text_.data();
|
|
||||||
|
|
||||||
std::weak_ptr<dfa_node> current_dfa_ = start_;
|
|
||||||
|
|
||||||
size_t begin_ = 0;
|
|
||||||
|
|
||||||
size_t cur_pos_ = 0;
|
|
||||||
|
|
||||||
while(text_[cur_pos_] != 0){
|
|
||||||
|
|
||||||
char symbol_ = text_[cur_pos_];
|
|
||||||
|
|
||||||
std::weak_ptr<regex::dfa_node> next_state = (*current_dfa_.lock())[symbol_];
|
|
||||||
|
|
||||||
if(next_state.lock() == nullptr && current_dfa_.lock()->getFinish()){
|
|
||||||
|
|
||||||
match_.setMatched(std::string(recognazing_text_.begin() + begin_, recognazing_text_.begin() + cur_pos_));
|
|
||||||
|
|
||||||
return cur_pos_ + 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
++cur_pos_;
|
|
||||||
|
|
||||||
if(next_state.lock() == nullptr)
|
|
||||||
|
|
||||||
return cur_pos_ + 1;
|
|
||||||
|
|
||||||
else
|
|
||||||
current_dfa_ = next_state;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::weak_ptr<regex::dfa_node> next_state = (*current_dfa_.lock())[text_[cur_pos_]];
|
|
||||||
|
|
||||||
if(next_state.lock() == nullptr && current_dfa_.lock()->getFinish()){
|
|
||||||
|
|
||||||
match_.setMatched(std::string(recognazing_text_.begin() + begin_, recognazing_text_.begin() + cur_pos_));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return cur_pos_ + 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::dfa regex::dfa::copy() const{
|
|
||||||
|
|
||||||
dfa new_dfa_;
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>> new_nodes_;
|
|
||||||
|
|
||||||
for(auto i: all_nodes_){
|
|
||||||
|
|
||||||
new_nodes_[i] = std::make_shared<dfa_node>(dfa_node());
|
|
||||||
|
|
||||||
new_dfa_.all_nodes_.emplace(new_nodes_[i]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto i: common_nodes_){
|
|
||||||
|
|
||||||
new_dfa_.common_nodes_.emplace(new_nodes_[i]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto i: finish_nodes_){
|
|
||||||
|
|
||||||
new_nodes_[i]->setFinish(i->getFinish());
|
|
||||||
|
|
||||||
new_dfa_.finish_nodes_.emplace(new_nodes_[i]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
new_dfa_.start_ = new_nodes_[start_];
|
|
||||||
|
|
||||||
for(auto i: all_nodes_){
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> next_states_ = i->getnextstates();
|
|
||||||
|
|
||||||
for(auto j: next_states_){
|
|
||||||
|
|
||||||
new_nodes_[i]->addEdge(j.first, (new_nodes_[j.second.lock()]));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return new_dfa_;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string regex::dfa::rebirth() const{
|
|
||||||
|
|
||||||
std::pair<std::map<std::shared_ptr<dfa_node>, size_t>, std::vector<std::vector<std::pair<bool, std::string>>>> id_s_matrix_ = matrix_generate();
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<dfa_node>, size_t> id_s_ = id_s_matrix_.first;
|
|
||||||
|
|
||||||
std::vector<std::vector<std::pair<bool, std::string>>> matrix_ = id_s_matrix_.second;
|
|
||||||
|
|
||||||
std::queue<size_t> queue_;
|
|
||||||
|
|
||||||
queue_.push(id_s_[start_]);
|
|
||||||
|
|
||||||
std::set<size_t> deleted_;
|
|
||||||
|
|
||||||
while(!queue_.empty()){
|
|
||||||
|
|
||||||
size_t node_to_be_deleted_ = queue_.front();
|
|
||||||
|
|
||||||
size_t old_size_ = deleted_.size();
|
|
||||||
|
|
||||||
deleted_.emplace(node_to_be_deleted_);
|
|
||||||
|
|
||||||
size_t new_size_ = deleted_.size();
|
|
||||||
|
|
||||||
if(old_size_ == new_size_){
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t node_before_ = 0; node_before_ < matrix_.size(); ++node_before_){
|
|
||||||
|
|
||||||
if(node_before_ == node_to_be_deleted_)
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(matrix_[node_before_][node_to_be_deleted_].first){
|
|
||||||
|
|
||||||
std::string new_expr_ = matrix_[node_before_][node_to_be_deleted_].second;
|
|
||||||
|
|
||||||
if(matrix_[node_to_be_deleted_][node_to_be_deleted_].first){
|
|
||||||
|
|
||||||
new_expr_ += "(?:" + matrix_[node_to_be_deleted_][node_to_be_deleted_].second + ")*";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t node_after_ = 1; node_after_ < matrix_.size(); ++node_after_){
|
|
||||||
|
|
||||||
if(matrix_[node_to_be_deleted_][node_after_].first){
|
|
||||||
|
|
||||||
if(node_after_ > 1)
|
|
||||||
|
|
||||||
queue_.push(node_after_);
|
|
||||||
|
|
||||||
std::string str_ = new_expr_ + matrix_[node_to_be_deleted_][node_after_].second;
|
|
||||||
|
|
||||||
if(matrix_[node_before_][node_after_].first){
|
|
||||||
|
|
||||||
if(!matrix_[node_before_][node_after_].second.size()){
|
|
||||||
|
|
||||||
matrix_[node_before_][node_after_].second = str_;
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
matrix_[node_before_][node_after_].second = "(?:" + matrix_[node_before_][node_after_].second + "|" + str_ + ")";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
matrix_[node_before_][node_after_].first = 1;
|
|
||||||
|
|
||||||
matrix_[node_before_][node_after_].second = str_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
for(size_t clear_ = 0; clear_ < matrix_.size(); ++clear_){
|
|
||||||
|
|
||||||
matrix_[node_to_be_deleted_][clear_].first = 0;
|
|
||||||
|
|
||||||
matrix_[clear_][node_to_be_deleted_].first = 0;
|
|
||||||
|
|
||||||
matrix_[node_to_be_deleted_][clear_].second = "$";
|
|
||||||
|
|
||||||
matrix_[clear_][node_to_be_deleted_].second = "$";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return matrix_[0][1].second;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<std::map<std::shared_ptr<regex::dfa_node>, size_t>, std::vector<std::vector<std::pair<bool, std::string>>>> regex::dfa::matrix_generate() const{
|
|
||||||
|
|
||||||
std::vector<std::vector<std::pair<bool, std::string>>> matrix_;
|
|
||||||
|
|
||||||
std::vector<std::pair<bool, std::string>> line_;
|
|
||||||
|
|
||||||
for(size_t i = 0; i < all_nodes_.size() + 2; ++i){
|
|
||||||
|
|
||||||
line_.push_back(std::make_pair<bool, std::string>(0, "$"));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t i = 0; i < all_nodes_.size() + 2; ++i){
|
|
||||||
|
|
||||||
matrix_.push_back(line_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<dfa_node>, size_t> id_s_;
|
|
||||||
|
|
||||||
size_t id_ = 2;
|
|
||||||
|
|
||||||
for(auto current_node_: finish_nodes_){
|
|
||||||
|
|
||||||
id_s_[current_node_] = id_++;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto current_node_: common_nodes_){
|
|
||||||
|
|
||||||
id_s_[current_node_] = id_++;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
matrix_[0][id_s_[start_]].first = 1;
|
|
||||||
|
|
||||||
for(auto current_finish_: finish_nodes_){
|
|
||||||
|
|
||||||
matrix_[id_s_[current_finish_]][1].first = 1;
|
|
||||||
|
|
||||||
for(auto next_node_: current_finish_->getnextstates()){
|
|
||||||
|
|
||||||
auto& cur_pair_ = matrix_[id_s_[current_finish_]][id_s_[next_node_.second.lock()]];
|
|
||||||
|
|
||||||
cur_pair_.first = 1;
|
|
||||||
|
|
||||||
std::string symbol(&next_node_.first, &next_node_.first + 1);
|
|
||||||
|
|
||||||
std::weak_ptr<regex::dfa_node> next_ = (*next_node_.second.lock())[next_node_.first];
|
|
||||||
|
|
||||||
if((current_finish_ != next_node_.second.lock() && next_.lock() != next_node_.second.lock()) || current_finish_ == next_node_.second.lock()){
|
|
||||||
|
|
||||||
if(cur_pair_.second == "$")
|
|
||||||
|
|
||||||
cur_pair_.second = symbol;
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
cur_pair_.second += "|" + symbol;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto& cur_pair_: matrix_[id_s_[current_finish_]]){
|
|
||||||
|
|
||||||
if(cur_pair_.second.size() > 1){
|
|
||||||
|
|
||||||
cur_pair_.second = "(" + cur_pair_.second + ")";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto current_node_: common_nodes_){
|
|
||||||
|
|
||||||
for(auto next_node_: current_node_->getnextstates()){
|
|
||||||
|
|
||||||
auto& cur_pair_ = matrix_[id_s_[current_node_]][id_s_[next_node_.second.lock()]];
|
|
||||||
|
|
||||||
cur_pair_.first = 1;
|
|
||||||
|
|
||||||
std::string symbol(&next_node_.first, &next_node_.first + 1);
|
|
||||||
|
|
||||||
std::weak_ptr<regex::dfa_node> next_ = (*next_node_.second.lock())[next_node_.first];
|
|
||||||
|
|
||||||
if((current_node_ != next_node_.second.lock() && next_.lock() != next_node_.second.lock()) || current_node_ == next_node_.second.lock()){
|
|
||||||
|
|
||||||
if(cur_pair_.second == "$")
|
|
||||||
|
|
||||||
cur_pair_.second = symbol;
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
cur_pair_.second += "|" + symbol;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto& cur_pair_: matrix_[id_s_[current_node_]]){
|
|
||||||
|
|
||||||
if(cur_pair_.second.size() > 1){
|
|
||||||
|
|
||||||
cur_pair_.second = "(" + cur_pair_.second + ")";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_pair(id_s_, matrix_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::dfa::operator==(dfa const & second_) const{
|
|
||||||
|
|
||||||
std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>> start_pair_ = std::make_pair(start_, second_.start_);
|
|
||||||
|
|
||||||
std::queue<std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>>> queue_;
|
|
||||||
|
|
||||||
std::set<std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>>> visited_;
|
|
||||||
|
|
||||||
queue_.push(start_pair_);
|
|
||||||
|
|
||||||
while(!queue_.empty()){
|
|
||||||
|
|
||||||
size_t old_size = visited_.size();
|
|
||||||
|
|
||||||
visited_.emplace(queue_.front());
|
|
||||||
|
|
||||||
size_t new_size = visited_.size();
|
|
||||||
|
|
||||||
if(old_size == new_size){
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> next_nodes_first_ = queue_.front().first->getnextstates();
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> next_nodes_second_ = queue_.front().second->getnextstates();
|
|
||||||
|
|
||||||
if(next_nodes_first_.size() != next_nodes_second_.size())
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
auto first_ = next_nodes_first_.begin();
|
|
||||||
|
|
||||||
auto second_ = next_nodes_second_.begin();
|
|
||||||
|
|
||||||
for(size_t i = 0; i < next_nodes_first_.size(); ++i){
|
|
||||||
|
|
||||||
if(first_->first == second_->first){
|
|
||||||
|
|
||||||
queue_.push(std::make_pair(first_->second.lock(), second_->second.lock()));
|
|
||||||
|
|
||||||
first_++;
|
|
||||||
|
|
||||||
second_++;
|
|
||||||
|
|
||||||
} else
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::dfa regex::dfa::intersection(regex::dfa const & second_){
|
|
||||||
|
|
||||||
dfa new_dfa_;
|
|
||||||
|
|
||||||
std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>> start_pair_ = std::make_pair(start_, second_.start_);
|
|
||||||
|
|
||||||
std::map<std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>>, std::shared_ptr<dfa_node>> new_nodes_;
|
|
||||||
|
|
||||||
std::queue<std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>>> queue_;
|
|
||||||
|
|
||||||
std::set<std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>>> vizited_;
|
|
||||||
|
|
||||||
queue_.push(start_pair_);
|
|
||||||
|
|
||||||
new_nodes_.emplace(start_pair_, std::make_shared<dfa_node>(dfa_node()));
|
|
||||||
|
|
||||||
new_dfa_.start_ = new_nodes_[start_pair_];
|
|
||||||
|
|
||||||
while(!queue_.empty()){
|
|
||||||
|
|
||||||
size_t old_size_ = vizited_.size();
|
|
||||||
|
|
||||||
vizited_.emplace(queue_.front());
|
|
||||||
|
|
||||||
size_t new_size_ = vizited_.size();
|
|
||||||
|
|
||||||
if(old_size_ == new_size_){
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> current_node_ = new_nodes_[queue_.front()];
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> first_node_ = queue_.front().first;
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> first_next_nodes_ = first_node_->getnextstates();
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> second_node_ = queue_.front().second;
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> second_next_nodes_ = second_node_->getnextstates();
|
|
||||||
|
|
||||||
size_t min_count_ = std::min(first_next_nodes_.size(), second_next_nodes_.size());
|
|
||||||
|
|
||||||
auto current_edge_it_ = first_next_nodes_.begin();
|
|
||||||
|
|
||||||
for(size_t edge_ = 0; edge_ < min_count_; ++edge_){
|
|
||||||
|
|
||||||
auto current_edge_ = *current_edge_it_;
|
|
||||||
|
|
||||||
if(second_next_nodes_.contains(current_edge_.first)){
|
|
||||||
|
|
||||||
std::pair<std::shared_ptr<dfa_node>, std::shared_ptr<dfa_node>> next_pair_ = std::make_pair(current_edge_.second.lock(), second_next_nodes_[current_edge_.first].lock());
|
|
||||||
|
|
||||||
if(!new_nodes_.contains(next_pair_))
|
|
||||||
|
|
||||||
new_nodes_.emplace(next_pair_, std::make_shared<dfa_node>(dfa_node()));
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> next_node_ = new_nodes_[next_pair_];
|
|
||||||
|
|
||||||
next_node_->setFinish(next_pair_.first->getFinish() * next_pair_.second->getFinish());
|
|
||||||
|
|
||||||
current_node_->addEdge(current_edge_.first, next_node_);
|
|
||||||
|
|
||||||
queue_.push(next_pair_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
++current_edge_it_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
new_dfa_.all_nodes_.emplace(current_node_);
|
|
||||||
|
|
||||||
if(current_node_->getFinish())
|
|
||||||
|
|
||||||
new_dfa_.finish_nodes_.emplace(current_node_);
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
new_dfa_.common_nodes_.emplace(current_node_);
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return new_dfa_;
|
|
||||||
|
|
||||||
}
|
|
||||||
59
dfa.hpp
59
dfa.hpp
|
|
@ -1,59 +0,0 @@
|
||||||
//
|
|
||||||
// dfa.hpp
|
|
||||||
// lab2
|
|
||||||
//
|
|
||||||
// Created by Владислав Агибалов on 19.07.2025.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef dfa_hpp
|
|
||||||
#define dfa_hpp
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "dfa_node.hpp"
|
|
||||||
#include "nfa.hpp"
|
|
||||||
#include "match.hpp"
|
|
||||||
|
|
||||||
namespace regex {
|
|
||||||
|
|
||||||
class dfa{
|
|
||||||
|
|
||||||
std::shared_ptr<dfa_node> start_;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> all_nodes_;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> common_nodes_;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<dfa_node>> finish_nodes_;
|
|
||||||
|
|
||||||
static std::set<size_t> getAlphabet(std::set<std::shared_ptr<dfa_node>> const&);
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<dfa_node>, std::set<std::shared_ptr<dfa_node>>> generateSets(dfa const&) const;
|
|
||||||
|
|
||||||
std::pair<std::map<std::shared_ptr<dfa_node>, size_t>, std::vector<std::vector<std::pair<bool, std::string>>>> matrix_generate() const;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
dfa();
|
|
||||||
|
|
||||||
void dfaGenerate(nfa const&);
|
|
||||||
|
|
||||||
void minimization(dfa const&);
|
|
||||||
|
|
||||||
void print(std::string const &) const;
|
|
||||||
|
|
||||||
size_t find(std::string const &, match&) const;
|
|
||||||
|
|
||||||
dfa copy() const;
|
|
||||||
|
|
||||||
std::string rebirth() const;
|
|
||||||
|
|
||||||
bool operator==(const dfa&) const;
|
|
||||||
|
|
||||||
dfa intersection(const dfa&);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* dfa_hpp */
|
|
||||||
54
dfa_node.cpp
54
dfa_node.cpp
|
|
@ -1,54 +0,0 @@
|
||||||
//
|
|
||||||
// dfa_node.cpp
|
|
||||||
// lab2
|
|
||||||
//
|
|
||||||
// Created by Владислав Агибалов on 19.07.2025.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "dfa_node.hpp"
|
|
||||||
|
|
||||||
regex::dfa_node::dfa_node(): finish_(0){}
|
|
||||||
|
|
||||||
void regex::dfa_node::setFinish(bool f_){
|
|
||||||
|
|
||||||
finish_ = f_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::dfa_node::getFinish() const{
|
|
||||||
|
|
||||||
return finish_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::dfa_node::addEdge(size_t symbol_, std::weak_ptr<dfa_node> next_){
|
|
||||||
|
|
||||||
if(next_dfa_states_.find(symbol_) == next_dfa_states_.end()){
|
|
||||||
|
|
||||||
next_dfa_states_.emplace(symbol_, next_);
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
if(next_dfa_states_.find(symbol_)->second.lock() != next_.lock())
|
|
||||||
|
|
||||||
throw std::runtime_error("переход по такому символу уже есть.");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<regex::dfa_node>> const& regex::dfa_node::getnextstates() const{
|
|
||||||
|
|
||||||
return next_dfa_states_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::weak_ptr<regex::dfa_node> regex::dfa_node::operator[](size_t symbol_) const{
|
|
||||||
|
|
||||||
if(next_dfa_states_.contains(symbol_))
|
|
||||||
|
|
||||||
return next_dfa_states_.find(symbol_)->second;
|
|
||||||
|
|
||||||
return null_node_;
|
|
||||||
|
|
||||||
}
|
|
||||||
43
dfa_node.hpp
43
dfa_node.hpp
|
|
@ -1,43 +0,0 @@
|
||||||
//
|
|
||||||
// dfa_node.hpp
|
|
||||||
// lab2
|
|
||||||
//
|
|
||||||
// Created by Владислав Агибалов on 19.07.2025.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef dfa_node_hpp
|
|
||||||
#define dfa_node_hpp
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "node.hpp"
|
|
||||||
|
|
||||||
namespace regex {
|
|
||||||
|
|
||||||
class dfa_node: public node{
|
|
||||||
|
|
||||||
bool finish_;
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> next_dfa_states_;
|
|
||||||
|
|
||||||
inline static std::shared_ptr<dfa_node> null_node_ = nullptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
dfa_node();
|
|
||||||
|
|
||||||
std::map<size_t, std::weak_ptr<dfa_node>> const& getnextstates() const;
|
|
||||||
|
|
||||||
void setFinish(bool);
|
|
||||||
|
|
||||||
bool getFinish() const;
|
|
||||||
|
|
||||||
void addEdge(size_t, std::weak_ptr<dfa_node>);
|
|
||||||
|
|
||||||
std::weak_ptr<dfa_node> operator[](size_t) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* dfa_node_hpp */
|
|
||||||
Binary file not shown.
47
main.cpp
47
main.cpp
|
|
@ -1,47 +0,0 @@
|
||||||
#include <iostream>
|
|
||||||
#include "regex.hpp"
|
|
||||||
|
|
||||||
/*
|
|
||||||
Разработать библиотеку для работы с регулярными выражениями. В качестве алфавита могут использовать любые печатные символы, метасимволы экранируются символом ‘\’.
|
|
||||||
|
|
||||||
O Кроме этого, должен поддерживаться символ, определяющий пустую подстроку ‘$’
|
|
||||||
|
|
||||||
O Регулярный выражения должны поддерживать следующие операции:
|
|
||||||
|
|
||||||
O Операция ‘или’: r1|r2 (метасимвол ‘|’)
|
|
||||||
|
|
||||||
O Операция ‘конкатенация’: r1r2
|
|
||||||
|
|
||||||
O Операция ‘замыкание Клини’: r* (метасимвол ‘*’)
|
|
||||||
|
|
||||||
O Операция ‘символ из набора’: [a1a2a3…] (метасимвол ‘[]’)
|
|
||||||
|
|
||||||
O Операция ‘повтор выражения’: r{x} (метасимвол ‘{х}’, где x – количество повторов)
|
|
||||||
|
|
||||||
O Операция ‘нумерованная группа захвата’: (r) (метасимвол ‘()’), выражения из групп захвата нумеруются в порядке их следования
|
|
||||||
|
|
||||||
------
|
|
||||||
X Операция ‘выражение из нумерованной группы захвата’: \n (метасимвол ‘\’, n – номер группы захвата)
|
|
||||||
------
|
|
||||||
O В регулярных выражениях могут использоваться операторные скобки (:r), определяющие приоритет операторов. (метасимвол ‘(: )’), т.е. если в скобках первых идет символ ‘:’, то это отменяет захват выражения.
|
|
||||||
|
|
||||||
Библиотека должна поддерживать следующие операции:
|
|
||||||
|
|
||||||
O findall – поиск всех непересекающихся вхождений подстрок в строку соответствующих регулярному выражению с перегрузкой без доступа и с доступом к группам захвата через специальный объект (через итератор и оператор индексации).
|
|
||||||
|
|
||||||
O Регулярные выражения могут быть заранее скомпилированы в ДКА через НКА (РВ->НКА->ДКА->минимальный ДКА), т.е. должен поддерживаться метод compile(), при этом допустим вызов операций с регулярными выражениями, как со скомпилированным объектом, так и со строковым представлением.
|
|
||||||
|
|
||||||
Для скомпилированных автоматов библиотека должна поддерживать операцию восстановления регулярного выражения методом исключения состояний.
|
|
||||||
|
|
||||||
Библиотека должна поддерживать операцию построения дополнения к языку, задаваемому регулярным выражением (скомпилированным ДКА).
|
|
||||||
|
|
||||||
Библиотека должна поддерживать операцию построения пересечения языков, задаваемых регулярными выражениями (скомпилированными ДКА).
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <regex>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
192
match.cpp
192
match.cpp
|
|
@ -1,192 +0,0 @@
|
||||||
//
|
|
||||||
// match.cpp
|
|
||||||
// lab2
|
|
||||||
//
|
|
||||||
// Created by Владислав Агибалов on 23.07.2025.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "match.hpp"
|
|
||||||
|
|
||||||
regex::groupiterator::groupiterator(const match* mat_, size_t index_): match_(mat_), group_index_(index_){}
|
|
||||||
|
|
||||||
regex::groupiterator& regex::groupiterator::operator++(){
|
|
||||||
|
|
||||||
if (group_index_ < match_->size()){
|
|
||||||
|
|
||||||
++group_index_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::groupiterator regex::groupiterator::operator++(int){
|
|
||||||
|
|
||||||
groupiterator tmp = *this;
|
|
||||||
|
|
||||||
operator++();
|
|
||||||
|
|
||||||
return tmp;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string_view regex::groupiterator::operator*() const{
|
|
||||||
|
|
||||||
return (*match_)[group_index_];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::groupiterator::operator == (const groupiterator& second_) const {
|
|
||||||
|
|
||||||
return ((match_ == second_.match_) && (group_index_ == second_.group_index_));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::groupiterator::operator != (const regex::groupiterator& second_) const {
|
|
||||||
|
|
||||||
return !(*this == second_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t regex::groupiterator::groupindex() const{
|
|
||||||
|
|
||||||
return group_index_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::groupiterator::isMatched() const{
|
|
||||||
|
|
||||||
return match_->isMatched();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::groupiterator regex::match::begin() const{
|
|
||||||
|
|
||||||
return groupiterator(this, 0);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::groupiterator regex::match::end() const {
|
|
||||||
|
|
||||||
return groupiterator(this, groups_.size());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::match::match(){
|
|
||||||
|
|
||||||
groups_.push_back(std::make_tuple(0, 0, 0, 0));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::match::operator<(const match& second_) const{
|
|
||||||
|
|
||||||
return groups_.size() < second_.groups_.size();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::match::operator>(const match& second_) const{
|
|
||||||
|
|
||||||
return groups_.size() > second_.groups_.size();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::match::operator==(const match& second_) const{
|
|
||||||
|
|
||||||
return groups_.size() == second_.groups_.size();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::match::match(size_t size_){
|
|
||||||
|
|
||||||
for(size_t i = 0; i < size_ + 1; ++i){
|
|
||||||
|
|
||||||
groups_.push_back(std::make_tuple(0, 0, 0, 0));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t regex::match::length() const{
|
|
||||||
|
|
||||||
return matched_text_.size();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t regex::match::size() const{
|
|
||||||
|
|
||||||
return groups_.size();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::match::isMatched() const{
|
|
||||||
|
|
||||||
if(groups_.size())
|
|
||||||
|
|
||||||
return (std::get<0>(groups_[0]));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::match::setMatched(std::string const & matched_){
|
|
||||||
|
|
||||||
if(!groups_.size())
|
|
||||||
|
|
||||||
groups_.push_back(std::make_tuple(0, 0, 0, 0));
|
|
||||||
|
|
||||||
groups_[0] = std::make_tuple(1, 0, matched_text_.size(), 0);
|
|
||||||
|
|
||||||
matched_text_ = matched_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::match::addGroupStart(size_t group_, size_t start_){
|
|
||||||
|
|
||||||
if(groups_.size() < (group_ + 1))
|
|
||||||
|
|
||||||
for(size_t i = groups_.size(); i < group_ + 1; ++i)
|
|
||||||
|
|
||||||
groups_.push_back(std::make_tuple(0, 0, 0, 0));
|
|
||||||
|
|
||||||
std::get<3>(groups_[group_]) = start_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::match::addGroupEnd(size_t group_, size_t end_){
|
|
||||||
|
|
||||||
if(groups_.size() < (group_ + 1))
|
|
||||||
|
|
||||||
for(size_t i = groups_.size(); i < group_ + 1; ++i)
|
|
||||||
|
|
||||||
groups_.push_back(std::make_tuple(0, 0, 0, 0));
|
|
||||||
|
|
||||||
if(std::get<3>(groups_[group_]) == end_)
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::get<0>(groups_[group_]) = 1;
|
|
||||||
|
|
||||||
std::get<1>(groups_[group_]) = std::get<3>(groups_[group_]);
|
|
||||||
|
|
||||||
std::get<2>(groups_[group_]) = end_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string_view const regex::match::operator [](size_t i) const{
|
|
||||||
|
|
||||||
if(!i){
|
|
||||||
|
|
||||||
return std::string_view(matched_text_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(groups_.size() <= i || !std::get<0>(groups_[i])){
|
|
||||||
|
|
||||||
return std::string_view(null_str_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::string_view(matched_text_.begin() + std::get<1>(groups_[i]), matched_text_.begin() + std::get<2>(groups_[i]));
|
|
||||||
|
|
||||||
}
|
|
||||||
86
match.hpp
86
match.hpp
|
|
@ -1,86 +0,0 @@
|
||||||
//
|
|
||||||
// match.hpp
|
|
||||||
// lab2
|
|
||||||
//
|
|
||||||
// Created by Владислав Агибалов on 23.07.2025.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef match_hpp
|
|
||||||
#define match_hpp
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "node.hpp"
|
|
||||||
|
|
||||||
namespace regex {
|
|
||||||
|
|
||||||
class match;
|
|
||||||
|
|
||||||
class groupiterator {
|
|
||||||
|
|
||||||
const match* match_;
|
|
||||||
|
|
||||||
size_t group_index_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
groupiterator(const match*, size_t);
|
|
||||||
|
|
||||||
groupiterator& operator++();
|
|
||||||
|
|
||||||
groupiterator operator++(int);
|
|
||||||
|
|
||||||
std::string_view operator*() const;
|
|
||||||
|
|
||||||
bool operator == (const groupiterator &) const;
|
|
||||||
|
|
||||||
bool operator != (const groupiterator &) const;
|
|
||||||
|
|
||||||
size_t groupindex() const;
|
|
||||||
|
|
||||||
bool isMatched() const;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
class match{
|
|
||||||
|
|
||||||
std::string matched_text_ = ("");
|
|
||||||
|
|
||||||
std::vector<std::tuple<size_t, size_t, size_t, size_t>> groups_;
|
|
||||||
|
|
||||||
std::string null_str_ = ("");
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
match();
|
|
||||||
|
|
||||||
match(size_t size_);
|
|
||||||
|
|
||||||
size_t size() const;
|
|
||||||
|
|
||||||
groupiterator begin() const;
|
|
||||||
|
|
||||||
groupiterator end() const;
|
|
||||||
|
|
||||||
bool isMatched() const;
|
|
||||||
|
|
||||||
void setMatched(std::string const &);
|
|
||||||
|
|
||||||
void addGroupStart(size_t, size_t);
|
|
||||||
|
|
||||||
void addGroupEnd(size_t, size_t);
|
|
||||||
|
|
||||||
bool operator<(const match &) const;
|
|
||||||
|
|
||||||
bool operator>(const match &) const;
|
|
||||||
|
|
||||||
bool operator==(const match &) const;
|
|
||||||
|
|
||||||
std::string_view const operator [](size_t) const;
|
|
||||||
|
|
||||||
size_t length() const;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* match_hpp */
|
|
||||||
721
nfa.cpp
721
nfa.cpp
|
|
@ -1,721 +0,0 @@
|
||||||
//
|
|
||||||
// nfa.cpp
|
|
||||||
// lab2
|
|
||||||
//
|
|
||||||
// Created by Владислав Агибалов on 19.07.2025.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "nfa.hpp"
|
|
||||||
|
|
||||||
regex::nfa::nfa(): start_(nullptr), end_(nullptr){}
|
|
||||||
|
|
||||||
regex::nfa::nfa(size_t symbol_){
|
|
||||||
|
|
||||||
end_ = std::make_shared<nfa_node>(nfa_node(1));
|
|
||||||
|
|
||||||
start_ = std::make_shared<nfa_node>(nfa_node(symbol_, end_));
|
|
||||||
|
|
||||||
all_nodes_.emplace(start_);
|
|
||||||
|
|
||||||
all_nodes_.emplace(end_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::nfaGenerate(std::string text_){
|
|
||||||
|
|
||||||
char const* text = text_.data();
|
|
||||||
|
|
||||||
std::vector<nfa> automats;
|
|
||||||
|
|
||||||
automats.push_back(nfa());
|
|
||||||
|
|
||||||
automats.push_back(nfa());
|
|
||||||
|
|
||||||
automats.push_back(nfa());
|
|
||||||
|
|
||||||
size_t group_id_ = 0;
|
|
||||||
|
|
||||||
std::stack<std::pair<bool, size_t>> current_id_stack_;
|
|
||||||
|
|
||||||
while(text[0] != 0){
|
|
||||||
|
|
||||||
switch (text[0]) {
|
|
||||||
|
|
||||||
case '(':
|
|
||||||
|
|
||||||
if(text[1] != '?' && text[2] != ':'){
|
|
||||||
|
|
||||||
++group_id_;
|
|
||||||
|
|
||||||
current_id_stack_.push(std::pair<bool, size_t>(1, group_id_));
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
current_id_stack_.push(std::pair<bool, size_t>(0, 0));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
automats[automats.size() - 2].concatenation(automats.back());
|
|
||||||
|
|
||||||
automats.back() = nfa();
|
|
||||||
|
|
||||||
automats.push_back(nfa());
|
|
||||||
|
|
||||||
automats.push_back(nfa());
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ')':
|
|
||||||
|
|
||||||
automats[automats.size() - 2].concatenation(automats.back());
|
|
||||||
|
|
||||||
automats[automats.size() - 3].alternation(automats[automats.size() - 2]);
|
|
||||||
|
|
||||||
if(!current_id_stack_.size())
|
|
||||||
|
|
||||||
throw std::runtime_error("Не все скобки открыты.");
|
|
||||||
|
|
||||||
if(current_id_stack_.top().first && !automats[automats.size() - 3].empty()){
|
|
||||||
|
|
||||||
nfa group_start_ = nfa(current_id_stack_.top().second, 0);
|
|
||||||
|
|
||||||
nfa group_end_ = nfa(0, current_id_stack_.top().second);
|
|
||||||
|
|
||||||
group_start_.concatenation(automats[automats.size() - 3]);
|
|
||||||
|
|
||||||
group_start_.concatenation(group_end_);
|
|
||||||
|
|
||||||
automats[automats.size() - 3] = group_start_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
automats.pop_back();
|
|
||||||
|
|
||||||
automats.pop_back();
|
|
||||||
|
|
||||||
current_id_stack_.pop();
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '|':
|
|
||||||
|
|
||||||
automats[automats.size() - 2].concatenation(automats.back());
|
|
||||||
|
|
||||||
automats.back() = nfa();
|
|
||||||
|
|
||||||
automats[automats.size() - 3].alternation(automats[automats.size() - 2]);
|
|
||||||
|
|
||||||
automats[automats.size() - 2] = nfa();
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '*':
|
|
||||||
|
|
||||||
automats.back().kleene();
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '[':
|
|
||||||
|
|
||||||
automats[automats.size() - 2].concatenation(automats.back());
|
|
||||||
|
|
||||||
automats.back() = nfa();
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
while(text[0] != ']'){
|
|
||||||
|
|
||||||
nfa new_graph_ = nfa(text[0]);
|
|
||||||
|
|
||||||
automats.back().alternation(new_graph_);
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ']':
|
|
||||||
|
|
||||||
throw std::runtime_error("неэкранированный символ ].");
|
|
||||||
|
|
||||||
case '{':{
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
size_t times_first_ = 0;
|
|
||||||
|
|
||||||
size_t times_second_ = 0;
|
|
||||||
|
|
||||||
size_t* current = ×_first_;
|
|
||||||
|
|
||||||
bool point_ = 0;
|
|
||||||
|
|
||||||
while(text[0] != '}'){
|
|
||||||
|
|
||||||
if(!std::isdigit(text[0])){
|
|
||||||
|
|
||||||
if(point_ || text[0] != ',')
|
|
||||||
|
|
||||||
throw std::runtime_error("не цифра внутри {...}.");
|
|
||||||
|
|
||||||
if(!point_ && text[0] == ','){
|
|
||||||
|
|
||||||
point_ = 1;
|
|
||||||
|
|
||||||
current = ×_second_;
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
*current = *current * 10 + text[0] - '0';
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(point_ && times_second_ != 0 && times_second_ <= times_first_)
|
|
||||||
|
|
||||||
throw std::runtime_error("второе количество повторений должно быть больше первого.");
|
|
||||||
|
|
||||||
if(point_)
|
|
||||||
|
|
||||||
automats.back().repeat(times_first_, times_second_);
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
automats.back().repeat(times_first_);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
case '}':
|
|
||||||
|
|
||||||
throw std::runtime_error("неэкранированный символ }.");
|
|
||||||
|
|
||||||
case '+':
|
|
||||||
|
|
||||||
automats.back().repeat(1, 0);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '?':
|
|
||||||
|
|
||||||
automats.back().question();
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '$':
|
|
||||||
|
|
||||||
automats[automats.size() - 2].concatenation(automats.back());
|
|
||||||
|
|
||||||
automats.back() = nfa();
|
|
||||||
|
|
||||||
automats.back().makevoid();
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '\\':
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
if(!text[0])
|
|
||||||
|
|
||||||
throw std::runtime_error("метасимвол '\' не может экранировать '\0'.");
|
|
||||||
|
|
||||||
default:
|
|
||||||
|
|
||||||
automats[automats.size() - 2].concatenation(automats.back());
|
|
||||||
|
|
||||||
automats.back() = nfa();
|
|
||||||
|
|
||||||
automats.back() = nfa(text[0]);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
++text;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(automats.size() != 3)
|
|
||||||
|
|
||||||
throw std::runtime_error("Не все скобки открыты или закрыты.");
|
|
||||||
|
|
||||||
automats[automats.size() - 2].concatenation(automats.back());
|
|
||||||
|
|
||||||
automats[automats.size() - 3].alternation(automats[automats.size() - 2]);
|
|
||||||
|
|
||||||
automats.pop_back();
|
|
||||||
|
|
||||||
automats.pop_back();
|
|
||||||
|
|
||||||
*this = automats.back();
|
|
||||||
|
|
||||||
size_ = group_id_;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
regex::nfa::nfa(size_t start__, size_t end__){
|
|
||||||
|
|
||||||
if(start__ != 0 && end__ != 0)
|
|
||||||
|
|
||||||
throw std::runtime_error("новый граф не может быть одновременно и началом, и концом группы.");
|
|
||||||
|
|
||||||
end_ = std::make_shared<nfa_node>(nfa_node(1));
|
|
||||||
|
|
||||||
start_ = std::make_shared<nfa_node>(nfa_node(start__, end__));
|
|
||||||
|
|
||||||
all_nodes_.emplace(start_);
|
|
||||||
|
|
||||||
all_nodes_.emplace(end_);
|
|
||||||
|
|
||||||
start_->addEps(end_);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::concatenation(nfa& second_){
|
|
||||||
|
|
||||||
if(second_.empty())
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
if(empty()){
|
|
||||||
|
|
||||||
*this = second_;
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
all_nodes_.merge(second_.all_nodes_);
|
|
||||||
|
|
||||||
*end_.get() = *second_.start_;
|
|
||||||
|
|
||||||
end_ = second_.end_;
|
|
||||||
|
|
||||||
all_nodes_.erase(second_.start_);
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool regex::nfa::empty() const{
|
|
||||||
|
|
||||||
return start_ == nullptr;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::makevoid(){
|
|
||||||
|
|
||||||
all_nodes_.clear();
|
|
||||||
|
|
||||||
end_ = std::make_shared<nfa_node>(nfa_node(1));
|
|
||||||
|
|
||||||
start_ = std::make_shared<nfa_node>(nfa_node(0));
|
|
||||||
|
|
||||||
start_->addEps(end_);
|
|
||||||
|
|
||||||
all_nodes_.emplace(start_);
|
|
||||||
|
|
||||||
all_nodes_.emplace(end_);
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::alternation(nfa& second_){
|
|
||||||
|
|
||||||
if(second_.empty())
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
if(empty()){
|
|
||||||
|
|
||||||
*this = second_;
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> new_start_ = std::make_shared<nfa_node>(nfa_node(0));
|
|
||||||
|
|
||||||
all_nodes_.emplace(new_start_);
|
|
||||||
|
|
||||||
new_start_->addEps(start_);
|
|
||||||
|
|
||||||
new_start_->addEps(second_.start_);
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> new_end_ = std::make_shared<nfa_node>(nfa_node(1));
|
|
||||||
|
|
||||||
all_nodes_.emplace(new_end_);
|
|
||||||
|
|
||||||
end_->setFinish(0);
|
|
||||||
|
|
||||||
second_.end_->setFinish(0);
|
|
||||||
|
|
||||||
end_->addEps(new_end_);
|
|
||||||
|
|
||||||
second_.end_->addEps(new_end_);
|
|
||||||
|
|
||||||
start_ = new_start_;
|
|
||||||
|
|
||||||
end_ = new_end_;
|
|
||||||
|
|
||||||
all_nodes_.merge(second_.all_nodes_);
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::kleene(){
|
|
||||||
|
|
||||||
if(empty())
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
end_->addEps(start_);
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> new_start_ = std::make_shared<nfa_node>(nfa_node(0));
|
|
||||||
|
|
||||||
all_nodes_.emplace(new_start_);
|
|
||||||
|
|
||||||
new_start_->addEps(start_);
|
|
||||||
|
|
||||||
start_ = new_start_;
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> new_end_ = std::make_shared<nfa_node>(nfa_node(1));
|
|
||||||
|
|
||||||
all_nodes_.emplace(new_end_);
|
|
||||||
|
|
||||||
end_->setFinish(0);
|
|
||||||
|
|
||||||
end_->addEps(new_end_);
|
|
||||||
|
|
||||||
end_=new_end_;
|
|
||||||
|
|
||||||
start_->addEps(end_);
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::repeat(size_t times_){
|
|
||||||
|
|
||||||
if(empty())
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
if(!times_){
|
|
||||||
|
|
||||||
*this = nfa();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
nfa copied_ = copy();
|
|
||||||
|
|
||||||
for(size_t i = 0; i < times_ - 1; ++i){
|
|
||||||
|
|
||||||
nfa graph_new_ = copied_.copy();
|
|
||||||
|
|
||||||
concatenation(graph_new_);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::repeat(size_t times_first_, size_t times_second_){
|
|
||||||
|
|
||||||
if(empty())
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
nfa copied_ = copy();
|
|
||||||
|
|
||||||
repeat(times_first_);
|
|
||||||
|
|
||||||
if(times_second_){
|
|
||||||
|
|
||||||
copied_.question();
|
|
||||||
|
|
||||||
copied_.repeat(times_second_ - times_first_);
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
copied_.kleene();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
concatenation(copied_);
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::question(){
|
|
||||||
|
|
||||||
if(empty())
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> new_start_ = std::make_shared<nfa_node>(nfa_node(0));
|
|
||||||
|
|
||||||
all_nodes_.emplace(new_start_);
|
|
||||||
|
|
||||||
new_start_->addEps(start_);
|
|
||||||
|
|
||||||
new_start_->addEps(end_);
|
|
||||||
|
|
||||||
start_ = new_start_;
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
regex::nfa regex::nfa::copy() const{
|
|
||||||
|
|
||||||
nfa new_nfa_;
|
|
||||||
|
|
||||||
new_nfa_.size_ = size_;
|
|
||||||
|
|
||||||
std::map<std::shared_ptr<nfa_node>, std::shared_ptr<nfa_node>> new_nodes_;
|
|
||||||
|
|
||||||
for(auto i: all_nodes_){
|
|
||||||
|
|
||||||
new_nodes_[i] = std::make_shared<nfa_node>(nfa_node(0));
|
|
||||||
|
|
||||||
new_nfa_.all_nodes_.emplace(new_nodes_[i]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
new_nfa_.start_ = new_nodes_[start_];
|
|
||||||
|
|
||||||
new_nfa_.end_ = new_nodes_[end_];
|
|
||||||
|
|
||||||
for(auto i: all_nodes_){
|
|
||||||
|
|
||||||
if(i->isEps()){
|
|
||||||
|
|
||||||
new_nodes_[i]->setFinish(i->getFinish());
|
|
||||||
|
|
||||||
new_nodes_[i]->setStart(i->getStart());
|
|
||||||
|
|
||||||
new_nodes_[i]->setEnd(i->getEnd());
|
|
||||||
|
|
||||||
for(size_t j = 0; j < i->size(); ++j){
|
|
||||||
|
|
||||||
new_nodes_[i]->addEps(new_nodes_[(*i)[j].lock()]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
new_nodes_[i]->addEdge(i->getSymbol(), (new_nodes_[(*i)[0].lock()]));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return new_nfa_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void regex::nfa::print(std::string const & file_name_) const{
|
|
||||||
|
|
||||||
std::ofstream out_;
|
|
||||||
|
|
||||||
out_.open(file_name_ + ".dot");
|
|
||||||
|
|
||||||
out_<< "digraph NFA{" << std::endl;
|
|
||||||
|
|
||||||
std::set<std::shared_ptr<nfa_node>> set_;
|
|
||||||
|
|
||||||
std::queue<std::shared_ptr<nfa_node>> queue_;
|
|
||||||
|
|
||||||
queue_.push(start_);
|
|
||||||
|
|
||||||
while (!queue_.empty() ) {
|
|
||||||
|
|
||||||
size_t old_size_ = set_.size();
|
|
||||||
|
|
||||||
set_.insert(queue_.front());
|
|
||||||
|
|
||||||
size_t new_size_ = set_.size();
|
|
||||||
|
|
||||||
if(old_size_ == new_size_){
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
out_ << " \"node" << queue_.front() << "\"";
|
|
||||||
|
|
||||||
out_ << "[";
|
|
||||||
|
|
||||||
if(queue_.front()->getFinish()){
|
|
||||||
|
|
||||||
out_ << "shape = box, ";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
out_ << "label=\"" << set_.size() << "\n";
|
|
||||||
|
|
||||||
if(queue_.front()->getStart())
|
|
||||||
|
|
||||||
out_ << "\ngroup starts: " << queue_.front()->getStart();
|
|
||||||
|
|
||||||
if(queue_.front()->getEnd())
|
|
||||||
|
|
||||||
out_ << "\ngroup ends: " << queue_.front()->getEnd();
|
|
||||||
|
|
||||||
out_ << "\"]\n";
|
|
||||||
|
|
||||||
if(queue_.front()->isEps()){
|
|
||||||
|
|
||||||
for(size_t i = 0; i < queue_.front()->size(); ++i){
|
|
||||||
|
|
||||||
out_ << " \"node" << queue_.front() << "\" -> \"node" << (*queue_.front())[i].lock() << "\"";
|
|
||||||
|
|
||||||
if((*queue_.front())[i].lock() != nullptr)
|
|
||||||
|
|
||||||
queue_.push((*queue_.front())[i].lock());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
out_ << " \"node" << queue_.front() << "\" -> \"node" << (*queue_.front())[0].lock() << "\"";
|
|
||||||
|
|
||||||
out_ << std::format("[label=\"{}\"]\n", (char)queue_.front()->getSymbol());
|
|
||||||
|
|
||||||
queue_.push((*queue_.front())[0].lock());
|
|
||||||
|
|
||||||
queue_.pop();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
out_<< "}";
|
|
||||||
|
|
||||||
out_.close();
|
|
||||||
|
|
||||||
std::string siscall("/opt/homebrew/bin/dot -Tpng " + file_name_ + ".dot -o " + file_name_ + ".png");
|
|
||||||
|
|
||||||
system(siscall.data());
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t regex::nfa::find(std::string recognazing_text_, match& match_) const{
|
|
||||||
|
|
||||||
const char* text_ = recognazing_text_.data();
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> current_nfa_ = start_;
|
|
||||||
|
|
||||||
size_t begin_ = 0;
|
|
||||||
|
|
||||||
size_t cur_pos_ = 0;
|
|
||||||
|
|
||||||
std::set<std::tuple<std::shared_ptr<nfa_node>, match, size_t>> passed_states_;
|
|
||||||
|
|
||||||
std::stack<std::tuple<std::shared_ptr<nfa_node>, match, size_t>> future_nodes_;
|
|
||||||
|
|
||||||
future_nodes_.push(std::make_tuple(start_, match(size_), 0));
|
|
||||||
|
|
||||||
while(!future_nodes_.empty()){
|
|
||||||
|
|
||||||
std::tuple<std::shared_ptr<nfa_node>, match, size_t> current_state_ = future_nodes_.top();
|
|
||||||
|
|
||||||
future_nodes_.pop();
|
|
||||||
|
|
||||||
size_t old_size = passed_states_.size();
|
|
||||||
|
|
||||||
passed_states_.emplace(current_state_);
|
|
||||||
|
|
||||||
size_t new_size = passed_states_.size();
|
|
||||||
|
|
||||||
if(old_size == new_size){
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<nfa_node> current_node_ = std::get<0>(current_state_);
|
|
||||||
|
|
||||||
cur_pos_ = std::get<2>(current_state_);
|
|
||||||
|
|
||||||
match new_match_ = std::get<1>(current_state_);
|
|
||||||
|
|
||||||
if(current_node_->isEps()){
|
|
||||||
|
|
||||||
if(current_node_->getFinish()){
|
|
||||||
|
|
||||||
new_match_.setMatched(std::string(recognazing_text_.begin() + begin_, recognazing_text_.begin() + cur_pos_));
|
|
||||||
|
|
||||||
if(!match_.length() || new_match_.length() > match_.length())
|
|
||||||
|
|
||||||
match_ = new_match_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(current_node_->getStart())
|
|
||||||
|
|
||||||
new_match_.addGroupStart(current_node_->getStart(), cur_pos_);
|
|
||||||
|
|
||||||
if(current_node_->getEnd())
|
|
||||||
|
|
||||||
new_match_.addGroupEnd(current_node_->getEnd(), cur_pos_);
|
|
||||||
|
|
||||||
for(size_t i = current_node_->size() - 1; i < (size_t)(-1); --i){
|
|
||||||
|
|
||||||
future_nodes_.push(std::make_tuple((*current_node_)[i].lock(), new_match_, cur_pos_));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} else{
|
|
||||||
|
|
||||||
size_t symbol_ = current_node_->getSymbol();
|
|
||||||
|
|
||||||
if(text_[cur_pos_] == symbol_){
|
|
||||||
|
|
||||||
future_nodes_.push(std::make_tuple((*current_node_)[0].lock(), new_match_, cur_pos_ + 1));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return match_.length() + 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr<regex::nfa_node> regex::nfa::getStart() const{
|
|
||||||
|
|
||||||
return start_;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 108 KiB |
|
|
@ -1,24 +0,0 @@
|
||||||
{
|
|
||||||
"configurations" : [
|
|
||||||
{
|
|
||||||
"id" : "9FFB7718-BC46-42FE-B959-9E4C740AEDF0",
|
|
||||||
"name" : "Configuration 1",
|
|
||||||
"options" : {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"defaultOptions" : {
|
|
||||||
|
|
||||||
},
|
|
||||||
"testTargets" : [
|
|
||||||
{
|
|
||||||
"target" : {
|
|
||||||
"containerPath" : "container:lab2.xcodeproj",
|
|
||||||
"identifier" : "2DD58AD82E3522F400805982",
|
|
||||||
"name" : "tests"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"version" : 1
|
|
||||||
}
|
|
||||||
1291
tests/tests.mm
1291
tests/tests.mm
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue