Advertisement
backstreetimrul

first and follow

Jul 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.95 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. #     g= input("")
  12. #     a=g[3:].split("|")
  13. #     for j in a:
  14. #         productions.append(g[:3]+j)
  15.  
  16. # productions=["P->i","P->c","P->nTs","Q->P","Q->aS","Q->bSc","R->b","R->#","S->c","S->Rn","S->#","T->RSq",]
  17. # productions=["E->TX","X->+TX","X->#","T->FY","Y->*FY","Y->#","F->(E)","F->i",]
  18. # productions=["S->(A)","S->#","A->TE","E->&TE","E->#","T->(A)","T->a","T->b","T->c",]
  19. # productions=["X->a","X->#","Y->b","Y->#","Z->c","Z->#","A->XYZ",]
  20. productions=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
  21.  
  22. #-----------------------------------------------------
  23. # tempProd=["P->i","P->c","P->nTs","Q->P","Q->aS","Q->bSc","R->b","R->#","S->c","S->Rn","S->#","T->RSq",]
  24. # tempProd=["E->TX","X->+TX","X->#","T->FY","Y->*FY","Y->#","F->(E)","F->i",]
  25. # tempProd=["S->(A)","S->#","A->TE","E->&TE","E->#","T->(A)","T->a","T->b","T->c",]
  26. # tempProd=["X->a","X->#","Y->b","Y->#","Z->c","Z->#","A->XYZ",]
  27. tempProd=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
  28.  
  29.  
  30. # tempProd.sort(key=lambda p: p[3].upper())
  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. #--------------------------------FOLLOW FUNCTION-----------------------
  98.  
  99. print("\n\nInitial Follow Dictionary:")
  100. for i in range(0,len(nterminals)):
  101.         follow[nterminals[i]]=[]
  102. [print(f"{i}:{j}") for i,j in follow.items()]
  103.  
  104. print("\nNon Terminal:")
  105. for i in nterminals:
  106.         print(i)
  107. print("\nProduction:")
  108. for i in tempProd:
  109.         print(i)
  110.  
  111.  
  112. print("Main Follow:")
  113. #CASE-0:Initial case:
  114. follow[tempProd[0][0]].extend('$')
  115.  
  116. #CASE-1: If follower is a terminal.
  117. for i in range(0,len(tempProd)):     # productions[i]
  118.     temp=0
  119.     flag=0
  120.     for k in tempProd[i][3:]:
  121.         print(k)
  122.         if k.isupper()==False and flag==1:
  123.            print(k)
  124.            print(temp)
  125.            follow[temp].extend(k)
  126.            break
  127.            
  128.            #follow[k-1].append(k)
  129.         if k.isupper():
  130.             temp=k
  131.             flag=1
  132.     print("\n")
  133.  
  134. print("\nAfter Terminal is added:")
  135. for i in follow.keys():
  136.     follow[i]=list(set(follow[i]))
  137. [print(f"First({i})={j}") for i,j in follow.items()]
  138. #----------------------------------1--------------------------------
  139.  
  140. #CASE-2: If follower is none. (No Follower)
  141. print("No Follower")
  142. for i in range(0,len(tempProd)):     # productions[i]
  143.     count=0
  144.     for k in tempProd[i][3:]:
  145.         count+=1
  146.         print(k)
  147.         if k.isupper() and count==len(tempProd[i][3:]):
  148.         #     print("**"+k+"**")
  149.         #     print(tempProd[i][0])
  150.             follow[k].extend(follow[tempProd[i][0]])
  151.     print("\n")
  152.  
  153. print("\nAfter Parent is added:")
  154. for i in follow.keys():
  155.     follow[i]=list(set(follow[i]))
  156. [print(f"First({i})={j}") for i,j in follow.items()]
  157. #-----------------------------2------------------------------
  158.  
  159. #CASE-3: If follower is a Non-Terminal
  160. print("Non Terminal:")
  161. for z in range(3):
  162.     for i in range(0,len(tempProd)):     # productions[i]
  163.         temp=0
  164.         flag=0
  165.         count=0
  166.         for k in tempProd[i][3:]:
  167.             count+=1
  168.             print("Target"+k)
  169.             print(flag)
  170.             if k.islower() and flag==2:
  171.                 # print("%%%% ")
  172.                 # print(k+" %%%%")
  173.                 follow[temp].extend(k)    
  174.             if k.isupper() and flag==1:
  175.                 print("append:"+k)
  176.                 print("\nFollow"+temp)
  177.                 if '#' in first[k]:
  178.                     first[k].remove('#')
  179.                     follow[temp].extend(first[k])
  180.                     first[k].extend('#')
  181.                     flag=2
  182.                     continue
  183.                 elif '#' not in first[k]:                          
  184.                     follow[temp].extend(first[k])
  185.                     break
  186.         #     if k.isupper() and count==len(tempProd[i][3:]):
  187.         #         follow[temp].extend(first[k])
  188.             if k.isupper():
  189.                 temp=k
  190.                 flag=1
  191.            
  192.            
  193.         print("\n")
  194.  
  195. print("\nAfter Non Terminal is added:")
  196. for i in follow.keys():
  197.     follow[i]=list(set(follow[i]))
  198. [print(f"First({i})={j}") for i,j in follow.items()]
  199.  
  200. #---------------------------------------3---------------------
  201. #CASE-2: If follower is none. (No Follower)
  202. print("No Follower")
  203. for i in range(0,len(tempProd)):     # productions[i]
  204.     count=0
  205.     for k in tempProd[i][3:]:
  206.         count+=1
  207.         print(k)
  208.         if k.isupper() and count==len(tempProd[i][3:]):
  209.         #     print("**"+k+"**")
  210.         #     print(tempProd[i][0])
  211.             follow[k].extend(follow[tempProd[i][0]])
  212.     print("\n")
  213.  
  214.  
  215. #-----------------------------2------------------------------
  216.  
  217. #To Uniquely Identify
  218. for i in follow.keys():
  219.     follow[i]=list(set(follow[i]))
  220.  
  221. print("\nFollw FUnction:")
  222. [print(f"Follow({i})={j}") for i,j in follow.items()]pyth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement