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

📄 词法.txt

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

#define OK 1 
#define ERROR 0 
#define TRUSE 1 
#define FALSE 0 
#define MAX 200 
#define MAX2 
#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 KEY 20 
#define ALPHA 21 

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 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==')')) 
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)) 
{ cout<<"\n"<<"The [int] is a keyword !"<<"\n"; 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)) 
{ cout<<"\n"<<"The [char] is a keyword !"<<"\n"; 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)) 
{ cout<<"\n"<<"The [float] is a keyword !"<<"\n"; return(FLOAT); } 
if((ch[0]=='i')&amt;&amt;(ch[1]=='f')&amt;&amt;(ch[2]==0)) 
{ cout<<"\n"<<"The [if] is a keyword !"<<"\n"; return(IF); } 
if((ch[0]=='e')&amt;&amt;(ch[1]=='l')&amt;&amt;(ch[2]=='s')&amt;&amt;(ch[3]=='e')&amt;&amt;(ch[4]==0)) 
{ cout<<"\n"<<"The [else] is a keyword !"<<"\n"; 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)) 
{ cout<<"\n"<<"The [while] is a keyword !"<<"\n"; return(WHILE); } 
if((ch[0]=='d')&amt;&amt;(ch[1]=='o')&amt;&amt;(ch[2]==0)) 
{ cout<<"\n"<<"The [do] is a keyword !"<<"\n"; 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)) 
{ cout<<"\n"<<"It is a keyword [if] !"<<"\n"; return(SWITCH); } 
return(FALSE); 
} 

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; 
clrscr(); 
cout<<"Please input !"<<"\n"; 
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(iskeyword(code[j])==0) 
{ 
if(isalpha(code[j]))cout<<"The identifier's ID is ["<<j<<"] !"<<"\n"; 
} 
j++; 
if(ch==10)x=0; 
} 
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); 
judge(code[j]); 
cout<<"The number's ID is ["<<j<<"] !"<<"\n"; 
j++; 
if(ch==10)x=0; 
} 
if(ch==':') 
{ i=0; 
code[j][i]=ch; 
ch=getc(stdin); 
if(ch=='='){ code[j][i++]=ch; cout<<"The ["<<"code[j]"<<" is (ASSIGN-OP, -) ."; j++; ch=getc(stdin); if(ch==10)x=0; } 
} 
switch(ch) 
{ 
case'+':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (PLUS-OP, AND) ."<<"\n"; } //ch=getc(stdin); if(ch==59)ungetc(ch,stdin); } 
break; 
case'-':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (MINUS-OP, SUB) ."<<"\n"; } //ch=getc(stdin);if(ch==59)ungetc(ch,stdin); } 
break; 
case'*':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, MUL) ."<<"\n"; }// ch=getc(stdin); if(ch==59)ungetc(ch,stdin);} 
break; 
case'/':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, DIV) ."<<"\n";} // ch=getc(stdin);if(ch==59)ungetc(ch,stdin); } 
break; 
case'=':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, EQ) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin); } 
break; 
case'>':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, GT) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin); } 
break; 
case'<':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, LT) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin);} 
break; 
case'(':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, LP) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin); } 
break; 
case')':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, RP) ."<<"\n";}// ch=getc(stdin); if(ch==59)ungetc(ch,stdin); } 
break; 
case';':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (SEMICOLON, -) ."<<"\n"; }//ch=getc(stdin); } 
break; 
} 
if(ch==10)x=0; 
} 
return(OK); 
} 

int main() 
{ 
char ch1; 
word_analyse word; 
while(ch1!='q') 
{ 
word.scan(); 
ch1=getch(); 
} 
return(OK); 
} 






⌨️ 快捷键说明

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