-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
218 lines (193 loc) · 4.75 KB
/
parse_test.go
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
package compiler
import (
"strconv"
"strings"
"testing"
)
func parseTestCases(s string) [][]Symbol {
var rst [][]Symbol
var chunk []Symbol
nums := strings.Fields(s)
for i := 0; i < len(nums); i += 2 {
id, err := strconv.ParseInt(nums[i], 10, 0)
if err != nil {
panic("check your input text")
}
att, err := strconv.ParseInt(nums[i+1], 10, 0)
if err != nil {
panic("check your input text")
}
sym := Symbol{SymbolID(id), int(att)}
chunk = append(chunk, sym)
if sym.ID == End {
rst = append(rst, chunk)
chunk = make([]Symbol, 0)
}
}
return rst
}
func TestDFSParser(t *testing.T) {
var suc, fail int
t.Logf("\n\n\n\nLRE\n\n\n\n\n")
suc, fail = 0, 0
testCases1 := parseTestCases(topDownParsingInput1)
for _, testcase := range testCases1 {
p := NewDFSParser(testcase, t, LRE, false)
s := p.RunDFS()
if s {
suc++
} else {
fail++
}
}
t.Logf("\nLRE input1 # of grammatical: %v\n", suc)
t.Logf("\nLRE input1 # of ungrammatical: %v\n\n\n", fail)
suc, fail = 0, 0
testCases2 := parseTestCases(topDownParsingInput2)
for _, testcase := range testCases2 {
p := NewDFSParser(testcase, t, LRE, false)
s := p.RunDFS()
if s {
suc++
} else {
fail++
}
}
t.Logf("\nLRE input2 # of grammatical: %v\n", suc)
t.Logf("\nLRE input2 # of ungrammatical: %v\n\n\n", fail)
suc, fail = 0, 0
t.Logf("\n\n\n\nLL1\n\n\n\n\n")
for _, testcase := range testCases1 {
p := NewDFSParser(testcase, t, LL1, false)
s := p.RunDFS()
if s {
suc++
} else {
fail++
}
}
t.Logf("\nLL1 input1 # of grammatical: %v\n", suc)
t.Logf("\nLL1 input1 # of ungrammatical: %v\n\n\n", fail)
suc, fail = 0, 0
for _, testcase := range testCases2 {
p := NewDFSParser(testcase, t, LL1, false)
s := p.RunDFS()
if s {
suc++
} else {
fail++
}
}
t.Logf("\nLL1 input2 # of grammatical: %v\n", suc)
t.Logf("\nLL1 input2 # of ungrammatical: %v\n\n\n", fail)
}
func TestPredictiveParsing(t *testing.T) {
suite1 := parseTestCases(topDownParsingInput1)
suite2 := parseTestCases(topDownParsingInput2)
var suc, fail int
for _, tc := range suite1 {
p := NewPredictiveParser(tc, t, false)
s := p.RunPredictiveParsing()
if s {
suc++
} else {
fail++
}
}
t.Logf("\n[PredictiveParser] input1 # of grammatical: %v\n", suc)
t.Logf("\n[PredictiveParser] input1 # of ungrammatical: %v\n\n", fail)
suc, fail = 0, 0
for _, tc := range suite2 {
p := NewPredictiveParser(tc, t, false)
s := p.RunPredictiveParsing()
if s {
suc++
} else {
fail++
}
}
t.Logf("\n[PredictiveParser] input2 # of grammatical: %v\n", suc)
t.Logf("\n[PredictiveParser] input2 # of ungrammatical: %v\n\n", fail)
}
func printFirstSets(t *testing.T, g Productions) {
for nt := range g {
for _, p := range g[nt] {
t.Logf("firstSet for p %v on nt %v:\n", p, nt)
t.Logf("%v\n", FirstSet(p.RHS, g))
}
}
}
func printFollowSet(t *testing.T, g Productions) {
nts := NonTerminals()
for _, nt := range nts {
fo := FollowSet(nt)
t.Logf("followset for nt %v:\n%v\n", nt, fo)
}
}
func TestDetailedPP(t *testing.T) {
idEqualNum := `2 2 10 0 1 33 99 0`
syms := parseTestCases(idEqualNum)[0]
p := NewPredictiveParser(syms, t, true)
p.RunPredictiveParsing()
}
func TestFirstFollowPredictiveTable(t *testing.T) {
// print firstset and followset and matrix
t.Log("\n\n\nFirstSets, FollowSets, PredictiveTable\n\n")
emptyParser := NewPredictiveParser([]Symbol{}, t, false)
printFirstSets(t, emptyParser.grammar)
printFollowSet(t, emptyParser.grammar)
t.Log(M)
}
// tom $$$
//
// not true $$$
//
// dick and harry or tom or true $$$
//
// tom or ( not ( dick ) or ( harry ) ) $$$
//
// 7 > 3 and not 8 = 11 $$$
//
// 7 >
// 3 and not
// 8 = 11
// $$$
var topDownParsingInput1 = `2 1 99 0
7 0 8 0 99 0
2 9 5 0 2 33 6 0 2 1 6 0 8 0 99 0
2 1 6 0 3 0 7 0 3 0 2 9 4 0 6 0 3 0 2 33 4 0 4 0 99 0
1 7 11 0 1 3 5 0 7 0 1 8 10 0 1 11 99 0
1 7 11 0
1 3 5 0 7 0
1 8 10 0 1 11
99 0`
// tom = 33 $$$
// 2 2 10 0 1 33 99 0
// not bill or not harry $$$
// 7 0 2 3 6 0 7 0 2 4 99 0
// ( bill and tom and harry and tom and mary = 66 ) $$$
// 3 0 2 3 5 0 2 2 5 0 2 4 5 0 2 2 5 0 2 5 10 0 1 66 4 0 99 0
// ( not not tom < harry ) $$$
// 3 0 7 0 7 0 2 2 11 0 2 4 4 0 99 0
// tom < harry = dick and mary $$$
// 2 2 11 0 2 4 10 0 2 6 5 0 2 5 99 0
// ( tom and ( not harry ) $$$
// 3 0 2 2 5 0 3 0 7 0 2 4 4 0 99 0
// true ) $$$
// 8 0 4 0 99 0
// 123 > > harry $$$
// 1 123 12 0 12 0 2 4 99 0
// false friend $$$
// 9 0 2 7 99 0
// false = not true $$$
// 9 0 10 0 7 0 8 0 99 0
var topDownParsingInput2 = `2 2 10 0 1 33 99 0
7 0 2 3 6 0 7 0 2 4 99 0
3 0 2 3 5 0 2 2 5 0 2 4 5 0 2 2 5 0 2 5 10 0 1 66 4 0 99 0
3 0 7 0 7 0 2 2 11 0 2 4 4 0 99 0
2 2 11 0 2 4 10 0 2 6 5 0 2 5 99 0
3 0 2 2 5 0 3 0 7 0 2 4 4 0 99 0
8 0 4 0 99 0
1 123 12 0 12 0 2 4 99 0
9 0 2 7 99 0
9 0 10 0 7 0 8 0 99 0 `