📄 zq.cpp
字号:
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
FILE *fp;//源文件指针
char cbuffer;
char *keyword[4]={"IF","THEN","ELSE","GOTO"};//关键字
char *border[6]={",",";",":=",".","(",")"};//指针数组,用于指向符号表;
char *arithmetic[4]={"+","-","*","/"};//指针数组,指向算术运算符表;
char *relation[6]={"<","<=","=",">",">=","<>"};//指针数组,指向关系运算符表;
char *consts[20];//指针数组,指向常数表;
char *label[20];//指针数组,指向标识符表;
int constnum=0,labelnum=0;//整型变量,分别用于存放当前常数个数和标识符个数。
int search(char searchchar[],int wordtype)//查找单词种别函数
{
int i=0;
switch (wordtype) {
case 1:for (i=0;i<=7;i++)
{
if (strcmp(keyword[i],searchchar)==0)
return(i+1);
};
case 2:{for (i=0;i<=5;i++)
{
if (strcmp(border[i],searchchar)==0)
return(i+1);
};
return(0);
}
case 3:{for (i=0;i<=3;i++)
{
if (strcmp(arithmetic[i],searchchar)==0)
{
return(i+1);
};
};
return(0);
};
case 4:{for (i=0;i<=5;i++)
{
if (strcmp(relation[i],searchchar)==0)
{
return(i+1);
};
};
return(0);
};
case 5:{for (i=0;i<=constnum;i++)
{
if (strcmp(consts[i],searchchar)==0)
{
return(i+1);
};
}
consts[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(consts[i-1],searchchar);
constnum++;
return(i);
};
case 6:{for (i=0;i<=labelnum;i++)
{
if (strcmp(label[i],searchchar)==0)
{
return(i+1);
};
}
label[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(label[i-1],searchchar);
labelnum++;
return(i);
};
}
}
char alphaprocess(char buffer)
{
int atype;
int i=-1;
char alphatp[20];
while ((isalpha(buffer))||(isdigit(buffer)))
{
alphatp[++i]=buffer;//相当于送入临时数组alphatp[0];
buffer=fgetc(fp);//再读入一个字符至buffer;
};
alphatp[i+1]='\0';//重复上面两步,直到后一步判断为假;在alphatp末尾添加'\0';
if (atype=search(alphatp,1))//调用search()子函数,在关键字表中匹配alphatp,若匹配成功,则返回序号;
printf("%s (1,%d)\n",alphatp,atype-1);
else
{
atype=search(alphatp,6);//调用search,在标识符表中匹配alphatp,若匹配成功,则返回序号;
printf("%s (6,%d)\n",alphatp,atype-1);//在标识符表中添加alphatp,并返回序号;
};
return(buffer);
}
char digitprocess(char buffer)//数字处理函数;
{
int i=-1;
char digittp[20];
int dtype;
while ((isdigit(buffer)))
{
digittp[++i]=buffer;
buffer=fgetc(fp);
}
digittp[i+1]='\0';
dtype=search(digittp,5);
printf("%s (5,%d)\n",digittp,dtype-1);
return(buffer);
}
char otherprocess(char buffer)
{
int i=-1;
char othertp[20];
int otype,otypetp;
othertp[0]=buffer;
othertp[1]='\0';
if (otype=search(othertp,3))
{
printf("%s (3,%d)\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
};
if (otype=search(othertp,4))
{
buffer=fgetc(fp);
othertp[1]=buffer;
othertp[2]='\0';
if (otypetp=search(othertp,4))
{
printf("%s (4,%d)\n",othertp,otypetp-1);
goto out;
}
else
othertp[1]='\0';
printf("%s (4,%d)\n",othertp,otype-1);
goto out;
};
if (buffer==':')
{
buffer=fgetc(fp);
if (buffer=='=')
printf(":= (2,2)\n");
buffer=fgetc(fp);
goto out;
}
else
{
if (otype=search(othertp,2))
{
printf("%s (2,%d)\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
}
};
if ((buffer!='\n')&&(buffer!=' '))
printf("%c error,not a word\n",buffer);
buffer=fgetc(fp);
out: return(buffer);
}
void main()
{
int i;
for (i=0;i<=20;i++)
{
label[i]=NULL;
consts[i]=NULL;
};
if ((fp=fopen("source.txt","r"))==NULL)
printf("打不开 或者是错了 ");
else
{
cbuffer = fgetc(fp); //读入一个字符ch
while (cbuffer!=EOF)
{
if (isalpha(cbuffer))
cbuffer=alphaprocess(cbuffer);//关键字和标识符处理子函数;
else if (isdigit(cbuffer))
cbuffer=digitprocess(cbuffer);//数字处理
else cbuffer=otherprocess(cbuffer);//其它字符处理
};
printf("over\n");
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -