-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.cpp
More file actions
166 lines (123 loc) · 3.14 KB
/
tokenizer.cpp
File metadata and controls
166 lines (123 loc) · 3.14 KB
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
158
159
160
161
162
163
164
165
#include <iostream>
#include <cstdlib>
#include <cctype>
#include<cstring>
#include<vector>
#include "tokenizer.hpp"
using namespace std;
static const char* keywords[] ={"if","else","for","while","return"}; //basic operator's list
//If the token is equal to number,alphabet(small or capital letter) and '_', return true.
bool Tokenizer::isAlnum(char c)
{
if(isalnum(c) || c == '_'){
return true;
}else{
return false;
}
}
//if the cursor matches a symbol, return true.
bool Tokenizer::isSymbol(void)
{
char* p = cursor;
for(const char* symbol:symbols){
int len =strlen(symbol);
if(memcmp(cursor,symbol,len) == 0){
Token Token(T_RESERVED,p,len);
tokens.push_back(Token);
cursor +=len;
return true;
}
}
return false;
}
//If the cursor is keyword, return true.
bool Tokenizer::isKeyword(void)
{
char* p =cursor;
for(const char* keyword: keywords){
int len = strlen(keyword);
if(memcmp(cursor,keyword,len) == 0 && isAlnum(*(cursor+len))){
Token Token(T_IDENT,p,len);
tokens.push_back(Token);
cursor +=len;
return true;
}
}
return false;
}
//If the cursor is identifer, return true.
bool Tokenizer::isIdentifier(void)
{
char* p = cursor;
int len = 0;
for(;isAlnum(*cursor);cursor++) len++;
if(len > 0){
Token Token(T_IDENT,p,len);
tokens.push_back(Token);
return true;
}
return false;
}
//If the cursor is equal to figure, return true.
bool Tokenizer::isNumber(void)
{
char* p =cursor;
int len =0;
for(;isdigit(*cursor);cursor++) len++;
if(len>0){
Token Token(T_NUM,p,len);
tokens.push_back(Token);
return true;
}
return false;
}
//if the cursor is equal to \n, return ture.
bool Tokenizer::isNewLine(void)
{
char* p =cursor;
if (*cursor == '\n'){
if(tokens.size() != 0 && tokens.back().str != "\n"){
Token Token(T_RESERVED,cursor,1);
tokens.push_back(Token);
}
cursor++;
return true;
}else{
return false;
}
}
//If the cursor is whitespace, skip to next character.
void Tokenizer::skip(void)
{
while(*cursor != '\n' && isspace(*cursor )){
cursor++;
}
}
//Tokenize the character
vector<Token> Tokenizer::tokenize(const char* input)
{
cursor = (char*)input;
tokens.clear();
while(*cursor){
skip();
if(isSymbol()){
}else if(isKeyword()){
}else if(isNumber()){
}else if(isIdentifier()){
}else if(isNewLine()){
}else{
cerr << "Error : invalid character "<< *cursor<<endl;
exit(1);
}
}
if(tokens.back().str == "\n"){
tokens.pop_back();
}
tokens.push_back(Token(T_EOF,cursor,0));
/*
cout << string(30,'-')<<endl;
for(Token token:tokens) token.print();
cout << string(30,'-')<<endl;
*/
return tokens;
}