gpt4 book ai didi

c - 在c中编码词法分析器时出错

转载 作者:行者123 更新时间:2023-11-30 14:30:49 24 4
gpt4 key购买 nike

#include<stdio.h>
#include<ctype.h>
#include<string.h>

/* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all the symbols. */

int main()
{
int i=0,j,k,count=0;
char a,b[100],c[10000],d[100];
memset ( d, 0, 100 );
j=30;



FILE *fp1,*fp2;


fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer
fp2=fopen("lext.txt","w");
//now lets remove all the white spaces and store the rest of the words in a file


if(fp1==NULL)
{
perror("failed to open source.txt");
//return EXIT_FAILURE;
}
i=0;
k=0;
while(!feof(fp1))
{


a=fgetc(fp1);


if(a!=' '&&a!='\n')
{
if (!isalpha(a))
{

switch(a)
{

case '+':{fprintf(fp2,"+ ----> PLUS \n");
i=0;break;}
case '-':{fprintf(fp2,"- ---> MINUS \n");
i=0;break;}
case '*':{fprintf(fp2, "* --->MULT \n");
i=0;break;}
case '/':{fprintf(fp2, "/ --->DIV \n");
i=0;break;}
//case '+=':fprintf(fp2, "%.20s\n", "ADD_ASSIGN");
//case '-=':fprintf(fp2, "%.20s\n", "SUB_ASSIGN");
case '=':{fprintf(fp2, "= ---> ASSIGN \n");
i=0;break;}
case '%':{fprintf(fp2, "% ---> MOD \n");
i=0;break;}
case '<':{fprintf(fp2, "< ---> LESSER_THAN \n");
i=0;break;}
case '>':{fprintf(fp2, "> --> GREATER_THAN \n");
i=0;break;}
//case '++':fprintf(fp2, "%.20s\n", "INCREMENT");
//case '--':fprintf(fp2, "%.20s\n", "DECREMENT");
//case '==':fprintf(fp2, "%.20s\n", "ASSIGNMENT");
case ';':{fprintf(fp2, "; --->SEMI_COLUMN \n");
i=0;break;}
case ':':{fprintf(fp2, ": --->COLUMN \n");
i=0;break;}
case '(':{fprintf(fp2, "( --->LPAR \n");
i=0;break;}
case ')':{fprintf(fp2, ") --->RPAR \n");
i=0;break;}
case '{':{fprintf(fp2, "{ --->LBRACE \n");
i=0;break;}
case '}':{fprintf(fp2, "} ---> RBRACE \n");
i=0;break;}
}
}
else
{

d[i]=a;
//printf("%c\n",d[i]);
i=i+1;


}
//}
/* we can make the lexer more complex by including even more depths of checks for the symbols*/









}
else
{


d[i+1]='\0';


printf("\n");

if((strcmp(d,"if ")==0)){fprintf(fp2,"if ----> IDENTIFIER \n");
//printf("%s \n",d);
memset ( d, 0, 100 );
//printf("%s \n",d);
count=count+1;}

else if(strcmp(d,"then")==0){fprintf(fp2,"then ----> IDENTIFIER \n");
count=count+1;}

else if(strcmp(d,"else")==0){fprintf(fp2,"else ----> IDENTIFIER \n");
count=count+1;}

else if(strcmp(d,"switch")==0){fprintf(fp2,"switch ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"printf")==0){fprintf(fp2,"prtintf ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"scanf")==0){fprintf(fp2,"scanf ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"NULL")==0){fprintf(fp2,"NULL ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"int")==0){fprintf(fp2,"INT ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"char")==0){fprintf(fp2,"char ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"float")==0){fprintf(fp2,"float ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"long")==0){fprintf(fp2,"long ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"double")==0){fprintf(fp2,"double ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"const")==0){fprintf(fp2,"const ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"continue")==0)fprintf(fp2,"continue ----> IDENTIFIER \n");

else if(strcmp(d,"size of")==0){fprintf(fp2,"size of ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"register")==0){fprintf(fp2,"register ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"short")==0){fprintf(fp2,"short ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"auto")==0){fprintf(fp2,"auto ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"while")==0){fprintf(fp2,"while ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"do")==0){fprintf(fp2,"do ----> IDENTIFIER \n");
count=count+1;}
else if(strcmp(d,"case")==0){fprintf(fp2,"case ----> IDENTIFIER \n");
count=count+1;}
else if (isdigit(d[i]))
{
fprintf(fp2,"%s ---->NUMBER",d);
}
else if (isalpha(a))
{
fprintf(fp2,"%s ----> Variable",d);
//printf("%s",d);
// memset ( d, 0, 100 );}
//fprintf(fp2, "s\n", b);
i=0;
k=k+1;

continue;
}

i=i+1;
k=k+1;


}
fclose(fp1);
fclose(fp2);
printf("%d",count);
return 0;
}

在此代码中,我的 source.txt 存储了 if (a+b) 。但只有 ( 、 + 和 ) 被写入 lext.txt,而不是标识符 if 或变量 a 和 b 。有什么具体原因吗?

最佳答案

存在不少问题:

一旦找到空格或换行符,您就尝试使用一系列 strcmp 来比较字符串 d 中的内容。但是您正在重置 else if (isalpha(a)) 主体中的 i 值,该值永远不会执行,因为 a 是空格或换行符。您应该立即无条件i的值设置为0

d[i+1]='\0';

字符串中的空格很重要,因此

if((strcmp(d,"if ")==0)) // d will never have a space as you never stuff it with one.

应该是

if((strcmp(d,"if")==0))

关于c - 在c中编码词法分析器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2669519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com