?? define.cpp
字號:
//單詞編碼定義
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
using namespace std;
string map[62]={
"", "and", "array", "begin", "bool", "call", "case", "char", "constant" ,
"dim", "do", "else", "end", "false", "for", "if", "input", "integer", "not",
"of", "or", "output", "procedure", "program", "read", "real", "repeat", "set",
"stop", "then", "to", "true", "until", "var", "while", "write","","","", "(",
")", "*", "*/", "+", ",", "-", ".", "..", "/", "/*", ":", ":=", ";", "<", "<=",
"<>", "=", ">", ">=", "[","]"
};
typedef struct ID
{
string cs1;
ID *next;
int count;
int att;
}id;
void main()
{
cout<<"作者:孫立超;計算機(jī)科學(xué)與技術(shù)2班;200530472370;"<<endl;
char word[20], ch ;
FILE *fp;
string cs;
static int sign=1;
static int i2=1;
ID *log=new ID;
ID *p; //字符常數(shù)的鏈表
p=new ID;
p->next=NULL;
ID *q; //整數(shù)的鏈表
q=new ID;
q->next=NULL;
ID *r; //標(biāo)記符的鏈表
r=new ID;
r->next=NULL;
fp=fopen("測試文件.txt","r");
if(fp==NULL)
{
cout<<"file can not be open\n";
exit(0);
}
while(!feof(fp))
{
for(int i=0;i<20;i++) //打開文件,以空格為分界,將字符串賦值給string類型的cs
{
ch=fgetc(fp);
if(ch!=' '&&ch!='\n')
word[i]=ch;
else
{
cs.assign(word,i);
break;
}
}
// cout<<"af"<<cs<<"fd"<<endl;
if(cs.compare("")==0)
continue;
for(int i1=1;i1<61;i1++) //遍歷比較map表,
{ if(cs.compare(map[i1])==0) //找出關(guān)鍵字和特殊符號的編碼,注意對注釋內(nèi)容的處理
{
log->cs1=cs;
log->att=i1;
log->count=0;
}
}
static int count7 =0; //count7哨兵
if(cs.compare("/*")==0)
{
count7=1;
}
if(count7&&cs.compare("*/")!=0)
{
continue;
}
if(cs.compare("*/")==0)
{ count7=0;}
//查找字符常數(shù),然后放入p鏈表
if(cs.find("'")==0)
{
cs=cs.substr(1,cs.length()-2);
ID *temporary=new ID;
temporary->cs1.assign(cs);
temporary->att=38;
temporary->next=NULL;
int count1=1;
ID *q1, *q3, *q2;
q1=q; q3=q;
while(q1!=NULL)
{
if((temporary->cs1).compare(q1->cs1)==0)
{
count1=0;
log->count=q1->count;
log->cs1=q1->cs1;
delete(temporary);
break;
}
else
{
q2=q1;
q1=q1->next;
}
}
if(count1)
{
temporary->count=sign;
q2->next=temporary;
log->cs1=temporary->cs1;
log->att=temporary->att;
log->count=sign;
sign++;
}
// cout<<log->cs1<<log->count<<log->att<<endl;
// q3=q3->next;
// while(q3!=NULL)
// { cout<<q3->cs1<<q3->count<<q3->att<<endl;q3=q3->next ;}
}
else
{
//查找標(biāo)識符,然后標(biāo)記.
int count4=1; //count4哨兵
for(int ic=0;ic<cs.length();ic++)
{
if(65>cs[ic]||cs[ic]>122||(90<cs[ic]&&cs[ic]<96))
{
count4=0;break;
}
}
if(count4)
{
int count5=1;
for(int ib=1;ib<=35;ib++)
if(cs.compare(map[ib])==0)
{
count5=0;break; //count5哨兵
}
if(count5)
{
ID *temporary2=new ID;
temporary2->cs1.assign(cs);
temporary2->att=36;
temporary2->next=NULL;
int count6=1; //count6哨兵
ID *r1, *r3, *r2;
r1=r; r3=r;
while(r1!=NULL)
{
if((temporary2->cs1).compare(r1->cs1)==0)
{
count6=0;
log->count=r1->count;
log->cs1=r1->cs1;
delete(temporary2);
break;
}
else
{
r2=r1;
r1=r1->next;
}
}
if(count6)
{
temporary2->count=sign;
r2->next=temporary2;
log->cs1=temporary2->cs1;
log->att=temporary2->att;
log->count=sign;
sign++;
}
//這個位置的cs為所要求的字符串,其他也是如此
// cout<<"標(biāo)識符"<<log->cs1<<log->count<<log->att<<endl;
// r3=r3->next;
// while(r3!=NULL)
// {cout<<r3->cs1<<r3->count<<r3->att<<endl;r3=r3->next ;}
}
}
//查找整數(shù),然后標(biāo)記
int count2=1;
for(int ia=0;ia<cs.length();ia++)
if(47>=cs[ia]||cs[ia]>=58)
{count2=0;break;}
if(count2) //篩選出整數(shù)的cs
{
ID *temporary1=new ID;
temporary1->cs1.assign(cs);
temporary1->att=37;
temporary1->next=NULL;
int count3=1;
ID *p1, *p3, *p2;
p1=p; p3=p;
while(p1!=NULL)
{
if((temporary1->cs1).compare(p1->cs1)==0)
{
count3=0;
log->count=p1->count;
log->cs1=p1->cs1;
delete(temporary1);
break;
}
else
{
p2=p1;
p1=p1->next;
}
}
if(count3)
{
temporary1->count=sign;
p2->next=temporary1;
log->cs1=temporary1->cs1;
log->att=temporary1->att;
log->count=sign;
sign++;
}
// cout<<log->cs1<<log->count<<log->att<<endl;
// p3=p3->next;
// while(p3!=NULL)
// { cout<<"整數(shù)"<<p3->cs1<<p3->count<<p3->att<<endl;p3=p3->next; }
}
}
if(log->cs1.compare("")==0)
continue;
//cout<<"df"<<log->cs1<<"gh"<<endl;
// cout<<"("<<log->att<<","<<log->count<<")\t";
if(log->count)
{
// cout<<log->cs1<<endl;
cout<<"("<<log->att<<","<<log->count<<")\t";
if(i2%5==0)
cout<<endl;
i2++;
}
else
{
if(log->cs1.compare("]")==0)
cout<<log->cs1;
cout<<"("<<log->att<<",-)\t";
if(i2%5==0)
cout<<endl;
i2++;
}
}
cout<<endl;
fclose(fp);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -