0
我想與C源代碼,但我有erorr在我的計劃詞法分析器用C eroor
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","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",
"unsigned","void","volatile","while"};
main()
{
\t char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";
\t int j;
\t char fname[50];
\t FILE *f1;
\t //clrscr();
printf("enter file path (drive:\\fold\\filename)\n");
scanf("%s",fname);
f1 = fopen(fname,"r");
//f1 = fopen("Input","r");
\t if(f1==NULL)
\t {
\t printf("file not found");
\t exit(0);
\t }
\t while((ch=fgetc(f1))!=EOF)
\t {
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator\n",ch);
op++;
str[i]='\0';
keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1)
break;
if(ch==seps[j])
{
if(ch=='#')
{
while(ch!='>')
{
printf("%c",ch);
ch=fgetc(f1);
}
printf("%c is a header file\n",ch);
i=-1;
break;
}
if(ch=='"')
{
do
{
ch=fgetc(f1);
printf("%c",ch);
}while(ch!='"');
printf("\b is an argument\n");
i=-1;
break;
}
str[i]='\0';
keyw(str);
}
}
if(i!=-1)
{
str[i]=ch;
i++;
}
else
i=0;
\t }
printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n",kw,id,op,num);
//getch();
}
void keyw(char *p)
{
int k,flag=0;
for(k=0;k<=31;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword\n",p);
kw++;
flag=1;
break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
printf("%s is a number\n",p);
num++;
}
else
{
//if(p[0]!=13&&p[0]!=10)
if(p[0]!='\0')
{
printf("%s is an identifier\n",p);
id++;
}
}
}
i=-1;
}
eroor
C:\用戶\ Jurs的\文檔\ ABC \ main.cpp在函數'int main()'中:
13 27 C:\ Users \ Jurs \ Documents \ abc \ main.cpp [錯誤] chars數組的初始化字符串太長[-fpermissive]
25 9 C:\用戶\ Jurs的\文件\ ABC \ main.cpp中[錯誤] '退出' 在此範圍
28℃未聲明:\用戶\ Jurs的\文件\ ABC \生成文件。贏得目標 'main.o' 配方失敗
感謝您的幫助 – 2015-04-01 10:16:04
我怎樣才能到檢測或掃描分號? – 2015-04-01 15:43:56
@Jujur - 分號已經在列表中。如果不起作用,請提供一個新問題,提供簡單輸入,預期結果和實際結果的示例。 – 4386427 2015-04-01 16:19:30