資源簡介
編譯原理實驗,用C++實現的一個對if then條件語句的翻譯過程,可以實現對簡單布爾表達式和簡單賦值語句的翻譯,可以識別長串標識符。

代碼片段和文件信息
#include
#include
#include
#include“bianyi_h.h“
using?namespace?std;
void?main()
{
lexi_analy();
cout<<“*******詞法分析的結果為:*******“< print();
cout< print_grammar();//輸出所用文法
Analyser();
cout<<“******語法分析的結果用三地址輸出為:******“< printexpression();
system(“pause“);
}
bool?init(char?filename[255])
{
fin.open(filename);
if(fin.fail())
{
cout<<“文件打開出錯!“< return?false;
}
return?true;
}
void?lexi_analy()
{
cout<<“輸入待編譯文件名:“;
char?fname[200];
cin>>fname;
if(!init(fname))???//文件打開初始化沒成功
return;
char?al[1000];
//int?pos=0;
int?i=0;
char?ch;
int?j=0;
fin.seekg(j);
fin.get(ch);
al[j]=ch;
while(al[j]!=‘#‘)
{?
if(al[j]>=‘a‘?&&?al[j]<=‘z‘||al[j]>=‘A‘?&&?al[j]<=‘Z‘||al[j]==‘_‘)
{ //變量或關鍵字
int?m=0;
char?b[20];
b[m++]=al[j++];
fin.seekg(j);
fin.get(ch);
al[j]=ch;
while(ch!=‘?‘&&ch!=‘\n‘&&(al[j]>=‘a‘?&&?al[j]<=‘z‘||al[j]>=‘A‘?&&?al[j]<=‘Z‘||al[j]==‘_‘||al[j]>=‘0‘&&al[j]<=‘9‘))
{
b[m++]=al[j++];
fin.seekg(j);
fin.get(ch);
al[j]=ch;
}
//j++;
b[m]=‘\0‘;
string?b1=b;
if(b1==“if“)
{
Word_table[W_length].ch=“if“;
Word_table[W_length++].type=1;
}
else?if(b1==“then“)
{
Word_table[W_length].ch=“then“;
Word_table[W_length++].type=2;
}
else
{//變量
Word_table[W_length].type=11;
Word_table[W_length++].ch=b;
}
}
else?if(al[j]==‘(‘)
{
Word_table[W_length].ch=“(“;
Word_table[W_length++].type=3;
j++;
}
else?if(al[j]==‘)‘)
{
Word_table[W_length].ch=“)“;
Word_table[W_length++].type=4;
j++;
}
else?if(al[j]>=‘0‘&&al[j]<=‘9‘)????????????//判斷數字
{
char?b[20];
int?m=0;
b[m++]=al[j++];
fin.seekg(j);
fin.get(ch);
al[j]=ch;//再讀入下一個字符
while(al[j]>=‘0‘&&al[j]<=‘9‘)
{
b[m++]=al[j++];
fin.seekg(j);
fin.get(ch);
al[j]=ch;
}
if(al[j]!=‘.‘)//如果當前讀入的符號不為小數點,則常數讀完畢
{
b[m++]=‘\0‘;
}
else//當前字符為小數點則繼續向下讀入
{
b[m++]=al[j++];
fin.seekg(j);
fin.get(ch);
al[j]=ch;
while(al[j]>=‘0‘&&al[j]<=‘9‘)
{
b[m++]=al[j++];
fin.seekg(j);
fin.get(ch);
al[j]=ch;
}
b[m]=‘\0‘;
}
Word_table[W_length].type=12;
Word_table[W_length++].ch=b;
}
else?if(al[j]==‘+‘||al[j]==‘-‘||al[j]==‘*‘||al[j]==‘/‘)
{
Word_table[W_length].type=13;
Word_table[W_length++].ch=al[j];
j++;
}
else?if(al[j]==‘>‘)
{
j++;
fin.seekg(j);
fin.get(ch);
if(ch==‘=‘)
{
al[j++]=ch;
Word_table[W_length].ch=“>=“;
Word_table[W_length++].type=6;
}
else
{
Word_table[W_length].ch=“>“;
Word_table[W_length++].type=9;
//j--;
}
}
else?if(al[j]==‘<‘)
{
j++;
fin.seekg(j);
fin.get(ch);
if(ch==‘=‘)
{
al[j++]=ch;
Word_table[W_length].ch=“<=“;
Word_table[W_length++].type=
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????81920??2010-01-08?14:43??遞歸下降法\debug\遞歸下降法.exe
?????文件?????663376??2010-01-08?14:43??遞歸下降法\debug\遞歸下降法.ilk
?????文件?????576512??2010-01-08?14:43??遞歸下降法\debug\遞歸下降法.pdb
?????文件???????8384??2010-01-08?14:43??遞歸下降法\遞歸下降法\bianyicpp.cpp
?????文件???????1300??2010-01-08?00:10??遞歸下降法\遞歸下降法\bianyi_h.h
?????文件?????116194??2010-01-08?14:43??遞歸下降法\遞歸下降法\Debug\bianyicpp.obj
?????文件???????6674??2010-01-08?14:43??遞歸下降法\遞歸下降法\Debug\BuildLog.htm
?????文件?????????63??2010-01-08?14:43??遞歸下降法\遞歸下降法\Debug\mt.dep
?????文件?????183296??2010-01-08?14:43??遞歸下降法\遞歸下降法\Debug\vc80.idb
?????文件?????208896??2010-01-08?14:43??遞歸下降法\遞歸下降法\Debug\vc80.pdb
?????文件????????403??2010-01-05?21:08??遞歸下降法\遞歸下降法\Debug\遞歸下降法.exe.em
?????文件????????468??2010-01-05?21:08??遞歸下降法\遞歸下降法\Debug\遞歸下降法.exe.em
?????文件????????385??2010-01-08?14:43??遞歸下降法\遞歸下降法\Debug\遞歸下降法.exe.intermediate.manifest
?????文件????????243??2010-01-10?17:13??遞歸下降法\遞歸下降法\result.txt
?????文件?????????45??2010-01-08?14:45??遞歸下降法\遞歸下降法\sample.txt
?????文件???????4041??2010-01-06?17:05??遞歸下降法\遞歸下降法\遞歸下降法.vcproj
?????文件???????1407??2010-01-10?22:18??遞歸下降法\遞歸下降法\遞歸下降法.vcproj.hp-PC.hp.user
?????文件????1428480??2010-01-10?22:18??遞歸下降法\遞歸下降法.ncb
?????文件????????910??2010-01-05?21:00??遞歸下降法\遞歸下降法.sln
????..A..H.?????25088??2010-01-10?22:18??遞歸下降法\遞歸下降法.suo
?????目錄??????????0??2010-01-08?14:43??遞歸下降法\遞歸下降法\Debug
?????目錄??????????0??2010-01-08?13:10??遞歸下降法\debug
?????目錄??????????0??2010-01-08?14:43??遞歸下降法\遞歸下降法
?????目錄??????????0??2010-01-08?14:30??遞歸下降法
-----------?---------??----------?-----??----
??????????????3308085????????????????????24
評論
共有 條評論