-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRecursive-descent-parser.py
76 lines (75 loc) · 1.52 KB
/
Recursive-descent-parser.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
print("Recursive Desent Parsing For following grammar\n")
print("E->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/i\n")
print("Enter the string want to be checked\n")
global s
s=list(input())
global i
i=0
def match(a):
global s
global i
if(i>=len(s)):
return False
elif(s[i]==a):
i+=1
return True
else:
return False
def F():
if(match("(")):
if(E()):
if(match(")")):
return True
else:
return False
else:
return False
elif(match("i")):
return True
else:
return False
def Tx():
if(match("*")):
if(F()):
if(Tx()):
return True
else:
return False
else:
return False
else:
return True
def T():
if(F()):
if(Tx()):
return True
else:
return False
else:
return False
def Ex():
if(match("+")):
if(T()):
if(Ex()):
return True
else:
return False
else:
return False
else:
return True
def E():
if(T()):
if(Ex()):
return True
else:
return False
else:
return False
if(E()):
if(i==len(s)):
print("String is accepted")
else:
print("String is not accepted")
else:
print("string is not accepted")