📄 未命名1.cpp
字号:
/* Experment 3_4 Define and Fullfill token file */
#include "stdio.h"
#include "string.h"
Src[80][80];
/* Static Array for source file */
struct entry
{
/*define a struct */
char word[9];
int token;
};
static struct entry word[60]={
"and",1,"array",2,"begin",3,"bool",4,"call",5,"case",6,
"char",7,"constant",8,"do",9,"else",10,"end",11,"false",12,
"for",13,"if",14,"input",15,"integer",16,"not",17,"of",18,
"or",19,"output",20,"procedure",21,"program",22,"read",23,"real",24,
"repeat",25,"set",26,"then",27,"to",28,"true",29,"until",30,
"var",31,"while",32,"write",33,"标识符",34,"整常数",35,"实常数",36,
"字符常数",37,"'",38,"(",39,")",40,"*",41,"*/",42,
"+",43,",",44,"-",45,"`",46,"..",47,"/",48,"/*",49,":",50,":=",51,
";",52,"<",53,"<=",54,"<>",55,"=",56,">",57,">=",58,"[",59,"]",60
};
int IsAlpha(char ch) ;
int IsNum(char ch) ;
int RecogId(char *str) ;
int RecogInt(char *str) ;
int RecogInt(char *str) ;
int RecogChar(char *str) ;
int RecogFloat(char *str) ;
int DelCom(char *str) ;
struct entry lookup(struct entry *words,char *str) ;
int InSrc() ;
int IsTm(char *ch);
void main()
{ int i=0,j=0;
char *ch="*";
/*InSrc();Read source file into static array*/
/*
for(t=0;t<80;t++)
printf("%s",Src[t]);
*/
printf("%d",IsTm(ch));
getch();
}
int IsAlpha(char ch)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))
return 1;
else
return 0;
}
int IsNum(char ch)
{
if(ch>='0'&&ch<='9')
return 1;
else
return 0;
}
int RecogInt(char *str)
{
/*Recog an leagle Int*/
int i=0,state=0;
while(i<=(strlen(str)-1))
{
if(IsNum(str[i]))
{
state=1;
i++;
}
else
{
state=0;
break;
}
}
return state;
}
int RecogId(char *str)
{
/*Recog an ligeal Identify */
int i=0,state=0;
if(IsAlpha(str[0])&&!IsNum(str[0]))
state=1;
i=1;
while(i<=(strlen(str)-1)&&(state==1))
{
if(IsAlpha(str[i])||IsNum(str[i]))
{
state=1;
i++;
}
else
{
state=0;
break;
}
}
return state;
}
int RecogChar(char *str)
{
/*Recog an leagle char*/
int i=0,state=0,len=strlen(str);
if(str[0]=='\''&&str[len-1]=='\'')
state=1;
i=1;
while(i<=(strlen(str)-2)&&state==1)
{
if(IsAlpha(str[i]))
{
state=1;
i++;
}
else
{
state=0;
break;
}
}
return state;
}
int RecogFloat(char *str)
{
/*Recog an leagle Float*/
int i,state=0,countdian=0,countE=0;
if(IsNum(str[0]))
state=1;
i=1;
while(i<=(strlen(str)-1)&&state==1)
{
if(IsNum(str[i])||str[i]=='.'||str[i]=='E')
{
state=1;
if(str[i]=='.')
countdian++;
if(countdian>1)
break;
if(str[i]=='E')
countE++;
if(countE>1)
break;
i++;
}
else
{
state=0;
break;
}
}
return state;
}
int DelCom(char *str)
{
/*Del comment int Src file */
int len=strlen(str);
if((str[0]=='/')&&(str[1]=='*')&&(str[len-2]=='*')&&(str[len-1]=='/'))
return 1;
else
return 0;
}
struct entry Lookup(char *str)
{
/*lookup if the input str is in table words */
int i=0,flag=0;
while(i<60&&flag==0)
{
if(strcmp(word[i].word,str)==0)
flag=1;
else
i++ ;
}
if(flag)
return word[i];
else if(RecogId(str))
return word[33];
else if(RecogInt(str))
return word[34];
else if(RecogFloat(str))
return word[35] ;
else if(RecogChar(str))
return word[36];
}
int InSrc()
{
int i=0;
char s[80],file1[20],file2[20];
FILE *fp1,*fp2; /*定义2个文件指针*/
printf("please tpye into src file1 'name:\n");
scanf("%s",file1);
printf("%s\n",file1);
printf("please tpye into des file2 'name:\n");
scanf("%s",file2);
printf("%s\n",file2);
if((fp1=fopen(file1, "rb"))!=NULL)
printf("file1 open ok !\n");/*打开当前目录名为1.txt的文件只读*/
else
{
printf("file1 open erro !\n");
exit(1);
}
if((fp2=fopen(file2, "wb"))!=NULL)
printf("file2 open ok !\n"); /*打开当前目录名为2.txt的文件追加*/
else
{
printf("file2 open erro !\n");
exit(1);
}
printf("+++++++++++++++++++++++++++ Programe Begin ++++++++++++++++++++++++++++++++\n");
while(!feof(fp1))
{
fgets(s,80,fp1); /*从文件中读取80个字符*/
printf("%s",s); /*输出所读的字符串*/
strcpy(Src[i],s);/*将当前文件中读出的一行拷贝到原程序数组中*/
i++;/*下次拷下一行*/
fputs(s,fp2);
}
printf("\n\n+++++++++++++++++++++++++ Programe End ++++++++++++++++++++++++++++++++\n");
if(!fclose(fp1)) /*关闭文件1*/
printf("Close file1 ok\n");
else
printf("Close file1 Erro\n");
if(!fclose(fp2)) /*关闭文件2*/
printf("Close file2 ok\n");
else
printf("Close file2 Erro\n");
getch();
return 0;
}
int IsTm(char *ch)
{
/*判断字符是否为终结符*/
struct entry temp;
temp=Lookup(ch);
if(temp.token>=38&&temp.token<=60)
return temp.token;
else
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -