Add new lex() function to streamline lexing, change main to reflect this

This commit is contained in:
2025-09-30 17:52:53 +03:00
committed by Emin Arslan
parent 08136c6492
commit 6f58051284
3 changed files with 23 additions and 4 deletions

View File

@@ -40,3 +40,5 @@ public:
std::vector<Token> collect(); std::vector<Token> collect();
}; };
// when you don't want to construct the object
std::vector<Token> lex(std::string);

View File

@@ -135,5 +135,19 @@ Token Lexer::next() {
} }
} }
vector<Token> Lexer::collect() {
vector<Token> v;
while (true) {
Token t = next();
if (t.type == TokenType::End)
break;
v.push_back(t);
}
return v;
}
std::vector<Token> lex(std::string s) {
Lexer l(s);
return l.collect();
}

View File

@@ -1,14 +1,17 @@
#include <iostream> #include <iostream>
#include <lex.hpp> #include <lex.hpp>
#include <sstream> #include <string>
using namespace std; using namespace std;
int main() { int main() {
string s; string s;
cin >> s; getline(cin, s);
cout << s << endl;
Lexer l(s); for (auto t : lex(s)) {
cout << l.next() << endl; cout << t << " ";
}
cout << endl;
return 0; return 0;
} }