📄 词法-语法(项目).cpp
字号:
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define max 600
#define ID 1
#define INT 2
#define ADD 10
#define SUB 11
#define MUL 12
#define DEV 13
#define LT 14
#define LE 15
#define GT 16
#define GE 17
#define EQ 18
#define NE 19
#define FZ 20
#define YK 21
#define ZK 22
#define FG 23
#define FY 24
#define YD 25
#define ZD 26
struct KeyWord //关键字结构
{
char *word;
int id;
};
KeyWord keyword[]={ //关键字数组
{"begin",3},
{"end",4},
{"if",5},
{"then",6},
{"else",7},
{"while",8},
{"do",9},
};
struct Recode
{
int xl;
char name[max];
};
Recode recode[max];
struct Token
{
int id;
char name[max];
int line;
};
Token token[max];
char jieshou[max];
char ErrorName[max];
int errorline=1; //记录错误所在的位置
int biaoshino=0 ; // 标识符总数
int n=0;
int flag=0;
int count=0;
int lookuphead;
int ErrorLine=1;
int lookup(char *string);
int jianlibs(char *string);
int errornum=0; //错误的总数
void out(int id ,int xl,char *string);
void reporterror(char ERRORCH);
void print(int id,char *string);
bool isalpha(char c) ;
bool isdigit(char c);
bool isalnum(char c);
void example(FILE *fp);
void Advance();
void yujubiao();
void yuju();
void fuzhi();
void tiaojian();
void fuhe();
void whileyuju();
void guanxi();
void suanshu();
void xiang();
void yinshi();
void bianliang();
void changshu();
void guanxifu();
void Zfun();
void Dfun();
void XXfun();
void Ksuanshu();
void Cbianliang();
int lookup(char *string)
{
for(int i=0;i<sizeof(keyword)/sizeof(KeyWord);i++)
{
if(strcmp(string,keyword[i].word)==0)
return keyword[i].id;
}
return 0;
}
void out(int id ,int xl,char *string)
{
printf("(%d,%d)\t%s\n",id,xl,string);
}
void print(int id ,char *string)
{
printf("(%d,%s)\t%s\n",id,string,string);
}
void reporterror(char ERRORCH) //错误处理程序
{
errornum++;
printf("非法字符 %c int %d line!\n",ERRORCH,errorline);
}
int jianlibs(char string[80]) //检查扫描到的标识符是否已加入到标识符表中
{
int i;
for(i=0;i<biaoshino;i++)
{
if(strcmp(recode[i].name,string)==0)
return recode[i].xl;
}
strcpy(recode[i].name,string);
recode[i].xl=biaoshino+1;
biaoshino++;
return recode[i].xl;
}
bool isalpha(char c) //判断是否为字母
{
if( (c>='a'&&c<='z') || (c>='A'&&c<='Z') )
return true;
else
return false;
}
bool isdigit(char c) //判断是否为数字
{
if(c>='0'&&c<='9')
return true;
else
return false;
}
bool isalnum(char c) //判断是否是字母或数字
{
if( isalpha(c) || isdigit(c) )
return true;
else
return false;
}
void example(FILE *fp)
{
char ch;
int i,c,p,u,k;
while(!feof(fp))
{
ch=fgetc(fp);
if(isalpha(ch))
{
jieshou[0]=ch;
ch=fgetc(fp);
i=1;
while(isalnum(ch))
{
jieshou[i]=ch;
i++;
ch=fgetc(fp);
}
jieshou[i]='\0';
fseek(fp,-1,1);
c=lookup(jieshou); //判断是是否是关键字
if(c==0)
{
p=jianlibs(jieshou);
for(u=0;u<3;u++)
{
token[n].id=ID;
strcpy(token[n].name,jieshou);
token[n].line=errorline;
}
n++;
out (ID,p,jieshou);
} //为标识符时输出
else
{
for(u=0;u<3;u++)
{
token[n].id=c;
strcpy(token[n].name,jieshou);
token[n].line=errorline;
}
n++;
out (c,0,jieshou); // 为关键字是输出
}
}
else if(isdigit(ch)) //判断是否是整型数据
{
jieshou[0]=ch;
ch=fgetc(fp);
i=1;
while(isalnum(ch))
{
if(isalpha(ch))
{
k=1;
jieshou[i]=ch;
i++;
ch=fgetc(fp);
}
else if(isdigit(ch))
{
jieshou[i]=ch;
i++;
ch=fgetc(fp);
}
}
if(k==1)
{
reporterror(ch);
//return;
}
jieshou[i]='\0';
fseek(fp,-1,1);
for(u=0;u<3;u++)
{
token[n].id=INT;
strcpy(token[n].name,jieshou);
token[n].line=errorline;
}
n++;
print(INT,jieshou);
}
else
switch(ch)
{
case '(':
for(u=0;u<3;u++)
{
token[n].id=LE;
strcpy(token[n].name,"(");
token[n].line=errorline;
}
n++;
out(YK,0,"(");
break;
case ')':
for(u=0;u<3;u++)
{
token[n].id=LE;
strcpy(token[n].name,")");
token[n].line=errorline;
}
n++;
out(ZK,0,")");
break;
case '{':
for(u=0;u<3;u++)
{
token[n].id=LE;
strcpy(token[n].name,"{");
token[n].line=errorline;
}
n++;
out(YD,0,"{");
break;
case '}':
for(u=0;u<3;u++)
{
token[n].id=LE;
strcpy(token[n].name,"}");
token[n].line=errorline;
}
n++;
out(ZD,0,"}");
break;
case '<': ch=fgetc(fp);
if(ch=='=')
{
for(u=0;u<3;u++)
{
token[n].id=LE;
strcpy(token[n].name,"<=");
token[n].line=errorline;
}
n++;
out(LE,0,"<=");
}
else if(ch=='>')
{
for(u=0;u<3;u++)
{
token[n].id=NE;
strcpy(token[n].name,"<>");
token[n].line=errorline;
}
n++;
out(NE,0,"<>");
}
else
{
fseek(fp,-1,1);
for(u=0;u<3;u++)
{
token[n].id=LT;
strcpy(token[n].name,"<");
token[n].line=errorline;
}
n++;
out(LT,0,"<");
}
break;
case '=': out(EQ,0,"=");
for(u=0;u<3;u++)
{
token[n].id=EQ;
strcpy(token[n].name,"=");
token[n].line=errorline;
}
n++;
break;
case '>': ch=fgetc(fp);
if(ch=='=')
{
for(u=0;u<3;u++)
{
token[n].id=GE;
strcpy(token[n].name,">=");
token[n].line=errorline;
}
n++;
out(GE,0,">=");
}
else
{
fseek(fp,-1,1);
for(u=0;u<3;u++)
{
token[n].id=GT;
strcpy(token[n].name,">");
token[n].line=errorline;
}
n++;
out(GT,0,">");
}
break;
case '#':
jieshou[0]=ch;
ch=fgetc(fp);
i=1;
while(isalnum(ch))
{
jieshou[i]=ch;
i++;
ch=fgetc(fp);
}
jieshou[i]='\0';
fseek(fp,-1,1);
ch=fgetc(fp);
if(strcmp(jieshou,"#include")==0&&(ch=='<'))
{
jieshou[i]='<';
ch=fgetc(fp);
i=i+1;
while(isalnum(ch)||ch=='.')
{
jieshou[i]=ch;
i++;
ch=fgetc(fp);
}
if(ch=='>')
{
jieshou[i]=ch;
fseek(fp,-1,1);
for(u=0;u<3;u++)
{
token[n].id=FZ;
strcpy(token[n].name,jieshou);
token[n].line=errorline;
}
n++;
out(FY,0,jieshou);
}
else reporterror(ch);
}
else reporterror(ch);
jieshou[i]='\0';
break;
case ':':
ch=fgetc(fp);
if(ch=='=')
{ // := 为赋值语句
for(u=0;u<3;u++)
{
token[n].id=FZ;
strcpy(token[n].name,":=");
token[n].line=errorline;
}
n++;
out(FZ,0,":=");
}
else
{
fseek(fp,-1,1);
reporterror(ch);
}
break;
case '/':
ch=fgetc(fp); //删除程序中的注释
if(ch=='/')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -