Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- productions=[]
- tempProd=[]
- nterminals=[]
- extra=[]
- first={}
- follow={}
- # n = int(input("Number of Grammers: "))
- # for i in range (0,n):
- # g= input("")
- # a=g[3:].split("|")
- # for j in a:
- # productions.append(g[:3]+j)
- # productions=["P->i","P->c","P->nTs","Q->P","Q->aS","Q->bSc","R->b","R->#","S->c","S->Rn","S->#","T->RSq",]
- # productions=["E->TX","X->+TX","X->#","T->FY","Y->*FY","Y->#","F->(E)","F->i",]
- # productions=["S->(A)","S->#","A->TE","E->&TE","E->#","T->(A)","T->a","T->b","T->c",]
- # productions=["X->a","X->#","Y->b","Y->#","Z->c","Z->#","A->XYZ",]
- productions=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
- #-----------------------------------------------------
- # tempProd=["P->i","P->c","P->nTs","Q->P","Q->aS","Q->bSc","R->b","R->#","S->c","S->Rn","S->#","T->RSq",]
- # tempProd=["E->TX","X->+TX","X->#","T->FY","Y->*FY","Y->#","F->(E)","F->i",]
- # tempProd=["S->(A)","S->#","A->TE","E->&TE","E->#","T->(A)","T->a","T->b","T->c",]
- # tempProd=["X->a","X->#","Y->b","Y->#","Z->c","Z->#","A->XYZ",]
- tempProd=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
- # tempProd.sort(key=lambda p: p[3].upper())
- #sort lowercase first
- print("Sorting")
- productions.sort(key=lambda p: p[3].lower())
- [print(i) for i in productions]
- print("\nNon Terminals:")
- for i in productions:
- if i[0] not in nterminals:
- nterminals.extend(i[0])
- [print(i) for i in nterminals]
- print("\nInitial Dictionaries:")
- for i in range(0,len(nterminals)):
- first[nterminals[i]]=[]
- [print(f"{i}:{j}") for i,j in first.items()]
- #for terminal symbols
- for i in range(0,len(nterminals)):
- for j in range(0,len(productions)):
- if nterminals[i]==productions[j][0]:
- if((productions[j][3].isupper()==False) or productions[j][3]=="#"):
- first[nterminals[i]].extend(productions[j][3])
- print("\nAfter appending terminals:")
- [print(f"{i}:{j}") for i,j in first.items()]
- #List out productions starting with a Non-Terminal
- print("productions starting with a Non-Terminal")
- [extra.append(i) for i in productions if i[3].isupper()]
- [print(i) for i in extra]
- for z in range(3):
- print("\nAfter appending terminals:")
- [print(f"{i}:{j}") for i,j in first.items()]
- for i in range(0,len(extra)):
- count=0
- for j in extra[i][3:]:
- count+=1
- if j.isupper() and count==len(extra[i][3:]):
- first[extra[i][0]].extend(first[j])
- break
- if j.isupper():
- if '#' in first[j]:
- first[j].remove('#')
- first[extra[i][0]].extend(first[j])
- first[j].extend('#')
- elif '#' not in first[j]:
- first[extra[i][0]].extend(first[j])
- break
- elif j.isupper()==False:
- first[extra[i][0]].extend(j)
- break
- #To Uniquely Identify
- for i in first.keys():
- first[i]=list(set(first[i]))
- print("\nFirst FUnction:")
- [print(f"First({i})={j}") for i,j in first.items()]
- #--------------------------------FOLLOW FUNCTION-----------------------
- print("\n\nInitial Follow Dictionary:")
- for i in range(0,len(nterminals)):
- follow[nterminals[i]]=[]
- [print(f"{i}:{j}") for i,j in follow.items()]
- print("\nNon Terminal:")
- for i in nterminals:
- print(i)
- print("\nProduction:")
- for i in tempProd:
- print(i)
- print("Main Follow:")
- #CASE-0:Initial case:
- follow[tempProd[0][0]].extend('$')
- #CASE-1: If follower is a terminal.
- for i in range(0,len(tempProd)): # productions[i]
- temp=0
- flag=0
- for k in tempProd[i][3:]:
- print(k)
- if k.isupper()==False and flag==1:
- print(k)
- print(temp)
- follow[temp].extend(k)
- break
- #follow[k-1].append(k)
- if k.isupper():
- temp=k
- flag=1
- print("\n")
- print("\nAfter Terminal is added:")
- for i in follow.keys():
- follow[i]=list(set(follow[i]))
- [print(f"First({i})={j}") for i,j in follow.items()]
- #----------------------------------1--------------------------------
- #CASE-2: If follower is none. (No Follower)
- print("No Follower")
- for i in range(0,len(tempProd)): # productions[i]
- count=0
- for k in tempProd[i][3:]:
- count+=1
- print(k)
- if k.isupper() and count==len(tempProd[i][3:]):
- # print("**"+k+"**")
- # print(tempProd[i][0])
- follow[k].extend(follow[tempProd[i][0]])
- print("\n")
- print("\nAfter Parent is added:")
- for i in follow.keys():
- follow[i]=list(set(follow[i]))
- [print(f"First({i})={j}") for i,j in follow.items()]
- #-----------------------------2------------------------------
- #CASE-3: If follower is a Non-Terminal
- print("Non Terminal:")
- for z in range(3):
- for i in range(0,len(tempProd)): # productions[i]
- temp=0
- flag=0
- count=0
- for k in tempProd[i][3:]:
- count+=1
- print("Target"+k)
- print(flag)
- if k.islower() and flag==2:
- # print("%%%% ")
- # print(k+" %%%%")
- follow[temp].extend(k)
- if k.isupper() and flag==1:
- print("append:"+k)
- print("\nFollow"+temp)
- if '#' in first[k]:
- first[k].remove('#')
- follow[temp].extend(first[k])
- first[k].extend('#')
- flag=2
- continue
- elif '#' not in first[k]:
- follow[temp].extend(first[k])
- break
- # if k.isupper() and count==len(tempProd[i][3:]):
- # follow[temp].extend(first[k])
- if k.isupper():
- temp=k
- flag=1
- print("\n")
- print("\nAfter Non Terminal is added:")
- for i in follow.keys():
- follow[i]=list(set(follow[i]))
- [print(f"First({i})={j}") for i,j in follow.items()]
- #---------------------------------------3---------------------
- #CASE-2: If follower is none. (No Follower)
- print("No Follower")
- for i in range(0,len(tempProd)): # productions[i]
- count=0
- for k in tempProd[i][3:]:
- count+=1
- print(k)
- if k.isupper() and count==len(tempProd[i][3:]):
- # print("**"+k+"**")
- # print(tempProd[i][0])
- follow[k].extend(follow[tempProd[i][0]])
- print("\n")
- #-----------------------------2------------------------------
- #To Uniquely Identify
- for i in follow.keys():
- follow[i]=list(set(follow[i]))
- print("\nFollw FUnction:")
- [print(f"Follow({i})={j}") for i,j in follow.items()]pyth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement