#include<stdio.h> #include<stdlib.h> #include<string.h> #define _KEY_WOED_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 *rwtab[]={"begin","if","then","while","do","end",_KEY_WOED_END}; //C语言关键字 WORD * scaner(); //词法扫描函数,获得关键字 main() { int over=1; WORD *oneword; oneword=(WORD *)malloc(sizeof(WORD)); printf("请输入您的字符串(以#作为结束标志):"); scanf("%[^#]s",input); p_input=0; printf("您输入的字符串是:%s ",input); while(over<1000&&over!=-1) { oneword=scaner(); printf("(%d,%s) ",oneword->typenum,oneword->word); over=oneword->typenum; } } //需要用到的自编函数参考实现 //从输入缓冲区读取一个字符到ch中 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]='