-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.go
More file actions
95 lines (91 loc) · 1.84 KB
/
Copy pathlexer.go
File metadata and controls
95 lines (91 loc) · 1.84 KB
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
package nfa
func StringToTokenSeq(inp string) TokenSeq {
bs := []byte(inp)
out := []Token{}
lastIsVal := false
mi := 0
for i := 0; i < len(bs); i++ {
switch bs[i] {
case '|':
out = append(out, Or)
lastIsVal = false
case '(':
if lastIsVal {
out = append(out, And)
}
lastIsVal = false
out = append(out, LBr)
case ')':
out = append(out, RBr)
lastIsVal = true
case '*':
out = append(out, Repeat)
case '#':
out = append(out, NewMark(mi))
mi += 1
case '\\':
if lastIsVal {
out = append(out, And)
}
lastIsVal = true
out = append(out, CommonCharToken(bs[i+1]))
i++
case '/':
if lastIsVal {
out = append(out, And)
}
lastIsVal = true
out = append(out, Token(SpecialCharMapping[bs[i+1]]))
i++
default:
if lastIsVal {
out = append(out, And)
}
lastIsVal = true
out = append(out, CommonCharToken(bs[i]))
}
}
return TokenSeq(out)
}
func InfixToPostfix(ts TokenSeq) TokenSeq {
// out := []Token{}
tmpStack := Stack[Token]{s: make([]Token, 0)}
opStack := Stack[Token]{s: make([]Token, 0)}
for _, t := range ts {
// fmt.Printf("%v %v %v\n", t.String(), TokenSeq(opStack.s), TokenSeq(tmpStack.s))
if t.IsChar() {
tmpStack.Push(t)
} else if t.Type() == LBR {
opStack.Push(t)
} else if t.Type() == RBR {
for {
_t := opStack.Pop()
if _t.Type() == LBR {
break
}
tmpStack.Push(_t)
}
} else {
for !opStack.Empty() {
if uint8(t.Type()) <= uint8(opStack.Curr().Type()) {
_t := opStack.Pop()
tmpStack.Push(_t)
} else {
break
}
}
opStack.Push(t)
}
// fmt.Printf("> %v %v\n", TokenSeq(opStack.s), TokenSeq(tmpStack.s))
}
valCount := 0
for !opStack.Empty() {
t := opStack.Pop()
if t.IsChar() {
valCount += 1
} else if t.Type() == OR {
}
tmpStack.Push(t)
}
return TokenSeq(tmpStack.s)
}