?? cp.cpp
字號:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct symbolrecord /*單詞編碼表*/
{
char symbol[10];
int id;
}symbolrecord[100];
struct entrytype /*符號表*/
{
char idname[10];
int address;
char type[10];
}entrytype[100];
struct tokentype /*字*/
{
int id;
int entry;
}tokentype[100];
struct idname
{
int id;
char name[10];
}idname[100];
///////////////////////////////////////////////////////////////////////////
int strlenth;
char str[1000];
char word[10];//識別標識符
int n_word=0;//為標識符計數
int n_idname=0;//為變量計數
int token=0;
int num_symbolrecord=0;
int num_entrytype=0;
int num_tokentype=0;
void scanner();
void sort(char ch);
void recogid(char ch,int token);//lookup char
void recogdig(char ch,int token);//lookup num
void handlecom(char ch,int token);
void recogdel(char ch,int token);
void initsymb();//讀入單詞編碼表
void inittoken();//讀入token表
void writeid();//將變量寫到文件中
void readme();
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
readme();
scanner();
writeid();
}
void scanner()
{
char a;
int n=0;
initsymb();//讀入單詞編碼表
inittoken();//讀入token表
FILE* pfile;
pfile=freopen("MY.txt","r",stdin);
if(pfile==NULL)
printf("open file failed!\n");
a=getchar();
if((int)a==10) a=getchar();
while(a!=EOF)
{
str[n]=a;
sort(a);
n++;
a=getchar();
if((int)a==10) a=getchar();
}
printf("\nread resource file complete!\n");
fclose(pfile);
}
//////////////////////////////////////////////////////////////////////////////
void initsymb()
{
FILE *pfile;
int i=0;
char c;
char strsymb[100]={""};
char *p=&c;
pfile=freopen("symbolrecord.txt","r",stdin);
if(pfile==NULL) printf("failed open token file!\n");
c=getchar();
while(c!=EOF)
{
if((int)c!=10&&(int)c!=32&&((int)c<48||(int)c>57)&&(int)c>=0)
{
strsymb[i]=c;
strsymb[i+1]='\0';
i++;
c=getchar();
if((int)c==32)
{
strcpy(symbolrecord[num_symbolrecord].symbol,strsymb);
printf("%s ",strsymb);
for(int a=0;a<i;a++)
strsymb[a]='\0';
i=0;
}
}
else if((int)c>=48&&(int)c<=57)//number
{
strsymb[i]=c;
strsymb[i+1]='\0';
i++;
c=getchar();
if((int)c==32||(int)c==10||(int)c==-1)
{
int temp=0;
sscanf(strsymb,"%d",&temp);
printf("%d\n",temp);
symbolrecord[num_symbolrecord].id=temp;
num_symbolrecord++;
for(int a=0;a<i;a++)
strsymb[a]='\0';
i=0;
}
}
else c=getchar();
}
printf("symbolrecord file read OK!\n");
fclose(pfile);
}
void inittoken()
{
FILE* pfile;
char cc;
char readnum[100]={""};
pfile=freopen("token.txt","r",stdin);
if(pfile==NULL) printf("open token file failed!\n");
cc=getchar();
int temp=0;
int s=0;
while(cc!=EOF)
{
if((int)cc!=32&&(int)cc!=10)
{
readnum[s]=cc;
s++;
cc=getchar();
}
else if((int)cc==32)
{
sscanf(readnum,"%d",&temp);
for(int f=0;f<=10;f++)
{
readnum[f]='\0';
}
tokentype[num_tokentype].id=temp;
num_tokentype++;
printf("%d ",temp);
s=0;
cc=getchar();
}
else if((int)cc==10)
{
sscanf(readnum,"%d",&temp);
for(int f=0;f<=10;f++)
{
readnum[f]='\0';
}
tokentype[num_tokentype].entry=temp;
num_tokentype++;
printf("%d\n",temp);
s=0;
cc=getchar();
}
else printf("error!\n");
}
printf("************open token file OK!************\n");
printf("****************token字說明****************\n");
printf("token 功能\n");
printf(" 1 關鍵字\n");
printf(" 2 變量\n");
printf(" 3 數字\n");
printf(" 4 界符\n");
printf(" 5 運算符\n");
printf("********************************************\n");
printf("程序中僅包含對<=號的處理,對于>=和==號處理方式與此相同\n");
printf("為了簡單,程序中沒有對界符進行處理\n");
printf("文檔說明:\n");
printf("MY.txt 源程序文件\n");
printf("symbolrecord.txt 單詞編碼表文件\n");
printf("token.txt token字文件\n");
printf("id.txt 變量存儲文件\n");
printf("*******************************************\n");
}
void sort(char ch)
{
if(((int)ch>=65&&(int)ch<=90)||((int)ch>=97&&(int)ch<=122)) /*字母 1*/
{
token=1;
recogid(ch,token);
}
else if((int)ch>=48&&(int)ch<=57) /*數字 3*/
{
token=3;
recogdig(ch,token);
}
else if(ch=='/') /*除法或注釋 4*/
{
token=4;
handlecom(ch,token);
}
else if((int)ch<65||(int)ch>90&&(int)ch<97||(int)ch>122)
{
token=5;
recogdel(ch,token); /*界符 5*/
}
}
void recogid(char ch,int token)
{
char ch2;
int flag=0;
char temp[10]={""};
word[n_word]=ch;
n_word++;
ch2=getchar();
while((int)ch2!=32&&(int)ch2!=10&&ch2!=';'&&((((int)ch2>=65&&(int)ch2<=90)||((int)ch2>=97&&(int)ch2<=122))||((int)ch2>=48&&(int)ch2<=57)))//字母或數字
{
word[n_word]=ch2;
ch2=getchar();
n_word++;
}
for(int i=0;i<=11;i++)
{
strcpy(temp,symbolrecord[i].symbol); //識別關鍵字
flag=(strcmp(word,temp));
if(flag==0)
{
printf("%s %d\n",word,token);
break;
}
}
if(flag!=0)
{
int flag2=0;
token=2;
printf("%s %d\n",word,token); //字符變量
strcpy(temp,word);
for(int j=0;j<=n_idname;j++)
{
if(int flag3=(strcmp(idname[j].name,word))==0) //查找已存在的變量
{
//printf("flag3=%d\n",flag3);
flag2=1;
break;
}
}
if(flag2==0)
{
strcpy(idname[n_idname].name,word);//如變量表中沒有該變量,存入表中
idname[n_idname].id=n_idname;
// printf("%d %s\n",idname[n_idname].id,idname[n_idname].name);
flag=0;
n_idname++;
}
}
for(i=0;i<=n_word;i++)
{
word[i]='\0';
}
n_word=0;
if(ch2==EOF) return;
else if(ch2==' '||(int)ch2==10)
return;
else if(ch2=='/')
handlecom(ch2,4);
else recogdel(ch2,5);
token=0;
}
void recogdig(char ch,int token)
{
printf("%c %d\n",ch,token);
token=0;
}
void handlecom(char ch,int token)
{
printf("%c %d\n",ch,token);
token=0;
}
void recogdel(char ch,int token)
{
char ch2;
ch2=getchar();
printf("%c %d ",ch,token);
switch(ch)
{
case '=':
{
printf("15\n");
if(ch2>=97&&ch2<=122||ch2>=65&&ch2<=90)
{
recogid(ch2,1);
}
else if(ch2>=48&&ch2<=57)
{
recogdig(ch2,3);
}
break;
}
case '!':{printf("16\n");break;}
case '&':{printf("17\n");break;}
case '|':{printf("18\n");break;}
case '<':
{
if(ch2=='=')
{
printf("%c 21 (<=)\n",ch2);
//printf("21\n");
break;
}
else
{
printf("19\n");
if((int)ch2>=97&&(int)ch2<=122||(int)ch2>=65&&(int)ch2<=90)
recogid(ch2,1);
else if((int)ch2>=48&&(int)ch2<=57)
recogdig(ch2,3);
else if(ch2=='/')
handlecom(ch2,4);
else recogdel(ch2,5);
break;
}
}
case '>':{printf("20\n");break;}
case '<=':{printf("21\n");break;}
case '>=':{printf("22\n");break;}
case '<>':{printf("23\n");break;}
case '==':{printf("24\n");break;}
case '+':
{
printf("25\n");
if(ch2>=97&&ch2<=122||ch2>=65&&ch2<=90)
{
recogid(ch2,1);
}
break;
}
case '*':{printf("26\n");break;}
case '(':{printf("27\n");break;}
case ')':{printf("28\n");break;}
default: {printf("NO such a letter!");}
}
token=0;
}
void writeid()
{
FILE* pfile=freopen("id.txt","w",stdout);
for(int i=0;i<n_idname;i++)
{
printf("%d %s\n",idname[i].id,idname[i].name);
}
if(pfile==NULL)
printf("failed open id file!\n");
else
{
fclose(pfile);
printf("Succeed write IDs to files!\n");
}
}
void readme()
{
printf(" ******************\n");
printf(" * 詞法分析 *\n");
printf(" *實驗學院:馬智超*\n");
printf(" *學號:6030310104*\n");
printf(" * 2006/03/22 *\n");
printf(" ******************\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -