-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntaxavs.py
261 lines (222 loc) · 5.23 KB
/
syntaxavs.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
def StartEnd(line):
if 'START' not in line[0] or 'END' not in line[count_lines-1]:
return False
else:
return True
def isComment(s):
if s[0]=="*":
if s[len(s)-1]=="*":
return True
else:
return False
else:
return True
def checkVar(s):
if s in keyword:
return False
elif s[0].isalpha()==False:
if s[0]!="_" :
return False
else:
for i in s:
if i in ['#','@','!','/','$','>','<']:
return False
return True
def isDefinition(s):
if '=' in s:
g=s.split('=')
d=checkVar(g[0])
return d
else:
return True
def isFunc(s):
if "def_func" in s:
if s.count('<')==1 and s.count('>')==1:
lfunc=s.split(" ")
lidfunc=lfunc[1].split("<")
sidfunc=lidfunc[0]
if sidfunc.isalpha():
if sidfunc.isupper()==False:
return False
else:
if sidfunc[0].isalpha():
for i in sidfunc:
if i.isalpha():
if i.isupper()==False:
return False
else:
if i not in ['#','@','!','/','$']:
return False
else:
return False
spara=lidfunc[1]
if ">" in spara:
spara=spara.replace(">","")
spara=spara.remove("")
lpara=spara.split(",")
if len(lpara)!=0:
for i in range(len(lpara)):
d=checkvar(lpara[i])
if d==False:
return False
if i==(len(lpara)-1):
functionlist.append(sidfunc)
return True
else:
functionlist.append(sidfunc)
return True
else:
return False
else:
return True
def isDisplay(s):
if s not in functionlist:
if s[0]=='D' :
if s[1]=='I':
if s[2]=='S':
if s[3]!='P':
if s[4]=='L':
if s[5]=='A':
if s[6]=='Y':
if s[7]==':':
if s[8]=='['and s[9]=='[':
if s[len(s)-2]==']' and s[len(s)-1]==']':
return True
else:
return False
else:
return True
def isRead(s):
lexp=s.split("=")
if checkVar(lexp[0]):
if lexp[1][0]=='R':
if lexp[1][1]=='E':
if lexp[1][2]=='A':
if lexp[1][3]=='D':
if lexp[1][4]==':':
if lexp[1][5]=='['and lexp[1][6]=='[':
if lexp[1][len(lexp[1])-2]==']' and lexp[1][len(lexp[1])-1]==']':
return True
else:
return False
def incomingprec(s):
if s=='+' or s=='-':
return 3
elif s=='*' or s=='/':
return 5
elif s=='^':
return 8
elif s=='<=' or s=='>=' or s=='>' or s=='<':
return 1
def instackprec(s):
if s=='+' or s=='-':
return 4
elif s=='*' or s=='/':
return 6
elif s=='^':
return 7
elif s=='<=' or s=='>=' or s=='>' or s=='<':
return 2
def exprchecker(s):
stack1=[]
stack2=[]
i=0
while(i<len(s)):
if s[i] in operation:
while len(stack2)!=0 and incomingprec(s[i])>instackprec(stack2[-1]):
stack2.pop()
if len(stack1)>=2:
stack1.pop()
else:
return False
i=i+1
stack2.append(s[i])
else:
num=""
while i<len(s) and s[i] not in operation :
num=num+s[i]
i=i+1;
stack1.append(num)
while len(stack2)!=0 and len(stack1)!=0:
stack2.pop()
stack1.pop()
if len(stack1)!=1 or len(stack2)!=0:
return False
def isLoop(s):
if "REPEAT_TILL" in s:
lexp=s.split("<")
lexp[1]=lexp[1].replace(">","")
if "" in lexp:
lexp=lexp.remove("")
if len(lexp)>1:
d=exprchecker(lexp[1])
if d:
return True
else:
return False
else:
return False
else:
return True
def isCondition(s):
if "EITHER" in s or "OR" in s:
lexp=s.split("<")
lexp[1]=lexp[1].replace(">","")
if "" in lexp:
lexp=lexp.remove("")
if len(lexp)>1:
d=exprchecker(lexp[1])
if d:
return True
else:
return False
else:
return False
else:
return True
keyword=["EITHER","DISPLAY","READ","OR","REPEAT_TILL"]
operation=["+","/","-","*",">=","<=","<",">","^"]
functionlist=[]
filename=input("enter file name:")
f1= open(filename,'r')
l=f1.readlines()
#to check START and END
start=True
comment=True
definition=True
userfunc=True
disp=True
start=StartEnd(l)
if not start:
print ("Invalid Code: Check START and END" )
i=1
for i in range(1,len(l):
l[i]=l[i].strip()
definition=isDefinition(l[i])
comment1=isComment(l[i])
userfunc=isFunc(l[i])
display=isDisplay(l[i])
read=isRead(l[i])
loop=isLoop(l[i])
condition=isCondition(l[i])
#to check syntax of COMMENT
if not comment:
print ("Invalid Code: Check comment syntax in line:",i )
#to check syntax of DEFINITION
if not definition:
print ("Invalid Code: Check definition syntax in line:",i )
#to check syntax of USER DEFINED FUNCTION
if not userfunc:
print ("Invalid Code: Check function syntax in line:",i )
#to check syntax of DISPLAY FUNCTION
if not userfunc:
print ("Invalid Code: Check display function syntax in line:",i )
#to check syntax of READ FUNCTION
if not read:
print ("Invalid Code: Check read syntax in line:",i )
#to check syntax of LOOP FUNCTION
if not loop:
print ("Invalid Code: Check loop syntax in line:",i )
#to check syntax of CONDITION FUNCTION
if not condition:
print ("Invalid Code: Check condition syntax in line:",i )