-
Notifications
You must be signed in to change notification settings - Fork 4
/
lexer-v1.c
156 lines (146 loc) · 2.9 KB
/
lexer-v1.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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "lexer-v1.h"
int current_position = 0;
const char *token_type_to_string(TokenType type)
{
switch (type)
{
case TOKEN_UNKNOWN:
return "Unknown";
case TOKEN_INTEGER:
return "Integer";
case TOKEN_VARIABLE:
return "Variable";
case TOKEN_PRINT:
return "Print";
case TOKEN_ADD_OP:
return "Add Operator";
case TOKEN_ASSIGN_OP:
return "Assign Operator";
case TOKEN_LPAREN:
return "Left Parenthesis";
case TOKEN_RPAREN:
return "Right Parenthesis";
case TOKEN_SEMICOLON:
return "Semicolon";
case TOKEN_END:
return "End";
default:
return "Invalid TokenType";
}
}
int is_numeric(char c)
{
return isdigit(c);
}
int is_alpha(char c)
{
return isalpha(c);
}
int is_alphanumeric(char c)
{
return is_alpha(c) || is_numeric(c);
}
int is_whitespace(char c)
{
return isspace(c);
}
Token get_next_token(void)
{
char c;
Token token = {TOKEN_UNKNOWN, {0}};
while ((c = getchar()) != EOF)
{
if (is_whitespace(c))
{
current_position++;
continue;
}
else if (c == '+' || c == '-')
{
token.type = TOKEN_ADD_OP;
token.position = current_position++;
sprintf(token.lexeme, "%c", c);
break;
}
else if (c == '=')
{
token.type = TOKEN_ASSIGN_OP;
token.position = current_position++;
sprintf(token.lexeme, "%c", c);
break;
}
else if (c == '(')
{
token.type = TOKEN_LPAREN;
token.position = current_position++;
sprintf(token.lexeme, "%c", c);
break;
}
else if (c == ')')
{
token.type = TOKEN_RPAREN;
token.position = current_position++;
sprintf(token.lexeme, "%c", c);
break;
}
else if (c == ';')
{
token.type = TOKEN_SEMICOLON;
token.position = current_position++;
sprintf(token.lexeme, "%c", c);
break;
}
else if (is_numeric(c))
{
token.type = TOKEN_INTEGER;
token.position = current_position;
int i = 0;
while (is_numeric(c))
{
token.lexeme[i++] = c;
c = getchar();
}
current_position += i;
ungetc(c, stdin);
break;
}
else if (is_alpha(c))
{
int i = 0;
int start_position = current_position;
while (is_alphanumeric(c))
{
token.lexeme[i++] = c;
c = getchar();
}
ungetc(c, stdin);
current_position += i;
if (strcmp(token.lexeme, "print") == 0)
{
token.type = TOKEN_PRINT;
token.position = start_position;
}
else
{
token.type = TOKEN_VARIABLE;
token.position = start_position;
}
break;
}
else
{
token.type = TOKEN_UNKNOWN;
token.position = current_position;
break;
}
}
if (c == EOF)
{
token.type = TOKEN_END;
token.position = current_position;
}
return token;
}