cifa.cpp
上传用户:sales9
上传日期:2022-01-26
资源大小:2k
文件大小:5k
- #include<iostream.h>
- #include<string.h>
- char *KeyWord[45]={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","return","short","signed","sizeof","static","struct","switch","typedef","unsigned","void","while","catch","class","const_cast","delete","friend",
- "inline","new","operator","private","protected","public","template","this","throw","try","virtual"};
- int i=0,j=0,k=0,t=0;//搜索指示器
- char ch;//存放最新读入的原程序字符
- char strToken[20];//存放构成单词符号的字符串
- char * chr_form[100];//字符表
- char * int_form[100];//常数表
- char form[1000];
- int q=0;
- int temp;
- void GetChar()//将下一个字符读入ch中,搜索指示器前移一字符位
- {
- ch=form[k];
- k++;
- }
- void GetBC()//检查ch中的字符是否为空白,若是则调用Getchar直至ch中进入一个非空白字符
- {
- while(ch==' ')
- {
- //k--;
- GetChar();
- }
- }
- void Concat()//将ch中的字符连接到strToken之后,
- {
- strToken[i]=ch;
- i++;
- }
- bool IsLetter()//判断ch中的字符是否为字符
- {
- if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
- return (1);
- else
- return(0);
- }
- bool IsDigit()//判断ch中的字符是否为数字
- {
- //k--;
- if(((ch)<='9')&&( (ch)>='0'))
- return (1);
- else
- return (0);
- }
- int Reserve()//对strToken中的字符串查找保留字表,若它是一个保留字
- //则返回它的编码,否则返回-1值
- {
- for(int q=0;q<45;q++)
- {
- if (strcmp(KeyWord[q],strToken)==0)
- return q;
- if(q==44)
- return -1;
- }
- }
- void Retract()//将搜索指示器回调一个字符位置,将ch置为空白字符
- {
- k--;
- ch=NULL;
- }
- char*InsertId()//将strToken中的标识符插入符号表,返回符号表的指针
- {
- chr_form[j]=strToken;
- j++;
- return chr_form[0];
- }
- char * InsertConst()//将strToken中的常数插入常数表,返回常数表指针
- {
- int_form[t]=strToken;
- t++;
- return int_form[0];
- }
- int code;
- //////////////////////////////////////////////////////////////////////
- void analyze()
- {
- GetChar();
- GetBC();
- //cout<<"此处没有错"<<endl;
- if(IsLetter())
- {
- while(IsLetter()||IsDigit())
- {
- Concat();
- GetChar();
- }
- //cout<<"此处没有错"<<endl;
- Retract();
- code=Reserve();
- switch(code)
- {
- case -1:cout<<"字符串, "<<strToken<<endl;break;
- default:cout<<"关键字, "<<strToken<<endl;
- }
- }
- else
- {
- if(IsDigit())
- {
- while(IsDigit()||ch=='.')
- {
- Concat();
- GetChar();
- }
- Retract();
- cout<<"常数, "<<strToken<<endl;
- }
- else
- {
- switch (ch)
- {
- case '=':
- case '/':
- case '%':
- case '*':cout<<"运算符,"<<ch<<endl;break;
- case '-': GetChar();
- if(ch=='-')
- {
- cout<<"运算符,"<<"--"<<endl;break;
- }
- else
- {
- Retract();
- cout<<"运算符,"<<"-"<<endl;break;
- }
- case '+':
- GetChar();
- if(ch=='+')
- {
- cout<<"运算符,"<<"++"<<endl;break;
- }
- else
- {
- Retract();
- cout<<"运算符,"<<"+"<<endl;break;
- }
- case '|':
- GetChar();
- if(ch=='|')
- {
- cout<<"运算符,"<<"||"<<endl;break;
- }
- else
- {
- Retract();
- cout<<"非法符号,"<<"|"<<endl;break;
- }
- case '&':
- GetChar();
- if(ch=='&')
- {
- cout<<"运算符,"<<"&&"<<endl;break;
- }
- case '(':
- case ';':
- case ')':
- case ':':
- case'[':
- case']':
- case '"':
- case''':
- case '{':
- case ',':
- case '}':cout<<"界符, "<<ch<<endl;break;
- case '<':
- GetChar();
- if(ch=='<')
- {
- cout<<"运算符,"<<"<<"<<endl;break;
- }
- else
- {
- cout<<"界符, "<<"<"<<endl;break;
- }
- case '>':
- GetChar();
- if(ch=='>')
- {
- cout<<"运算符,"<<">>"<<endl;break;
- }
- else
- {
- Retract();
- cout<<"界符, "<<">"<<endl;break;
- }
- }
- }
- }
- while(k<q)
- {
- // strcpy(strToken," ");
- //strcpy(strToken,"