Scanner:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _KEY_WORD_END "waiting for your expanding"
typedef struct
{
int typenum;
char * word;
} WORD;
char input[255];
char token[255]="";
int p_input;
int p_token;
char ch;
char* KEY_WORDS[]={"main","int","char","if","else","for","while",_KEY_WORD_END};
WORD* scaner();
void main()
{
int over=1;
WORD* oneword=new WORD;
printf("Enter Your words(end with $):");
scanf("%[^$]s",input);
p_input=0;
printf("Your words:\n%s\n",input);
while(over<1000&&over!=-1){
oneword=scaner();
if(oneword->typenum<1000)
printf("(%d,%s)",oneword->typenum,oneword->word);
over=oneword->typenum;
}
printf("\npress # to exit:");
scanf("%[^#]s",input);
}
char m_getch(){
ch=input[p_input];
p_input=p_input+1;
return (ch);
}
void getbc(){
while(ch==' '||ch==10){
ch=input[p_input];
p_input=p_input+1;
}
}
void concat(){
token[p_token]=ch;
p_token=p_token+1;
token[p_token]='\0';
}
int letter(){
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')return 1;
else return 0;
}
int digit(){
if(ch>='0'&&ch<='9')return 1;
else return 0;
}
int reserve(){
int i=0;
while(strcmp(KEY_WORDS[i],_KEY_WORD_END)){
if(!strcmp(KEY_WORDS[i],token)){
return i+1;
}
i=i+1;
}
return 10;
}
void retract(){
p_input=p_input-1;
}
char* dtb(){
return NULL;
}
WORD* scaner(){
WORD* myword=new WORD;
myword->typenum=10;
myword->word="";
p_token=0;
m_getch();
getbc();
if(letter()){
while(letter()||digit()){
concat();
m_getch();
}
retract();
myword->typenum=reserve();
myword->word=token;
return(myword);
}
else if(digit()){
while(digit()){
concat();
m_getch();
}
retract();
myword->typenum=20;
myword->word=token;
return(myword);
}
else switch(ch){
case '=': m_getch();
if (ch=='='){
myword->typenum=39;
myword->word="==";
return(myword);
}
retract();
myword->typenum=21;
myword->word="=";
return(myword);
break;
case '+': myword->typenum=22;
myword->word="+";
return(myword);
break;printf怎么读的
case '-': myword->typenum=23;
myword->word="-";
return(myword);
break;
case '*': myword->typenum=24;
myword->word="*";
return(myword);
break;
case '/': myword->typenum=25;
myword->word="/";
return(myword);
break;
case '(': myword->typenum=26;
myword->word="(";
return(myword);
break;
case ')': myword->typenum=27;
myword->word=")";
return(myword);
break;
case '[': myword->typenum=28;
myword->word="[";
return(myword);
break;
case ']': myword->typenum=29;
myword->word="]";
return(myword);
break;
case '{': myword->typenum=30;
myword->word="{";
return(myword);
break;
case '}': myword->typenum=31;
myword->word="}";
return(myword);
break;
case ',': myword->typenum=32;
myword->word=",";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论