Advertisement
backstreetimrul

final

Jul 20th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.73 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.  
  105.  
  106.  
  107. print("Main Follow:")
  108. #CASE-0:Initial case:
  109. follow[tempProd[0][0]].extend('$')
  110.  
  111. #CASE-1: If follower is a terminal.
  112. for i in range(0,len(tempProd)):     # productions[i]
  113.     temp=0
  114.     flag=0
  115.     for k in tempProd[i][3:]:
  116.         if k.isupper()==False and flag==1:
  117.            follow[temp].extend(k)
  118.            break
  119.         if k.isupper():
  120.             temp=k
  121.             flag=1
  122.  
  123.  
  124. #----------------------------------1--------------------------------
  125.  
  126. #CASE-2: If follower is none. (No Follower)
  127. for i in range(0,len(tempProd)):     # productions[i]
  128.     count=0
  129.     for k in tempProd[i][3:]:
  130.         count+=1
  131.         if k.isupper() and count==len(tempProd[i][3:]):
  132.             follow[k].extend(follow[tempProd[i][0]])
  133.  
  134. #-----------------------------2------------------------------
  135.  
  136. #CASE-3: If follower is a Non-Terminal
  137. for z in range(3):
  138.     for i in range(0,len(tempProd)):     # productions[i]
  139.         temp=0
  140.         flag=0
  141.         count=0
  142.         for k in tempProd[i][3:]:
  143.             count+=1
  144.             if k.islower() and flag==2:
  145.                 follow[temp].extend(k)    
  146.             if k.isupper() and flag==1:
  147.                 if '#' in first[k]:
  148.                     first[k].remove('#')
  149.                     follow[temp].extend(first[k])
  150.                     first[k].extend('#')
  151.                     flag=2
  152.                     continue
  153.                 elif '#' not in first[k]:                          
  154.                     follow[temp].extend(first[k])
  155.                     break
  156.         #     if k.isupper() and count==len(tempProd[i][3:]):
  157.         #         follow[temp].extend(first[k])
  158.             if k.isupper():
  159.                 temp=k
  160.                 flag=1
  161.            
  162.  
  163. #---------------------------------------3---------------------
  164. #CASE-2: If follower is none. (No Follower)
  165. for i in range(0,len(tempProd)):     # productions[i]
  166.     count=0
  167.     for k in tempProd[i][3:]:
  168.         count+=1
  169.         if k.isupper() and count==len(tempProd[i][3:]):
  170.             follow[k].extend(follow[tempProd[i][0]])
  171. #-----------------------------2------------------------------
  172.  
  173. #To Uniquely Identify
  174. for i in follow.keys():
  175.     follow[i]=list(set(follow[i]))
  176.  
  177. print("\nFollw FUnction:")
  178. [print(f"Follow({i})={j}") for i,j in follow.items()]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement