-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_test.go
75 lines (67 loc) · 2.06 KB
/
lexer_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
package nfa
import "testing"
func TestLexar(t *testing.T) {
var DigitalTransCondict = TransitCond{}
for i := '0'; i <= '9'; i++ {
DigitalTransCondict = DigitalTransCondict.Allow(byte(i))
}
AddExtend('d', "{0-9}", DigitalTransCondict)
var AlphabetTransCondict = TransitCond{}
for i := 'a'; i <= 'z'; i++ {
AlphabetTransCondict = AlphabetTransCondict.Allow(byte(i))
}
for i := 'A'; i <= 'Z'; i++ {
AlphabetTransCondict = AlphabetTransCondict.Allow(byte(i))
}
AddExtend('a', "{a-z,A-Z}", AlphabetTransCondict)
var AnyTransCondict = TransitCond{}
for i := 0; i <= 255; i++ {
AnyTransCondict = AnyTransCondict.Allow(byte(i))
}
AddExtend('*', "{any}", AnyTransCondict)
inp := `ab(ab|(a\*c|a*d))e`
tokens := StringToTokenSeq(inp)
t.Log(tokens.String())
if tokens.String() != "a[&]b[&][(]a[&]b[|][(]a[&]*[&]c[|]a[*][&]d[)][)][&]e" {
t.FailNow()
}
}
func TestInfixToPostfix(t *testing.T) {
var DigitalTransCondict = TransitCond{}
for i := '0'; i <= '9'; i++ {
DigitalTransCondict = DigitalTransCondict.Allow(byte(i))
}
AddExtend('d', "{0-9}", DigitalTransCondict)
var AlphabetTransCondict = TransitCond{}
for i := 'a'; i <= 'z'; i++ {
AlphabetTransCondict = AlphabetTransCondict.Allow(byte(i))
}
for i := 'A'; i <= 'Z'; i++ {
AlphabetTransCondict = AlphabetTransCondict.Allow(byte(i))
}
AddExtend('a', "{a-z,A-Z}", AlphabetTransCondict)
var AnyTransCondict = TransitCond{}
for i := 0; i <= 255; i++ {
AnyTransCondict = AnyTransCondict.Allow(byte(i))
}
AddExtend('*', "{any}", AnyTransCondict)
inp := `ab(ab|(a\*c|a*d))e`
tokens := StringToTokenSeq(inp)
tokens = InfixToPostfix(tokens)
if tokens.String() != "ab[&]ab[&]a*[&]c[&]a[*]d[&][|][|][&]e[&]" {
t.FailNow()
}
inp = `a/ b(a/ b|(a\*c|a*d))e`
tokens = StringToTokenSeq(inp)
tokens = InfixToPostfix(tokens)
if tokens.String() != "a{ε}[&]b[&]a{ε}[&]b[&]a*[&]c[&]a[*]d[&][|][|][&]e[&]" {
t.FailNow()
}
inp = `/d/a/ /*`
tokens = StringToTokenSeq(inp)
tokens = InfixToPostfix(tokens)
if tokens.String() != "{0-9}{a-z,A-Z}[&]{ε}[&]{any}[&]" {
t.Logf(tokens.String())
t.FailNow()
}
}