-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
75 lines (58 loc) · 1.35 KB
/
main.cpp
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
72
73
74
//
// Created by Marwan Tammam on 3/21/18.
//
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "rexparser.h"
#include "lexer.h"
#include "tokenizer.h"
#include "dfa.h"
using namespace std;
int main (int argc, char** argv)
{
ifstream specs_ifs;
ifstream src_ifs;
ofstream ttab_ofs;
ofstream ttok_ofs;
if (argc < 5) {
perror("Invalid number of arguments!\n \
USAGE: ./Kompiler <specs_file> <input_file> \
<transition_table_output> <token_list_output>");
errno = EINVAL;
exit(-1);
}
specs_ifs.open(argv[1]);
src_ifs.open(argv[2]);
ttab_ofs.open(argv[3]);
ttok_ofs.open(argv[4]);
if (!specs_ifs || !src_ifs || !ttab_ofs || !ttok_ofs) {
perror("Unable to read file");
exit(-1);
}
auto s = [&specs_ifs] {
std::ostringstream ss;
ss << specs_ifs.rdbuf();
return ss.str();
}();
if (s.empty())
return 0;
rexparser rx;
machine nfa = rx.rules2nfa(s);
machine dfa = dfa::to_dfa(nfa);
machine min_dfa = dfa::minimize_dfa(dfa);
ttab_ofs << min_dfa;
lexer lex(min_dfa);
parser prs(&src_ifs, lex);
std::vector<lexer::token> v = prs.parse();
for (lexer::token tok : v) {
ttok_ofs << tok << endl;
cout << tok << endl;
}
specs_ifs.close();
src_ifs.close();
ttab_ofs.close();
ttok_ofs.close();
return 0;
}