测试文件:
运行结果:
单词种别码表:
源代码
lex.h
#ifndef LEX_H_INCLUDED
#define LEX_H_INCLUDED
#include <iostream>
#include <ctype.h>
#include <string>
#include <cstdio>
#include <stdlib.h>
using namespace std;
typedef struct /* 单词二元组的结构 */
{
int typenum; /* 种别码 */
string token; /* 自身字符串 */
} WORD;
WORD* scaner(FILE *fp); // 对文件扫描读取
int keyOrIdentifier(string token); // 判断是关键字还是标识符
#endif // LEX_H_INCLUDED
lex.cpp
#include "lex.h"
#define _KEY_WORD_END "waiting for your expanding" /* 定义关键字结束标志 */
string KEY_WORDS[]= {
"main","return","if","else","for","include","printf","int","char",_KEY_WORD_END}; /*可扩充的关键字词组*/
int line_num=1;
/* 判断是标识符还是关键字*/
int keyOrIdentifier(string token)
{
int i=0;
while(KEY_WORDS[i]!=_KEY_WORD_END)
{
if(KEY_WORDS[i]==token)
{
return i+1;
}
i=i+1;
}
return 10;
}
WORD* scaner(FILE *fp)
{
char ch;
string token="";
WORD* myword=new WORD;
myword->typenum=0;
myword->token="";
ch=fgetc(fp);
/* 判断回车 */
if(int(ch)==10)
{
line_num++;
return (myword);
}
/* 判断空格 */
else if(isspace(ch))
{
return (myword);
}
/* 标识符及关键字 */
else if(isalpha(ch)) //如果首字符是字母
{
while(isalpha(ch