Skip to content
Snippets Groups Projects
Commit 8e68577d authored by Singh, Amisha's avatar Singh, Amisha
Browse files

Update parser.py

parent 0780a28f
No related branches found
No related tags found
No related merge requests found
......@@ -124,20 +124,28 @@ def check_validity(string, start, table,follow,productions,first):
idx = 0
print("Stack\t\tInput\t\tMoves")
while (len(stack) > 0):
top = stack[-1]
if top != '&':
print(f"Top => {top}")
curr_string = input_string[idx]
# print("current",curr_string)
# print(idx,len(input_string))
if curr_string=='&' and idx+1==len(input_string):
accepted=True
break
if input_string[idx-1]+curr_string in ['Cl','Br','At','Ts','se','as']:
#print("here")
curr_string= input_string[idx-1]+curr_string
#print("done")
idx=idx+1
#print(input_string[idx])
if top=='Z':
idx=idx
curr_string=input_string[idx]
#print("NOW",input_string[idx])
if curr_string+input_string[idx+1] in ['Cl','Br','At','Ts','se','as']:
#print("me")
curr_string= curr_string+input_string[idx+1]
idx=idx
if input_string[idx+1]=='-':
......@@ -146,6 +154,7 @@ def check_validity(string, start, table,follow,productions,first):
print(f"Current input => {curr_string}")
if len(stack)==1 and idx<len(input_string)-1:
stack.append('body')
print("1")
top=stack[-1]
if curr_string=='&':
accepted=True
......@@ -155,15 +164,30 @@ def check_validity(string, start, table,follow,productions,first):
idx += 1
if len(stack)==1 and idx<len(input_string)-1:
top="body"
print("2")
else:
key = (top, curr_string)
print(f"Key => {key}")
if key not in table and len(stack)==1:
top='body'
print("3")
key = (top,curr_string)
if top==curr_string:
stack.pop()
idx+=1
if key not in table and top in productions.keys() and input_string[idx-2]+input_string[idx-1] in follow[top]:
stack.pop()
print("Try exempting ",top)
top=stack[-1]
key=(top,curr_string)
if key not in table and top in productions.keys():
accepted = False
print("Invalid literal =",curr_string)
break
elif key not in table and top=='&':
accepted = False
print("Invalid literal =",curr_string)
break
if key not in table and top in productions.keys() and input_string[idx-1] in follow[top]:
stack.pop()
print("Try exempting ",top)
......@@ -200,6 +224,7 @@ def check_validity(string, start, table,follow,productions,first):
stack.append("body")
elif top!=curr_string:
print("pop")
stack.pop()
if accepted:
......@@ -217,13 +242,10 @@ def mean_squared_error(act, pred):
def main():
productions = {}
grammar = open("grammar2.txt", "r")
first = {}
follow = {}
table = {}
table = {}
start = ""
for prod in grammar:
l = re.split(" > |\n| ", prod)
m = []
......@@ -275,7 +297,7 @@ def main():
# print(pred)
# print(mean_squared_error(1,pred))
ans=check_validity(input(), start, table,follow,productions,first)
print("ANSWER=",ans)
print("SMILES:","ANSWER=",ans)
grammar.close()
import numpy as np
import sys
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment