📄 yufa.cpp
字号:
#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')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
return(YES);
else return(NO);
}
int word_analyse::isalpha(char * ch)
{
if(((ch[0]>='a')&&(ch[0]<='z'))||((ch[0]>='A')&&(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')&&(ch<='9')))
return(YES);
else return(NO);
}
int word_analyse::isright(int ch)
{
if(((ch>='0')&&(ch<='9'))||((ch>='a')&&(ch<='z'))
||((ch>='A')&&(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')&&(ch[1]=='n')&&(ch[2]=='t')&&(ch[3]==0))return(INT);
if((ch[0]=='c')&&(ch[1]=='h')&&(ch[2]=='a')&&(ch[3]=='r')&&(ch[4]==0))return(CHAR);
if((ch[0]=='f')&&(ch[1]=='l')&&(ch[2]=='o')&&(ch[3]=='a')&&(ch[4]=='t')&&(ch[5]==0))return(FLOAT);
if((ch[0]=='i')&&(ch[1]=='f')&&(ch[2]==0))return(IF);
if((ch[0]=='t')&&(ch[1]=='h')&&(ch[2]=='e')&&(ch[3]=='n')&&(ch[4]==0))return(THEN);
if((ch[0]=='e')&&(ch[1]=='l')&&(ch[2]=='s')&&(ch[3]=='e')&&(ch[4]==0))return(ELSE);
if((ch[0]=='w')&&(ch[1]=='h')&&(ch[2]=='i')&&(ch[3]=='l')&&(ch[4]=='e')&&(ch[5]==0))return(WHILE);
if((ch[0]=='d')&&(ch[1]=='o')&&(ch[2]==0))return(DO);
if((ch[0]=='s')&&(ch[1]=='w')&&(ch[2]=='i')&&(ch[3]=='t')&&(ch[4]=='c')&&(ch[5]=='h')&&(ch==0))return(SWITCH);
return(ID);
}
int word_analyse::judge(char * ch)
{
if((ch[0]=='0')&&((ch[1]>='0')&&(ch[1]<='7')))
{
cout<<"\n"<<"This is a OTC number, OTC value is"<<"["<<ch<<"] !"<<"\n";
return(OTC);
}
if((ch[0]=='0')&&(ch[1]=='\0'))
{ cout<<"\n"<<"This is a DEC number, DEC value is"<<"[0] !"<<"\n";
return(DEC);
}
if((ch[0]>='0')&&(ch[0]<='9')&&(ch[0]!='0'))
{ cout<<"\n"<<"This is a DEC number, DEC value is"<<"["<<ch<<"] !"<<"\n";
return(DEC);
}
if((ch[0]=='0')&&(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')&&(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)&&(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)&&(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)&&(i2==0)&&(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')
{
cout<<"Please Input !"<<"\n";
lookhead=word.scan();
word.fun_s();
ch1=getch();
}
return(OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -