資源簡介
是使用C++書寫的Lex工具,通過了基本的測試。

代碼片段和文件信息
#include
#include
#include
#include
#include
#include?“source1.h“
ifstream?fin;
ofstream?fout;
main()
{
char?action[100][100];//用于記錄規則段動作部分
////////////////////////////////////////////////////////定義段掃描,分析
fin.open(“test.l“ios::out);//打開輸入文件
fout.open(“Seu_Lex.cpp“ios::out);//打凱輸出文件
char?c;
if(fin.get()!=‘%‘||fin.get()!=‘{‘){
cerr<<“Wrong?fileerror?1“< exit(0);
}
fin.get();
c=fin.get();
while(c!=‘%‘){
fout.put(c);
c=fin.get();
}
fin.get();
fin.get();
List?trans;//用于記錄定義段RE
char*?temp=new?char[100];
fin.getline(temp100);
while(temp[0]!=‘%‘||temp[1]!=‘%‘){
trans.AddTail(temp);
fin.getline(temp100);
}
// trans.print();
////////////////////////////////////////////////////////規則段掃描,分析
string?s;
stack?st;?
NFA?nfa1nfa2;
int?pos=0;
List?workl;//結果DFA
fin.getline(temp100‘?‘);
while(temp[0]!=‘%‘||temp[1]!=‘%‘){
s=string(temp);
RE?re(s);
bool?flag=true;
if(re.re[0]==‘{‘){//RE的替換(對{num}等形式,用定義段RE替換)
for(ListNode*?p=trans.first;p!=NULL;p=p->link){
int?i=1;
flag=true;
while(re.re[i]!=‘}‘){
if(p->data[i-1]!=re.re[i]){
flag=false;
break;
}
i++;
}
if(flag){
re.re.erase(0i+1);
while(1){
if(p->data[0]!=‘:‘){
p->data.erase(01);
}
else{
p->data.erase(01);
break;
}
}
re.re.insert(0p->data);
}
}
if(re.re[0]==‘{‘){
cerr<<“Error2!!!“< exit(0);
}
}
re.extend();//擴展
re.toPostOrder();//轉為后綴
for(int?i=0;i if(!re.isOp(re.re[i]))
st.push(NFA(re.re[i]));//不是運算符?進棧
else?if(re.re[i]==‘*‘){//‘*’運算,出棧一個,運算后入棧
nfa1.copy(st.top());
nfa1.copy(nfa1.operate(nfa1STAR));
st.pop();
st.push(nfa1);
}
else?if(re.re[i]==‘~‘){//‘~’運算,出棧兩個,運算后入棧
nfa1.copy(st.top());
st.pop();
nfa2.copy(st.top());
st.pop();
nfa1.copy(nfa2.operate(nfa1CONCAT));
st.push(nfa1);
}
else{//‘|’運算,出棧兩個,運算后入棧
nfa1.copy(st.top());
st.pop();
nfa2.copy(st.top());
st.pop();
nfa1.copy(nfa2.operate(nfa1UNION));
st.push(nfa1);
}
}
nfa1.copy(st.top());
// nfa1.view();
DFA?dfa(nfa1);//轉DFA
// dfa.view();
dfa.minDFA();//最小化DFA
// dfa.view();
workl.AddTail(dfa);//DFA加入工作鏈表
st.pop();
fin.getline(action[pos]100);//記錄相應動作
fin.getline(temp100‘?‘);
pos++;
}
///////////////////////////////////////////////////////////轉換為代碼
string?code;
DFA?workdfa;
int?flag=0;
code=“int?analysis(string?s)\n{\n\tint?state=0;\n\tbool?match=false;\n\tint?i;\n\t“;
for(int?i=0;i fout.put(code[i]);
}
for(ListNode*?p=workl.first;p!=NULL;p=p->link)//對工作鏈表中每一個DFA
{
workdfa.copy(p->data);
code=workdfa.toCode();
for(i=0;i
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3400??2008-05-08?00:02??Lex\源代碼\setli
?????文件???????3873??2008-05-08?08:53??Lex\源代碼\SeuLex.cpp
?????文件??????14957??2008-05-08?00:45??Lex\源代碼\source1.cpp
?????文件???????2536??2008-05-08?00:28??Lex\源代碼\source1.h
?????文件???????2400??2008-05-08?08:40??Lex\源代碼\test.l
?????文件????????141??2009-04-02?19:17??Lex\源代碼\說明.txt
?????文件???????1193??2008-05-08?08:57??Lex\生成代碼\matchtab.h
?????文件??????32059??2008-05-08?08:53??Lex\生成代碼\Seu_Lex.cpp
?????文件?????????63??2008-05-08?09:01??Lex\生成代碼\說明.txt
?????目錄??????????0??2009-04-02?19:17??Lex\源代碼
?????目錄??????????0??2009-04-02?19:16??Lex\生成代碼
?????目錄??????????0??2009-03-14?02:38??Lex
-----------?---------??----------?-----??----
????????????????60622????????????????????12
- 上一篇:C語言實現 學生信息管理系統 雙鏈表
- 下一篇:VC調用agilent矢量網絡分析儀
評論
共有 條評論