?? yanboyufa.cpp
字號:
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
char *nexttoken();
void S();
void C();
void E();
void T();
void F();
char lookahead[20];
int code=0;
int flag1=0;
char chart[20][20];
void match( char *t)
{ // cout<<lookahead<<"loodahead*******"<<endl;
// cout<<t<<"TTTTTTTTTTTTTTTT"<<endl;
if(strcmp(lookahead,t)==0)
{ // cout<<"+++++++++"<<endl;
strcpy(lookahead,nexttoken());
// cout<<lookahead<<endl;
}
else
{
printf("yu fa cuo wu\n");
exit(0);
}
}
char *nexttoken()
{ int flag=0;
int ch,i=0;
int isletter(int);
int isdigit(int);
int rightchar(char);
int key(char *a);
ch=getc(stdin);
while ((ch==' ')||(ch==10))//10 is const
ch=getc(stdin);
if(ch==-1) return(0);
if ((!rightchar(ch))&&(ch!=-1))
{
cout<<"\nerror %c isn't in my language"<<ch;
// return(-1);
}
if (isdigit(ch)&&flag==0)
{
while(isdigit(ch))
{
chart[code][i]=ch;
ch=getc(stdin);
i=i+1;
}
chart[code][i+1]='\0';
ungetc(ch,stdin);
code=code+1;
flag=1;
i=0;
//return(10);
}
if(isletter(ch)&&flag==0)
{
while (isletter(ch)||isdigit(ch))
{
chart[code][i]=ch;
ch=getc(stdin);
i=i+1;
}
chart[code][i]='\0';
ungetc(ch,stdin);
code=code+1;
i=0;
flag=1;
//return(key(chart[code]));
}
/*if(ch==':')
{
chart[code][i]=ch;
code=code+1;
ch=getc(stdin);
/*if(ch=='=')
return (2);
else
return(0);
}*/
if(ch=='+'&&flag==0)
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch=='-'&&flag==0) //return (4);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch=='*'&&flag==0) //return (5);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch=='/'&&flag==0) //return (6);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch=='('&&flag==0) //return (21);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch==')'&&flag==0) //return (22);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch=='>'&&flag==0) //return (23);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch=='<'&&flag==0) //return (24);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if(ch=='='&&flag==0) //return (25);
{
chart[code][i]=ch;
chart[code][i+1]='\0';
code=code+1;//return (3);
flag=1;
}
if( key(chart[code-1])==1)
return chart[code-1];
else return "id";
}
int isletter(int ch)
{
if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
return(1);
else return(0);
}
int isdigit(int ch)
{
if((ch>='0')&&(ch<='9'))
return(1);
else return(0);
}
int rightchar(char ch)
{ if (((ch>='0')&&(ch<='9'))
||((ch>='a')&&(ch<='z'))
||((ch>='A')&&(ch<='Z'))
||(ch==10)||(ch==':')||(ch=='=')
||(ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')
||(ch=='(')||(ch==')')||(ch=='>')||(ch=='<'))
return(1);
else return(0);
}
int key(char *a)
{
if( (a[0]=='=')||(a[0]=='+')||(a[0]=='*')||(a[0]=='/')||(a[0]=='-')||(a[0]=='>')
||((a[0]=='<')&&(a[1]=='=')&&(a[2]=='\0'))
||((a[0]=='i')&&(a[1]=='f')&&(a[2]=='\0'))
|| ((a[0]=='i')&&(a[1]=='d')&&(a[2]=='\0'))
||((a[0]=='t')&&(a[1]=='h')&&(a[2]=='e')&&(a[3]=='n')&&(a[4]=='\0'))
|| ((a[0]=='w')&&(a[1]=='h')&&(a[2]=='i')&&(a[3]=='l')&&(a[4]=='e')&&(a[5]=='\0'))
||((a[0]=='d')&&(a[1]=='o')&&(a[2]=='\0'))
||((a[0]=='e')&&(a[1]=='l')&&(a[2]=='s')&&(a[3]=='e')&&(a[4]=='\0'))
)
return 1;
else return 0;
}
void S()
{
// strcpy(lookahead,nexttoken());
if(strcmp(lookahead,"id")==0)
{
match("id");
match("=");
// cout<<"MMMMMMM"<<endl;
//strcpy(lookahead,nexttoken());
E();
printf("\t<S->id=E>\n");
}
else if(strcmp(lookahead,"if")==0)
{ //cout<<"IFIFIFIFIF"<<endl;
match("if");
C();
match("then");
// cout<<"^^^^^^^^^^^"<<endl;
S();
printf("\t<if C then S>\n");
}
else if(strcmp(lookahead,"while")==0)
{
match("while");
C();
match("do");
S();
printf("\t<while C do S>\n");
}
else
{
printf("SS yu fa cuo wu\n");
exit(1);
}
}
void C()
{ E();
//strcpy(lookahead,nexttoken());
if(strcmp(lookahead,">")==0)
{
match(">");
E();
printf("\t<C->E>E>\n");
}
else if(strcmp(lookahead,"<")==0)
{ match("<");
match("=");
E();
printf("\t<C->E<=E>\n");
}
else
{
printf("CC yu fa cuo wu\n");
exit(1);
}
}
void E()
{ T();
if(strcmp(lookahead,"+")==0)
{
match("+");
E();
printf("\t<E->E+T>\n");
}
else if(strcmp(lookahead,"-")==0)
{ match("-");
E();
printf("\t<E->E-T>\n");
}
/*else if(strcmp(lookahead,"#")==0)
{
printf("\t<E->T>\n");
}
else
{
printf("EE yu fa cuo wu\n");
exit(1);
}*/
else
{
printf("\t<E->T>\n");
}
}
void T()
{ F();
if(strcmp(lookahead,"*")==0)
{
match("*");
T();
printf("\t<T->T*F>\n");
}
else if(strcmp(lookahead,"/")==0)
{
match("/");
T();
printf("\t<T->T/F>\n");
}
/*else if(strcmp(lookahead,"#")==0)
{
printf("\t<T->F>\n");
}
else
{
printf("TT yu fa cuo wu\n");
exit(1);
}*/
else
{
printf("\t<T->F>\n");
}
}
void F()
{ //flag1=1;
// cout<<"FFFFFFF"<<endl;
// cout<<lookahead<<endl;
if(strcmp(lookahead,"(")==0)
{
match("(");
E();
match(")");
printf("\t<F->(E)>\n");
}
else if(strcmp(lookahead,"id")==0)
{ // cout<<"**FFFFIDIDIDID"<<endl;
match("id");
// cout<<"**###########"<<endl;
printf("\t<F->id>\n");
}
else if(strcmp(lookahead,"const")==0)
{
match("const");
printf("\t<F->const>\n");
}
else
{
printf("FF yu fa cuo wu\n");
exit(1);
}
}
main()
{ clrscr();
strcpy(lookahead,nexttoken());
S();
exit(1);
getch();
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -