-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
73 lines (64 loc) · 2.38 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
#include "definition.h"
void printCST(const string prefix, const CSTNode* node, ofstream *outfile, bool isTail = true)
{
if( node != nullptr )
{
(*outfile) << prefix;
(*outfile) << "|--";
(*outfile) << "<" << node->type << "> " << node->s << endl;
CSTNode* child = node->first_child;
while (child != nullptr) {
printCST(prefix + (isTail ? " " : "| "), child, outfile, child->next == nullptr);
child = child->next;
}
}
}
void printAST(const string prefix, const ASTNode* node, ofstream *outfile, bool isTail = true)
{
if( node != nullptr )
{
(*outfile) << prefix;
(*outfile) << "|--";
(*outfile) << "<" << node->type << "> " << node->s;
if (node->datatype.size() > 0)
(*outfile) << " " << node->datatype.to_string();
(*outfile) << endl;
ASTNode* child = node->first_child;
while (child != nullptr) {
printAST(prefix + (isTail ? " " : "| "), child, outfile, child->next == nullptr);
child = child->next;
}
}
}
int main(int argc, char* argv[])
{
ifstream in_file;
ofstream lex_file, grammar_file, cst_file, ast_file, cpp_cst_file, cpp_ast_file;
string testfile_name = "files/testfile.txt";
int DEBUG = 0;
TOKEN *token=nullptr, *p;
CSTNode* croot = new CSTNode("", "CompUnit", 0);
ASTNode* aroot = new ASTNode("", "CompUnit");
NestDataType expDataType = NestDataType();
if (argc > 1) testfile_name = argv[1];
if (argc > 2) DEBUG = stoi(argv[2]);
in_file.open(testfile_name);
lex_file.open("files/out/lex_file.txt");
grammar_file.open("files/out/grammar_file.txt");
ast_file.open("files/out/ast_file.txt");
cst_file.open("files/out/cst_file.txt");
cpp_cst_file.open("files/out/cpp_cst_file.cpp");
cpp_ast_file.open("files/out/cpp_ast_file.cpp");
lexAnalysis(&in_file, &token, &lex_file, DEBUG);
grammarAnalysis(&token, "CompUnit", croot, &grammar_file, DEBUG);
printCST("", croot, &cst_file);
semanticAnalysis(croot, aroot, "CompUnit", expDataType, 0, DEBUG);
printAST("", aroot, &ast_file);
genCSTCppCode(croot, "CompUnit", &cpp_cst_file, "", DEBUG);
vector<string> cpp_lines;
genASTCppCode(aroot, "CompUnit", cpp_lines, "", DEBUG);
for (string line : cpp_lines) {
cpp_ast_file << line << endl;
}
return 0;
}