#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct word{
char sign[16];
short type;
};
//保留字数组
word res[9]={"begin",1,"end",2,"integer",3,"if",4,"then",5,"else",6,"function",7,"read",8,"write",9};
//判断字符串token是否是保留字
short reserve(string tokn){
for(short i=0;i<9;i++)
{ if(tokn==string(res[i].sign))
return i+1;
}
return 0;
}
void main(){
char input[180];
// short type;
string token;
char tmp;
int counte,index=0,line=0;
ifstream sourcefile;
sourcefile.open("source.pas");
if(!sourcefile){
cout<<"source.pas open error!"<<endl;
exit(1);
}
/* else{
sourcefile>>input;
}
counte=strlen(input);
*/
ofstream wordoutfile;
wordoutfile.open("word.dyd",ios::out|ios::trunc);
if(!wordoutfile){
cout<<"word.dyd open error!"<<endl;
exit(1);
}
ofstream errfile;
errfile.open("error.err",ios::out|ios::trunc);
if(!errfile){
cout<<"error.err open error!"<<endl;
exit(1);
}
while(sourcefile.getline(input,180)){ //读入一行源代码
line++; //行数加一
int linelength=strlen(input);
counte=0;
cout<<"LINE:"<<line<<endl;
while(counte<linelength){
token="";
char sign[16];
while(input[counte]==' '){counte++;} //跳过空格
//cout<<counte<<"word"<<endl;
tmp=input[counte];
if((tmp>='a'&&tmp<='z')||(tmp>='A'&&tmp<='Z')){ //当前字符串是标识符或保留字
while((tmp>='a'&&tmp<='z')||(tmp>='0'&&tmp<='9')||(tmp>='A'&&tmp<='Z'))
{ token+=tmp;
counte++;
tmp=input[counte];
// cout<<"word "<<counte<<endl;
}
//counte--;
short num=reserve(token);
if(num!=0){
token.copy(sign,token.length());
sign[token.length()]='\0';
wordoutfile<<sign<<' '<<num<<endl;
}
else{
token.copy(sign,token.length());
sign[token.length()]='\0';
wordoutfile<<sign<<' '<<10<<endl;
}
}
else if(tmp>='0'&&tmp<='9'){ //当前字符串是数字
while(tmp>='0'&&tmp<='9'){
token+=input[counte];
counte++;
tmp=input[counte];
}
token.copy(sign,token.length());
sign[token.length()]='\0';
wordoutfile<<sign<<' '<<11<<endl;
}
else if(tmp=='<'){
counte++;
if(input[counte]=='='){ //当前字符串是“<=”
counte++;
sign[0]='<';
sign[1]='=';
sign[2]='\0';
wordoutfile<<sign<<' '<<14<<endl;
}
else if(input[counte]=='>'){ //当前字符串是"<>"
counte++;
sign[0]='<';
sign[1]='>';
sign[2]='\0';
wordoutfile<<sign<<' '<<13<<endl;
}
else{ ////当前字符串是"<"
sign[0]='<';
sign[1]='\0';
wordoutfile<<sign<<' '<<15<<endl;
}
}
else if(tmp=='>'){
counte++;
if(input[counte]=='='){ //当前字符串是">="
counte++;
sign[0]='>';
sign[1]='=';
sign[2]='\0';
wordoutfile<<sign<<' '<<16<<endl;
}
else{ //当前字符串是">"
sign[0]='>';
sign[1]='\0';
wordoutfile<<sign<<' '<<17<<endl;
}
}
else if(tmp=='='){
counte++;
if(input[counte]=='='){
counte++;
sign[0]='=';
sign[1]='=';
sign[2]='\0';
wordoutfile<<sign<<' '<<12<<endl;
}
else{
errfile<<"***LINE:"<<line<<' '<<' '<<"非法字符'='"<<endl;
}
}
else if(tmp=='-'){
counte++;
sign[0]=tmp;
sign[1]='\0';
wordoutfile<<sign<<' '<<18<<endl;
}
else if(tmp=='*'){
counte++;
sign[0]=tmp;
sign[1]='\0';
wordoutfile<<sign<<' '<<19<<endl;
}
else if(tmp==':'){
counte++;
if(input[counte]=='='){
sign[0]=':';
sign[1]='=';
sign[2]='\0';
counte++;
wordoutfile<<sign<<' '<<20<<endl;
}
else
errfile<<"***LINE:"<<line<<' '<<' '<<"非法字符':'"<<endl;
}
else if(tmp=='('){
counte++;
sign[0]='(';
sign[1]='\0';
wordoutfile<<sign<<' '<<21<<endl;
}
else if(tmp==')'){
counte++;
sign[0]=')';
wordoutfile<<sign<<' '<<22<<endl;
}
else if(tmp==';'){
counte++;
sign[0]=';';
sign[1]='\0';
wordoutfile<<sign<<' '<<23<<endl;
}
else
{ errfile<<"***LINE:"<<line<<' '<<' '<<"非法字符:"<<tmp<<endl;
counte++;
}
}
wordoutfile<<"EOLN "<<24<<endl; //一行结束
}
wordoutfile<<"EOF"<<' '<<25<<endl;
errfile.close();
sourcefile.close();
wordoutfile.close();
cout<<"词法分析完成:"<<endl;
}