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
|
function func4() {
var tmp = document.getElementById('qwe');
var str = tmp.value;
var fstr = '';
var dict = {
'S' : {
'm' : 'AT',
'(' : 'AT'
},
'T' : {
'+' : '+AT',
')' : '$',
'#' : '$',
},
'A' : {
'm' : 'BU',
'(' : 'BU'
},
'U' : {
'+' : '$',
'*' : '*BU',
')' : '$',
'#' : '$'
},
'B' : {
'm' : 'm',
'(' : '(S)'
}
};
no_tm = ['m', '+', '*', '(', ')', '#'];
function PRINT(no, stk, s, pd) {
var tchar = stk.slice(stk.length - 1, stk.length);
if(no_tm.indexOf(tchar) == -1){
while(stk.length < 20){
stk += ' ';
}
while (s.length < 20){
s = ' ' + s;
}
fstr += no;
fstr += ('\t' + stk + s + '\t\t\t' + tchar + '->' + pd + '\n');
}else{
while(stk.length < 20){
stk += ' ';
}
while (s.length < 20){
s = ' ' + s;
}
fstr += no;
fstr += ('\t' + stk + s + '\t\t\t' + pd + '\n');
}
}
var num = 1;
var ind = 0;
var stack = '#S';
while (stack.length != 1){
if(stack.slice(stack.length - 1, stack.length) == str[ind]){
PRINT(num, stack, str, '\'' + str[ind] + '\' match');
tt_str = str;
str = '';
for(var j = 0; j < ind + 1; ++j){
str += ' ';
}
str += tt_str.slice(ind + 1, tt_str.length);
ind++;
num++;
stack = stack.slice(0, stack.length - 1);
}else if(no_tm.indexOf(stack.slice(stack.length - 1, stack.length)) != -1){
PRINT(num, stack, str, '[ERROR] not match');
tmp = document.getElementById('zxc');
tmp.value = fstr;
throw new Error('error');
}else if(!(str[ind] in dict[stack.slice(stack.length - 1, stack.length)])){
PRINT(num, stack, str, '[ERROR] not match');
tmp = document.getElementById('zxc');
tmp.value = fstr;
throw new Error('error');
}else{
var prod = dict[stack.slice(stack.length - 1, stack.length)][str[ind]];
PRINT(num, stack, str, prod);
num++;
stack = stack.slice(0, stack.length - 1);
if(prod != '$'){
stack += prod.split("").reverse().join("");
}
}
}
PRINT(num, stack, str, 'acc');
tmp = document.getElementById('zxc');
tmp.value = fstr;
}
|