-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
129 lines (116 loc) · 3.65 KB
/
main.java
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
package lr.tfya.com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
static int i = 0;
static Stack<Integer> stack = new Stack<>();
static String lenta;
final static String e = "Error";
static String[][] table = {
{"o","S2",e,e,"S3",e,"S4","S11",e},//1
{e,e,e,e,e,e,e,e,"R1"},//2
{e,e,e,e,e,e,e,e,"R2"},//3
{e,e,"S5",e,e,e,e,"S6","R5"},//4
{e,e,e,e,e,e,e,e,"R3"},//5
{e,e,e,e,e,e,"S7",e,e},//6
{e,e,e,e,e,e,e,"S8",e},//7
{e,e,e,e,e,e,"S9",e,e},//8
{e,e,"S10",e,e,e,e,"S6","R5"},//9
{e,e,e,e,e,e,e,e,"R4"},//10
{e,e,e,e,e,"S12","S13",e,"R8"},//11
{e,e,e,e,e,e,e,e,"R6"},//12
{e,e,e,e,e,e,"S14",e,e},//13
{e,e,e,e,"S15",e,e,"S11",e},//14
{e,e,e,e,e,e,e,e,"R7"}//15
};
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input line: ");
lenta = input.readLine() + "$";
stack.push(0);
while(true){
System.out.println("Lenta = "+lenta);
//System.out.println("Stack = "+stack);
String command = table[stack.peek()][getCol(String.valueOf(lenta.charAt(i)))];
System.out.println("table["+(stack.peek()+1)+"]["+lenta.charAt(i)+"] = "+command);
if(command.startsWith("S")){
// shift
shift(Integer.parseInt(command.substring(1)));
}
else if(command.startsWith("R")){
// reduction
reduction(Integer.parseInt(command.substring(1)));
}
else if(command=="o"){
// Допуск
System.out.println("String is true");
break;
}
else if(command==e){
// Ошибка
System.out.println(e);
break;
}
else { // never
System.out.println("index of table is not correct");
break;
}
}
}
/* Получить номер колонки */
static int getCol(String chr){
return "ABZCDXab$".indexOf(chr);
}
/* shift */
static void shift(int rule){
System.out.println("Shift -> "+rule);
stack.push(rule-1);
i++;
}
/* reduction */
static void reduction(int rule){
System.out.println("Reduction -> "+rule);
switch (rule-1){
case 0:
drop(1);
pushAt("A");
break;
case 1:
drop(1);
pushAt("A");
break;
case 2:
drop(2);
pushAt("B");
break;
case 3:
drop(5);
pushAt("Z");
break;
case 4:
pushAt("Z");
break;
case 5:
drop(2);
pushAt("D");
break;
case 6:
drop(3);
pushAt("X");
break;
case 7:
pushAt("X");
break;
}
}
/* Удалить N символов из стека */
static void drop(int n){
for(int k=0;k<n;k++) stack.pop();
}
/* поместить в ленту */
static void pushAt(String chr){
lenta = lenta.substring(0,i)+chr+lenta.substring(i);
}
}