#include <stdio.h>
#include <string.h>
#define boolean int
#define true 1;
#define false 0;
char ch=' ';
char temp_all[40];
char* keyword[32] ={"auto","break","caes","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto","if","int","long",
"register","return","short","signed","sizeof","static","struct","switch",
"typedef","union","unsiged","viod","volatile","while"};
//是否是关键词
boolean isKeyWord(char* temp){
for(int j=0;j<32;j++){
if(strcmp(temp,keyword[j])==0){
return true;
}
}
return false;
}
//是否是字母
boolean isLetter(char letter){
/*ascii码表中大小字母之前有其他的字符*/
if((letter>='A'&&letter<='Z')||(letter>='a'&&letter<='z')){
return true;
}
else {
return false;
}
}
//是否是数字
boolean isDigit(char digit){
if(digit>='0'&&digit<='9'){
return true;
}
else{
return false;
}
}
//是否是引号
boolean isQuote(char quote){
if(quote=='"'||quote=='\''){
return true;
}
else{
return false;
}
}
//是否是分界符
boolean isBoundary(char boundary){
switch (boundary)
{
case ',':
case ';':
case ':':
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
case '#':
case '.':
{
return true;
}
default:
{
return false;
}
}
}
//是否是操作符 存在一些占双字符或三字节的运算符
boolean isOperator(char operator){
switch (operator)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '>':
case '<':
case '=':
case '&':
case '|':
case '!':
case '?':
{ return true;
}
default:
return false;
}
}
//是否是下划线
boolean isUnderline(char ul){
if(ul=='_'){
return true;
}
else{
return false;
}
}
void analyze(FILE* fp){
while((ch=fgetc(fp))!=EOF){
//读入tab,空字符,换行符等,什么也不做
if(ch==' ') { }
if(ch=='\t'){ }
if(ch=='\n'){ }
//读入数字后,拼数
else if(isDigit(ch)){
char temp_local[40]={'\0'};
int i=0;
while(ch=='.'||isDigit(ch)){
temp_local[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1L,SEEK_CUR);
printf("字面量\t\t%s\n",temp_local);
}
//读入引号
else if(isQuote(ch)){
if(ch=='"'){
char temp_local[40]={'\0'};
int i=0;
do{
temp_local[i]=ch;
i++;
ch=fgetc(fp);
}
while(ch!='"');
temp_local[i]=ch;
printf("字面量\t\t%s\n",temp_local);
}
else if(ch=='\''){
if(ch=='\''){
char temp_local[40]={'\0'};
int i=0;
do{
temp_local[i]=ch;
i++;
ch=fgetc(fp);
}
while(ch!='\'');
temp_local[i]=ch;
printf("字面量\t\t%s\n",temp_local);
}
}
}
//读入字母
else if(isLetter(ch)||isUnderline(ch)){
char temp_local[40]={'\0'};
int i=0;
while(isLetter(ch)||isDigit(ch)||isUnderline(ch)){
temp_local[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1L,SEEK_CUR);
if(isKeyWord(temp_local)){
printf("保留字\t\t%s\n",temp_local);
}
else {
printf("标识符\t\t%s\n",temp_local);
}
}
else if(isBoundary(ch)){
printf("分界符\t\t%c\n",ch);
}
else if(isOperator(ch)){
char temp_local[40]={'\0'};
int i=0;
while(isOperator(ch)){
temp_local[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1L,SEEK_CUR);
printf("操作数\t\t%s\n",temp_local);
}
}
}
//图形界面
int GUI(){
printf("\t 词法分析器 \n");
printf("\t-----------------------------------------\n");
printf("\t1.分析指定路径文件词法\t\t");
printf("2.退出\n");
printf("\t-----------------------------------------\n");
printf("\t请输入1-2\n");
int x;
scanf_s("%d",&x);
return x;
}
void main(){
int x;
LA:
x=0;
x=GUI();
if(x==1){
system("cls");
FILE* fp;
printf("请输入文件路径:(例如: D:/A.txt)\n");
char post[40];
scanf_s("%s",post);
fp= fopen(post,"r");
if(fp!=NULL){
printf("成功打开文件\n\n");
printf("单词类型\t单词值\n");
printf("______________________\n");
analyze(fp);
fclose(fp);
}
else{
printf("打开文件失败,请输入正确的路径\n");
}
printf("______________________\n\n");
printf("1.返回主界面\n");
printf("2.退出\n");
int y;
scanf("%d",&y);
if(y==1){
system("cls");
goto LA;
}
else{
exit(0);
}
system("pause");
}
else if (x==2)
{
exit(0);
}
}