词法分析程序
这个是我编译原理课程设计的 内容,呵呵 我遍的是词法分析,基本的功能都能够实现,如词法分析,词法查错,删除无实质意义的字符等,不懂编译的就不用看了,编译真的很难!!!不过好象没什么用
#include
#include
#include
#include
#include
#define ID 6
#define INT 7
#define LT 8
#define LE 9
#define EQ 10
#define NE 11
#define GT 12
#define GE 13
#define FZ 14
#define DEV 15
strUCt KeyWord //要害字结构
{
char *word;
int id;
};
KeyWord keyword[]={ //要害字数组
,
,
,
,
,
,
,
};
char TOKEN[20];
int graphnum=1; //记录错误所在的位置
int lookup(char *string);
void out(int id ,char *string);
void report_error(char ERR_CH);
bool isalpha(char c) ;
bool isdigit(char c);
bool isalnum(char c);
void scanner_example(FILE *fp);
int lookup(char *string)
{
for(int i=0;i { if(strcmp(string,keyword[i].word)==0) return keyword[i].id; } return 0; } void out(int id ,char *string) { printf("(%d,%s) ",id,string);; } void report_error(char ERR_CH) //错误处理程序 { printf("undeclared identifler %c int %d line! ",ERR_CH,graphnum); } bool isalpha(char c) { if( (c>='a'&&c<='z') (c>='A'&&c<='Z') ) return true; else return false; } bool isdigit(char c) { if(c>='0'&&c<='9') return true; else return false; } bool isalnum(char c) { if( isalpha(c) isdigit(c) ) return true; else return false; } void scanner_example(FILE *fp) { char ch; int i,c; while(!feof(fp)) { ch=fgetc(fp); if(isalpha(ch)) { TOKEN[0]=ch; ch=fgetc(fp); i=1; while(isalnum(ch)) TOKEN[i]='
- 上一篇:点阵字模工具编程辅助效果示例
- 下一篇:一般线性链表类的C++实现