Advertisement
backstreetimrul

First and Follow Function(Final)

Jul 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.04 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.  
  17. # productions=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
  18. # tempProd=["S->aSe","S->B","B->bBCf","B->C","C->cCg","C->d","C->#",]
  19.  
  20.  
  21. # tempProd.sort(key=lambda p: p[3].upper())
  22.  
  23. #sort lowercase first
  24. print("Sorting")
  25. productions.sort(key=lambda p: p[3].lower())
  26. [print(i) for i in productions]
  27.    
  28. print("\nNon Terminals:")
  29. for i in productions:
  30.     if i[0] not in nterminals:
  31.         nterminals.extend(i[0])
  32. [print(i) for i in nterminals]
  33.    
  34.  
  35. print("\nInitial Dictionaries:")
  36. for i in range(0,len(nterminals)):
  37.         first[nterminals[i]]=[]
  38. [print(f"{i}:{j}") for i,j in first.items()]
  39.    
  40. #for terminal symbols
  41. for i in range(0,len(nterminals)):
  42.     for j in range(0,len(productions)):
  43.         if nterminals[i]==productions[j][0]:
  44.             if((productions[j][3].isupper()==False) or productions[j][3]=="#"):
  45.                 first[nterminals[i]].extend(productions[j][3])
  46.                
  47. print("\nAfter appending terminals:")
  48. [print(f"{i}:{j}") for i,j in first.items()]
  49.    
  50.  
  51. #List out productions starting with a Non-Terminal
  52. print("productions starting with a Non-Terminal")
  53. [extra.append(i) for i in productions if i[3].isupper()]
  54. [print(i) for i in extra]
  55.  
  56. for z in range(3):  
  57.     print("\nAfter appending terminals:")
  58.     [print(f"{i}:{j}") for i,j in first.items()]
  59.     for i in range(0,len(extra)):
  60.         count=0
  61.         for j in extra[i][3:]:    
  62.             count+=1    
  63.             if j.isupper() and count==len(extra[i][3:]):
  64.                 first[extra[i][0]].extend(first[j])
  65.                 break
  66.                        
  67.             if j.isupper():    
  68.                 if '#' in first[j]:    
  69.                     first[j].remove('#')
  70.                     first[extra[i][0]].extend(first[j])
  71.                     first[j].extend('#')
  72.                 elif '#' not in first[j]:                          
  73.                     first[extra[i][0]].extend(first[j])
  74.                     break                  
  75.             elif j.isupper()==False:
  76.                 first[extra[i][0]].extend(j)
  77.                 break  
  78.  
  79.  
  80. #To Uniquely Identify
  81. for i in first.keys():
  82.     first[i]=list(set(first[i]))
  83.                                        
  84. print("\nFirst FUnction:")
  85. [print(f"First({i})={j}") for i,j in first.items()]
  86.  
  87.  
  88. #--------------------------------FOLLOW FUNCTION-----------------------
  89.  
  90. print("\n\nInitial Follow Dictionary:")
  91. for i in range(0,len(nterminals)):
  92.         follow[nterminals[i]]=[]
  93. [print(f"{i}:{j}") for i,j in follow.items()]
  94.  
  95.  
  96.  
  97.  
  98. print("Main Follow:")
  99. #CASE-0:Initial case:
  100. follow[tempProd[0][0]].extend('$')
  101.  
  102. #CASE-1: If follower is a terminal.
  103. for i in range(0,len(tempProd)):     # productions[i]
  104.     temp=0
  105.     flag=0
  106.     for k in tempProd[i][3:]:
  107.         if k.isupper()==False and flag==1:
  108.            follow[temp].extend(k)
  109.            break
  110.         if k.isupper():
  111.             temp=k
  112.             flag=1
  113.  
  114.  
  115. #----------------------------------1--------------------------------
  116.  
  117. #CASE-2: If follower is none. (No Follower)
  118. for i in range(0,len(tempProd)):     # productions[i]
  119.     count=0
  120.     for k in tempProd[i][3:]:
  121.         count+=1
  122.         if k.isupper() and count==len(tempProd[i][3:]):
  123.             follow[k].extend(follow[tempProd[i][0]])
  124.  
  125. #-----------------------------2------------------------------
  126.  
  127. #CASE-3: If follower is a Non-Terminal
  128. for z in range(3):
  129.     for i in range(0,len(tempProd)):     # productions[i]
  130.         temp=0
  131.         flag=0
  132.         count=0
  133.         for k in tempProd[i][3:]:
  134.             count+=1
  135.             if k.islower() and flag==2:
  136.                 follow[temp].extend(k)    
  137.             if k.isupper() and flag==1:
  138.                 if '#' in first[k]:
  139.                     first[k].remove('#')
  140.                     follow[temp].extend(first[k])
  141.                     first[k].extend('#')
  142.                     flag=2
  143.                     continue
  144.                 elif '#' not in first[k]:                          
  145.                     follow[temp].extend(first[k])
  146.                     break
  147.         #     if k.isupper() and count==len(tempProd[i][3:]):
  148.         #         follow[temp].extend(first[k])
  149.             if k.isupper():
  150.                 temp=k
  151.                 flag=1
  152.            
  153.  
  154. #---------------------------------------3---------------------
  155. #CASE-2: If follower is none. (No Follower)
  156. for i in range(0,len(tempProd)):     # productions[i]
  157.     count=0
  158.     for k in tempProd[i][3:]:
  159.         count+=1
  160.         if k.isupper() and count==len(tempProd[i][3:]):
  161.             follow[k].extend(follow[tempProd[i][0]])
  162. #-----------------------------2------------------------------
  163.  
  164. #To Uniquely Identify
  165. for i in follow.keys():
  166.     follow[i]=list(set(follow[i]))
  167.  
  168. print("\nFollw FUnction:")
  169. [print(f"Follow({i})={j}") for i,j in follow.items()]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement