-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
313 lines (282 loc) · 8.16 KB
/
parser.py
File metadata and controls
313 lines (282 loc) · 8.16 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import re
import sys
#re contains the following useful methods:
#compile
#match
#search
#findall
#and support for groups
inputString = ''
nextToken = ''
progNameFound = False
termlist = ["program", "begin", "end", "read", "write", "if", "then", "else", "while", "do", ":=", ";", ',', '(', ')']
def lex():
global inputString
global nextToken
global progNameFound
#print (inputString + " this is the input string in lex")
if (inputString == ''):
print("Syntax Error: end token expected")
sys.exit(0)
if ((inputString == '') & (nextToken == '')):
print("Syntax Error: inputString is equal to ''")
sys.exit(0)
if (nextToken == '<progname>'): #Done just to make sure that a single capital letter variable can still work
progNameFound = True #and it will find <variable> instead of <progname>
p = re.compile('(\d*?) ') #Constant Finding
m = p.match(inputString)
if (m != None):
nextToken = '<constant>'
inputString = inputString.replace(m.group(1) + ' ', '', 1)
return
p = re.compile('([A-Z].*?) ') #ProgName Finding
m = p.match(inputString)
if ((m != None) & (not progNameFound)):
nextToken = '<progname>'
inputString = inputString.replace(m.group(1) + ' ', '', 1)
return
p = re.compile('([=<>][>=]?) ') #Relational Operator Finding
m = p.match(inputString)
if (m != None):
nextToken = '<relational_operator>'
inputString = inputString.replace(m.group(1) + ' ', '', 1)
return
p = re.compile('([*\/]) ') #Multiplying Operator
m = p.match(inputString)
if (m != None):
nextToken = '<multiplying_operator>'
inputString = inputString.replace(m.group(1) + ' ', '', 1)
return
p = re.compile('([+-]) ') #Sign or Adding operator
m = p.match(inputString)
if (m != None):
nextToken = '<adding_operator>'
inputString = inputString.replace(m.group(1) + ' ', '', 1)
return
p = re.compile('(.*?) ') #Terminal
m = p.match(inputString)
if (m != None):
if (m.group(1) in termlist):
nextToken = m.group(1)
inputString = inputString.replace(m.group(1) + ' ', '', 1)
return
p = re.compile('([a-zA-Z].*?) ') #Variable
m = p.match(inputString)
if (m != None):
nextToken = '<variable>'
inputString = inputString.replace(m.group(1) + ' ', '', 1)
return
print("Syntax Error: Unknown Symbol")
sys.exit(0)
#Starts the parser
def parser():
global inputString
global nextToken
lex() #Load nextToken for start of program;
#call lex within functions whenever possible
if (nextToken == ''):
sys.exit(0)
if (nextToken == 'program'):
lex()
if (nextToken == '<progname>'): #function not needed, checked in lex()
lex()
compoundStmt()
else:
print("Syntax Error: Invalid Program Name")
sys.exit(0)
else:
print("Syntax Error: Missing 'program'")
sys.exit(0)
if (len(inputString) != 0):
print("Syntax Error: Garbage after end")
sys.exit(0)
return
#Compound statement function (if we are supposed to be in a compound statement)
def compoundStmt():
global inputString
global nextToken
if (nextToken == 'begin'):
stmt()
while (nextToken == ';'):
stmt()
if (nextToken != 'end'):
print("Syntax Error: End of Compound Statement expected!")
sys.exit(0)
else:
print("Syntax Error: Beginning of Compound Statement expected!")
sys.exit(0)
return
#Statement function
def stmt():
global inputString
global nextToken
lex()
if ((nextToken == '<variable>') | (nextToken == 'read') | (nextToken == 'write')):
simpleStmt()
elif ((nextToken == 'begin') | (nextToken == 'if') | (nextToken == 'while')):
structuredStmt()
else:
print("Syntax Error: Statement expected")
sys.exit(0)
return
#simple statment decides what type of simple statement exists based ff nextToken
def simpleStmt():
global inputString
global nextToken
if (nextToken == '<variable>'):
assignmentStmt()
elif (nextToken == 'read'):
readStmt()
elif (nextToken == 'write'):
writeStmt()
else:
print("Syntax Error: Assignment Statement or Read Statement or Write Statement expected")
sys.exit(0)
return
#structured statment decides what type of structured statement exists based ff nextToken
def structuredStmt():
global inputString
global nextToken
if (nextToken == 'begin'):
compoundStmt()
elif (nextToken == 'if'):
ifStmt()
elif (nextToken == 'while'):
whileStmt()
else:
print("Syntax Error: compound statement or read statement or write statement expected")
sys.exit(0)
return
def assignmentStmt():
global inputString
global nextToken
lex()
#get to the assignment op
if(nextToken == ":="):
lex()
#then assignment op to expression
expression()
else:
print("Syntax Error: := sign expected")
sys.exit(0)
return
def readStmt():
global inputString
global nextToken
lex()
if (nextToken == '('):
lex()
if (nextToken == '<variable>'):
lex()
while (nextToken == ','):
lex()
if (nextToken == '<variable>'):
lex()
else:
print("Syntax Error: variable expected")
sys.exit(0)
if (nextToken == ')'):
lex()
else:
print("Syntax Error: ')' expected")
sys.exit(0)
else:
print("Syntax Error: variable expected")
sys.exit(0)
else:
print("Syntax Error: '(' expected")
sys.exit(0)
return
def writeStmt():
global inputString
global nextToken
lex()
if (nextToken == '('):
lex()
expression()
while (nextToken == ','):
lex()
expression()
if (nextToken == ')'):
lex()
else:
print("Syntax Error: ')' expected")
sys.exit(0)
else:
print("Syntax Error: '(' expected")
sys.exit(0)
return
def whileStmt():
global inputString
global nextToken
lex()
expression()
if(nextToken == 'do'):
stmt()
return
def ifStmt():
global inputString
global nextToken
lex()
expression()
if(nextToken == 'then'):
stmt()
if(nextToken == 'else'):
stmt()
else:
print("Syntax Error: 'then' expected")
sys.exit(0)
return
def expression():
global inputString
global nextToken
simpleExpr()
if (nextToken == '<relational_operator>'):
lex()
simpleExpr()
#ending an expression, but having another terminal left. These are the only terminals that are necessary
elif ((nextToken == 'end') | (nextToken == 'else') | (nextToken == 'do') | (nextToken == 'then')):
return
else:
print("Syntax error: relational operator expected")
sys.exit(0)
return
def simpleExpr():
global inputString
global nextToken
if (nextToken == '<adding_operator>'):
lex()
term()
while (nextToken == '<adding_operator>'):
lex()
term()
return
def term():
global inputString
global nextToken
factor()
while(nextToken == '<multiplying_operator>'):
lex()
factor()
return
def factor():
global inputString
global nextToken
if((nextToken == '<variable>') | (nextToken == '<constant>')):
lex()
else:
if (nextToken == '('):
lex()
expression()
if (nextToken == ')'):
lex()
else:
print("Syntax Error: ')' expected")
sys.exit(0)
else:
print("Syntax Error: '(' expected")
sys.exit(0)
return
inputString = input("Enter an inputString: ")
parser()
print("The string is syntactically correct!! :<")