aboutsummaryrefslogtreecommitdiff
path: root/grammar/first_test.go
blob: 578825e34b8bedf4c52aec082a064562414a89e6 (plain) (blame)
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 grammar

import (
	"strings"
	"testing"

	"github.com/nihei9/vartan/spec"
)

type first struct {
	lhs     string
	num     int
	dot     int
	symbols []string
	empty   bool
}

func TestGenFirst(t *testing.T) {
	tests := []struct {
		caption string
		src     string
		first   []first
	}{
		{
			caption: "productions contain only non-empty productions",
			src: `
%name test

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_]*";
`,
			first: []first{
				{lhs: "expr'", num: 0, dot: 0, symbols: []string{"l_paren", "id"}},
				{lhs: "expr", num: 0, dot: 0, symbols: []string{"l_paren", "id"}},
				{lhs: "expr", num: 0, dot: 1, symbols: []string{"add"}},
				{lhs: "expr", num: 0, dot: 2, symbols: []string{"l_paren", "id"}},
				{lhs: "expr", num: 1, dot: 0, symbols: []string{"l_paren", "id"}},
				{lhs: "term", num: 0, dot: 0, symbols: []string{"l_paren", "id"}},
				{lhs: "term", num: 0, dot: 1, symbols: []string{"mul"}},
				{lhs: "term", num: 0, dot: 2, symbols: []string{"l_paren", "id"}},
				{lhs: "term", num: 1, dot: 0, symbols: []string{"l_paren", "id"}},
				{lhs: "factor", num: 0, dot: 0, symbols: []string{"l_paren"}},
				{lhs: "factor", num: 0, dot: 1, symbols: []string{"l_paren", "id"}},
				{lhs: "factor", num: 0, dot: 2, symbols: []string{"r_paren"}},
				{lhs: "factor", num: 1, dot: 0, symbols: []string{"id"}},
			},
		},
		{
			caption: "productions contain the empty start production",
			src: `
%name test

s
    :
    ;
`,
			first: []first{
				{lhs: "s'", num: 0, dot: 0, symbols: []string{}, empty: true},
				{lhs: "s", num: 0, dot: 0, symbols: []string{}, empty: true},
			},
		},
		{
			caption: "productions contain an empty production",
			src: `
%name test

s
    : foo bar
    ;
foo
    :
    ;
bar: "bar";
`,
			first: []first{
				{lhs: "s'", num: 0, dot: 0, symbols: []string{"bar"}, empty: false},
				{lhs: "s", num: 0, dot: 0, symbols: []string{"bar"}, empty: false},
				{lhs: "foo", num: 0, dot: 0, symbols: []string{}, empty: true},
			},
		},
		{
			caption: "a start production contains a non-empty alternative and empty alternative",
			src: `
%name test

s
    : foo
    |
    ;
foo: "foo";
`,
			first: []first{
				{lhs: "s'", num: 0, dot: 0, symbols: []string{"foo"}, empty: true},
				{lhs: "s", num: 0, dot: 0, symbols: []string{"foo"}},
				{lhs: "s", num: 1, dot: 0, symbols: []string{}, empty: true},
			},
		},
		{
			caption: "a production contains non-empty alternative and empty alternative",
			src: `
%name test

s
    : foo
    ;
foo
    : bar
    |
    ;
bar: "bar";
`,
			first: []first{
				{lhs: "s'", num: 0, dot: 0, symbols: []string{"bar"}, empty: true},
				{lhs: "s", num: 0, dot: 0, symbols: []string{"bar"}, empty: true},
				{lhs: "foo", num: 0, dot: 0, symbols: []string{"bar"}},
				{lhs: "foo", num: 1, dot: 0, symbols: []string{}, empty: true},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.caption, func(t *testing.T) {
			fst, gram := genActualFirst(t, tt.src)

			for _, ttFirst := range tt.first {
				lhsSym, ok := gram.symbolTable.toSymbol(ttFirst.lhs)
				if !ok {
					t.Fatalf("a symbol was not found; symbol: %v", ttFirst.lhs)
				}

				prod, ok := gram.productionSet.findByLHS(lhsSym)
				if !ok {
					t.Fatalf("a production was not found; LHS: %v (%v)", ttFirst.lhs, lhsSym)
				}

				actualFirst, err := fst.find(prod[ttFirst.num], ttFirst.dot)
				if err != nil {
					t.Fatalf("failed to get a FIRST set; LHS: %v (%v), num: %v, dot: %v, error: %v", ttFirst.lhs, lhsSym, ttFirst.num, ttFirst.dot, err)
				}

				expectedFirst := genExpectedFirstEntry(t, ttFirst.symbols, ttFirst.empty, gram.symbolTable)

				testFirst(t, actualFirst, expectedFirst)
			}
		})
	}
}

func genActualFirst(t *testing.T, src string) (*firstSet, *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)
	}
	if fst == nil {
		t.Fatal("genFiest returned nil without any error")
	}

	return fst, gram
}

func genExpectedFirstEntry(t *testing.T, symbols []string, empty bool, symTab *symbolTable) *firstEntry {
	t.Helper()

	entry := newFirstEntry()
	if empty {
		entry.addEmpty()
	}
	for _, sym := range symbols {
		symSym, ok := symTab.toSymbol(sym)
		if !ok {
			t.Fatalf("a symbol was not found; symbol: %v", sym)
		}
		entry.add(symSym)
	}

	return entry
}

func testFirst(t *testing.T, actual, expected *firstEntry) {
	if actual.empty != expected.empty {
		t.Errorf("empty is mismatched\nwant: %v\ngot: %v", expected.empty, actual.empty)
	}

	if len(actual.symbols) != len(expected.symbols) {
		t.Fatalf("invalid FIRST set\nwant: %+v\ngot: %+v", expected.symbols, actual.symbols)
	}

	for eSym := range expected.symbols {
		if _, ok := actual.symbols[eSym]; !ok {
			t.Fatalf("invalid FIRST set\nwant: %+v\ngot: %+v", expected.symbols, actual.symbols)
		}
	}
}