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
|
package grammar
import (
"strings"
"testing"
"github.com/nihei9/vartan/spec"
)
type follow struct {
nonTermText string
symbols []string
eof bool
}
func TestFollowSet(t *testing.T) {
tests := []struct {
caption string
src string
follow []follow
}{
{
caption: "productions contain only non-empty productions",
src: `
expr
: expr add term
| term
;
term
: term mul factor
| factor
;
factor
: l_paren expr r_paren
| id
;
add: "\+";
mul: "\*";
l_paren: "\(";
r_paren: "\)";
id: "[A-Za-z_][0-9A-Za-z_]*";
`,
follow: []follow{
{nonTermText: "expr'", symbols: []string{}, eof: true},
{nonTermText: "expr", symbols: []string{"add", "r_paren"}, eof: true},
{nonTermText: "term", symbols: []string{"add", "mul", "r_paren"}, eof: true},
{nonTermText: "factor", symbols: []string{"add", "mul", "r_paren"}, eof: true},
},
},
{
caption: "productions contain an empty start production",
src: `
s
:
;
`,
follow: []follow{
{nonTermText: "s'", symbols: []string{}, eof: true},
{nonTermText: "s", symbols: []string{}, eof: true},
},
},
{
caption: "productions contain an empty production",
src: `
s
: foo
;
foo
:
;
`,
follow: []follow{
{nonTermText: "s'", symbols: []string{}, eof: true},
{nonTermText: "s", symbols: []string{}, eof: true},
{nonTermText: "foo", symbols: []string{}, eof: true},
},
},
{
caption: "a start production contains a non-empty alternative and empty alternative",
src: `
s
: foo
|
;
foo: "foo";
`,
follow: []follow{
{nonTermText: "s'", symbols: []string{}, eof: true},
{nonTermText: "s", symbols: []string{}, eof: true},
},
},
{
caption: "a production contains non-empty alternative and empty alternative",
src: `
s
: foo
;
foo
: bar
|
;
bar: "bar";
`,
follow: []follow{
{nonTermText: "s'", symbols: []string{}, eof: true},
{nonTermText: "s", symbols: []string{}, eof: true},
{nonTermText: "foo", symbols: []string{}, eof: true},
},
},
}
for _, tt := range tests {
t.Run(tt.caption, func(t *testing.T) {
flw, gram := genActualFollow(t, tt.src)
for _, ttFollow := range tt.follow {
sym, ok := gram.symbolTable.toSymbol(ttFollow.nonTermText)
if !ok {
t.Fatalf("a symbol '%v' was not found", ttFollow.nonTermText)
}
actualFollow, err := flw.find(sym)
if err != nil {
t.Fatalf("failed to get a FOLLOW entry; non-terminal symbol: %v (%v), error: %v", ttFollow.nonTermText, sym, err)
}
expectedFollow := genExpectedFollowEntry(t, ttFollow.symbols, ttFollow.eof, gram.symbolTable)
testFollow(t, actualFollow, expectedFollow)
}
})
}
}
func genActualFollow(t *testing.T, src string) (*followSet, *Grammar) {
ast, err := spec.Parse(strings.NewReader(src))
if err != nil {
t.Fatal(err)
}
b := GrammarBuilder{
AST: ast,
}
gram, err := b.Build()
if err != nil {
t.Fatal(err)
}
fst, err := genFirstSet(gram.productionSet)
if err != nil {
t.Fatal(err)
}
flw, err := genFollowSet(gram.productionSet, fst)
if flw == nil {
t.Fatal("genFollow returned nil without any error")
}
return flw, gram
}
func genExpectedFollowEntry(t *testing.T, symbols []string, eof bool, symTab *symbolTable) *followEntry {
t.Helper()
entry := newFollowEntry()
if eof {
entry.addEOF()
}
for _, sym := range symbols {
symID, _ := symTab.toSymbol(sym)
if symID.isNil() {
t.Fatalf("a symbol '%v' was not found", sym)
}
entry.add(symID)
}
return entry
}
func testFollow(t *testing.T, actual, expected *followEntry) {
if actual.eof != expected.eof {
t.Errorf("eof is mismatched; want: %v, got: %v", expected.eof, actual.eof)
}
if len(actual.symbols) != len(expected.symbols) {
t.Fatalf("unexpected symbol count of a FOLLOW entry; want: %v, got: %v", expected.symbols, actual.symbols)
}
for eSym := range expected.symbols {
if _, ok := actual.symbols[eSym]; !ok {
t.Fatalf("invalid FOLLOW entry; want: %v, got: %v", expected.symbols, actual.symbols)
}
}
}
|