-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
101 lines (95 loc) · 2.87 KB
/
Copy pathlexer.cpp
File metadata and controls
101 lines (95 loc) · 2.87 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
#include "lexer.h"
#include <cctype>
using namespace std;
Lexer::Lexer(const string &input): input(input), pos(0) {}
char Lexer::peek(){
if(pos>=input.size())return '\0';
return input[pos];
}
char Lexer::get(){
return input[pos++];
}
vector<Token> Lexer::tokenize(){
vector<Token>tokens;
while(pos<input.size()){
char c=peek();
if(isspace(c)){
get();
continue;
}
if(isalpha(c)){
string word;
while(pos<input.size() && isalnum(peek())) word+=get();
if(word =="int")tokens.push_back({INT,word});
else if (word == "return") tokens.push_back({RETURN,word});
else if (word == "if") tokens.push_back({IF,word});
else if (word=="else")tokens.push_back({ELSE,word});
else if(word=="while")tokens.push_back({WHILE,word});
else if(word=="for")tokens.push_back({FOR,word});
else if(word=="break")tokens.push_back({BREAK,word});
else if(word=="continue")tokens.push_back({CONTINUE,word});
else tokens.push_back({IDENTIFIER,word});
}else if(isdigit(c)){
string num;
while(pos<input.size() && isdigit(peek())) num+=get();
tokens.push_back({NUMBER,num});
}else if(c=='+'){
get();
tokens.push_back({PLUS,"+"});
}else if(c=='='){
get();
if(peek()=='='){
get();
tokens.push_back({EQUAL_EQUAL,"=="});
}else{
tokens.push_back({EQUAL,"="});
}
}else if(c=='-'){
get();
tokens.push_back({MINUS,"-"});
}else if(c=='*'){
get();
tokens.push_back({MULTIPLY,"*"});
}else if(c=='/'){
get();
tokens.push_back({DIVIDE,"/"});
} else if(c==';'){
get();
tokens.push_back({SEMICOLON,";"});
}else if(c=='<'){
get();
if(peek()=='='){
get();
tokens.push_back({LESS_EQUAL,"<="});
}else{
tokens.push_back({LESS,"<"});
}
}else if(c=='>'){
get();
if(peek()=='='){
get();
tokens.push_back({GREATER_EQUAL,">="});
}else{
tokens.push_back({GREATER,">"});
}
}else if(c=='('){
get();
tokens.push_back({LPAREN,"("});
}else if(c==')'){
get();
tokens.push_back({RPAREN,")"});
}else if(c=='{'){
get();
tokens.push_back({LBRACE,"{"});
}else if(c=='}'){
get();
tokens.push_back({RBRACE,"}"});
}
else
{
get();
}
}
tokens.push_back({END,""});
return tokens;
}