-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlex.c
68 lines (62 loc) · 1.25 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
68
/**
* @filename: lex.c
*
* @author: QinYUN575
*
* @create date: 2019/11/16
*
*
*
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "chtbl.h"
#include "lex.h"
#include "symtol.h"
/**
* lex
*
*
*/
Token lex(const char *istream, CHTbl *symtbl)
{
Token token;
Symbol *symbol;
int length, 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 = strlen(symbol->token);
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 (reval == 1)
{
/* 符号已经存储在符号表中 */
free(symbol);
}
}
return token;
}