-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathlex.c
67 lines (48 loc) · 1.23 KB
/
lex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// lex.c
// Algorithms - lex
//
// Created by YourtionGuo on 28/04/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "chtbl.h"
#include "lex.h"
#include "symbol.h"
#pragma mark - Public
Token lex(const char *istream, CHTbl *symtbl)
{
Token token;
Symbol *symbol;
int length, retval, i;
/// 创建符号表的空间
if ((symbol = (Symbol *)malloc(sizeof(Symbol))) == NULL) return error;
/// 对输入流进行分割
if ((symbol->lexeme = next_token(istream)) == NULL) {
/// 如果没有更多数据则返回
free(symbol);
return lexit;
} else {
/// 判断符号类型
symbol->token = digit;
length = (int)strlen(symbol->lexeme);
for (i = 0; i < length; i++) {
if (!isdigit(symbol->lexeme[i])) {
symbol->token = other;
}
}
memcpy(&token, &symbol->token, sizeof(Token));
/// 插入到符号表
if ((retval = chtbl_insert(symtbl, symbol)) < 0) {
free(symbol);
return error;
} else if (retval == 1) {
/// 符号已经存在
free(symbol);
}
}
/// 返回结果
return token;
}