C词法分析,以小程序试验可以,大点的就缺些数据?
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char c,a[30],b[20];
int i=0,count0=0,count1=0,count2=0,count3=0,count5=0;
bool flag=true;
FILE *fp1,*fp2;
char *key[]={" ","auto","bool","break","case","char","complex","const","continue","default","restrict","do","double",
"else","enum","extern","float","for","goto","if","imaginary","inline","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while","#define","#include"}; /*C语言所有关键字,共37个*/
char *oper[]={" ","!","%","^","&","*","-","+","=","~","|",".",
"<",">","/","?",":","+=","-=","*=","/=","%=","<<==",">>==","|=","->","++",
"--","<<",">>","<=",">=","==","!=","&&","||"};/*运算符*/
char *sep[]={" ","#","(",")","[","]","\'",";",":","\"","{","}",",","\\"," "};/*分隔符*/
/************************
是否为保留字
************************/
int iskey(char *a){
for(i=1;i<40;i++)
if(strcmp(a,key[i])==0){
fprintf(fp2,"0\t\t\t%d\t\t\t%s\n",i,a);
return 1;
}
return 0;
}
/**************************
是否为运算符
***************************/
void isoper(char *a){
for(i=1;i<35;i++)
if(strcmp(a,oper[i])==0){
fprintf(fp2,"2\t\t\t%d\t\t\t%s\n",i,a);
}
}
/************************
是否为分隔符
************************/
void issep(char *a){
for(i=1;i<14;i++)
if(strcmp(a,sep[i])==0){
fprintf(fp2,"1\t\t\t%d\t\t\t%s\n",i,a);
}
}
/*是否为标识符*/
void issyb(char *a){
FILE *fp0;
int k;
k=strlen(a);
if(((a[0]>='A')&&(a[0]<='Z'))||((a[0]>='a')&&(a[0]<='z'))||(a[0]=='_')){
for(i=1;i<k-1;i++){
if(((a[i]<48||a[i]>122)||(a[i]>57&&a[i]<65)||(a[i]>90&&a[i]<97))&&(a[i]!='_'))
flag=false;
continue;
}
if(flag==true){
count5++;
fp0=fopen("bzf.txt","a");
fprintf(fp0,"%s\t\t\t3\t\t\t%d\n",a,count5);
fclose(fp0);
fprintf(fp2,"3\t\t\t%d\t\t\t%s\n",count5,a);
}
}
}
/**********************
是否为整型或浮点型常量
***********************/
void isdigit(char *a){
FILE *fp6;
if((a[0]>='0')&&(a[0]<='9')){
for(i=0;i<100;i++){
if(a[i]=='.')
flag=false;
continue;}
if(flag==false){ /*浮点型常量输出*/
fp6=fopen("cl.txt","a");
count1++;
fprintf(fp6,"%s\t\t\t4\t1\t\t%d\n",a,count1);
fclose(fp6);
fprintf(fp2,"4\t\t\t1\t%d\t\t%s\n",count1,a);
}
else{ /*整型常量*/
fp6=fopen("cl.txt","a");
count0++;
fprintf(fp6,"%s\t\t\t4\t0\t\t%d\n",a,count0);
fclose(fp6);
fprintf(fp2,"4\t\t\t0\t%d\t\t%s\n",count0,a);
}
}
}
/***********************************
字符、字符串常量判断
************************************/
void ischar(char *a){
FILE *fp6;
int k;
k=strlen(a);
if(((a[1]>='A')&&(a[1]<='Z'))||((a[1]>='a')&&(a[1]<='z'))||(a[1]=='_')){
fp6=fopen("cl.txt","a");
if((a[0]=='\'')&&(a[2]=='\'')){ /*是字符常量*/
count2++;
fprintf(fp6,"%s\t\t\t4\t2\t\t%d\n",a,count2);
fprintf(fp2,"4\t\t\t2\t%d\t\t%s\n",count2,a);
}
else
if((a[0]=='\"')&&(a[k-1]=='\"')){ /*是字符串常量*/
count3++;
fprintf(fp6,"%s\t\t\t4\t3\t\t%d\n",a,count3);
fprintf(fp2,"4\t\t\t3\t%d\t\t%s\n",count3,a);
}
fclose(fp6);
}
}
/******************************
分割单词
******************************/
void cut(){
int j;
while(!feof(fp1)){
do{
a[i++]=fgetc(fp1);
}while(a[i-1]!='\0'&&a[i-1]!='\n'&&a[i-1]!=' '&&a[i-1]!=EOF&&a[i-1]!='+'&&a[i-1]!='-'&&a[i-1]!='*'&&a[i-1]!='/'
&&a[i-1]!='%'&&a[i-1]!='='&&a[i-1]!='('&&a[i-1]!=')'&&a[i-1]!='{'&&a[i-1]!='}'&&a[i-1]!='>'&&a[i-1]!='<'&&
a[i-1]!='|'&&a[i-1]!='&'&&a[i-1]!=';'&&a[i-1]!=','&&a[i-1]!='['&&a[i-1]!=']');
if(a[i-1]=='\0'|| a[i-1]=='\n'||a[i-1]==' '|| a[i-1]==EOF){
a[i-1]=0;
j=iskey(a);
isoper(a);
issep(a);
if(j==0){
issyb(a);
}
j=0;
isdigit(a);
ischar(a);
}
if(a[i-1]=='+'||a[i-1]=='-'||a[i-1]=='*'||a[i-1]=='/'
||a[i-1]=='%'||a[i-1]=='='||a[i-1]=='('||a[i-1]==')'||a[i-1]=='{'||a[i-1]=='}'||a[i-1]=='>'||a[i-1]=='<'
||a[i-1]=='|'||a[i-1]=='&'||a[i-1]==';'||a[i-1]==','||a[i-1]=='['||a[i-1]==']') {
b[0]=a[i-1];
a[i-1]=0;
j=iskey(a);
isoper(a);
issep(a);
if(j==0){
issyb(a);}
j=0;
isdigit(a);
ischar(a);
if(b[0]=='='||b[0]=='+'||b[0]=='-'||b[0]=='>'||b[0]=='<'||b[1]=='|'||b[1]=='&'){
b[1]=fgetc(fp1);
if(b[1]=='='||b[1]=='+'||b[1]=='-'||b[1]=='<'||b[1]=='>'||b[1]=='|'||b[1]=='&'){
b[2]=0;
isoper(b);
}
else{
ungetc(b[1],fp1);
b[1]=0;
issep(b);
isoper(b);
}}
else{
b[1]=0;
issep(b);
isoper(b);
}
}
if(a[i]=='\0'||a[i]=='\n'||a[i]==' '||a[i-1]!=EOF||a[i-1]=='+'||a[i-1]=='-'||a[i-1]=='*'||a[i-1]=='/'
||a[i-1]=='%'||a[i-1]=='='||a[i-1]=='('||a[i-1]==')'||a[i-1]=='{'||a[i-1]=='}'||a[i-1]=='>'||a[i-1]=='<'
||a[i-1]=='|'||a[i-1]=='&'||a[i-1]==';'||a[i-1]==','||a[i-1]=='['||a[i-1]==']') /*有问题*/
i=0;
for(j=0;j<=i-1;j++)
a[j]=0;
}
}
/*各表初始化程序*/
void init()
{
FILE *fp;
int i;
char c;
fp=fopen("blz.txt","w"); /*初始化保留字表*/
for(i=1;i<=39;i++){
if(i<=9)
fprintf(fp,"%-20s 0 0%d\n",key[i],i);
else
fprintf(fp,"%-20s 0 %2d\n",key[i],i);
}
fclose(fp);
fp=fopen("fgf.txt","w"); /*初始化限制符表*/
for(i=1;i<=13;i++){
if(i<=9)
fprintf(fp,"%-20s 1 0%d\n",sep[i],i);
else
fprintf(fp,"%-20s 1 %2d\n",sep[i],i);
}
fclose(fp);
fp=fopen("ysf.txt","w"); /*初始化运算符表*/
for(i=1;i<=35;i++){
if(i<=9)
fprintf(fp,"%-20s 2 0%d\n",oper[i],i);
else
fprintf(fp,"%-20s 2 %2d\n",oper[i],i);
}
fclose(fp);
c='"';
fprintf(fp,"%c\n",c);
fclose(fp);
fp=fopen("bzf.txt","w"); /*初始化标识符表*/
fclose(fp);
fp=fopen("cl.txt","w"); /*初始化常数表*/
fclose(fp);
fp=fopen("output.txt","w"); /*初始化输出文件*/
fclose(fp);
}
void main()
{
init();
fp1=fopen("input.txt","r");
fp2=fopen("output.txt","w");
cut();
fclose(fp1);
fclose(fp2);
}