Advertisement
backstreetimrul

First and Follow Function

Jul 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 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. # productionsFollow=["E->TX","X->+TX","X->$","T->FY","Y->*FY","Y->$","F->(E)","F->i",
  19. productions=["S->(A)","S->$","A->TE","E->&TE","E->$","T->(A)","T->a","T->b","T->c",]
  20. # productions=["X->a","X->$","Y->b","Y->$","Z->c","Z->$","A->XYZ",]
  21. # productions=["X->a","X->$","Y->b","Y->$","Z->c","Z->$","A->XYZ",]
  22. tempProd=["S->(A)","S->$","A->TE","E->&TE","E->$","T->(A)","T->a","T->b","T->c",]
  23.  
  24.  
  25. #sort lowercase first
  26. print("Sorting")
  27. productions.sort(key=lambda p: p[3].lower())
  28. [print(i) for i in productions]
  29.    
  30. print("\nNon Terminals:")
  31. for i in productions:
  32.     if i[0] not in nterminals:
  33.         nterminals.extend(i[0])
  34. [print(i) for i in nterminals]
  35.    
  36.  
  37. print("\nInitial Dictionaries:")
  38. for i in range(0,len(nterminals)):
  39.         first[nterminals[i]]=[]
  40. [print(f"{i}:{j}") for i,j in first.items()]
  41.    
  42. #for terminal symbols
  43. for i in range(0,len(nterminals)):
  44.     for j in range(0,len(productions)):
  45.         if nterminals[i]==productions[j][0]:
  46.             if((productions[j][3].isupper()==False) or productions[j][3]=="$"):
  47.                 first[nterminals[i]].extend(productions[j][3])
  48.                
  49. print("\nAfter appending terminals:")
  50. [print(f"{i}:{j}") for i,j in first.items()]
  51.    
  52.  
  53. #List out productions starting with a Non-Terminal
  54. print("productions starting with a Non-Terminal")
  55. [extra.append(i) for i in productions if i[3].isupper()]
  56. [print(i) for i in extra]
  57.    
  58. for i in range(0,len(extra)):
  59.     for j in extra[i][3:]:                
  60.         if j.isupper():    
  61.             if '$' in first[j]:    
  62.                 first[j].remove('$')
  63.                 first[extra[i][0]].extend(first[j])
  64.                 first[j].extend('$')
  65.             elif '$' not in first[j]:                          
  66.                 first[extra[i][0]].extend(first[j])
  67.                 break                  
  68.         elif j.isupper()==False:
  69.             first[extra[i][0]].extend(j)
  70.             break        
  71.  
  72.  
  73.  
  74. #To Uniquely Identify
  75. for i in first.keys():
  76.     first[i]=list(set(first[i]))
  77.                                        
  78. print("\nFirst FUnction:")
  79. [print(f"First({i})={j}") for i,j in first.items()]
  80.  
  81.  
  82. #--------------------------------FOLLOW FUNCTION-----------------------
  83.  
  84. print("\n\nInitial Follow Dictionary:")
  85. for i in range(0,len(nterminals)):
  86.         follow[nterminals[i]]=[]
  87. [print(f"{i}:{j}") for i,j in follow.items()]
  88.  
  89. print("\nNon Terminal:")
  90. for i in nterminals:
  91.         print(i)
  92. print("\nProduction:")
  93. for i in tempProd:
  94.         print(i)
  95.  
  96.  
  97. print("Main Follow:")
  98. # for i in range(0,len(tempProd)):     # productions[i]
  99. #     for j in range(0,len(nterminals)):  #nterminals[j]
  100. #         for k in tempProd[i][3:]:
  101. #             if k.isupper():
  102.  
  103. temp=0
  104. for i in range(0,len(tempProd)):     # productions[i]
  105.     flag=0
  106.     for k in tempProd[i][3:]:
  107.         print(k)
  108.         if k.isupper()==False and flag==1:
  109.            print(k)
  110.            print(temp)
  111.            follow[temp].extend(k)
  112.            break
  113.            #follow[k-1].append(k)
  114.         if k.isupper():
  115.             temp=k
  116.             flag=1
  117.     print("\n")
  118.  
  119.  
  120.  
  121. #To Uniquely Identify
  122. for i in follow.keys():
  123.     follow[i]=list(set(follow[i]))
  124.  
  125. print("\nFollw FUnction:")
  126. [print(f"First({i})={j}") for i,j in follow.items()]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement