📄 accidence.cpp
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<iostream.h>
#include<windows.h>
//FILE *tokenfout;
/*typedef struct token
{
char *name;
int code;
}token;
token mytoken[50];*/
typedef struct keyword
{
char *name;
int code;
}keyword;
keyword key[7]={{"if",3},{"then",4},{"else",5},{"while",6},{"do",7},{"begin",8},{"end",9}};
#define ID 1
#define INT 2
#define ADD 10
#define MIN 11
#define MUL 12
#define DIV 13
#define LT 14
#define LE 15
#define EQ 16
#define GE 17
#define GT 18
#define NE 19
#define EVA 20
#define LP 21
#define RP 22
#define SEM 23
FILE *in;
char token[50];
int error_count=0;
int k,b;
char ch1;
int row=0;
int queue=0;
void report_error(int a)
{error_count++;
switch(a)
{
case 1:cout<<"标识符长度超过6,不合法"<<" "<<"行"<<row<<","<<"列"<<queue<<endl;break;
case 2:cout<<"非法字符"<<" "<<"行"<<row<<","<<"列"<<queue<<endl;break;
case 3:cout<<"数值超过最大表示范围,溢出"<<" "<<"行"<<row<<","<<"列"<<queue<<endl;break;
case 4:cout<<"没有匹配的注释符 '*/' "<<" "<<"行"<<row<<","<<"列"<<queue<<endl;break;
case 5:cout<<"标识符以数字开头,不合法"<<" "<<"行"<<row<<","<<"列"<<queue<<endl;break;
}
}
void out(char *t,int s)
{
cout<<"("<<t<<" , "<<s<<")"<<" "<<"行"<<row<<","<<"列"<<queue<<endl;
}
int lookup(char *c)
{
int h=1;
for(int i=0;i<7;i++)
if((h=strcmp(c,key[i].name))==0) break;
if(h) return -1;
if(!h)
return i;
}
void scanner()
{char ch;
int i=0,c;
while((ch=fgetc(in))!=EOF)
{
if(isalpha(ch))
{
token[0]=ch;
ch=fgetc(in);i=1;
while(isalnum(ch))
{
token[i]=ch;i++;
ch=fgetc(in);
}
token[i]='\0';
fseek(in,-1,1);
c=lookup(token);
if(c==-1)
if((k=strlen(token))<7)
out(token,ID);
else report_error(1);
else out(key[c].name,key[c].code);
}
else
if(isdigit(ch))
{
token[0]=ch;
ch=fgetc(in);i=1;
while(isdigit(ch))
{
token[i]=ch;i++;
ch=fgetc(in);
}
if(isalpha(ch))
{
token[i]=ch;
i++;
ch=fgetc(in);
while(isalnum(ch))
{
token[i]=ch;
i++;
ch=fgetc(in);
}
token[i]='\0';
fseek(in,-1,1);
report_error(5);
}
else{
token[i]='\0';
fseek(in,-1,1);
b=atoi(token);
if((b<=65535)&&(b>=0))
out(token,INT);
else if(b>65535) report_error(3);
}
}
else
switch(ch)
{
case'<':ch=fgetc(in);
if(ch=='=')
out("<=",LE);
else if(ch=='>')
out("<>",NE);
else {fseek(in,-1,1);
out("<",LT);
}
break;
case'=':out("=",EQ);
break;
case'>':ch=fgetc(in);
if(ch=='=')
out(">=",GE);
else{fseek(in,-1,1);
out(">",GT);
}
break;
case'+':out("+",ADD);
break;
case'-':out("-",MIN);
break;
case'*':out("*",MUL);
break;
case'/': ch=fgetc(in);
if(ch=='*')
{ for(;;)
{ ch=fgetc(in);
if(ch==EOF)
{ report_error(4);
break;
}
if(ch=='*')
{ ch1=ch;
ch=fgetc(in);
if(ch=='/')
{ ch=fgetc(in);break;
}
}
}
}
else out("/",DIV);
break;
case':':ch=fgetc(in);
if(ch=='=')
out(":=",EVA);
break;
case'(':
out("(",LP);
break;
case')':
out(")",RP);
break;
case';':
out(";",SEM);
break;
case' ':queue++;break;
case'\t':break;
case'\n':row++;break;
default:report_error(2);
break;
}
}
return;
}
void main()
{
char filename[30];
cout<<"please input your filename"<<endl;
cin>>filename;
if((in=fopen(filename,"rt"))==NULL)
{
printf("cannot open the file %s.\n",filename);
exit(1);
}
scanner();
cout<<"**********************************************"<<endl;
cout<<endl;
cout<<"错误个数"<<" "<<error_count<<endl;endl;
//if((tokenfout=fopen("token.txt","wt+"))==NULL)
//{cout<<"无法打开文件 token.txt"<<endl;
//exit(1);
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -