作者:野孩子1991
来源:CSDN
原文:https://blog.csdn.net/u010668907/article/details/39141925
版权声明:本文为博主原创文章,转载请附上博文链接!
如下是我对野孩子代码的修正:
预处理代码:
#include<stdio.h>
#include<String.h>
int main()
{
FILE *p;
int i=0,j=0;//
char str[1000],str1[1000],c;
if((p=fopen("e:\\test.txt","rt"))==NULL){
printf("无法打开");
return 0;
}
else{
while((c=getc(p))!=EOF){
str[i] = c;
i++;
}
fclose(p);
str[i] = '\0';
for(i=0;i<strlen(str);i++){
if(str[i]=='/'&&str[i+1]=='/'){
while(str[i++]!='\n'){}
i--;
}//单行注释
else if(str[i]=='/'&&str[i+1]=='*'){
while(!(str[i]=='*'&&str[i+1]=='/'))
{
i++;
}
i+=1;
}//多行注释
else if(str[i]==' '&&str[i+1]==' '){
while(str[i]==' '){i++;}
i--;
if(str1[j-1]!=' ')
str1[j++]=' ';
}//多个空格,去除空格
else if(str[i]=='\n') {
if(str1[j-1]!=' ')
str1[j++]=' ';}
//换行处理,
else if(str[i]==9){
while(str[i]==9){
i++;
}
if(str1[j-1]!=' ')
str1[j++]=' ';
i--;
}//tab键处理
else str1[j++] = str[i];//其他字符处理
}
str1[j] = '\0';
if((p = fopen("e:\\test1.txt","w"))==NULL){
printf("can not find it!");
return 0;
}
else{
if(fputs(str1,p)!=0){
printf("存储失败!");
}
else printf("存储成功!");
}
fclose(p);
}
return 0;
}
词法分析器的代码如下:
#include<stdio.h>
#include<String.h>
int main(){
FILE *p;
int falg = 0,len,i=0,j=0;//
char *rwtab[27]={"","main","if","then","while","do",
"static","int","double","struct","break","else","long",
"switch","case","typedef","char","return","const","float",
"short","continue","for","void","sizeof","default","do"};//26个
char str[1000],str1[1000],c;
int syn,num; int k;
char token[200];
if((p=fopen("e:\\test1.txt","rt"))==NULL){
printf("无法打开");
return 0;
}
//fgets(str,1000,p);
while((c=getc(p))!=EOF){
str[i++] = c;
}
fclose(p);
if((p=fopen("e:\\test2.txt","w"))==NULL){
printf("无法打开");
return 0;
}
str[i] = '\0';
//printf("%s\n",str);
for(i=0;str[i]!='\0';){
j = 0;
num = -1;
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
while((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||(str[i]>='0'&&str[i]<='9')||str[i]=='_'){
token[j++] = str[i++];
}
token[j] = '\0';
for( k=1;k<27;k++){
if(strcmp(rwtab[k],token)==0)
break;
}
if(k<25)
syn = k;
else if(k==25)
syn = 39;
else if(k==26)
syn = 40;
else syn = 25;
}//关键字和标示符处理
else if(str[i]>='0'&&str[i]<='9'){
num = 0;
while(str[i]>='0'&&str[i]<='9'){//当读到第一个为0~9的数字时接着读之后的数字
num = num*10+str[i]-'0';
token[j++] = str[i++];
}
if((str[i]>='a'&&str[i]<='z')||
(str[i]>='A'&&str[i]<='Z'))
{
num=-1;syn=-3;
while((str[i]>='a'&&str[i]<='z')||
(str[i]>='A'&&str[i]<='Z')||(str[i]>='0'&&str[i]<='9')){
token[j++] = str[i++];
}
}
else syn = 26;
}//数字在此处处理
else {
if(str[i]==':'&&str[i+1]=='='){
syn = 32;
token[j++] = str[i];
token[j++] = str[++i];
i++;
}
else if(str[i]=='<'&&str[i+1]=='>'){
syn = 34;
token[j++] = str[i];
token[j++] = str[++i];
i++;
}
else if(str[i]=='<'&&str[i+1]=='='){
syn = 35;
token[j++] = str[i];
token[j++] = str[++i];
i++;
}
else if(str[i]=='>'&&str[i+1]=='='){
syn = 36;
token[j++] = str[i];
token[j++] = str[++i];
i++;
}
else if(str[i]==' '){
i++;
syn = -2;
}//空格处理
/*
else if(str[i]!=' '){
syn = -1;
while(str[i++]!=' '){
token[j++] = str[i];
}
}//不明字符的处理
*/
else{
switch(str[i]){
case '+': syn = 27;break;
case '-': syn = 28;break;
case '*': syn = 29;break;
case '/': syn = 30;break;
case ':': syn = 31;break;
case '<': syn = 33;break;
case '>': syn = 36;break;
case ';': syn = 41;break;
case ')': syn = 43;break;
case '(': syn = 42;break;
case '#': syn = 0;break;
case '=':syn = 38;break;
default: syn = -1;break;
}
token[j++] = str[i++];
}
}
token[j] = '\0';
if(num!=-1){
printf("%d %d\n",num,syn);
}
else if(syn!=-1&&syn !=-2 &&syn!=-3){
printf("%s %d\n",token,syn);
}
else if(syn==-1){
printf("%s error\n",token);
}
else if(syn == -3){
printf("%s error ID\n",token);
}
}
fclose(p);
return 0;
}