资源简介
自制编程语言 原书代码
代码片段和文件信息
#include
#include
#include
#include “token.h“
static char *st_line;
static int st_line_pos;
typedef enum {
INITIAL_STATUS
IN_INT_PART_STATUS
DOT_STATUS
IN_FRAC_PART_STATUS
} LexerStatus;
void
get_token(Token *token)
{
int out_pos = 0;
LexerStatus status = INITIAL_STATUS;
char current_char;
token->kind = BAD_TOKEN;
while (st_line[st_line_pos] != ‘\0‘) {
current_char = st_line[st_line_pos];
if ((status == IN_INT_PART_STATUS || status == IN_FRAC_PART_STATUS)
&& !isdigit(current_char) && current_char != ‘.‘) {
token->kind = NUMBER_TOKEN;
sscanf(token->str “%lf“ &token->value);
return;
}
if (isspace(current_char)) {
if (current_char == ‘\n‘) {
token->kind = END_OF_LINE_TOKEN;
return;
}
st_line_pos++;
continue;
}
if (out_pos >= MAX_TOKEN_SIZE-1) {
fprintf(stderr “token too long.\n“);
exit(1);
}
token->str[out_pos] = st_line[st_line_pos];
st_line_pos++;
out_pos++;
token->str[out_pos] = ‘\0‘;
if (current_char == ‘+‘) {
token->kind = ADD_OPERATOR_TOKEN;
return;
} else if (current_char == ‘-‘) {
token->kind = SUB_OPERATOR_TOKEN;
return;
} else if (current_char == ‘*‘) {
token->kind = MUL_OPERATOR_TOKEN;
return;
} else if (current_char == ‘/‘) {
token->kind = DIV_OPERATOR_TOKEN;
return;
} else if (isdigit(current_char)) {
if (status == INITIAL_STATUS) {
status = IN_INT_PART_STATUS;
} else if (status == DOT_STATUS) {
status = IN_FRAC_PART_STATUS;
}
} else if (current_char == ‘.‘) {
if (status == IN_INT_PART_STATUS) {
status = DOT_STATUS;
} else {
fprintf(stderr “syntax error.\n“);
exit(1);
}
} else {
fprintf(stderr “bad character(%c)\n“ current_char);
exit(1);
}
}
}
void
set_line(char *line)
{
st_line = line;
st_line_pos = 0;
}
#if 0
void
parse_line(char *buf)
{
Token token;
set_line(buf);
for (;;) {
get_token(&token);
if (token.kind == END_OF_LINE_TOKEN) {
break;
} else {
printf(“kind..%d str..%s\n“ token.kind token.str);
}
}
}
int
main(int argc char **argv)
{
char buf[1024];
while (fgets(buf 1024 stdin) != NULL) {
parse_line(buf);
}
return 0;
}
#endif
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 10150 2015-11-11 20:17 molokai.vim
目录 0 2018-10-16 22:56 win_sjis\new\
目录 0 2013-12-12 16:16 win_sjis\
目录 0 2013-12-12 16:16 win_sjis\calc\
目录 0 2013-12-12 16:16 win_sjis\calc\llparser\
文件 2873 2008-12-19 07:38 win_sjis\calc\llparser\lexicalanalyzer.c
文件 68 2008-12-19 07:38 win_sjis\calc\llparser\make.bat
文件 41 2008-12-19 07:38 win_sjis\calc\llparser\make.sh
文件 2374 2008-12-19 07:38 win_sjis\calc\llparser\parser.c
文件 481 2008-12-19 07:38 win_sjis\calc\llparser\token.h
目录 0 2013-12-12 16:16 win_sjis\calc\llparser_ex\
文件 2965 2008-12-19 07:38 win_sjis\calc\llparser_ex\lexicalanalyzer.c
文件 68 2008-12-19 07:38 win_sjis\calc\llparser_ex\make.bat
文件 41 2008-12-19 07:38 win_sjis\calc\llparser_ex\make.sh
文件 2850 2008-12-19 07:38 win_sjis\calc\llparser_ex\parser.c
文件 528 2008-12-19 07:38 win_sjis\calc\llparser_ex\token.h
目录 0 2013-12-12 16:16 win_sjis\calc\mycalc\
文件 74 2008-12-19 07:38 win_sjis\calc\mycalc\make.bat
文件 64 2008-12-19 07:38 win_sjis\calc\mycalc\make.sh
文件 468 2008-12-19 07:38 win_sjis\calc\mycalc\mycalc.l
文件 1147 2008-12-19 07:38 win_sjis\calc\mycalc\mycalc.y
目录 0 2013-12-12 16:16 win_sjis\calc\mycalc_ex\
文件 74 2008-12-19 07:38 win_sjis\calc\mycalc_ex\make.bat
文件 64 2008-12-19 07:38 win_sjis\calc\mycalc_ex\make.sh
文件 524 2008-12-19 07:38 win_sjis\calc\mycalc_ex\mycalc.l
文件 1330 2008-12-19 07:38 win_sjis\calc\mycalc_ex\mycalc.y
目录 0 2013-12-12 16:17 win_sjis\crowbar_book_0_1\
文件 396 2008-12-19 07:38 win_sjis\crowbar_book_0_1\CRB.h
文件 1320 2008-12-19 07:38 win_sjis\crowbar_book_0_1\CRB_dev.h
文件 8893 2008-12-19 07:38 win_sjis\crowbar_book_0_1\create.c
文件 12205 2008-12-19 07:38 win_sjis\crowbar_book_0_1\crowbar.h
............此处省略566个文件信息
- 上一篇:点云生成mesh的算法
- 下一篇:在VisualC_中利用COM实现对Word的调用
评论
共有 条评论