⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 一个语法词法分析器
💻 TXT
字号:
#include"string.h" 
#include"iostream.h" 
#include"stdio.h" 
#include"conio.h" 

#define OK 1 
#define ERROR 0 
#define FALSE 0 
#define MAX 200 
#define YES 1 
#define NO 0 
#define IF 2 
#define ELSE 3 
#define WHILE 4 
#define DO 5 
#define SWITCH 6 
#define INT 22 
#define CHAR 23 
#define FLOAT 24 
#define OTC 8 
#define DEC 10 
#define HEX 16 
#define ASSIGN 20 
#define SEMICOLON 21 
#define AND 25 
#define SUB 26 
#define MUL 27 
#define DIV 28 
#define GT 29 
#define LT 30 
#define LP 31 
#define RP 32 
#define EQ 33 
#define ID 34 
#define CONST 35 
#define THEN 36 

int lookhead; 

class word_analyse 
{ int ch; 
char code[MAX][MAX]; 
public: 
//word_analyse(); 
int scan(); 
int isletter(int ch); 
int isdigit(int ch); 
int isright(int ch); 
int iskeyword(char * ch); 
int isalpha(char * ch); 
int judge(char * ch); 
int yufa(int ch); 
int match(int ch); 
int fun_e(); 
int fun_t(); 
int fun_f(); 
int fun_s(); 
int fun_c(); 
}; 

int word_analyse::isletter(int ch) 
{ 
if(((ch>='a')&amt;&amt;(ch<='z'))||((ch>='A')&amt;&amt;(ch<='Z'))) 
return(YES); 
else return(NO); 
} 

int word_analyse::isalpha(char * ch) 
{ 
if(((ch[0]>='a')&amt;&amt;(ch[0]<='z'))||((ch[0]>='A')&amt;&amt;(ch[0]<='Z'))) 
{ cout<<"\n"<<"The ["<<ch<<"] is a identifier !"<<"\n"; 
return(YES); 
} 
else return(NO); 
} 

int word_analyse::isdigit(int ch) 
{ if(((ch>='0')&amt;&amt;(ch<='9'))) 
return(YES); 
else return(NO); 
} 

int word_analyse::isright(int ch) 
{ 
if(((ch>='0')&amt;&amt;(ch<='9'))||((ch>='a')&amt;&amt;(ch<='z')) 
||((ch>='A')&amt;&amt;(ch<='Z'))||(ch=='+')||(ch=='-') 
||(ch=='*')||(ch=='/')||(ch=='=')||(ch=='==') 
||(ch=='>')||(ch=='<')||(ch=='>')||(ch=='(')||(ch==')')||(ch==';')) 
return(YES); 
else return(NO); 
} 

int word_analyse::iskeyword(char * ch) 
{ 
if((ch[0]=='i')&amt;&amt;(ch[1]=='n')&amt;&amt;(ch[2]=='t')&amt;&amt;(ch[3]==0))return(INT); 
if((ch[0]=='c')&amt;&amt;(ch[1]=='h')&amt;&amt;(ch[2]=='a')&amt;&amt;(ch[3]=='r')&amt;&amt;(ch[4]==0))return(CHAR); 
if((ch[0]=='f')&amt;&amt;(ch[1]=='l')&amt;&amt;(ch[2]=='o')&amt;&amt;(ch[3]=='a')&amt;&amt;(ch[4]=='t')&amt;&amt;(ch[5]==0))return(FLOAT); 
if((ch[0]=='i')&amt;&amt;(ch[1]=='f')&amt;&amt;(ch[2]==0))return(IF); 
if((ch[0]=='t')&amt;&amt;(ch[1]=='h')&amt;&amt;(ch[2]=='e')&amt;&amt;(ch[3]=='n')&amt;&amt;(ch[4]==0))return(THEN); 
if((ch[0]=='e')&amt;&amt;(ch[1]=='l')&amt;&amt;(ch[2]=='s')&amt;&amt;(ch[3]=='e')&amt;&amt;(ch[4]==0))return(ELSE); 
if((ch[0]=='w')&amt;&amt;(ch[1]=='h')&amt;&amt;(ch[2]=='i')&amt;&amt;(ch[3]=='l')&amt;&amt;(ch[4]=='e')&amt;&amt;(ch[5]==0))return(WHILE); 
if((ch[0]=='d')&amt;&amt;(ch[1]=='o')&amt;&amt;(ch[2]==0))return(DO); 
if((ch[0]=='s')&amt;&amt;(ch[1]=='w')&amt;&amt;(ch[2]=='i')&amt;&amt;(ch[3]=='t')&amt;&amt;(ch[4]=='c')&amt;&amt;(ch[5]=='h')&amt;&amt;(ch==0))return(SWITCH); 
return(ID); 
} 

int word_analyse::judge(char * ch) 
{ 
if((ch[0]=='0')&amt;&amt;((ch[1]>='0')&amt;&amt;(ch[1]<='7'))) 
{ 
cout<<"\n"<<"This is a OTC number, OTC value is"<<"["<<ch<<"] !"<<"\n"; 
return(OTC); 
} 
if((ch[0]=='0')&amt;&amt;(ch[1]=='\0')) 
{ cout<<"\n"<<"This is a DEC number, DEC value is"<<"[0] !"<<"\n"; 
return(DEC); 
} 
if((ch[0]>='0')&amt;&amt;(ch[0]<='9')&amt;&amt;(ch[0]!='0')) 
{ cout<<"\n"<<"This is a DEC number, DEC value is"<<"["<<ch<<"] !"<<"\n"; 
return(DEC); 
} 
if((ch[0]=='0')&amt;&amt;(ch[1]=='x')) 
{ cout<<"\n"<<"This is a HEX number, HEX value is"<<"["<<ch<<"] !"<<"\n"; 
return(HEX); 
} 
return(OK); 
} 

int word_analyse::scan() 
{ int i=0; 
int j=0; 
int x=1; 
ch=getc(stdin); 
while((ch==' ')||(ch==10))ch=getc(stdin); 
if((isright(ch)==NO)){ cout<<"\n"<<"ERROR"<<"\n"; return(ERROR); } 
ungetc(ch,stdin); 
while(x) 
{ ch=getc(stdin); 
if((isletter(ch)==YES)) 
{ i=0; 
while (isletter(ch)||isdigit(ch)) 
{ 
code[j][i]=ch; 
ch=getc(stdin); 
i++; 
} 
code[j][i]=0; 
ungetc(ch,stdin); 
if(ch==10)x=0; 
j++; 
return(iskeyword(code[j-1])); 
} 
if((isdigit(ch)==YES)) 
{ i=0; 
while(isdigit(ch)||(ch=='x')||((ch>='a')&amt;&amt;(ch<='f'))) 
{ 
code[j][i]=ch; 
ch=getc(stdin); 
i++; 
} 
code[j][i]=0; 
ungetc(ch,stdin); 
j++; 
if(ch==10)x=0; 
return(judge(code[j-1])); 
} 
if(ch==':') 
{ i=0; 
code[j][i]=ch; 
ch=getc(stdin); 
if(ch=='='){ code[j][i++]=ch; code[j][i++]=0; j++; return(ASSIGN);} 
} 
switch(ch) 
{ 
case'+':{ i=0; code[j][i]=ch; code[j][i++]=0; return(AND); } 
case'-':{ i=0; code[j][i]=ch; code[j][i++]=0; return(SUB); } 
case'*':{ i=0; code[j][i]=ch; code[j][i++]=0; return(MUL); } 
case'/':{ i=0; code[j][i]=ch; code[j][i++]=0; return(DIV); } 
case'=':{ i=0; code[j][i]=ch; code[j][i++]=0; return(EQ); } 
case'>':{ i=0; code[j][i]=ch; code[j][i++]=0; return(GT); } 
case'<':{ i=0; code[j][i]=ch; code[j][i++]=0; return(LT); } 
case'(':{ i=0; code[j][i]=ch; code[j][i++]=0; return(LP); } 
case')':{ i=0; code[j][i]=ch; code[j][i++]=0; return(RP); } 
case';':{ i=0; code[j][i]=ch; code[j][i++]=0; return(SEMICOLON); } 
} 
if(ch==10)x=0; 
} 
return(OK); 
} 

int word_analyse::match(int ch) 
{ 
if(lookhead==ch) 
{ 
lookhead=scan(); 
return(OK); 
} 
else return(FALSE); 
} 

int word_analyse::fun_e() 
{ 
fun_t(); 
if(lookhead==AND) 
{ match(AND); 
fun_t(); 
cout<<"\t"<<"(E->E+T)"<<"\n"; 
return(OK); 
} 
if(lookhead==SUB) 
{ 
match(SUB); 
fun_t(); 
cout<<"\t"<<"(E->E-T)"<<"\n"; 
return(OK); 
} 
if((lookhead!=AND)&amt;&amt;(lookhead!=SUB)){ cout<<"\t"<<"(E->T)"<<"\n"; return(OK); } 
//else { cout<<"ERROR"<<"\n"; return(ERROR); } 
return(OK); 
} 

int word_analyse::fun_t() 
{ 
fun_f(); 
if(lookhead==MUL) 
{ match(MUL); 
fun_f(); 
cout<<"\t"<<"(T->T*F)"<<"\n"; 
return(OK); 
} 
if(lookhead==DIV) 
{ match(DIV); 
fun_f(); 
cout<<"\t"<<"(T->T/F)"<<"\n"; 
return(OK); 
} 
if((lookhead!=DIV)&amt;&amt;(lookhead!=MUL)){ cout<<"\t"<<"(T->F)"<<"\n"; return(OK); } 
//else { cout<<"ERROR"<<"\n"; return(ERROR); } 
return(OK); 
} 

int word_analyse::fun_f() 
{ 
if(lookhead==LP) 
{ 
match(LP); 
fun_e(); 
match(RP); 
cout<<"\t"<<"(F->(E))"<<"\n"; 
return(OK); 
} 
if(lookhead==ID){ match(ID); cout<<"\t"<<"(F->id)"<<"\n"; return(OK); } 
if(lookhead==CONST){ match(CONST); cout<<"\t"<<"(F->Const)"<<"\n"; return(OK); } 
else { cout<<"ERROR"<<"\n"; return(ERROR); } 
} 

int word_analyse::fun_s() 
{ 
if(lookhead==IF) 
{ 
match(IF); 
fun_c(); 
match(THEN); 
fun_s(); 
cout<<"\t"<<"(S->if C then S)"<<"\n"; 
return(OK); 
} 
if(lookhead==ID) 
{ 
match(ID); 
match(EQ); 
fun_e(); 
cout<<"\t"<<"(S->id=E)"<<"\n"; 
return(OK); 
} 
if(lookhead==WHILE) 
{ 
match(WHILE); 
fun_c(); 
match(DO); 
fun_s(); 
cout<<"\t"<<"(S->while C do S)"<<"\n"; 
return(OK); 
} 
else { cout<<"ERROR"<<"\n"; return(ERROR); } 
} 

int word_analyse::fun_c() 
{ 
int c=0,d=0,i1=0,i2=0,i3=0; 
fun_e(); 
if((lookhead==GT)||(lookhead==LT)||(lookhead==EQ)) 
{ 
d=lookhead; 
if(lookhead==GT)i1=match(GT); 
if(lookhead==LT)i2=match(LT); 
if(lookhead==EQ)i3=match(EQ); 
c=fun_e(); 
if((i1==0)&amt;&amt;(i2==0)&amt;&amt;(i3==0)||(c==0)){ cout<<"ERROR"<<"\n"; return(ERROR); } 
if(d==GT){ cout<<"\t"<<"(C->E>E)"<<"\n"; return(OK); } 
if(d==LT){ cout<<"\t"<<"(C->E<E)"<<"\n"; return(OK); } 
if(d==EQ){ cout<<"\t"<<"(C->E=E)"<<"\n"; return(OK); } 
} 
return(OK); 
} 

int main() 
{ 
char ch1; 
word_analyse word; 
while(ch1!='q') 
{ 
clrscr(); 
cout<<"Please Input !"<<"\n"; 
lookhead=word.scan(); 
word.fun_s(); 
ch1=getch(); 
} 
return(OK); 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -