-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
238 lines (212 loc) · 3.94 KB
/
parser.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"unicode"
)
//assign each operator a token
const INT_LIT = 10
const IDENT = 11
const ASSIGN_OP = 20
const ADD_OP = 21
const SUB_OP = 22
const MULT_OP = 23
const DIV_OP = 24
const LEFT_PAREN = 25
const RIGHT_PAREN = 26
var charClass int
var nextChar rune
var lexeme [100]rune
var lexLen int
var token int
var nextToken int
var in_fp *bufio.Scanner
var isAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
const LETTER = 0
const DIGIT = 1
const UNKNOWN = 99
const EOF = 98
//function to lookup operators and return the token that they were assigned
func lookup(ch rune) int {
switch ch {
case '(':
addChar()
nextToken = LEFT_PAREN
break
case ')':
addChar()
nextToken = RIGHT_PAREN
break
case '+':
addChar()
nextToken = ADD_OP
break
case '-':
addChar()
nextToken = SUB_OP
break
case '*':
addChar()
nextToken = MULT_OP
break
case '/':
addChar()
nextToken = DIV_OP
break
default:
addChar()
nextToken = EOF
break
}
return nextToken
}
//a function to add Char to the lexeme
func addChar() {
lexeme[lexLen] = nextChar
lexLen++
lexeme[lexLen] = 0
}
//function to get next character of input and determine its character class
func getChar() {
in_fp.Scan()
nextString := in_fp.Text()
if len(nextString) > 0 {
nextChar = []rune(nextString)[0]
if unicode.IsLetter(nextChar) /* (65 <= nextChar && nextChar <= 90) || (97 <= nextChar && nextChar <= 122)*/ {
charClass = LETTER
} else if unicode.IsDigit(nextChar) {
charClass = DIGIT
} else {
charClass = UNKNOWN
}
} else {
charClass = EOF
}
}
//function to call getChar until it returns a non-whitespace character
func getNonBlank() {
for unicode.IsSpace(nextChar) && charClass != EOF {
getChar()
}
}
//a lexical analyzer for arithmetic expressions
func lex() int {
lexLen = 0
getNonBlank()
switch charClass {
case LETTER:
addChar()
getChar()
for charClass == LETTER || charClass == DIGIT {
addChar()
getChar()
}
nextToken = IDENT
break
case DIGIT:
addChar()
getChar()
for charClass == DIGIT {
addChar()
getChar()
}
nextToken = INT_LIT
break
case UNKNOWN:
lookup(nextChar)
getChar()
break
case EOF:
nextToken = EOF
lexLen = 3
lexeme[0] = 'E'
lexeme[1] = 'O'
lexeme[2] = 'F'
lexeme[3] = 0
break
}
fmt.Printf("Next token is: ")
fmt.Print(nextToken)
fmt.Printf(", Next lexeme is: ")
for i := 0; i < lexLen; i++ {
fmt.Printf(string(lexeme[i]))
}
fmt.Println()
return nextToken
}
// parses strings in the language generated by the rule:
// <expr> -> <term> {(+ | -) <term>}
func expr() {
fmt.Printf("Enter <expr>\n")
// parse the first term
term()
for nextToken == ADD_OP || nextToken == SUB_OP {
lex()
term()
}
fmt.Printf("Exit <expr>\n")
}
/*
parses strings in the language generated by the rule :
<term> -> <factor> {(* | /) <factor>}
*/
func term() {
fmt.Printf("Enter <term>\n")
factor()
for nextToken == MULT_OP || nextToken == DIV_OP {
lex()
factor()
}
fmt.Printf("Exit <term>\n")
}
/*
parses strings in the language generated by the rule:
<factor> -> id | int_constant | ( <expr> )
*/
func factor() {
fmt.Printf("Enter <factor>\n")
// determine which RHS
if nextToken == IDENT || nextToken == INT_LIT {
lex()
/*
if the RHS is ( <expr> ), call lex to pass over the left parentheses, call expr, and check for the right parentheses
*/
} else {
if nextToken == LEFT_PAREN {
lex()
expr()
if nextToken == RIGHT_PAREN {
lex()
} else {
errors.New("oops")
}
} else {
errors.New("oops")
}
}
fmt.Printf("Exit <factor>\n")
}
func parse() {
expr()
}
func main() {
filename := "test.txt"
filebuffer, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
inputdata := string(filebuffer)
in_fp = bufio.NewScanner(strings.NewReader(inputdata))
in_fp.Split(bufio.ScanRunes)
for charClass != EOF {
getChar()
lex()
parse()
}
}