0 Star 0 Fork 71

yanpanting/compiler-homework2

forked from zren/compiler-homework2 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lexer.c 813 Bytes
一键复制 编辑 原始数据 按行查看 历史
ren 提交于 2016-04-20 16:13 . test hook
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;
void lexical(FILE *fp) {
char x = getc(fp);
num = 0;
strcpy(id, "\0");
//process x
//process each regex here
//store the result in global variables id, num, or token
//in case of backtrack, use
//ungetc(x, fp);
}
int main(int argc, const char *argv[])
{
if(argc < 2) {
printf("insufficient args\n");
return 1;
}
FILE *fp = fopen(argv[1], "r");
do {
lexical(fp);
switch(token) {
case 10: printf("(id, %s)\n", id); break;
case 11: printf("(num, %d)\n", num); break;
case -1: printf("error!\n"); break;
default: printf("(%d, %s)\n", token, id);
}
} while(token != 0);
fclose(fp);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yanpanting/compiler-homework2.git
git@gitee.com:yanpanting/compiler-homework2.git
yanpanting
compiler-homework2
compiler-homework2
master

搜索帮助