-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRDP.py
428 lines (364 loc) · 9.97 KB
/
RDP.py
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# Michael Balas
# This program can be run as python2 or as python3
from __future__ import print_function
# A recursive descent parser in Python for simple arithemic expressions.
# The scanner is not separated from the parser. Each nonterminal corresponds
# to a function, the correspoding productions are shown as a comment at the
# header of the function. The grammar is modified to be predictive.
# tokens:
# NUM is an unsigned decimal number with at most 4 digits after . , at most
# 9 significant digits and no leading zeroes
# VAR is a 1 character identifier of upper case letters A to Z
# = - + * / ( ) ;
# define tokens
EOI = 0
NUM = 1
VAR = 2
EQ = 3
MINUS = 4
PLUS = 5
MULT = 6
DIVI = 7
LP = 8
RP = 9
SEMI = 10
ERR = 11
# Grammar
# prog -> VAR tail1 | NUM tail2 | MINUS {VAR NUM} tail2 | ( expr )
# tail1 -> = expr ; prog | {+ -} term tail2 | {* /} factor tail3 | epsilon
# expr -> term tail2
# tail2 -> {+ -} term tail2 | epsilon
# term -> factor tail3
# tail3 -> {* /} factor tail3 | epsilon
# factor -> ( expr ) | - {NUM VAR} | NUM | VAR
import sys
debug = True
def show(indent,name,s,spp):
if debug:
print(indent+name+'("',end='');
j = len(s)
for i in range(spp,j):
print(s[i],sep="",end="")
print('")',end='\n')
return
else:
return
#end show
def x(indent,name1,name2):
if debug:
print(indent+"returned to "+name1+" from "+name2)
else:
return
#end x
def EatWhiteSpace(s,spp):
j = len(s)
if spp >= j:
return spp
while s[spp] == ' ' or s[spp] == '\n':
spp=spp+1
if spp >= j:
break
return spp
#end EatWhiteSpace
# prog -> VAR tail1 | NUM tail2 | MINUS {VAR NUM} tail2 | ( expr ) | epsilon
# function prog ------------------------------------------------------------
def prog(s,spp,indent):
show(indent,'prog',s,spp)
indent1 = indent+' '
token = LookAhead(s,spp)
if token == VAR:
spp = ConsumeToken(s,spp)
res,spp = tail1(s,spp,indent1)
x(indent1,"prog","tail1")
return res,spp
elif token == NUM:
spp = ConsumeToken(s,spp)
res,spp = tail2(s,spp,indent1)
x(indent1,"prog","tail2")
return res,spp
elif token == MINUS:
spp = ConsumeToken(s,spp)
token,spp = NextToken(s,spp)
if token == NUM or token == VAR:
res,spp = tail2(s,spp,indent1)
x(indent1,"prog","tail2")
return res,spp
else:
return False,spp
elif token == LP:
spp = ConsumeToken(s,spp)
res,spp = expr(s,spp,indent1)
x(indent1,"prog","expr")
if not res:
return False,spp
token,spp = NextToken(s,spp)
if token == RP:
token = LookAhead(s,spp)
if token == EOI:
return True,spp
else:
res,spp = tail1(s,spp,indent1)
x(indent1,"prog","tail1")
return res,spp
return (token == RP),spp
else:
return True,spp
#end prog
# tail1 -> = expr ; prog | {+ -} term tail2 | {* /} factor tail3 | epsilon
# function tail1 --------------------------------------------------------
def tail1(s,spp,indent):
show(indent,'tail1',s,spp)
indent1 = indent+' '
token = LookAhead(s,spp)
if token == EQ:
spp = ConsumeToken(s,spp)
res,spp = expr(s,spp,indent1)
x(indent1,"tail1","expr")
if not res:
return False,spp
token,spp = NextToken(s,spp)
if token != SEMI:
return False,spp
res,spp = prog(s,spp,indent1)
x(indent1,"tail1","prog")
return res,spp
elif token == PLUS or token == MINUS:
spp = ConsumeToken(s,spp)
res,spp = term(s,spp,indent1)
x(indent1,"tail1","term")
if not res:
return False,spp
res,spp = tail2(s,spp,indent1)
x(indent1,"tail1","tail2")
return res,spp
elif token == MULT or token == DIVI:
spp = ConsumeToken(s,spp)
res,spp = factor(s,spp,indent1)
x(indent1,"tail1","factor")
if not res:
return False,spp
res,spp = tail3(s,spp,indent1)
x(indent1,"tail1","tail3")
return res,spp
else:
return True,spp #epsilon
#end tail1
# expr -> term tail2
# function expr ---------------------------------------------
def expr(s,spp,indent):
show(indent,'expr',s,spp)
indent1 = indent+' '
res,spp = term(s,spp,indent1)
x(indent1,"expr","term")
if not res:
return False,spp
res,spp = tail2(s,spp,indent1)
x(indent1,"expr","tail2")
return res,spp
#end expr
# tail2 -> {+ -} term tail2 | epsilon
# function tail2 ---------------------------------------------
def tail2(s,spp,indent):
show(indent,'tail2',s,spp)
indent1 = indent+' '
token = LookAhead(s,spp)
if token == PLUS or token == MINUS:
spp = ConsumeToken(s,spp)
res,spp = term(s,spp,indent1)
x(indent1,"tail2","term")
if not res:
return False,spp
res,spp = tail2(s,spp,indent1)
x(indent1,"tail2","tail2")
return res,spp
else:
return True,spp #epsilon
#end tail2
# term -> factor tail3
# function term ---------------------------------------------
def term(s,spp,indent):
show(indent,'term',s,spp)
indent1 = indent+' '
res,spp = factor(s,spp,indent1)
x(indent1,"term","factor")
if not res:
return False,spp
res,spp = tail3(s,spp,indent1)
x(indent1,"term","tail3")
return res,spp
#end term
# tail3 -> {* /} factor tail3 | epsilon
# function tail3 ---------------------------------------------
def tail3(s,spp,indent):
show(indent,'tail3',s,spp)
indent1 = indent+' '
token = LookAhead(s,spp)
if token == MULT or token == DIVI:
spp = ConsumeToken(s,spp)
res,spp = factor(s,spp,indent1)
x(indent1,"tail3","factor")
return res,spp
else:
return True,spp #epsilon
#end tail3
# factor -> ( expr ) | - {NUM VAR} | NUM | VAR
# function factor ---------------------------------------------
def factor(s,spp,indent):
show(indent,'factor',s,spp)
indent1 = indent+' '
token,spp = NextToken(s,spp)
if token == LP:
res,spp = expr(s,spp,indent1)
x(indent1,"factor","expr")
if not res:
return False,spp
token,spp = NextToken(s,spp);
return (token == RP),spp
elif token == MINUS:
token,spp = NextToken(s,spp)
return (token == NUM or token == VAR),spp
elif token == NUM or token == VAR:
return True,spp
else:
return False,spp
#end factor
# the scanner ####################################################
# function LookAhead -------------------------------------------
def LookAhead(s,spp):
j = len(s);
i = spp
if i >= j:
return EOI
while s[i]==" " or s[i]=="\n":
i = i+1
if i >= j:
return EOI
if s[i] == '=':
return EQ
elif s[i] == "-":
return MINUS
elif s[i] == "+":
return PLUS
elif s[i] == "*":
return MULT
elif s[i] == "/":
return DIVI
elif s[i] == "(":
return LP
elif s[i] == ")":
return RP
elif s[i] == ";":
return SEMI
elif (ord(s[i]) >= ord('0') and ord(s[i]) <= ord('9')):
return NUM
elif (ord(s[i]) >= ord('A') and ord(s[i]) <= ord('Z')):
return VAR
else:
return ERR
#end LookAhead
# function NextToken ---------------------------------------------
def NextToken(s,spp):
spp1 = spp
spp = EatWhiteSpace(s,spp)
j = len(s)
if spp >= j:
return ERR,spp1
if spp >= j:
return EOI,spp
elif s[spp] == '=':
return EQ,spp+1
elif s[spp] == "-":
return MINUS,spp+1
elif s[spp] == "+":
return PLUS,spp+1
elif s[spp] == "*":
return MULT,spp+1
elif s[spp] == "/":
return DIVI,spp+1
elif s[spp] == "(":
return LP,spp+1
elif s[spp] == ")":
return RP,spp+1
elif s[spp] == ";":
return SEMI,spp+1
elif (ord(s[spp])>=ord('0') and ord(s[spp])<=ord('9')):
res,spp = Lnum(s,spp)
if not res:
return ERR,spp1
if spp >= j:
return True,spp
if s[spp] != '.':
return NUM,spp
spp = spp+1 # eat .
if spp >= j:
return ERR,spp1
res,spp = Rnum(s,spp);
if not res:
return ERR,spp1
return NUM,spp
elif (ord(s[spp])>=ord('A') and ord(s[spp])<=ord('Z')):
return VAR,spp+1
else:
return ERR,spp1
#end NUM
def ConsumeToken(s,spp):
token,spp = NextToken(s,spp)
return spp
#end ConsumeToken
# function Rnum --------------------------------------------
def Rnum(s,spp):
spp1 = spp # save position
j = len(s)
found = False
i = 0
while i<4 :
if spp < j and ord(s[spp]) >= ord('0') and ord(s[spp]) <= ord('9'):
found = True
spp = spp+1
if spp >= j:
break
i = i+1
else:
break
#endwhile
if found:
return True,spp
else:
return False,spp1
#end Rnum
# function Lnum --------------------------------------------
def Lnum(s,spp):
spp1 = spp # save position
j = len(s)
if spp >= j:
return False,spp1
if s[spp] == '0':
spp = spp+1 # eat 0
return True,spp
i = 0
found = False
while i<4 :
if spp < j and ord(s[spp]) >= ord('0') and ord(s[spp]) <= ord('9'):
found = True
spp = spp+1
i = i+1
else:
break
#endwhile
if found:
return True,spp
else:
return False,spp1
#end Lnum
#main section
s = "(5+2)*3"
res,spp = prog(s,0,"");
# is there a leftover ?
if spp < len(s)-1:
print("parse Error")
else:
if res:
print("parsing OK")
else:
print("parse Error")
#end main section