-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (66 loc) · 1.9 KB
/
main.cpp
File metadata and controls
71 lines (66 loc) · 1.9 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
68
69
70
71
#include <iostream>
#include <istream>
#include <fstream>
#include <algorithm>
#include "stack.hh"
// Main function to verify the number of argument and instantiate our main class.
// It also catch all our exceptions from the code.
std::string change(std::string str, std::string toFind, std::string toReplace)
{
if (size_t pos = str.find(toFind) != std::string::npos)
{
str.replace(pos, toFind.length(), toReplace);
}
return str;
}
std::string tokenFormatting(std::string file)
{
std::ifstream is;
is.open(file);
std::string str;
if (is) {
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
// read data as a block and clean opened file and useless buffer:
is.read(buffer,length);
str = buffer;
delete[] buffer;
is.close();
} else
throw std::string("Bad file");
std::string tmp;
std::string line;
std::string final;
int len = std::count(str.begin(), str.end(), '\n');
for (int i = 0; i < len;++i) // iterating over the number of line
{
line = str.substr(0, str.find('\n')); // splitting into line
tmp = line.substr(0, line.find('\t')); // removing tab from our previous program output
final.append(tmp + '\n');
size_t pos = str.find('\n');
str.erase(0, pos + 1); // erasing all the useless part after our keyboard
}
final += "$\n"; // adding the endmarker at the end of the file
return (final);
}
int main(int ac, char** av) {
if (ac != 2) {
std::cout << "Usage: ./syntax_analyser file" << std::endl;
return (1);
}
try {
Stack s(tokenFormatting(av[1]));
if (s.loop())
return (0);
return (1);
} catch (std::string const & e) {
std::cout << e << std::endl;
return (1);
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
return (1);
}
}