I am working on a lexer and when I run the test program, I keep getting this error:
Segmentation fault
I read about the error and I am pretty sure that I am trying to write read only memory. This is my code:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector <string> split(string line) {
vector <string> tokens;
stringstream check1(line);
string intermediate;
while(getline(check1, intermediate, ';')) {
tokens.push_back(intermediate + ";");
}
return tokens;
}
vector <vector<string>> lex(vector <string> tokens) {
vector <vector<string>> lexed;
lexed[0] = tokens;
string token;
for (int i = 0; i < tokens.size(); i++) {
token = tokens[i];
cout << token;
}
return lexed;
}
int main() {
string line = "print(32); print(54); print(432);";
vector <string> tokens = split(line);
vector <vector<string>> x = lex(tokens);
for(int i = 0; i < tokens.size(); i++) {
cout << tokens[0][i] << "\n";
}
}
Please help!