-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
108 lines (91 loc) · 1.8 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "Lexical/Lexical.h"
#include "Grammar/Grammar.h"
#include "Semantic/Semantic.h"
#include <iostream>
//#define TEST
using namespace std;
void print(TreeNode *p)
{
cout<<"Current Address: "<<p<<endl;
cout<<"Word: ("<<p->Node_Word.Number<<","<<p->Node_Word.Value<<")"<<endl;
cout<<"Son_Count: "<<p->Next.size()<<endl;
for(int i=0;i<p->Next.size();i++)
{
cout<<"\tAddress: "<<p->Next[i]<<endl;
}
cout<<"Parent: "<<p->Parent<<endl;
cout<<endl;
for(int i=0;i<p->Next.size();i++)
{
print(p->Next[i]);
}
}
char Read[100005];
int main(int argc,char *argv[])
{
Lexical L;
L.init();
Grammar G;
G.init();
Semantic S;
S.init();
FILE *Input;
if(argc!=1)Input=fopen(argv[1],"r");
else cout<<"[Naive by Caproner]\nWelcome to use Naive_C!\n > ";
string s;
while(true)
{
if(argc==1)
{
if(!getline(cin,s))break;
}
else
{
if(fgets(Read,100000,Input)==NULL)break;
s=Read;
}
L.Input(s);
if(!L.Analysis())
{
cout<<"Error:词法分析出错"<<endl<<" > ";
continue;
}
Word_List WL=L.Output();
#ifdef TEST
for(int i=0;i<WL.List.size();i++)
{
cout<<"["<<WL.List[i].Number<<","<<WL.List[i].Value<<"]"<<endl;
}
cout<<"词法分析结束"<<endl;
#endif
G.Input(WL);
if(!G.Analysis())
{
cout<<"Error:语法分析出错"<<endl<<" > ";
continue;
}
Forest F=G.Output();
#ifdef TEST
for(int i=0;i<F.forest.size();i++)
{
cout<<"输出第"<<i+1<<"棵语法树"<<endl;
print(F.forest[i]);
}
cout<<"语法分析结束"<<endl;
#endif
if(!S.Analysis(F))
{
cout<<"Error:语义分析出错"<<endl<<" > ";
continue;
}
#ifdef TEST
for(int i=0;i<F.forest.size();i++)
{
cout<<"输出第"<<i+1<<"棵语法树"<<endl;
print(F.forest[i]);
}
cout<<"语义分析结束"<<endl;
#endif
cout<<" > ";
}
}