编译原理 算符优先法文法分析


编译原理 算符优先法文法分析

由于水平有限,这里就不写原理了,请参考书《编译原理》(第3版)——清华大学出版社。
书中相关内容为第110页5.3.3节到第117页。其中FIRSTVT集和LASTVT集是根据第114页的简单关系图形所求的,规约过程参考第116页表5.7和表5.8。

代码如下:

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

//输入文法
string input;
//存储文法
vector<string>Context;
//非终结符
vector<char>Vn;
//终结符
vector<char>Vt;
//FIRSTVT集
map<char, string>FIRSTVT;
//LASTVT集
map<char, string>LASTVT;
//算符优先关系表
map<char, map<char, char>>OperatorTable;


//处理文法、终结符、非终结符
void InputContext()
{<!-- -->
    while (cin>>input)
    {<!-- -->
        if (input == "@") break;
        Context.push_back(input);
        for (int i = 0; i < input.size(); i++)
        {<!-- -->
            if ((input[i] >= 'a' && input[i] <= 'z')||input[i]=='(' || input[i] == ')' || input[i] == '+' || input[i] == '*' || input[i] == '!' || input[i] == '#')
            {<!-- -->
                if (find(Vt.begin(), Vt.end(), input[i]) == Vt.end())
                {<!-- -->
                    Vt.push_back(input[i]);
                }
            }

            if (input[i] >= 'A' && input[i] <= 'Z')
            {<!-- -->
                if (find(Vn.begin(), Vn.end(), input[i]) == Vn.end())
                {<!-- -->
                    Vn.push_back(input[i]);
                }
            }
        }
    }
    cout << endl;
    //输出终结符
    cout << "终结符:  ";
    for (int i = 0; i < Vn.size(); i++)
    {<!-- -->
        cout << Vn[i]<<"   ";
    }
    cout << endl;

    //输出非终结符
    cout << "非终结符:  " ;
    for (int i = 0; i < Vt.size(); i++)
    {<!-- -->
        cout << Vt[i] <<"   ";
    }
    cout << endl;
}

//计算FIRSTVT集,one
void InitFIRSTVT()
{<!-- -->
    for (int i = 0; i < Context.size(); i++)
    {<!-- -->  
        if ((Context[i][3] >= 'a' && Context[i][3] <= 'z') || Context[i][3] == '(' || Context[i][3] == ')' || Context[i][3] == '+' || Context[i][3] == '*' || Context[i][3] == '!' || Context[i][3] == '#')
        {<!-- -->
            FIRSTVT[Context[i][0]] += Context[i][3];
        }

        if (Context[i].size()>= 4)
        {<!-- -->
            if ((Context[i][3] >= 'A' && Context[i][3] <= 'Z') &&( (Context[i][4] >= 'a' && Context[i][4] <= 'z') || Context[i][4] == '(' || Context[i][4] == ')' || Context[i][4] == '+' || Context[i][4] == '*' || Context[i][4] == '!' || Context[i][4] == '#'))
            {<!-- -->
               
                FIRSTVT[Context[i][0]] += Context[i][4];
            }
        }
       
        if (Context[i][3] >= 'A' && Context[i][3] <= 'Z'&& Context[i][3]!= Context[i][0])
        {<!-- -->
            if (-1==FIRSTVT[Context[i][0]].find(Context[i][3]))
            {<!-- -->
                FIRSTVT[Context[i][0]] += Context[i][3];
            }
           
        }
    }
    //输出
    cout << endl;
    cout << "中间结果" << endl;
    map<char, string>::iterator it;
    for (it = FIRSTVT.begin(); it != FIRSTVT.end(); it++)
    {<!-- -->
        cout << it->first << ":  " << it->second << endl;
    }
}
//计算FIRSTVT集,two
void CalFIRSTVT(char start)
{<!-- -->
    for (int i = 0; i <FIRSTVT[start].size(); i++)
    {<!-- -->
        if (FIRSTVT[start][i]>= 'A' && FIRSTVT[start][i] <= 'Z')
        {<!-- -->
            char c = FIRSTVT[start][i];
            FIRSTVT[start].erase(i, 1);
            FIRSTVT[start] += FIRSTVT[c];
            i = 0;
        }
    }
}
//计算FIRSTVT,Finally
void AllCalFIRSTVT()
{<!-- -->
    cout <<endl;
    cout << "FIRSTVT集" << endl;
    map<char, string>::iterator it;
    for (it = FIRSTVT.begin(); it != FIRSTVT.end(); it++)
    {<!-- -->
        CalFIRSTVT(it->first);
        cout << it->first<<" 的FIRSVT集:  "<< FIRSTVT[it->first] << endl;
    }
}

//计算LASTVT集,one
void InitLASTVT()
{<!-- -->
    for (int i = 0; i < Context.size(); i++)
    {<!-- -->
        if ((Context[i][Context[i].size()-1] >= 'a' && Context[i][Context[i].size() - 1] <= 'z') || Context[i][Context[i].size() - 1] == '(' || Context[i][Context[i].size() - 1] == ')' || Context[i][Context[i].size() - 1] == '+' || Context[i][Context[i].size() - 1] == '*' || Context[i][Context[i].size() - 1] == '!' || Context[i][Context[i].size() - 1] == '#')
        {<!-- -->
            LASTVT[Context[i][0]] += Context[i][Context[i].size() - 1];
        }

        if (Context[i].size() >= 5)
        {<!-- -->
            if ((Context[i][Context[i].size() - 1] >= 'A' && Context[i][Context[i].size() - 1] <= 'Z') && ((Context[i][Context[i].size() - 2] >= 'a' && Context[i][Context[i].size() - 2] <= 'z') || Context[i][Context[i].size() - 2] == '(' || Context[i][Context[i].size() - 2] == ')' || Context[i][Context[i].size() - 2] == '+' || Context[i][Context[i].size() - 2] == '*' || Context[i][Context[i].size() - 2] == '!' || Context[i][Context[i].size() - 2] == '#'))
            {<!-- -->

                LASTVT[Context[i][0]] += Context[i][Context[i].size() - 2];
            }
        }

        if (Context[i][Context[i].size() - 1] >= 'A' && Context[i][Context[i].size() - 1] <= 'Z' && Context[i][Context[i].size() - 1] != Context[i][0])
        {<!-- -->
            if (-1 ==LASTVT[Context[i][0]].find(Context[i][Context[i].size() - 1]))
            {<!-- -->
                LASTVT[Context[i][0]] += Context[i][Context[i].size() - 1];
            }

        }
    }
    //输出
    cout << endl;
    cout << "中间结果" << endl;
    map<char, string>::iterator it;
    for (it =LASTVT.begin(); it != LASTVT.end(); it++)
    {<!-- -->
        cout << it->first << ":  " << it->second << endl;
    }
}
//计算LASTVT集,two
void CalLASTVT(char start)
{<!-- -->
    for (int i = 0; i < LASTVT[start].size(); i++)
    {<!-- -->
        if (LASTVT[start][i] >= 'A' && LASTVT[start][i] <= 'Z')
        {<!-- -->
            char c = LASTVT[start][i];
            LASTVT[start].erase(i, 1);
            LASTVT[start] += LASTVT[c];
            i = 0;
        }
    }
    //cout << LASTVT[start] << endl;
}
//计算FIRSTVT,Finally
void AllCalLASTVT()
{<!-- -->
    cout << endl;
    cout << "LASTVT集" << endl;
    map<char, string>::iterator it;
    for (it = LASTVT.begin(); it != LASTVT.end(); it++)
    {<!-- -->
        CalLASTVT(it->first);
        cout << it->first << " 的LASTVT集:  " << LASTVT[it->first] << endl;
    }
}

//生成算符优先关系表
void CalOperatorTable()
{<!-- -->
    for (int i = 0; i < Context.size(); i++)
    {<!-- -->
        for (int j = 3; j < Context[i].size(); j++)
        {<!-- -->
            if (Context[i].size() >= 5&&j<(Context[i].size()-1))
            {<!-- -->
                if (((Context[i][j] >= 'a' && Context[i][j] <= 'z') || Context[i][j] == '('
                    || Context[i][j] == ')' || Context[i][j] == '+' || Context[i][j] == '*'
                    || Context[i][j] == '!' || Context[i][j] == '#')
                    && Context[i][j + 1] >= 'A' && Context[i][j + 1] <= 'Z')
                {<!-- -->
                    for (int k = 0; k < FIRSTVT[Context[i][j + 1]].size(); k++)
                    {<!-- -->
                        OperatorTable[Context[i][j]][ FIRSTVT[Context[i][j + 1]][k] ] = '<';
                    }
                }


               
                if (((Context[i][j+1] >= 'a' && Context[i][j + 1] <= 'z') || Context[i][j + 1] == '('
                    || Context[i][j + 1] == ')' || Context[i][j + 1] == '+' || Context[i][j + 1] == '*'
                    || Context[i][j + 1] == '!' || Context[i][j + 1] == '#') && Context[i][j] >= 'A'
                    && Context[i][j] <= 'Z')
                {<!-- -->
                    for (int k = 0; k < LASTVT[Context[i][j]].size(); k++)
                    {<!-- -->
                        OperatorTable[LASTVT[Context[i][j]][k]][Context[i][j+1]] = '>';
                    }
                }

            }
            if (Context[i].size() >= 5&&j < (Context[i].size() -1))
            {<!-- -->
                if (((Context[i][j] >= 'a' && Context[i][j] <= 'z') || Context[i][j] == '(' || Context[i][j] == ')'
                    || Context[i][j] == '+' || Context[i][j] == '*' || Context[i][j] == '!' || Context[i][j] == '#')
                    && ((Context[i][j+1] >= 'a' && Context[i][j+1] <= 'z') || Context[i][j+1] == '('
                        || Context[i][j+1] == ')' || Context[i][j+1] == '+' || Context[i][j+1] == '*'
                        || Context[i][j+1] == '!' || Context[i][j+1] == '#'))
                {<!-- -->
                    OperatorTable[Context[i][j]][Context[i][j + 1]] = '=';
                }
            }
            if (Context[i].size() >= 6 && j < (Context[i].size() - 2))
            {<!-- -->
                if (((Context[i][j] >= 'a' && Context[i][j] <= 'z') || Context[i][j] == '(' || Context[i][j] == ')'
                    || Context[i][j] == '+' || Context[i][j] == '*' || Context[i][j] == '!' || Context[i][j] == '#')
                    && ((Context[i][j + 2] >= 'a' && Context[i][j + 2] <= 'z') || Context[i][j + 2] == '(' ||
                        Context[i][j + 2] == ')' || Context[i][j + 2] == '+' || Context[i][j + 2] == '*' ||
                        Context[i][j + 2] == '!' || Context[i][j + 2] == '#')
                    && (Context[i][j + 1] >= 'A' && Context[i][j + 1] <= 'Z'))
                {<!-- -->
                    OperatorTable[Context[i][j]][Context[i][j + 2]] = '=';
                }
            }
        }
       
           
       
    }

    cout << endl;
    cout << "算符优先关系表" << endl;
    map<char, map<char, char>>::iterator it;
    for (it = OperatorTable.begin(); it != OperatorTable.end(); it++)
    {<!-- -->
        map<char, char>::iterator it2;
        for (it2 = it->second.begin(); it2 != it->second.end(); it2++)
        {<!-- -->
            cout << it->first << "  " << it2->first << "  " << it2->second << endl;
        }
    }
}

//规约
void GY(string s)
{<!-- -->
    string stks;
    stks += '#';
    for (int i = 0; i < s.size(); i++)
    {<!-- -->
        int len = stks.size();
        string output;
        while (isalpha(stks[len - 1])&&stks[len-1]!='i')
        {<!-- -->
            len--;
        }
        char c = stks[len - 1];
        if (OperatorTable[c][s[i]] != '>' && OperatorTable[c][s[i]] != '<' && OperatorTable[c][s[i]] != '=')
        {<!-- -->
            cout << "失败" << endl;
            break;
        }
        if (OperatorTable[c][s[i]] == '<')
        {<!-- -->
            for (int j = i; j < s.size(); j++) {<!-- --> output += s[j]; }
            cout << stks << "     " << '<' << "     " << output << "     " << "移进" << endl;
            stks += s[i];
            continue;
        }
        if (OperatorTable[c][s[i]] == '>')
        {<!-- -->
            for (int j = i; j < s.size(); j++) {<!-- --> output += s[j]; }
            cout << stks << "     " << '>' << "     " << output << "     " << "规约" << endl;
            for (int k = stks.size() - 1; k >= 0; k--)
            {<!-- -->
                if (stks[k] == 'i')
                {<!-- -->
                    stks[k] = 'E';
                    i--;
                    break;
                }
                if ((stks[k] == '*'|| stks[k] == '+') &&stks[k+1]=='E'&&stks[k-1]=='E')
                {<!-- -->
                    stks.erase(k - 1, 3);
                    stks += 'E';
                    i--;
                    break;
                }
                if (stks[k] == '(' && stks[k + 2] == ')')
                {<!-- -->
                    stks.erase(k, 3);
                    stks += 'E';
                    i--;
                    break;
                }
            }
            continue;
        }
        if (OperatorTable[c][s[i]] == '=')
        {<!-- -->
            int num = 0;
            for (int j = i; j < s.size(); j++) {<!-- --> output += s[j]; }
            for (int t = 0; t < stks.size(); t++)
            {<!-- -->
                if (!isalpha(stks[t]))
                {<!-- -->
                    num++;
                }
            }
            if (num==1)
            {<!-- -->
                cout << stks << "     " << '=' << "     " << output << "     " << "接受" << endl;
                break;
            }
            else
            {<!-- -->
                cout << stks << "     " << '<' << "     " << output << "     " << "移进" << endl;
                stks += s[i];
            }
           

        }
       
    }
}

int main()
{<!-- -->
    //初始化
    InputContext();
    //FIRSTVT集
    InitFIRSTVT();
    AllCalFIRSTVT();
    //LASTVT集
    InitLASTVT();
    AllCalLASTVT();
    //算符优先关系表
    CalOperatorTable();
    //分析
    cout << endl;
    cout << "请输入串" << endl;
    string in;
    cin >> in;
    cout << endl;
    GY(in);
    return 0;
}