一个翻译源程序的代码,往下不知道该怎么做了?
编一个程序,要求:a:本程序从一个磁盘文件上读入一个c语言源程序,将其翻译成一个单词内部码表示的C语言程序。
b:把C单词分成如下几类:
* 保留字(例如:for,if,int,struct等);
*分隔符(例如:( # ;等);
*运算符
*标字符(例如:color,main等);
*常量(包括整数类型常量,浮点类型常量,字符类型常量,字符串类型常量)
c:定义C语言中的所有保留字,分隔符,运算符编码,造出编码表,编出C语言翻译程序。
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void printf_0();
void printf_1();
void printf_2();
void printf_3();
void printf_4();
void search_0(char a[]);
void search_1();
void search_2();
void search_3();
void search_4();
typedef struct cell{
char ch;
struct cell *next;
}celltype;
typedef celltype *ptype;
ptype top;
void main(){
printf_0();.................打印保留字表
printf_1();.................打印分隔符表
printf_2();.................打印运算符号表
printf_3();................打印标识符表,还没想出来。
printf_4();....................打印数字表,包括整数和浮点数
FILE *fp;
char p1[20],p2[20],ch;
int i;
i=0;
if((fp=fopen("e:\1.txt","r"))==NULL){....................打开文件
printf("can not open file!");
exit(0);
}
while((ch=fgetc(fp))!=EOF){
while(ch==' '||ch=='\n'){
printf("%c",ch);
ch=fgetc(fp);
}
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){
p1[i]=ch;
ch=fgetc(fp);
while(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch>=0&&ch<=9||ch=='_'){.........检查保留字
p1[i++]=ch;
ch=fgetc(fp);
}
search_0(p1);.................检索保留字表
}
if(ch>=0&&ch<=9){
p1[i]=ch;
ch=fgetc(fp);
while(ch>=0&&ch<=9||ch=='.'){
p1[i++]=ch;
ch=fgetc(fp);
}
search_1(p1);..........数字这块不知道怎么弄
}
if(ch=='''){
p1[i]=ch;
ch=fgetc(fp);
while(ch!='''){
p1[i++]=ch;
ch=fgetc(fp);.............字符常量也不知道怎么弄
}
search_3(p1);
}
if(ch=='"'){
p1[i]=ch;
ch=fgetc(fp);
while(ch!='"'){
p1[i++]=ch;
ch=fgetc(fp);
}
search_4(p1);..............字符串常量还没想出来
}
}
void printf_0(){
char p[36][13]={"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","singed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
int i;
for(i=0;i<36;i++){
printf("%-20s 0%d",p[i],i);
printf("\n");
}
}
void printf_1(){
char a[]={'#','(',')','\'',';','\"','\,','{','}','[',']','\\',':'};
for(int i=0;i<13;i++){
printf("1 ");
printf("%2d %c",i,a[i]);
printf("\n");
}
}
void printf_2(){
char * p[14]={"+","-","&","==","&&","||","!","*","/","%","!=","<<",">>",","};
int i;
for(i=0;i<14;i++){
printf("%-20c 2 %d ",p[i],i);
printf("\n");
}
}
void printf_4(){
int j;
float k;
for(j=0;j++;j<10){
printf("%d 4 0 %d",j,j)
}
for(j=0,k=1.0;j++,k++;j<10,k<10.0){
printf("%f 4 1 %d",k,j);
}
}
void search_0(char a[]){
char p[36][13]={"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","singed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
int i;
for(i=0;i<36;i++){
if((strcmp(p[i][13],a[]))==0)
printf("0 %d",i);
}
}
void search_1(char a[]){
int i;
i=0;
while(a[i]!='\0'){
if(
哪位高手有更简单的代码,发出来借鉴一下吧