• 大小: 13KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-05-29
  • 语言: C/C++
  • 标签: 编译原理  NFA  DFA  

资源简介

编译原理课的大作业 包含三个小实验 在一个cpp文件里 正则表达式转换为nfa nfa转换为dfa dfa最小化 个人原创代码

资源截图

代码片段和文件信息

#include
using namespace std;
#include
#include
#include
#include
#include



class NFA_Node;

class Trans
{
public:
char incept;
NFA_Node* des;
Trans(char inceptNFA_Node* des)
{
this->incept=incept;
this->des=des;
}
};

class NFA_Node
{
public:
int stateID;
vector t;
bool visit;
NFA_Node(int stateID)
{
visit=false;
this->stateID=stateID;
}
void AddTrans(Trans* tt)
{
t.push_back(tt);
}

}; 

class NFA
{
public:
NFA_Node* start;
NFA_Node* end;
NFA(){}
NFA(int SIDchar c)
{
NFA_Node* s1=new NFA_Node(SID);
NFA_Node* s2=new NFA_Node(SID+1);
Trans* tt=new Trans(cs2);
s1->AddTrans(tt);
start=s1;
end=s2;
}

};

class Converter
{
  public:
        int S_ID;
        Converter(string str)
        {
            pretreat(str);
            Houzhui(this->lamb);
            S_ID=1;
        }
        Converter(){S_ID=1;}
        
        void show()
        {
            cout<lamb<        }
        
        NFA ToNFA()
        {
            //stNFA.Clear();
            //Operator_Stack.Clear();
            NFA tempbtempb1tempb2;
           
            char tempc1;
            for(int i=0;i            {
                tempc1 = lamb[i];
                if (isOperator(tempc1))
                {
                    switch (tempc1)
                    {
                        case ‘|‘:
                            tempb1 = stNFA.top();
                            stNFA.pop();
                            tempb2 = stNFA.top();
                            stNFA.pop();
                            tempb1=Union(tempb2tempb1);
                            stNFA.push(tempb1);
                            break;
                        case ‘&‘:
                            tempb1 = stNFA.top();
                            stNFA.pop();
                            tempb2 = stNFA.top();
                            stNFA.pop();
                            tempb2=Connect(tempb1tempb2);
                            stNFA.push(tempb2);
                            break;
                        case ‘*‘:
                            tempb1 = stNFA.top();
                            stNFA.pop();
                            tempb1=Closure(tempb1);
                            stNFA.push(tempb1);
                            break;
                    }
                }
                else
                {
                    tempb = NFA(S_IDtempc1);
                    S_ID+=2;
                    stNFA.push(tempb);
                }
            }
            tempb = stNFA.top();
            stNFA.pop();
            return tempb;
        }
      
        
  private:
        stack stNFA;
        stack Operator_Stack;
        string lamb;
        
        bool isOperator(

评论

共有 条评论