?? compile.txt
字號:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define N 256
char str[N];
char strtoken[20];
char *key[32]={ "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"};
int len;
int count=0;
void write(char *wstr,char wc,FILE *wout) //將分析結果按照規則寫入到文件
{
fputc('(',wout);
fputs(wstr,wout);
fputc(',',wout);
fputc(wc,wout);
fputc(')',wout);
}
void write2(char *wstr,char *wc,FILE *wout) //將分析結果按照規則寫入到文件
{
fputc('(',wout);
fputs(wstr,wout);
fputc(',',wout);
fputs(wc,wout);
fputc(')',wout);
}
int readstr(FILE *fp)
{
char ch;
len=0;
ch=fgetc(fp);
while(!feof(fp) && ch!='\n')
{
str[len]=ch;
ch=fgetc(fp);
len++;
}
len--;
if(feof(fp))
return 0;
else
return 1;
}
void error(int type)
{
if(type==1)
printf("is a invalid char,the%d line has a error the id should not begin from a digit\n",count);
else if(type==2)
printf("the%d lines has a error,the evaluate should be <variable>=<expression> \n ",count);
else printf("is a invalid character,the %d line has a error\n",count);
}
void check(char *str,FILE *out);
void fenxi(char *row,FILE *op)
{
int k=0;
int i=0;
int ferror=0;
char str0[20]; //臨時存放單詞的變量
int j=0;
//printf("this is the %dth line\n",count);
while(i<=len)
{
k=0;
strcpy(strtoken,"\0");
if(isspace(row[i]))
{
i++;
continue;
}
while(!isalpha(row[i])&&!isdigit(row[i])&&i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]=='{'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]=='}' || row[i]==';' || row[i]=='='))
{
putchar(row[i]);
i++;
ferror=1;
}
if(ferror==1)
{
error(3);
ferror=0;
}
if(row[i]=='/') //判斷是否為注釋
{
i++;
if(row[i]=='/')
{
i=len+1;
continue;
}
else
i--;
}
if(isdigit(row[i]))
{
while(i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]==';' || row[i]=='='||row[i]=='{'||row[i]=='}' ))
{
if(isdigit(row[i]))
{
str0[k]=row[i]; //是數字則將字符逐個存入臨時數組
i++;
k++;
}
else
{
ferror=1;break;
}
}
if(ferror==1)
{
for( j=0;j<k;j++)
putchar(str0[j]);
while(i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]==':'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]==';'||row[i]=='='))
{
putchar(row[i]);
i++;
}
error(1);
ferror=0;
i--;
strcpy(strtoken,"");
}
else
{
str0[k]='\0';
strcpy(strtoken,str0);
i--;
str0[0]='\0';
}
}
if(isalpha(row[i]))
{
k=0;
while(i<=len&&row[i]!=32&&!(row[i]=='\0'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]==';'|| row[i]=='='))//關鍵字和標志符由數字和字母組成
{
if(isalpha(row[i])||isdigit(row[i]))
{
str0[k]=row[i];
i++;
k++;
}
else
{
ferror=1;
break;
}
}
if(ferror)
{
for( j=0;j<k;j++)
putchar(str0[j]);
while(i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')'|| row[i]==';'||row[i]=='='))
{
putchar(row[i]);
i++;
}
ferror=0;
error(3);
i--;
}
else
{
str0[k]='\0';
strcpy(strtoken,str0);
str0[0]='\0';
i--;
}
}
if(row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]==';'||row[i]=='{'||row[i]=='}' || row[i]=='=')
{
str0[0]=row[i];
str0[1]='\0';
strcpy(strtoken,str0);
str0[0]='\0';
}
if(row[i]=='=')
{
i++;
if(row[i]=='=')
{
strtoken[0]='=';
strtoken[1]='=';
strtoken[2]='\0';
strcpy(strtoken,"==");
}
else
{
strcpy(strtoken,"=");
i--;
}
}
if(row[i]=='+')
{
i++;
if(row[i]=='+')
{
strtoken[0]='+';
strtoken[1]='+';
strtoken[2]='\0';
strcpy(strtoken,"++");
}
else
{
strcpy(strtoken,"+");
i--;
}
}
if(row[i]=='-')
{
i++;
if(row[i]=='-')
{
strtoken[0]='-';
strtoken[1]='-';
strtoken[2]='\0';
strcpy(strtoken,"--");
}
else
{
strcpy(strtoken,"-");
i--;
}
}
if(row[i]=='*')
{
i++;
if(row[i]=='*')
{
strtoken[0]='*';
strtoken[1]='*';
strtoken[2]='\0';
strcpy(strtoken,"**");
}
else
{
strcpy(strtoken,"*");
i--;
}
}
if(row[i]=='>')
{
i++;
if(row[i]=='=')
{
strcpy(strtoken,">=");
}
else
{
strcpy(strtoken,">");
i--;
}
}
if(row[i]=='<')
{
i++;
if(row[i]=='=')
{
strcpy(strtoken,"<=");
}
else
{ strcpy(strtoken,"<");i--;}
}
puts(strtoken);
check(strtoken,op);
strtoken[0]='\0';
i++;
}
}
void check(char *str,FILE *out)
{ int fyiyong=0;
int ct;
if(isdigit(str[0]))
{
write2(str,"digit",out);
}
if(isalpha(str[0]))
{
for( ct=0;ct<32;ct++)
{
if(!strcmp(str,key[ct]))
{
write2(str,"key",out);
fyiyong=1;
}
}
if(fyiyong!=1)
{
write2(str,"identifier",out);
}
}
if(str[0]=='>')
{
if(str[1]=='=')
{
write2(str,">=logic expression",out);
}
else
{
write2(str,">logic expression",out);
}
}
if(str[0]=='<')
{
if(str[1]=='=')
{
write2(str,"<=logic expression",out);
}
else
{
write2(str,"<logic expression",out);
}
}
if(!strcmp(str,"=="))
{
write2(str,"==logic expression",out);
}
if(str[0]=='+')
{
if(str[1]=='+')
{
write2(str,"++selfplus",out);
}
else
{
write2(str,"+plus",out);
}
}
if(str[0]=='-')
{
if(str[1]=='-')
{
write2(str,"--selfsub",out);
}
else
{
write2(str,"+sub",out);
}
}
if(str[0]=='*')
{
if(str[1]=='*')
{
write2(str,"power",out);
}
else
{
write2(str,"mul",out);
}
}
if(str[0]=='/' ){ write2(str,"div",out);}
if(str[0]=='=') { write2(str,"=assign",out);}
if(str[0]=='(' ){ write2(str,"lpar",out);}
if(str[0]==')' ){ write2(str,"rpar",out);}
if(str[0]==';' ){ write2(str,"semicolon",out);}
if(str[0]=='{' ){ write2(str,"lbrace",out);}
if(str[0]=='}' ){ write2(str,"rbrace",out);}
}
void main()
{
int count=1;
char scfilename[20],rsfilename[20];
FILE *fp,*op;
printf("Please input your source file name:");
gets(scfilename);
printf("Please input your result file name:");
gets(rsfilename);
fp=fopen(scfilename,"r");
op=fopen(rsfilename,"w");
if(fp)
{
while(readstr(fp))
{
fenxi(str,op);
count++;
}
}
else
{
printf("Your source file not exist!!!\n");
exit(0);
}
fclose(fp);
fclose(op);
printf("ok!");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -