词法分析器主要功能是处理源程序的字符流生成一个词法单元序列,它为语法分析器服务。下面的程序只是一个小实验,实现了主要功能,但总体设计和接口有待改进。
#include <stdio.h>
#include <string.h>
#define N 100
#define M 100
#define T 100
#define L 100
const char keyword[][10] = {
"int", "double", "for", "return", "if", "else"};
const char id[][10] = {
"id", "number", "keyword", "+", "-", "*", "/", "<", "=", "<=", "==", "(", ")", "{", "}"};
char text[N];
char objlist[T][L];
char buffer[L];
int trans[M][128];
int cnt;
void input() {
char ch, *p = text;
freopen("in.txt", "r", stdin);
while ((ch = getchar()) != EOF) {
*p++ = ch;
}
puts("**************************************"