-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
91 lines (83 loc) · 2.22 KB
/
lexer.cpp
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
#include <bits/stdc++.h>
#include "lexer.h"
using namespace std;
void init(vector<string> & tkns){
tkns = {
"not in", "`","#","(",")","[","]","{","}",":",";","\n","\"","\'","\\\"","\\\'",",",".","?","++","--","->","~","!","@","$","%","^","&","&&","*","**","-","+","|","||","<","<<",">",">>","/","//","=","~=","!=","@=","$=","%=","^=","&=","&&=","*=","**=","-=","+=","|=","||=","<=","<<=",">=",">>=","/=","//=","=="
};
}
bool match(string s,string pat,int offset){
int i;
for(i = offset;i < min(s.size(),pat.size() + offset);i++){
if(s[i] != pat[i - offset]) return false;
}
return i - offset == pat.size();
}
vector<token> lex(int argc,char * argv[]){
if(argc < 2){
cerr << "No input files!\n";
exit(1);
}
ifstream inFile(argv[1]);
if(!inFile){
cerr << "File '" << argv[1] << "' not found!\n";
exit(1);
}
stringstream sstr;
sstr << inFile.rdbuf();
inFile.close();
string program = sstr.str();
vector<token> lexed;
vector<string> tokens;
init(tokens);
int line=1;
int col=1;
string current = "";
for(int i = 0; i < program.size(); i++){
int longestMatch = 0;
string tkn = "";
for(string s:tokens){
if(match(program,s,i)){
if(s.size() > longestMatch){
longestMatch = s.size();
tkn = s;
}
}
}
if(longestMatch){
if(current.size()){
lexed.push_back(token(current, line, col - current.size()));
current = "";
}
lexed.push_back(token(tkn, line, col));
if(tkn=="\n") {
line++;
col = 1;
}
else{
col += tkn.size();
}
i += tkn.size() - 1;
}
else if(!isspace(program[i])){
if(!isalnum(program[i]) && program[i] != '_') {
cerr << "Lex Error: Unexpected token on line " << line << " column " << col << ": '" << program[i] << "'" << endl;
}
current += program[i];
col++;
}
else{
if(current.size()) lexed.push_back(token(current, line, col - current.size()));
current = "";
col++;
}
}
if(current.size()) lexed.push_back(token(current, line, col - current.size()));
return lexed;
}
//debugging
void print_tokens(vector<token> a) {
for(auto b:a) {
b.print();
}
}