您的位置:首页 > 其它

编译原理上机程序 之 词法分析器

2014-11-21 21:54 357 查看
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <string.h.>
# include <windows.h>
using namespace std;
int file(FILE *fp);
int shuxing()
{
system("cls");//清屏
system("color 1E"); //设
printf("********************************************************************************");
printf("**********************************使用说明**************************************");
printf("********************************************************************************\n\n\n\n\n");
printf("\n\n>>>>>>>>>>>>>>>>>使用前请先建好文件夹\n");
printf("\n\n>>>>>>>>>>>>>>>>>文件没建好请按 1 结束操作,继续进行请按任意键(不包括1)!\n");
char n[1000];
printf("\t\t\t\t");
gets(n);
if(strcmp(n,"1")==0)
{
printf("\n\nn>>>>>>>>>>>>>>>>>谢谢使用,再见!\n");
system("pause");//清屏
exit(0);
}

return 0;

}
int start()
{
system("cls");//清屏
system("color 1E"); //设置文字颜色为黄色 背景色蓝色
printf("\n\n\n\n\n\n");
printf("********************************************************************************");
printf("*******************************欢迎使用词法分析器*******************************");
printf("********************************************************************************\n\n\n\n\n");
printf("\t\t\t\t    制作: 宋国鹏 \n\n");
printf("\t\t\t\t\t       2014年10月\n\n\n\n");

printf("\t\t\t\t\t***按任意键进入***\n");
return 0;
}

int file(FILE *fp)
{
char str1[1000][1000];
int i,j;
i=0;
j=0;
char ch;
while(!feof(fp))
{
if((ch=fgetc(fp))!='\n')
str1[i][j++]=ch;
else
{

str1[i][j]='\0';
i++;
j=0;
}
}
system("cls");//清屏
system("color 1E"); //设
printf("\n>>>>>>>>>>文件中的程序是!\n\n\n");
for(int a=0;a<i;a++)
printf("%s\n",str1[a]);
printf("\t\t\t\t\t***按任意键继续***\n");
getchar();
char word[1000][100];
int a=0;
int b=0;

bool flag1;
int n=i;
for(i=0;i<n;i++)
{
flag1=0;
for(j=0;(str1[i][j])!='\0';j++)
{
while(str1[i][j]==' ')
{
j++;
flag1=0;
}
char ch=str1[i][j];
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'))
{
if(flag1)
{
word[a][b]=ch;
b++;
word[a][b]='\0';
}
else
{
a++;              //第一行a从1开始取
b=0;
word[a][b]=ch;
b++;
word[a][b]='\0';
flag1=1;
}
}
else
{
a++;              //第一行a从1开始取
b=0;
word[a][b]=ch;
b++;
word[a][b]='\0';
flag1=0;
}
}
}
system("cls");//清屏
system("color 1E"); //设
printf("\n>>>>>>>>>拆分后单词为:\n\n\n");
for(int i=1;i<a;i++)
{
printf("%10s",word[i]);
if(i%6==0)
printf("\n");
}
printf("\n\n\t\t\t\t\t***按任意键继续***\n");
getchar();
system("cls");//清屏
system("color 1E");
printf("\n>>>>>>>>>>拆分后单词的属性:\n\n");
char biao[100][30]={
"void","int","float","double","char","if","else","while","do","return","for",
"auto","break","case","const","continue","default","switch","enum","extern","goto","long",
"register","short","signed","sizoef","static","struct","switch","typedef","union","unsigned","volatile","include"
};
bool flag=0;
for(int i=1;i<a;i++)
{
flag=1;
if(strcmp(word[i],"main")==0)
printf("%8s  : <1,主函数>\n",word[i]);
else
{
for(int j=0;j<34;j++)
if(strcmp(word[i],biao[j])==0)
{
printf("%8s  : <2,关键字>\n",word[i]);
flag=0;
break;
}
if(flag)
{
if(strcmp(word[i],"+")==0||strcmp(word[i],"-")==0)
{
if(strcmp(word[i],word[i+1])==0)
{
strcat(word[i],word[i+1]);
printf("%8s  : <3,自增运算符>\n",word[i]);
i++;
}
else
printf("%8s  : <4,普通运算符>\n",word[i]);
}
else if(strcmp(word[i],"%")==0)
{
if(word[i+1][0]>='0'&&word[i+1][0]<='9')
printf("%8s  : <5,取余运算符>\n",word[i]);
else
printf("%8s  : <6,格式化字符串>\n",word[i]);
}
else if(strcmp(word[i],"*")==0||strcmp(word[i],"/")==0)
printf("%8s  :<4,普通运算符>\n",word[i]);
else if(strcmp(word[i],">")==0||strcmp(word[i],"<")==0||strcmp(word[i],"=")==0)
{
if(strcmp(word[i+1],"=")==0)
{
strcat(word[i],word[i+1]);
printf("%8s  : <7,关系运算符>\n",word[i]);
i++;
}
else if(strcmp(word[i],"=")==0)
printf("%8s  : <8,赋值运算符>\n",word[i]);
else
printf("%8s  : <7,关系运算符>\n",word[i]);
}
else if(strcmp(word[i],"!")==0)
{
if(strcmp(word[i],"=")==0)
{
strcat(word[i],word[i+1]);
printf("%8s  : <7,关系运算符>\n",word[i]);
i++;
}
else
printf("%8s  : <9,逻辑非>\n",word[i]);
}
else if (strcmp(word[i],"&")==0||strcmp(word[i],"|")==0)
{
if(word[i][0]=='&'||word[i][0]=='|')
{
strcat(word[i],word[i+1]);
printf("%8s  : <7,关系运算符>\n",word[i]);
i++;
}
else
printf("%8s  : <10,逻辑运算的符>\n",word[i]);
}
else if(strcmp(word[i],"(")==0||strcmp(word[i],")")==0||strcmp(word[i],"[")==0||strcmp(word[i],"]")==0||strcmp(word[i],"}")==0||strcmp(word[i],"{")==0||strcmp(word[i],";")==0||strcmp(word[i],",")==0)
printf("%8s  : <11,界符>\n",word[i]);
else if(strlen(word[i])<=1)
{
if((word[i][0]>='0'&&word[i][0]<='9'))
printf("%8s  : <12,整数>\n",word[i]);
else if((word[i][0]>='A'&&word[i][0]<='Z')||(word[i][0]>='a'&&word[i][0]<='z'))
printf("%8s  : <13,标识符>\n",word[i]);
}
else
{
bool z=1;
bool f=0;
if(word[i][0]=='0')
{
for(int h=1;word[i][h]!='\0';h++)
{
if((word[i][h]>='A'&&word[i][h]<='Z')||(word[i][h]<='z'&&word[i][h]>='a')||(word[i][h]=='_'))
{
printf("%8s  :error 不符合标示符的定义\n",word[i]);
z=0;
break;
}
else if(word[i][h]=='.')
f=1;
}
if(z)
{
if(f)
printf("%8s  : <4,实数>\n",word[i]);
else
printf("%8s  : <12,整数>\n",word[i]);
}
}
else
{
bool l=0;
for(int h=1;word[i][h]!='\0';h++)
{
if((word[i][h]>='0'&&word[i][h]<='9')||(word[i][h]<='z'&&word[i][h]>='a')||(word[i][h]<='Z'&&word[i][h]>='A')||word[i][h]=='_')
;
else
{
l=1;
printf("%8s  :error 不符合标示符定义,存在其他不符合标准的符号\n",word[i]);
}
}
if(!l)
printf("%8s  : <13,标示符>\n",word[i]);
}
}
}
}
}

return 0;
}
int main()
{
char str[100];

FILE *fp;
start();
getchar();
shuxing();
system("cls");//清屏
system("color 1E"); //设
printf("\n\n\n\n>>>>>>>>>>>>>请输入文件位置!\n");
for(;;)
{
scanf("%s",str);
if((fp=fopen(str,"r"))==NULL)
printf("\n\n>>>>>>>>>>>>>输入错误,请重新输入!\n");
else
{
printf("\n\n>>>>>>>>>>>>>输入成功!\n");
break;
}
}
printf("\n\n\t\t\t\t\t***按任意键继续***\n");
getchar();

file(fp);

fclose(fp);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: