⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yufa.txt

📁 词法分析
💻 TXT
📖 第 1 页 / 共 2 页
字号:
while(ch>='0'&&ch<='9') 
{ 
array[i++]=ch; 
infile.get(ch); 
} 
word=new char[i+1]; 
memcpy(word,array,i); 
word[i]='\0'; 
intdeal(word); 
if(ch!=EOF) 
infile.seekg(-1,ios::cur); 
} 
else if((ch==' ')||(ch=='\t')) 
; /*消除空格符和水平制表符*/ 
else if(ch=='\n') 
line++; /*消除回车并记录行数*/ 
else if(ch=='/') 
{ /*消除注释*/ 
infile.get(ch); 
if(ch=='=') 
{ /*判断是否为‘/=’符号*/ 
outfile.open("output.txt",ios::noreplace|ios::app); 
outfile<<"/=\t\t\t4\t\t\t32\n"; 
outfile.close(); 
} 
else if(ch!='*') 
{ /*若为除号,写入输出文件*/ 
outfile.open("output.txt",ios::noreplace|ios::app); 
outfile<<"/\t\t\t4\t\t\t13\n"; 
outfile.close(); 
outfile.seekg(-1,ios::cur); 
} 
else if(ch=='*') 
{ /*若为注释的开始,消除包含在里面的所有字符*/ 
count=0; 
infile.get(ch); 
while(count!=2) 
{ /*当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束*/ 
count=0; 
while(ch!='*') 
infile.get(ch); 
count++; 
infile.get(ch); 
if(ch=='/') 
count++; 
else 
infile.get(ch); 
} 
} 
} 
else if(ch=='"') 
{ /*消除包含在双引号中的字符串常量*/ 
outfile.open("output.txt",ios::noreplace|ios::app); 
outfile<<ch<<"\t\t\t4\t\t\t37\n"; 
outfile.close(); 
while(ch!='"') 
infile.get(ch); 
infile<<ch<<"\t\t\t4\t\t\t37\n"; 
infile.close(); 
} 
else 
{ /*首字符为其它字符,即运算限界符或非法字符*/ 
array[0]=ch; 
infile.get(ch); /*再读入下一个字符,判断是否为双字符运算、限界符*/ 
if(ch!=EOF) 
{ /*若该字符非文件结束符*/ 
array[1]=ch; 
word=new char[3]; 
memcpy(word,array,2); 
word[2]='\0'; 
result=search(word,4,1); /*先检索是否为双字符运算、限界符*/ 
if(result==0) 
{ /*若不是*/ 
word=new char[2]; 
memcpy(word,array,1); 
word[1]='\0'; 
result=search(word,4,1); /*检索是否为单字符运算、限界符*/ 
if(result==0) 
{ /*若还不是,则为非法字符*/ 
errordeal(array[0],line); 
errorno++; 
infile.seekg(-1,ios::cur); 
} 
else 
{ /*若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符*/ 
outfile.open("output.txt",ios::noreplace|ios::app); 
outfile<<word<<"\t\t\t4\t\t\t"<<result<<"\t"<<endl; 
outfile.close(); 
infile.seekg(-1,ios::cur); 
} 
} 
else 
{ /*若为双字符运算、限界符,写入输出文件*/ 
outfile.open("output.txt",ios::noreplace|ios::app); 
outfile<<word<<"\t\t\t4\t\t\t"<<result<<endl;; 
outfile.close( ); 
} 
} 
else 
{ /*若读入的下一个字符为文件结束符*/ 
word=new char[2]; 
memcpy(word,array,1); 
word[1]='\0'; 
result=search(word,4,1); /*只考虑是否为单字符运算、限界符*/ 
if(result==0) /*若不是,转出错处理*/ 
errordeal(array[0],line); 
else 
{ /*若是,写输出文件*/ 
outfile.open("output.txt",ios::noreplace|ios::app); 
outfile<<word<<"\t\t\t4\t\t\t"<<result<<"\t"<<endl; 
outfile.close(); 
} 
} 
} 
infile.get(ch); 
} 
infile.close(); 
cout<<"\nThere are "<<errorno<<" error(s).\n"; /*报告错误字符个数*/ 
} 
void main() 
{ char yn; 

do{ 
init(); /*初始化*/ 
scanner();/*扫描源程序*/ 

printf("Are You continue(y/n)\n"); //判断是否继续? 
yn=getch(); 
}while(yn=='y'||yn=='Y'); 
}




语义分析:
#include <iostream> 
#include <string> 
#include <fstream> 
#include <queue> 
#include <string.h> 
#include <stdio.h> 

using namespace std; 

enum Datatype { RESERVE_WORD=1,IDENTIFIER=2,DIGIT=3,OPERATOR=4,SEPRATOR=5 }; 

struct OutputStruct 
{ 
public: 
Datatype type; 
string value; 
}; 

string operate[]={"sin","cos","pow"}; 
string KeyWord[]={"main","int","if","char","cout"}; 
const int MAX_SIZE=255; 
char BUFF[MAX_SIZE]; //buffer to contain a char line. 
ifstream inFile; 
ofstream outFileStream; 
queue<OutputStruct> tt; 

bool IsKeyWord(string& cs) 
{ 
for(int i=0;i<5;++i) 
if(cs==KeyWord[i]) 
return true; //Exist 
return false; 
} 

void ReadLineAndAnalyze() 
{ 
int strSize=0; 
int i; 
int errFlag=0; 
char ch; 
string outStructStr,str; 
struct OutputStruct outStruct; 
{ 
i=0; 
inFile.getline(BUFF,MAX_SIZE,'\n'); 
strSize=inFile.gcount(); 
cout<<BUFF; 
do{ 
str=""; 
do{ 
ch=BUFF[i]; 
i++; 
}while(ch==' '||ch==' '||ch=='\n'); 
switch(ch) 
{ 

case '+': 
case '-': 
case '*': 
case '/': 
outStruct.type=OPERATOR; 
outStruct.value=ch; 
break; 
case '=': 
case '>': 
case '<': 
outStructStr=ch; 
if(BUFF[i]=='=') 
{ 
outStruct.type=OPERATOR; 
outStructStr+=BUFF[i]; 
outStruct.value=outStructStr; 
i++; 
} 
else 
{ 
outStruct.type=OPERATOR; 
outStruct.value=ch; 
}; 
break; 

case ',': 
case ';': 
case '{': 
case '}': 
case '(': 
case ')': 
case '[': 
case ']': 
case '\"': 
outStruct.type=SEPRATOR; 
outStruct.value=ch; 
break; 

case '0': 
case '1': 
case '2': 
case '3': 
case '4': 
case '5': 
case '6': 
case '7': 
case '8': 
case '9': 
outStructStr+=ch; 
while(BUFF[i]>='0'&&BUFF[i]<='9'||BUFF[i]=='.') 
{ 
outStructStr+=BUFF[i]; 
i++; 
}//while 
outStruct.type=DIGIT; 
outStruct.value=outStructStr; 
break; 

default: 
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') 
{ 
outStructStr+=ch; 
while(BUFF[i]>='a'&&BUFF[i]<='z'||BUFF[i]>='A'&&BUFF[i]<='Z') 
{ 
outStructStr+=BUFF[i]; 
i++; 
}//while 
if(IsKeyWord(outStructStr)) 
{ 
outStruct.type=RESERVE_WORD; 
outStruct.value=outStructStr; 
} 
else 
{ 
outStruct.type=IDENTIFIER; 
outStruct.value=outStructStr; 
} 
break; 
} 
else 
errFlag=1; 
}//switch; 
if(!errFlag) 
tt.push(outStruct); 
errFlag=0; 
outStructStr=""; 
}while(i<strSize-1); 

}//while(i<MAX_SIZE&&!inFile.eof());//do_while 
return; 
} 

float F(); 
float T(); 
float E(); 
float S(); 

float F() 
{ 
float ret; 
if((tt.front().type==IDENTIFIER)||(tt.front().type==DIGIT)) 
{ 
ret=atof(tt.front().value.c_str()); 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
return ret; 
} 
if(tt.front().value=="(") 
{ 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
ret=E(); 
if(tt.front().value==")") 
{ 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
return ret; 
} 
else 
{ 
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少右括号"<<endl; 
cout<<"Press \"enter\" to modify the data file!"; 
getchar(); 
system("notepad data.txt"); 
exit(0); 
} 
} 
else 
{ 
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少因子"<<endl; 
cout<<"Press \"enter\" to modify the data file!"; 
getchar(); 
system("notepad data.txt"); 
exit(0); 
} 
} 
float T() 
{ 
float i,j; 
i=F(); 
if(tt.front().value=="*") 
{ 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
j=T(); 
return i*j; 
} 
else if(tt.front().value=="/") 
{ 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
j=T(); 
if(abs(j)<0.0000001) 
{ 
cout<<"\b ----ERROR! 除数为零!"<<endl; 
cout<<"Press \"enter\" to modify the data file!"; 
getchar(); 
system("notepad data.txt"); 
exit(0); 
} 
return i/j; 
} 
return i; 
} 

float E() 
{ 
float i,j; 
i=T(); 
if(tt.front().value=="+") 
{ 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
j=E(); 
i=i+j; 
} 
else if(tt.front().value=="-") 
{ 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
j=E(); 
i=i-j; 
} 
if(tt.front().value==";"||tt.front().type==OPERATOR||tt.front().value==")") 
return i; 
else 
{ 
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少运算符"<<endl; 
cout<<"Press \"enter\" to modify the data file!"; 
getchar(); 
system("notepad data.txt"); 
exit (0); 
} 
} 

float S() 
{ 
float i; 
i=E(); 
if(tt.front().value==";") 
{ 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
tt.pop(); 
return i; 
} 
cout<<"\b ----ERROR! "<<tt.front().value<<" 缺少左括号"<<endl; 
cout<<"Press \"enter\" to modify the data file!"; 
getchar(); 
system("notepad data.txt"); 
exit(0); 
} 

void GrammaAnalize() 
{ 
float i; 
if(tt.empty()) 
{ 
cout<<"END"<<endl;exit(0); 
} 
i=S(); 
cout<<"\b="<<i<<endl; 
} 

int main() 
{ 
inFile.open("data.txt"); 
if(!inFile) 
{ 
cout<<"打开源文件失败!"; 
return 1; 
} 
while(!inFile.eof()) 
{ 
ReadLineAndAnalyze(); 
GrammaAnalize(); 
} 
inFile.close(); 
return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -