-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_c_sytax.go
163 lines (147 loc) · 4 KB
/
print_c_sytax.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
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
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
//"time"
"unicode"
)
func analyse_c_sytax(s string, sk *Stack) error {
con := -1
tmp := s
for con == -1 {
s, con = get_identifier(tmp, sk)
tmp = s
}
if con == 1 {
return fmt.Errorf("%s", "未找到标识符")
}
if sk.NumOfStack() == 1 {
return fmt.Errorf("%s", "语法错误")
}
id := sk.Pop()
fmt.Printf("%s是一个", id)
for sk.Empty() != true {
fmt.Printf("+++%s+++\n", sk.Pop())
}
return nil
}
func get_identifier(s string, sk *Stack) (rs string, ri int) {
i := 0
if s == "" {
ri = 1
return
}
iLen := len(s)
// fmt.Printf("@@@%s@@@\n", s)
con := true
for i < iLen && con {
switch {
case unicode.IsLetter(rune(s[i])), unicode.IsNumber(rune(s[i])), s[i] == '_':
i++
for i < iLen {
if unicode.IsLetter(rune(s[i])) || unicode.IsNumber(rune(s[i])) || s[i] == '_' {
i++
} else {
break
}
}
con = false
case s[i] == ' ', s[i] == '\t':
i++
default:
con = false
i++
}
}
// time.Sleep(time.Second)
ts := s[:i]
s1 := strings.Trim(ts, " ")
s = s[i:]
sk.Push(s1)
rs = s
switch s1 {
case "":
ri = -1
case "char", "short", "int", "long", "float", "double", "auto", "signed", "unsigned", "const", "volatile", "static", "enum", "struct", "union", "void":
ri = -1
default:
if !unicode.IsLetter(rune(s1[0])) && s1[0] != '_' {
ri = -1
} else {
ri = 0
}
}
return
// fmt.Printf("<<<%d|%s|%s|%d>>>\n", iLen, ts[:i], ts[i:], i)
}
type Stack struct {
cap int
data []string
pos int
}
func NewStack(cap int) *Stack {
var v Stack
v.data = make([]string, cap)
v.cap = cap
v.pos = -1
return &v
}
func (this *Stack) NumOfStack() int {
return this.pos + 1
}
func (this *Stack) Push(s string) {
if !this.Full() {
this.pos++
this.data[this.pos] = s
}
}
func (this *Stack) Pop() string {
var s string
if !this.Empty() {
s = this.data[this.pos]
this.pos--
}
return s
}
func (this *Stack) Empty() bool {
return this.pos == -1
}
func (this *Stack) Full() bool {
return this.pos == this.cap-1
}
func (this *Stack) ClearStack() {
this.pos = -1
}
func (this *Stack) Destory() {
this.data = nil
this.cap = 0
this.pos = -1
runtime.GC()
}
func main() {
x := NewStack(1024)
defer x.Destory()
LOOP:
for {
fmt.Fprintf(os.Stderr, "%s", "Please input data: ")
rin := bufio.NewReader(os.Stdin)
s, err := rin.ReadString('\n')
if err != nil {
break
}
ss := strings.Trim(s, "\n\r")
// ss := string(s[:len(s)-1])
switch strings.ToUpper(ss) {
case "EXIT", "QUIT", "Q":
break LOOP
}
// x.Push(s)
if err := analyse_c_sytax(ss, x); err != nil {
x.ClearStack()
fmt.Printf("Error: %v\n", err)
}
}
}