Advertisement
backstreetimrul

first follow 2

Jul 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.49 KB | None | 0 0
  1. productions=[]
  2. tempProd=[]
  3. nterminals=[]
  4. extra=[]
  5. first={}
  6.  
  7. follow={}
  8.  
  9. # n = int(input("Number of Grammers: "))
  10. # for i in range (0,n):
  11. #     gr= input("")
  12. #     x=gr[3:].split("|")
  13. #     for j in x:
  14. #         productions.append(gr[:3]+j)
  15. #         tempProd.append(gr[:3]+j)
  16. # for i in productions:
  17. #     print(i)
  18.  
  19.  
  20. # productions=["S->(A)","S->$","A->TE","E->&TE","E->$","T->(A)","T->a","T->b","T->c",]
  21. # productions=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
  22. # productions=["P->i","P->c","P->nTs","Q->P","Q->aS","Q->bSc","R->b","R->$","S->c","S->Rn","S->$","T->RSq",]
  23. productions=["E->TX","X->+TX","X->#","T->FY","Y->*FY","Y->#","F->(E)","F->i",]
  24.  
  25. #--------------------------------------
  26. # tempProd=["S->(A)","S->$","A->TE","E->&TE","E->$","T->(A)","T->a","T->b","T->c",]
  27. # tempProd=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
  28. # tempProd=["P->i","P->c","P->nTs","Q->P","Q->aS","Q->bSc","R->b","R->$","S->c","S->Rn","S->$","T->RSq",]
  29. tempProd=["E->TX","X->+TX","X->#","T->FY","Y->*FY","Y->#","F->(E)","F->i",]
  30.  
  31.  
  32. #sort lowercase first
  33. print("Sorting")
  34. productions.sort(key=lambda p: p[3].lower())
  35. [print(i) for i in productions]
  36.    
  37. print("\nNon Terminals:")
  38. for i in productions:
  39.     if i[0] not in nterminals:
  40.         nterminals.extend(i[0])
  41. [print(i) for i in nterminals]
  42.    
  43.  
  44. print("\nInitial Dictionaries:")
  45. for i in range(0,len(nterminals)):
  46.         first[nterminals[i]]=[]
  47. [print(f"{i}:{j}") for i,j in first.items()]
  48.    
  49. #for terminal symbols
  50. for i in range(0,len(nterminals)):
  51.     for j in range(0,len(productions)):
  52.         if nterminals[i]==productions[j][0]:
  53.             if((productions[j][3].isupper()==False) or productions[j][3]=="#"):
  54.                 first[nterminals[i]].extend(productions[j][3])
  55.                
  56. # print("\nAfter appending terminals:")
  57. # [print(f"{i}:{j}") for i,j in first.items()]
  58.    
  59.  
  60. #List out productions starting with a Non-Terminal
  61. print("productions starting with a Non-Terminal")
  62. [extra.append(i) for i in productions if i[3].isupper()]
  63. [print(i) for i in extra]
  64.  
  65. for z in range(3):  
  66.     # print("\nAfter appending terminals:")
  67.     # [print(f"{i}:{j}") for i,j in first.items()]
  68.     for i in range(0,len(extra)):
  69.         count=0
  70.         for j in extra[i][3:]:    
  71.             count+=1    
  72.             if j.isupper() and count==len(extra[i][3:]):
  73.                 first[extra[i][0]].extend(first[j])
  74.                 break
  75.                        
  76.             if j.isupper():    
  77.                 if '#' in first[j]:    
  78.                     first[j].remove('#')
  79.                     first[extra[i][0]].extend(first[j])
  80.                     first[j].extend('#')
  81.                 elif '#' not in first[j]:                          
  82.                     first[extra[i][0]].extend(first[j])
  83.                     break                  
  84.             elif j.isupper()==False:
  85.                 first[extra[i][0]].extend(j)
  86.                 break  
  87.  
  88.  
  89. #To Uniquely Identify
  90. for i in first.keys():
  91.     first[i]=list(set(first[i]))
  92.                                        
  93. print("\nFirst FUnction:")
  94. [print(f"First({i})={j}") for i,j in first.items()]
  95.  
  96.  
  97.  
  98.  
  99. #--------------------------------FOLLOW FUNCTION---------------------------------
  100. print("\n\nInitial Follow Dictionary:")
  101. for i in range(0,len(nterminals)):
  102.     follow[nterminals[i]]=[]
  103. [print(f"{i}:{j}") for i,j in follow.items()]
  104.  
  105.  
  106.  
  107.  
  108. print("Main Follow:")
  109. #CASE-0:Initial case:
  110. follow[tempProd[0][0]].extend('$')
  111.  
  112. #CASE-1: If follower is a terminal.-----------------------------------------
  113. for i in range(0,len(tempProd)):     # productions[i]
  114.     temp=0
  115.     flag=0
  116.     for k in tempProd[i][3:]:
  117.         if k.isupper()==False and flag==1:
  118.            follow[temp].extend(k)
  119.            break
  120.         if k.isupper():
  121.             temp=k
  122.             flag=1
  123. #----------------------------------1--------------------------------
  124.  
  125. print("Terminal:")
  126. for i in follow.keys():
  127.     follow[i]=list(set(follow[i]))
  128. print("\nFollw FUnction:")
  129. [print(f"Follow({i})={j}") for i,j in follow.items()]
  130.  
  131. #CASE-2: If follower is none. (No Follower)--------------------------------
  132. for i in range(0,len(tempProd)):     # productions[i]
  133.     count=0
  134.     for k in tempProd[i][3:]:
  135.         count+=1
  136.         if k.isupper() and count==len(tempProd[i][3:]):
  137.             follow[k].extend(follow[tempProd[i][0]])
  138.  
  139. #-----------------------------2-------------------------------------------
  140.  
  141. for i in follow.keys():
  142.     follow[i]=list(set(follow[i]))
  143. print("\nFollw FUnction:")
  144. [print(f"Follow({i})={j}") for i,j in follow.items()]
  145.  
  146.  
  147. #CASE-3: If follower is a Non-Terminal----------------------------------------
  148. for z in range(3):
  149.     for i in range(0,len(tempProd)):     # productions[i]
  150.         temp=0
  151.         flag=0
  152.         count=0
  153.         # f=0
  154.         for k in tempProd[i][3:]:
  155.             # print(f"\n{f}\n")
  156.             count+=1
  157.             if k.islower() and flag==2:
  158.                 follow[temp].extend(k)    
  159.             if k.isupper() and flag==1:
  160.                 if '#' in first[k]:
  161.                     first[k].remove('#')
  162.                     follow[temp].extend(first[k])
  163.                     first[k].extend('#')
  164.                     flag=2
  165.                     if count == len(tempProd[i][3:]):
  166.                         follow[temp].extend(follow[k])
  167.                     continue
  168.                 elif '#' not in first[k]:                          
  169.                     follow[temp].extend(first[k])
  170.                     break
  171.             # print(count)
  172.             # if k.isupper() and count==len(tempProd[i][3:]):
  173.             #     # follow[temp].extend(first[k])
  174.             #     print(count)
  175.             if k.isupper():
  176.                 temp=k
  177.                 flag=1
  178. #---------------------------------------3---------------------
  179.  
  180. print("\n\nNon Terminal:")
  181. for i in follow.keys():
  182.     follow[i]=list(set(follow[i]))
  183. print("Follw FUnction:")
  184. [print(f"Follow({i})={j}") for i,j in follow.items()]
  185.  
  186.  
  187. #CASE-2: If follower is none. (No Follower)-----------------------------------
  188. for i in range(0,len(tempProd)):     # productions[i]
  189.     count=0
  190.     for k in tempProd[i][3:]:
  191.         count+=1
  192.         if k.isupper() and count==len(tempProd[i][3:]):
  193.             follow[k].extend(follow[tempProd[i][0]])
  194. #-----------------------------2-----------------------------------------------
  195.  
  196.  
  197. #To Uniquely Identify
  198. for i in follow.keys():
  199.     follow[i]=list(set(follow[i]))
  200. print("\nFollw FUnction:")
  201. [print(f"Follow({i})={j}") for i,j in follow.items()]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement