📄 fxq.c
字号:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <alloc.h>
#include <conio.h>
int IsLetter(char ch)
{/*判断是否是字母,是则返回 1,否则返回 0 */
if(isalpha(ch)) return 1;
return 0;
}
int IsDigit(char ch)
{/*判断是否为数字,是则返回 1,否则返回 0 */
if(isalnum(ch)) return 1;
return 0;
}
int IsSpace(char ch)
{/*判断是否为空白符(空格、换行、制表符等),是则返回 1,否则返回 0*/
if(isspace(ch)) return 1;
return 0;
}
void GetChar(FILE *fp,char *ch)
{/*读取字符送ch*/
*ch = fgetc(fp);
}
void GetBC(FILE *fp,char *ch)
{/*如果是空白则继续读下一个,直到不是空白*/
do {
GetChar(fp,ch);
}while(IsSpace(*ch)&&(*ch != EOF));
}
void Retract(FILE *fp,char *ch)
{/*光标回退一位,并使 ch 为空*/
fseek(fp,-1,1);
*ch = '';
}
char Reserve(char *strToken)
{/*返回关键字或字符的编码*/
int i;
if(strcmp(strToken,"main") == 0) return '2';
if(strcmp(strToken,"for") == 0) return '6';
if(strcmp(strToken,"char") == 0) return '3';
if(strcmp(strToken,"if") == 0) return '4';
if(strcmp(strToken,"else") == 0) return '5';
if(strcmp(strToken,"while") == 0) return '7';
if(strcmp(strToken,"NUM") == 0) return '20';
if(strcmp(strToken,"a") == 0) return '8';
if(strcmp(strToken,"b") == 0) return '9';
if(strcmp(strToken,"int") == 0) return '1';
return '0';
}
void Concat(char *strToken, char *ch)
{/*将ch中的字符连接到strToken后面*/
int i;
for(i=0;i<80;i++) {
if(*strToken == NULL) {
*strToken = *ch;
break;
}
strToken++;
}
}
int scance(FILE *fp1,FILE *fp2)
{/*词法分析子程序*/
char ch,code;
int i;
char strToken[80];
while(1) {
GetBC(fp1,&ch);
for(i=0;i<80;i++) strToken[i]=NULL;
if(ch == EOF) return 0;
if (IsLetter(ch)) {
while (IsLetter(ch) || IsDigit(ch)) {
Concat(strToken,&ch);
GetChar(fp1,&ch);
}
Retract(fp1,&ch);
code = Reserve(strToken);
if (code == '0') {
printf("<$ID,%s>\n",strToken);
fputs("<$ID,",fp2);
fputs(strToken,fp2);
fputs(">\n",fp2);
}
else {
printf("<%c,->\n",code);
fputs("<",fp2);
fputc(code,fp2);
fputs(",",fp2);
fputs("“",fp2);
fputs(strToken,fp2);
fputs("”",fp2);
fputs("->\n",fp2);
}
}
else if (IsDigit(ch)) {
while (IsDigit(ch)) {
Concat(strToken,&ch);
GetChar(fp1,&ch);
}
printf("<$INT,%s>\n",strToken);
fputs("<$INT,",fp2);
fputs(strToken,fp2);
fputs(">\n",fp2);
}
else if (ch == '=') {
printf("<$ASSIGN,->\n");
fputs("<21,“=”->\n",fp2);
}
else if (ch == '+') {
printf("<$PLUS,->\n");
fputs("<22,“+”->\n",fp2);
}
else if (ch == '*') {
GetChar(fp1,&ch);
if (ch == '*') {
printf("<$POWER,->\n");
fputs("<$POWER,->\n",fp2);
}
else {
Retract(fp1,&ch);
printf("<$STAR,->\n");
fputs("<$STAR,->\n",fp2);
}
}
else if (ch == ';') {
printf("<$SEMICOLON,->\n");
fputs("<34,“;”->\n",fp2);
}
else if (ch == '(') {
printf("<%s,->\n",code);
fputs("<26,“(”->\n",fp2);
}
else if (ch == ')') {
printf("<27,->\n");
fputs("<27,“)”->\n",fp2);
}
else if (ch == '{') {
printf("<30,->\n");
fputs("<30,“{”->\n",fp2);
}
else if (ch == '}') {
printf("<31,->\n");
fputs("<31,“}”->\n",fp2);
}
/*else if (ch >=0&&ch<=100) {
printf("<$PLUS,->\n");
fputs("<35,“%d”->\n",ch);
} */
}
}
void main(int argc, char *argv[])
{/*主程序*/
FILE *fp1,*fp2;
/*if(argc==1)
{
printf("have not enter file name. strike any key exit");
getch(); exit(0);
} */
if((fp1=fopen("cx.txt","rt"))==NULL)
{
printf("Cannot open %s\n",argv[1]);
getch(); exit(1);
}
if((fp2=fopen("outfile.txt","wt+"))==NULL)
{
printf("Cannot create out.txt FILE.strike any key exit");
getch(); exit(1);
}
scance(fp1,fp2);
fclose(fp1);
fclose(fp2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -