-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlex.c
157 lines (143 loc) · 2.87 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*************************************************************************
> File Name: lex.c
> Author: ellochen
> Mail: [email protected]
> Created Time: Mon Apr 29 21:57:57 2013
************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "token.h"
#include "lex.h"
typedef struct Token (*Matcher)(void);
char *CURSOR = 0;
#define END_OF_FILE 255
static Matcher matchers[END_OF_FILE+1];
static struct Token matchDouble(void)
{
double d = 0;
char *start = CURSOR;
CURSOR += 1;
while (IsDigit(*CURSOR)) {
CURSOR += 1;
}
if ('.' == *CURSOR) {
CURSOR += 1;
while (IsDigit(*CURSOR)) {
CURSOR += 1;
}
}
d = strtod(start,0);
struct Token token;
token.var.d = d;
token.type = TOKEN_DOUBLECONST;
return token;
}
static struct Token matchPlus(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_PLUS;
return token;
}
static struct Token matchMinus(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_MINUS;
return token;
}
static struct Token matchStar(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_STAR;
return token;
}
static struct Token matchSlash(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_SLASH;
return token;
}
static struct Token matchBadChar(void)
{
printf("无法识别的字符:%c\n",*CURSOR);
exit(0);
}
static struct Token matchLineEnd(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_LINEEND;
return token;
}
static struct Token matchLparen(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_LPAREN;
return token;
}
static struct Token matchRparen(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_RPAREN;
return token;
}
static struct Token matchIdentifier(void)
{
char *start = CURSOR;
CURSOR += 1;
while (IsLetterOrDigit(*CURSOR)) {
CURSOR += 1;
}
struct Token token;
token.type = TOKEN_ID;
char *pTokenVar = (char*)malloc(CURSOR - start);
strncpy(pTokenVar,start,CURSOR - start);
token.var.p = (void*)pTokenVar;
return token;
}
static struct Token matchEqual(void)
{
CURSOR += 1;
struct Token token;
token.type = TOKEN_EQUAL;
return token;
}
void lexerInit(void)
{
int i;
for (i = 0; i < END_OF_FILE + 1; i++) {
if (IsDigit(i)) {
matchers[i] = matchDouble;
}else if (IsLetter(i)) {
matchers[i] = matchIdentifier;
}else {
matchers[i] = matchBadChar;
}
}
matchers['\0'] = matchLineEnd;
matchers['+'] = matchPlus;
matchers['-'] = matchMinus;
matchers['*'] = matchStar;
matchers['/'] = matchSlash;
matchers['('] = matchLparen;
matchers[')'] = matchRparen;
matchers['='] = matchEqual;
}
static void SkipWhiteSpace(void)
{
while (*CURSOR == '\t' || *CURSOR == '\v' || *CURSOR == '\f' || *CURSOR == ' ' ||
*CURSOR == '\r' || *CURSOR == '\n') {
CURSOR += 1;
}
}
struct Token getNextToken()
{
SkipWhiteSpace();
return (matchers[*CURSOR])();
}