-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntermediateCodeGeneration.cpp
More file actions
130 lines (123 loc) · 3.24 KB
/
IntermediateCodeGeneration.cpp
File metadata and controls
130 lines (123 loc) · 3.24 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
using namespace std;
string operators[] = {"+", "-", "*", "/"};
bool isOperator(const string &token) // 判断是不是运算符
{
for (int i = 0; i < 4; i++)
if (operators[i] == token)
return true;
return false;
}
void G_S_translate(string buf)
{
char n_terminal = buf[0];
int i = 3;
string s = "";
while (i < buf.length())
{
if (buf[i] == ':' && buf[i + 1] == '=') // i:=E
{
string temp = buf.substr(i + 2);
cout << "(:=, " << temp << ", -, " << s << ")\n";
break;
}
else if (buf.length() == 4)
{
cout << "(:=, " << buf[3] << ", -, " << n_terminal << ")\n";
break;
}
else if (buf[i] == '(')
{
int j = i + 1;
string temp = "";
temp += buf[j++];
while (buf[j] != ')' && j < buf.length())
{
temp += buf[j];
j++;
}
cout << "(:=, " << temp << ", -, " << n_terminal << ")\n";
break;
}
else if (isOperator(string(1, buf[i]))) // 运算符or负号
{
if (s != "") // 运算符
{
char op = buf[i];
string temp = buf.substr(i + 1);
cout << "(" << op << ", " << s << ", " << temp << ", " << n_terminal << ")\n";
break;
}
else
{
s = buf.substr(i + 1);
cout << "(-, " << s << ", -, " << n_terminal << ")\n";
break;
}
}
else
s += buf[i];
i++;
}
}
void G_D_translate(string buf)
{
if (buf.length() <= 3)
cout << "格式错误!\n";
char n_terminal = buf[0]; // 记录非终结符
int i = 3;
string c = "";
while (i < buf.length()) // 从->之后开始
{
if (buf[i] == ':')
{
string temp = buf.substr(i + 1);
cout << "(:=, " << temp << ", -, " << n_terminal << ")\n";
}
else if (buf[i] == ' ' && c == "var")
{
string temp = buf.substr(i + 1);
cout << "(:=, " << temp << ", -, " << n_terminal << ")\n";
}
else if (buf[i] == ',')
{
string temp = buf.substr(i + 1) + "1";
cout << "(:=, " << temp << ", -, " << n_terminal << ")\n";
}
else
c += buf[i];
i++;
}
}
void open_files(string file_name, int mode) // 0->G[S],1->G[D]
{
ifstream ifs;
ifs.open(file_name, ios::in);
if (ifs.is_open())
{
cout << "\n开始翻译...\n";
if (mode == 0) // 赋值语句文法的翻译
{
string buf;
while (getline(ifs, buf))
G_S_translate(buf);
}
else // 说明文法的翻译
{
string buf;
while (getline(ifs, buf))
G_D_translate(buf);
}
cout << "翻译完毕...\n";
}
ifs.close();
}
int main()
{
cout << "说明文法G[D]的翻译:\n";
open_files("G[D].txt", 1);
cout << "\n赋值语句文法G[S]的翻译:\n";
open_files("G[S].txt", 0);
system("pause");
return 0;
}