-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (58 loc) · 2.03 KB
/
main.cpp
File metadata and controls
67 lines (58 loc) · 2.03 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
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include "parser.h"
using namespace std;
int main(int argc, const char **argv) {
if (argc >= 2) {
int filePathIndex;
bool isASTFlagged = false;
if (argc == 3) {
// if AST flag is present
if (strcmp(argv[1], "-ast") == 0) {
// if -ast flag is present as first argument
filePathIndex = 2;
isASTFlagged = true;
} else if (strcmp(argv[2], "-ast") == 0) {
// if -ast flag is present as second argument
filePathIndex = 1;
isASTFlagged = true;
} else {
cout << " Error : Invalid flag" << endl;
return 1;
}
} else if (argc == 2) {
// if no flag is present
filePathIndex = 1;
} else {
cout << " Error : Incorrect no. of inputs " << endl;
return 1;
}
string filepath = argv[filePathIndex]; // Get the file path
// Read file
ifstream input(filepath);
if (!input.is_open()) {
// Check if file exists
std::cout << "File " << "\"" << filepath << "\"" << " not found!" << "\n";
return 1;
}
// Read file contents into a string
string inputString((istreambuf_iterator<char>(input)), (istreambuf_iterator<char>()));
input.close();
// Convert string to a char array
const int fileLength = inputString.size();
char characterArray[fileLength];
for (int i = 0; i < inputString.size(); i++) characterArray[i] = inputString[i];
// Initialize parser and start parsing
parser rpal_parser(characterArray, fileLength, isASTFlagged);
rpal_parser.parse();
return 0;
} else if (argc == 1) {
cout << " Error :No Input Program " << endl;
return 1;
} else
cout << " Error : Incorrect no. of inputs " << endl;
return 1;
return 1;
}