Add files via upload
RegEx
This commit is contained in:
commit
b48d3ec773
13 changed files with 2817 additions and 0 deletions
900
dfa.cpp
Normal file
900
dfa.cpp
Normal file
|
|
@ -0,0 +1,900 @@
|
|||
//
|
||||
// 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
Normal file
59
dfa.hpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
//
|
||||
// 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
Normal file
54
dfa_node.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
//
|
||||
// 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
Normal file
43
dfa_node.hpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
//
|
||||
// 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 */
|
||||
192
match.cpp
Normal file
192
match.cpp
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
//
|
||||
// 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
Normal file
86
match.hpp
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
//
|
||||
// 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
Normal file
721
nfa.cpp
Normal file
|
|
@ -0,0 +1,721 @@
|
|||
//
|
||||
// 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_;
|
||||
|
||||
}
|
||||
|
||||
66
nfa.hpp
Normal file
66
nfa.hpp
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
//
|
||||
// nfa.hpp
|
||||
// lab2
|
||||
//
|
||||
// Created by Владислав Агибалов on 19.07.2025.
|
||||
//
|
||||
|
||||
#ifndef nfa_hpp
|
||||
#define nfa_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
#include "nfa_node.hpp"
|
||||
#include "match.hpp"
|
||||
|
||||
namespace regex {
|
||||
|
||||
class nfa{
|
||||
|
||||
std::shared_ptr<nfa_node> start_;
|
||||
|
||||
std::shared_ptr<nfa_node> end_;
|
||||
|
||||
std::size_t size_;
|
||||
|
||||
std::set<std::shared_ptr<nfa_node>> all_nodes_;
|
||||
|
||||
public:
|
||||
|
||||
explicit nfa();
|
||||
|
||||
explicit nfa(size_t);
|
||||
|
||||
explicit nfa(size_t, size_t);
|
||||
|
||||
void nfaGenerate(std::string);
|
||||
|
||||
void concatenation(nfa&);
|
||||
|
||||
void alternation(nfa&);
|
||||
|
||||
void kleene();
|
||||
|
||||
void question();
|
||||
|
||||
void repeat(size_t);
|
||||
|
||||
void repeat(size_t, size_t);
|
||||
|
||||
nfa copy() const;
|
||||
|
||||
bool empty() const;
|
||||
|
||||
void makevoid();
|
||||
|
||||
void print(std::string const &) const;
|
||||
|
||||
std::shared_ptr<nfa_node> getStart() const;
|
||||
|
||||
size_t find(std::string, match&) const;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif /* nfa_hpp */
|
||||
107
nfa_node.cpp
Normal file
107
nfa_node.cpp
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
//
|
||||
// nfa_node.cpp
|
||||
// lab2
|
||||
//
|
||||
// Created by Владислав Агибалов on 19.07.2025.
|
||||
//
|
||||
|
||||
#include "nfa_node.hpp"
|
||||
|
||||
regex::nfa_node::nfa_node(size_t isfinish_): is_finish_(isfinish_), eps_(1), symbol_(0), is_start_(0), is_end_(0){}
|
||||
|
||||
regex::nfa_node::nfa_node(size_t start_, size_t end_): is_finish_(0), eps_(1), symbol_(0), is_start_(start_), is_end_(end_){
|
||||
|
||||
if(start_ != 0 && end_ != 0)
|
||||
|
||||
throw std::runtime_error("узел не может быть одновременно и началом, и концом группы.");
|
||||
|
||||
}
|
||||
|
||||
regex::nfa_node::nfa_node(char sym_, std::weak_ptr<nfa_node> next_): eps_(0), symbol_(sym_), is_finish_(0), is_start_(0), is_end_(0), next_node_(next_){}
|
||||
|
||||
bool regex::nfa_node::getFinish() const{
|
||||
|
||||
return is_finish_;
|
||||
|
||||
}
|
||||
|
||||
bool regex::nfa_node::isEps() const{
|
||||
|
||||
return eps_;
|
||||
|
||||
}
|
||||
|
||||
void regex::nfa_node::setFinish(bool finish_){
|
||||
|
||||
is_finish_ = finish_;
|
||||
|
||||
}
|
||||
|
||||
size_t regex::nfa_node::size() const{
|
||||
|
||||
return eps_edges_.size() + !eps_;
|
||||
|
||||
}
|
||||
|
||||
const std::weak_ptr<regex::nfa_node> regex::nfa_node::operator[](size_t i) const{
|
||||
|
||||
if(eps_)
|
||||
|
||||
return eps_edges_[i];
|
||||
|
||||
if(!i)
|
||||
|
||||
return next_node_;
|
||||
|
||||
throw std::runtime_error("такого ребра нету.");
|
||||
}
|
||||
|
||||
void regex::nfa_node::addEps(std::weak_ptr<regex::nfa_node> const new_){
|
||||
|
||||
if (!eps_)
|
||||
|
||||
throw std::runtime_error("это не эпсилон узел");
|
||||
|
||||
eps_edges_.push_back(new_);
|
||||
|
||||
}
|
||||
|
||||
size_t regex::nfa_node::getSymbol() const{
|
||||
|
||||
return symbol_;
|
||||
|
||||
}
|
||||
|
||||
size_t regex::nfa_node::getStart() const{
|
||||
|
||||
return is_start_;
|
||||
|
||||
}
|
||||
|
||||
size_t regex::nfa_node::getEnd() const{
|
||||
|
||||
return is_end_;
|
||||
|
||||
}
|
||||
|
||||
void regex::nfa_node::addEdge(size_t sym_, std::weak_ptr<nfa_node> const new_){
|
||||
|
||||
eps_ = 0;
|
||||
|
||||
symbol_ = sym_;
|
||||
|
||||
next_node_ = new_;
|
||||
|
||||
}
|
||||
|
||||
void regex::nfa_node::setStart(size_t start_){
|
||||
|
||||
is_start_ = start_;
|
||||
|
||||
}
|
||||
|
||||
void regex::nfa_node::setEnd(size_t end_){
|
||||
|
||||
is_end_ = end_;
|
||||
|
||||
}
|
||||
74
nfa_node.hpp
Normal file
74
nfa_node.hpp
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
//
|
||||
// nfa_node.hpp
|
||||
// lab2
|
||||
//
|
||||
// Created by Владислав Агибалов on 19.07.2025.
|
||||
//
|
||||
|
||||
#ifndef nfa_node_hpp
|
||||
#define nfa_node_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
#include "node.hpp"
|
||||
|
||||
namespace regex{
|
||||
|
||||
class nfa_node: public node{
|
||||
|
||||
bool is_finish_;
|
||||
|
||||
bool eps_;
|
||||
|
||||
size_t is_start_;
|
||||
|
||||
size_t is_end_;
|
||||
|
||||
std::vector<std::weak_ptr<nfa_node>> eps_edges_;
|
||||
|
||||
size_t symbol_;
|
||||
|
||||
std::weak_ptr<nfa_node> next_node_;
|
||||
|
||||
inline static std::shared_ptr<nfa_node> null_node_ = nullptr;
|
||||
|
||||
public:
|
||||
|
||||
nfa_node() = delete;
|
||||
|
||||
explicit nfa_node(size_t);
|
||||
|
||||
explicit nfa_node(size_t, size_t);
|
||||
|
||||
explicit nfa_node(char, std::weak_ptr<nfa_node>);
|
||||
|
||||
bool getFinish() const;
|
||||
|
||||
bool isEps() const;
|
||||
|
||||
void setFinish(bool);
|
||||
|
||||
size_t size() const;
|
||||
|
||||
const std::weak_ptr<regex::nfa_node> operator[](size_t i) const;
|
||||
|
||||
void addEps(std::weak_ptr<regex::nfa_node>);
|
||||
|
||||
size_t getSymbol() const;
|
||||
|
||||
size_t getStart() const;
|
||||
|
||||
size_t getEnd() const;
|
||||
|
||||
void setStart(size_t);
|
||||
|
||||
void setEnd(size_t);
|
||||
|
||||
void addEdge(size_t, std::weak_ptr<regex::nfa_node>);
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif /* nfa_node_hpp */
|
||||
30
node.hpp
Normal file
30
node.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
//
|
||||
// node.hpp
|
||||
// lab2
|
||||
//
|
||||
// Created by Владислав Агибалов on 19.07.2025.
|
||||
//
|
||||
|
||||
#ifndef node_hpp
|
||||
#define node_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <format>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
namespace regex {
|
||||
|
||||
class node{};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif /* node_hpp */
|
||||
398
regex.cpp
Normal file
398
regex.cpp
Normal file
|
|
@ -0,0 +1,398 @@
|
|||
//
|
||||
// regex.cpp
|
||||
// lab2
|
||||
//
|
||||
// Created by Владислав Агибалов on 15.07.2025.
|
||||
//
|
||||
|
||||
#include "regex.hpp"
|
||||
|
||||
regex::regex::regex(){}
|
||||
|
||||
regex::regex::regex(std::string pat_): pattern_(pat_){}
|
||||
|
||||
void regex::regex::compile(){
|
||||
|
||||
if(!compiled_){
|
||||
|
||||
nfa_.nfaGenerate(pattern_);
|
||||
|
||||
dfa_.dfaGenerate(nfa_);
|
||||
|
||||
min_dfa_.minimization(dfa_);
|
||||
|
||||
compiled_ = 1;
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void regex::regex::compile(std::string new_pattern_){
|
||||
|
||||
compiled_ = 0;
|
||||
|
||||
pattern_ = new_pattern_;
|
||||
|
||||
compile();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool regex::regex::rmatch(std::string text_){
|
||||
|
||||
compile();
|
||||
|
||||
recognazing_text_ = text_;
|
||||
|
||||
current_pos_ = 0;
|
||||
|
||||
end_pos_ = text_.size() + 1;
|
||||
|
||||
match new_match_(0);
|
||||
|
||||
size_t step = min_dfa_.find(recognazing_text_, new_match_);
|
||||
|
||||
if(!new_match_.isMatched() || step != end_pos_)
|
||||
|
||||
return addition_;
|
||||
|
||||
//match_ = new_match_;
|
||||
|
||||
return !addition_;
|
||||
|
||||
}
|
||||
|
||||
size_t regex::regex::findAll(std::string text_, std::vector<match>& matches_){
|
||||
|
||||
matches_.clear();
|
||||
|
||||
compile();
|
||||
|
||||
size_t res_ = 0;
|
||||
|
||||
match result_;
|
||||
|
||||
recognazing_text_ = text_;
|
||||
|
||||
current_pos_ = 0;
|
||||
|
||||
end_pos_ = text_.size() + 1;
|
||||
|
||||
while(find(result_)){
|
||||
|
||||
res_ += 1;
|
||||
|
||||
matches_.push_back(result_);
|
||||
|
||||
result_ = match();
|
||||
|
||||
}
|
||||
|
||||
return res_;
|
||||
}
|
||||
|
||||
bool regex::regex::find(std::string text_, match& match_){
|
||||
|
||||
recognazing_text_ = text_;
|
||||
|
||||
current_pos_ = 0;
|
||||
|
||||
end_pos_ = text_.size() + 1;
|
||||
|
||||
return find(match_);
|
||||
|
||||
}
|
||||
|
||||
bool regex::regex::find(match& match_){
|
||||
|
||||
match_ = match();
|
||||
|
||||
compile();
|
||||
|
||||
while(current_pos_ != end_pos_){
|
||||
|
||||
if(addition_){
|
||||
|
||||
if(rmatch(recognazing_text_))
|
||||
|
||||
match_.setMatched(recognazing_text_);
|
||||
|
||||
current_pos_ = end_pos_;
|
||||
|
||||
return match_.isMatched();
|
||||
|
||||
}
|
||||
|
||||
size_t step = min_dfa_.find(std::string(recognazing_text_.begin() + current_pos_, recognazing_text_.end()), match_);
|
||||
|
||||
if(!match_.isMatched())
|
||||
|
||||
++current_pos_;
|
||||
|
||||
else{
|
||||
|
||||
current_pos_ += step;
|
||||
|
||||
return match_.isMatched();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return match_.isMatched();
|
||||
|
||||
}
|
||||
|
||||
bool regex::regex::rmatch(std::string text_, match& match_){
|
||||
|
||||
if(addition_)
|
||||
|
||||
throw std::runtime_error("у дополнения не может быть групп захвата.");
|
||||
|
||||
|
||||
match_ = match();
|
||||
|
||||
compile();
|
||||
|
||||
recognazing_text_ = text_;
|
||||
|
||||
current_pos_ = 0;
|
||||
|
||||
end_pos_ = text_.size() + 1;
|
||||
|
||||
match new_match_(0);
|
||||
|
||||
size_t step = nfa_.find(recognazing_text_, new_match_);
|
||||
|
||||
if(!new_match_.isMatched() || step != end_pos_)
|
||||
|
||||
return 0;
|
||||
|
||||
match_ = new_match_;
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
size_t regex::regex::findAllWithGroups(std::string text_, std::vector<match>& matches_){
|
||||
|
||||
matches_.clear();
|
||||
|
||||
compile();
|
||||
|
||||
if(addition_)
|
||||
|
||||
throw std::runtime_error("у дополнения не может быть групп захвата.");
|
||||
|
||||
size_t res_ = 0;
|
||||
|
||||
match result_;
|
||||
|
||||
recognazing_text_ = text_;
|
||||
|
||||
current_pos_ = 0;
|
||||
|
||||
end_pos_ = text_.size() + 1;
|
||||
|
||||
while(findWithGroups(result_)){
|
||||
|
||||
res_ += 1;
|
||||
|
||||
matches_.push_back(result_);
|
||||
|
||||
result_ = match();
|
||||
|
||||
}
|
||||
|
||||
return res_;
|
||||
}
|
||||
|
||||
bool regex::regex::findWithGroups(std::string text_, match& match_){
|
||||
|
||||
recognazing_text_ = text_;
|
||||
|
||||
current_pos_ = 0;
|
||||
|
||||
end_pos_ = text_.size() + 1;
|
||||
|
||||
return findWithGroups(match_);
|
||||
|
||||
}
|
||||
|
||||
bool regex::regex::findWithGroups(match& match_){
|
||||
|
||||
if(addition_)
|
||||
|
||||
throw std::runtime_error("у дополнения не может быть групп захвата.");
|
||||
|
||||
match_ = match();
|
||||
|
||||
compile();
|
||||
|
||||
while(current_pos_ != end_pos_){
|
||||
|
||||
size_t step = nfa_.find(std::string(recognazing_text_.begin() + current_pos_, recognazing_text_.end()), match_);
|
||||
|
||||
if(!match_.isMatched())
|
||||
|
||||
++current_pos_;
|
||||
|
||||
else{
|
||||
|
||||
current_pos_ += step;
|
||||
|
||||
return match_.isMatched();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return match_.isMatched();
|
||||
|
||||
}
|
||||
|
||||
regex::regex regex::regex::addition(){
|
||||
|
||||
|
||||
compile();
|
||||
|
||||
regex new_regex_;
|
||||
|
||||
new_regex_.addition_ = !addition_;
|
||||
|
||||
new_regex_.compiled_ = 1;
|
||||
|
||||
new_regex_.pattern_ = pattern_;
|
||||
|
||||
new_regex_.nfa_ = nfa_.copy();
|
||||
|
||||
new_regex_.dfa_ = dfa_.copy();
|
||||
|
||||
new_regex_.min_dfa_ = min_dfa_.copy();
|
||||
|
||||
return new_regex_;
|
||||
|
||||
|
||||
}
|
||||
|
||||
regex::regex regex::regex::combine(std::string first_str_, std::string second_str_){
|
||||
|
||||
regex first_reg_(first_str_);
|
||||
|
||||
return first_reg_.combine(second_str_);
|
||||
|
||||
}
|
||||
|
||||
regex::regex regex::regex::combine(std::string second_str_){
|
||||
|
||||
regex second_reg_(second_str_);
|
||||
|
||||
return combine(second_reg_);
|
||||
|
||||
}
|
||||
|
||||
regex::regex regex::regex::combine(regex second_){
|
||||
|
||||
if(addition_ || second_.addition_)
|
||||
|
||||
throw std::runtime_error("дополнение нельзя совместить");
|
||||
|
||||
regex new_regex_;
|
||||
|
||||
compile();
|
||||
|
||||
second_.compile();
|
||||
|
||||
new_regex_.pattern_ = pattern_ + "|" + second_.pattern_;
|
||||
|
||||
new_regex_.nfa_ = nfa_.copy();
|
||||
|
||||
nfa second_nfa_;
|
||||
|
||||
second_nfa_ = second_.nfa_.copy();
|
||||
|
||||
new_regex_.nfa_.alternation(second_nfa_);
|
||||
|
||||
new_regex_.dfa_.dfaGenerate(new_regex_.nfa_);
|
||||
|
||||
new_regex_.min_dfa_.minimization(new_regex_.dfa_);
|
||||
|
||||
new_regex_.compiled_ = 1;
|
||||
|
||||
return new_regex_;
|
||||
|
||||
}
|
||||
|
||||
std::string regex::regex::rebirth(){
|
||||
|
||||
compile();
|
||||
|
||||
return dfa_.rebirth();
|
||||
}
|
||||
|
||||
void regex::regex::print(std::string name_){
|
||||
|
||||
|
||||
compile();
|
||||
|
||||
nfa_.print(name_ + "_nfa");
|
||||
|
||||
dfa_.print(name_ + "_dfa");
|
||||
|
||||
min_dfa_.print(name_ + "_min_dfa");
|
||||
|
||||
}
|
||||
|
||||
bool regex::regex::operator==(regex second_){
|
||||
|
||||
compile();
|
||||
|
||||
second_.compile();
|
||||
|
||||
return (min_dfa_ == second_.min_dfa_);
|
||||
|
||||
}
|
||||
|
||||
regex::regex regex::regex::intersection(std::string first_str_, std::string second_str_){
|
||||
|
||||
regex first_reg_(first_str_);
|
||||
|
||||
return first_reg_.intersection(second_str_);
|
||||
|
||||
}
|
||||
|
||||
regex::regex regex::regex::intersection(std::string second_str_){
|
||||
|
||||
regex second_reg_(second_str_);
|
||||
|
||||
return intersection(second_reg_);
|
||||
|
||||
}
|
||||
|
||||
regex::regex regex::regex::intersection(regex second_){
|
||||
|
||||
compile();
|
||||
|
||||
second_.compile();
|
||||
|
||||
regex new_regex_;
|
||||
|
||||
new_regex_.compiled_ = 1;
|
||||
|
||||
new_regex_.dfa_ = dfa_.intersection(second_.dfa_);
|
||||
|
||||
new_regex_.pattern_ = new_regex_.dfa_.rebirth();
|
||||
|
||||
new_regex_.nfa_.nfaGenerate(new_regex_.pattern_);
|
||||
|
||||
dfa dfa_v2_;
|
||||
|
||||
dfa_v2_.dfaGenerate(new_regex_.nfa_);
|
||||
|
||||
new_regex_.min_dfa_.minimization(dfa_v2_);
|
||||
|
||||
return new_regex_;
|
||||
|
||||
}
|
||||
87
regex.hpp
Normal file
87
regex.hpp
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
//
|
||||
// regex.hpp
|
||||
// lab2
|
||||
//
|
||||
// Created by Владислав Агибалов on 15.07.2025.
|
||||
//
|
||||
|
||||
#ifndef regex_hpp
|
||||
#define regex_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
#include "nfa.hpp"
|
||||
#include "dfa.hpp"
|
||||
|
||||
namespace regex{
|
||||
|
||||
class regex{
|
||||
|
||||
bool addition_ = 0;
|
||||
|
||||
std::string pattern_ = "";
|
||||
|
||||
bool compiled_ = 0;
|
||||
|
||||
nfa nfa_;
|
||||
|
||||
dfa dfa_;
|
||||
|
||||
dfa min_dfa_;
|
||||
|
||||
std::string recognazing_text_ = "";
|
||||
|
||||
size_t current_pos_ = 0;
|
||||
|
||||
size_t end_pos_ = 0;
|
||||
|
||||
public:
|
||||
|
||||
regex();
|
||||
|
||||
regex(std::string);
|
||||
|
||||
void compile();
|
||||
|
||||
void compile(std::string);
|
||||
|
||||
bool rmatch(std::string);
|
||||
|
||||
size_t findAll(std::string, std::vector<match>&);
|
||||
|
||||
bool find(std::string, match&);
|
||||
|
||||
bool find(match&);
|
||||
|
||||
bool rmatch(std::string, match&);
|
||||
|
||||
size_t findAllWithGroups(std::string, std::vector<match>&);
|
||||
|
||||
bool findWithGroups(std::string, match&);
|
||||
|
||||
bool findWithGroups(match&);
|
||||
|
||||
regex addition();
|
||||
|
||||
static regex combine(std::string, std::string);
|
||||
|
||||
regex combine(std::string);
|
||||
|
||||
regex combine(regex);
|
||||
|
||||
static regex intersection(std::string, std::string);
|
||||
|
||||
regex intersection(std::string);
|
||||
|
||||
regex intersection(regex);
|
||||
|
||||
std::string rebirth();
|
||||
|
||||
void print(std::string);
|
||||
|
||||
bool operator==(regex);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* regex_hpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue