/*需要的库和全局变量、函数及主程序*/
#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 toke[255] = ""; //单词缓冲区
int p_input; //输入缓冲区指针
int p_token; //单词缓冲区指针
char ch; //当前读入字符
char *rwtab[] = {"begin", "if", "then", "while", "do", "end", _KEY_WORD_END}; //可扩充的关键字数组
WORD *scaner(); //词法扫描函数,获得一个单词
int 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);
return 0;
}
/*需要用到自编函数参考实现*/
/*从输入缓冲区读取一个字符到ch*/
char m_getch()
{
ch = input[p_input];
p_input = p_input + 1;
return (ch);
}
/*去掉空白符号*/
void getbc()
{
while(ch == ''||ch == 10