-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
54 lines (48 loc) · 1.99 KB
/
main.cpp
File metadata and controls
54 lines (48 loc) · 1.99 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
#include "lexer.h"
#include "parser.h"
#include "ptable_reader.h"
#include "exceptions.h"
#include <iostream>
#include <string>
#include <iomanip>
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: ./parser <input file>" << std::endl;
return 1;
}
std::string fileName = argv[1];
std::vector<Token> tokens;
try {
Lexer lex = Lexer(fileName);
while (true) {
Token *token = lex.getNextToken(); //fetch stored tokens from vector tokens
if (token == NULL) { // NULL will be returned if request exceeded the stored tokens
break; // to break the while loop
}
// std::cout << std::left << "Token " << std::setw(7) << token->tokenId << "Line " << std::setw(6) << token->lineNumber << std::setw(15) << token->type << token->tokenString << std::endl << std::right;
// std::cout << token->tokenString << " ";
tokens.push_back(*token);
}
ParsingTableReader *ptable = new ParsingTableReader("actions.csv", "goto.csv");
Parser parser = Parser(tokens, ptable);
} catch (FileNotFoundException e) {
std::cout << "\033[1;31mFileNotFoundException\033[0m ";
std::cout << "File not found: " << e.getFileName() << std::endl;
} catch (LexicalException e) {
std::cout << "\033[1;31mLexicalException\033[0m ";
std::cout << "Lexical error on line " << e.getLineNumber() << std::endl << std::endl;
std::cout << "\033[1;32m" << e.getLineNumber() << " \033[0m" << e.getLine();
std::cout << " ";
int lineNumber = e.getLineNumber();
while (lineNumber > 9) {
lineNumber /= 10;
std::cout << " ";
}
for (int i = 0; i < e.getLinePointer(); i++) {
std::cout << " ";
}
std::cout << "\033[1;31m^\033[0m" << std::endl;
std::cout << "\033[1;36mMessage: \033[0m" << e.getMessage() << std::endl;
}
return 0;
}