-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWrite a C program to find the leading terminals.txt
More file actions
64 lines (56 loc) · 1.79 KB
/
Copy pathWrite a C program to find the leading terminals.txt
File metadata and controls
64 lines (56 loc) · 1.79 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
#include<stdio.h>
char arr[18][3] ={{'E', '+', 'F'},{'E', '*', 'F'},{'E', '(', 'F'}, {'E', ')', 'F'},{'E', 'i', 'F'},{'E', '$', 'F'},
{'F', '+', 'F'},{'F', '*', 'F'},{'F', '(', 'F'},{'F', ')', 'F'},{'F', 'i', 'F'},{'F', '$', 'F'}, {'T', '+', 'F'},
{'T', '*', 'F'}, {'T', '(', 'F'},{'T', ')', 'F'},{'T', 'i', 'F'},{'T', '$', 'F'}};
char prod[] = "EETTFF";
char res[6][3] ={ {'E', '+', 'T'}, {'T', '\0'}, {'T', '*', 'F'}, {'F', '\0'}, {'(', 'E', ')'}, {'i', '\0'}};
char stack [5][2];
int top = -1;
void install(char pro, char re) {
int i;
for (i = 0; i < 18; ++i) {
if (arr[i][0] == pro && arr[i][1] == re) {
arr[i][2] = 'T';
break;
}
}
++top;
stack[top][0] = pro;
stack[top][1] = re;
}
int main() {
int i = 0, j;
char pro, re, pri = ' ';
for (i = 0; i < 6; ++i) {
for (j = 0; j < 3 && res[i][j] != '\0'; ++j) {
if (res[i][j] == '+' || res[i][j] == '*' || res[i][j] == '(' || res[i][j] == ')' || res[i][j] == 'i' || res[i][j] == '$') {
install(prod[i], res[i][j]);
break;
}
}
}
while (top >= 0) {
pro = stack[top][0];
re = stack[top][1];
--top;
for (i = 0; i < 6; ++i) {
if (res[i][0] == pro && res[i][0] != prod[i]) {
install(prod[i], re);
}
}
}
for (i = 0; i < 18; ++i) {
printf("\n\t");
for (j = 0; j < 3; ++j)
printf("%c\t", arr[i][j]);
}
printf("\n\n");
for (i = 0; i < 18; ++i) {
if (pri != arr[i][0]) {
pri = arr[i][0];
printf("\n\t%c -> ", pri);
}
if (arr[i][2] == 'T')
printf("%c ", arr[i][1]);
}
}