資源簡介
編譯原理課的大作業 包含三個小實驗 在一個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(
- 上一篇:板材切割c源碼
- 下一篇:操作系統文件管理系統模擬實現
評論
共有 條評論