- #include<stdio.h>
- #include<string.h>
- #include<iostream.h>
- #include<stdlib.h>
- char ch;
- char stra[256];
- struct baoliuzi
- {
- int no;
- char sx[20];
- }blz[17]={
- {1,"break"},
- {2,"char"},
- {3,"continue"},
- {4,"do"},
- {5,"double"},
- {6,"else"},
- {7,"extern"},
- {8,"float"},
- {9,"for"},
- {10,"int"},
- {11,"if"},
- {12,"long"},
- {13,"short"},
- {14,"static"},
- {15,"switch"},
- {16,"void"},
- {17,"while"}
- };
- int IsLetter(char ch);
- int IsDigit(char ch);
- int Reserve(char str[]);
- char Concat(char str[],char a);
- void cifa();
- void GetBC();
- void qingling(char str[]);
- void inPut();
- void GetChar();
- void main()
- {
- GetChar();
- GetBC();
- while(ch!=' '&&ch!='\\n'&&ch!='\\t'){
- cifa();
- ch=getchar();
- GetBC();}
- }
- void cifa()
- {
- char strToken[50]="";
- if(IsLetter(ch)){
- while(IsLetter(ch)||IsDigit(ch)){
- Concat(strToken,ch);
- GetChar();
- }
- Reserve(strToken);
- if(Reserve(strToken)){
- cout<<'<'<<Reserve(strToken)<<','<<strToken<<'>'<<endl;
- qingling(strToken);
- }
- else {
- cout<<'<'<<"70,"<<strToken<<'>'<<endl;
- qingling(strToken);
- }
- }
- else if(IsDigit(ch)){
- while(IsDigit(ch)){
- Concat(strToken,ch);
- GetChar();}
- cout<<'<'<<"80,"<<strToken<<'>'<<endl;
- }
- else
- switch(ch){
- case '<' : GetChar();
- if(ch=='=') cout<<'<'<<"31,"<<"<="<<'>'<<endl;
- else if(ch=='>') cout<<'<'<<"32,"<<"<>"<<'>'<<endl;
- else cout<<'<'<<"30,"<<'<'<<'>'<<endl;
- break;
- case '>' : GetChar();
- if(ch=='=') {cout<<'<'<<"34,"<<">="<<'>'<<endl;break;}
- else {cout<<'<'<<"33,"<<'>'<<'>'<<endl;break;}
- case '=' :cout<<'<'<<"35,"<<'='<<'>'<<endl;break;
- case '(' :cout<<'<'<<"36,"<<'('<<'>'<<endl;break;
- case ')' :cout<<'<'<<"37,"<<')'<<'>'<<endl;break;
- case '*' :GetChar();
- if(ch=='*') {cout<<'<'<<"38,"<<"**"<<'>'<<endl;break;}
- else {cout<<'<'<<"39,"<<'*'<<'>'<<endl;break;}
- case ':' : GetChar();
- if(ch=='=') {cout<<'<'<<"40,"<<":="<<'>'<<endl;break;}
- else break;
- case '+' : cout<<'<'<<"41,"<<'+'<<'>'<<endl;break;
- case '-' : cout<<'<'<<"42,"<<'-'<<'>'<<endl;break;
- case '?' : cout<<'<'<<"43,"<<'?'<<'>'<<endl;break;
- case ',' : cout<<'<'<<"44,"<<','<<'>'<<endl;break;
- case ';' : cout<<'<'<<"45,"<<';'<<'>'<<endl;break;
- case '\\n' : break;
- default : cout<<'<'<<"0,"<<ch<<'>'<<endl;break;
- }
- }
- void GetBC()
- {
- while(ch==' '||ch=='\\n'||ch=='\\t')
- GetChar();
- }
- int IsLetter(char ch)
- {
- if((ch<=90)&&(ch>=65)||(ch<=122)&&(ch>=97))
- return 1;
- else return 0;
- }
- int IsDigit(char ch)
- {
- if((ch<=57)&&(ch>=48))
- return 1;
- else return 0;
- }
- int Reserve(char str[])
- {
- int i;
- for(i=0;i<17;i++){
- if(strcmp(str,blz[i].sx)==0)
- return blz[i].no;
- }
- return 0;
- }
- char Concat(char str[],char a)
- {
- int i=0;
- i=strlen(str);
- str[i]=a;
- str[i+1]='\\0';
- return *str;
- }
- void qingling(char str[])
- {
- int i,j;
- i=strlen(str);
- for(j=0;j<i;j++)
- str[i]='\\0';
- }
- void inPut()
- {
- int i;
- for(i=0;ch!='$';i++)
- {stra[i]=ch;
- ch=getchar();}
- }
- void GetChar()
- {
- int i=1;
- ch=stra[i];
- i++;
- }
- //该片段来自于http://www.codesnippet.cn/detail/150520133360.html
来源: http://www.codesnippet.cn/detail/150520133360.html