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
|
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
str []byte
pointer int
error_str string = "Error: 非法的符号串"
withNoSharp string = "Error: 符号串必须以#结尾"
)
// 打印颜色设置
const Red = "\033[31m"
const Green = "\033[32m"
const Reset = "\033[0m"
const Yellow = "\033[33m"
func match(token byte) {
if pointer >= len(str) {
errors(error_str)
return
}
if str[pointer] == token {
pointer++
} else {
errors(error_str)
}
}
func errors(info string) {
fmt.Println(Red, info, Reset)
os.Exit(0)
}
func E() {
fmt.Println("E() -> T() -> G()")
T()
G()
}
func T() {
fmt.Println("T() -> F() -> S()")
F()
S()
}
func G() {
if str[pointer] == '+' {
match('+')
fmt.Println("G() -> + -> T() -> G()")
T()
G()
} else if str[pointer] == '-' {
match('-')
fmt.Println("G() -> - -> T() -> G()")
T()
G()
} else {
fmt.Println("G() -> epsilon")
}
}
func F() {
if str[pointer] == 'i' {
match('i')
fmt.Println("F() -> i")
} else if str[pointer] == '(' {
match('(')
fmt.Println("F() -> ( -> E()")
E()
if pointer < len(str) && str[pointer] == ')' {
match(')')
fmt.Println("F() -> ( -> E() -> )")
} else {
errors("Error: 缺少右括号")
}
} else {
errors("Error: 与i或者(不匹配")
}
}
func S() {
if str[pointer] == '*' {
match('*')
fmt.Println("S() -> * -> F() -> S()")
F()
S()
} else if str[pointer] == '/' {
match('/')
fmt.Println("S() -> / -> F() -> S()")
F()
S()
} else {
fmt.Println("S() -> epsilon")
}
}
func checkSuccess() {
if pointer == len(str)-1 && str[pointer] == '#' {
fmt.Println(Green, string(str), "Success: 符号串匹配成功", Reset)
} else {
errors(error_str)
}
os.Exit(0)
}
func main() {
fmt.Println(Yellow, "编制人: ", Reset)
scanner := bufio.NewScanner(os.Stdin)
fmt.Println(Yellow, "请输入符号串:", Reset)
if scanner.Scan() {
input := scanner.Text()
if !strings.HasSuffix(input, "#") { //判断是否以#结尾
errors(withNoSharp)
return
}
str = []byte(input)
fmt.Println("读入的字符串为:", string(str))
E()
checkSuccess()
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "读取输入时发生错误:", err)
}
}
|