📄 词法分析.cpp
字号:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define BEGIN 1
#define END 2
#define IF 3
#define WHILE 4
#define THEN 5
#define ELSE 6
#define ID 7
#define INT 8
#define LT 9
#define LE 10
#define EQ 11
#define NE 12
#define GT 13
#define GE 14
char TOKEN[20];
extern int lookup(char *);
extern void out (int,char*);
extern report_error(void);
void scanner_example(FILE*fp)
{
char ch;
int i,c;
ch=fgetc(fp);
if(isalpha(ch))/*it must be a identifier!*/
{
TOKEN[0]=ch;
ch=fgetc(fp);i=1;
while(isalnum(ch))
{
TOKEN[i]=ch;i++;
ch=fgetc(fp);
}
TOKEN[i]='\n';
fseek(fp,-1,1);/* retract*/
c=iookup(TOKEN);
if(c==0)out(ID,TOKEN);
else out(c,"");
}
else
if(isdigit(ch))
{
TOKEN[0]=ch;
ch=fgetc(fp);i=1;
while(isdigit(ch))
{
TOKEN[i]=ch;i++;
ch=fgetc(fp);
}
TOKEN[i]='\0';
fseek(fp,-1,-1);
out(INT,TOKEN);
}
else
switch(ch)
{
case'<':ch=fgetc(fp);
if(ch=='=')out(LE,"");
else if(ch=='>')out(NE,"");
else
{
fseek(fp,-1,1);
out(LT,"");
}
break;
case'=':out(EQ,"");break;
case'>':ch=fgetc(fp);
if(ch=='=')out(GE,"");
else
{
fseek(fp,-1,1);
out(GT,"");
}
break;
default:report_error();
break;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -