You are on page 1of 5

Rules for First Sets

1. 2. 3. 4. If X is a terminal then First(X) is just X! If there is a Production X then add to first(X) If there is a Production X Y1Y2..Yk then add first(Y1Y2..Yk) to first(X) First(Y1Y2..Yk) is either 1. First(Y1) (if First(Y1) doesn't contain ) 2. OR (if First(Y1) does contain ) then First (Y1Y2..Yk) is everything in First(Y1) <except for > as well as everything in First(Y2..Yk) 3. If First(Y1) First(Y2)..First(Yk) all contain then add to First(Y1Y2..Yk) as well. 5. Follow Sets 6. We Want to make First sets so first we list the sets we need 7. FIRST(E) = {} 8. FIRST(E') = {} 9. FIRST(T) = {} 10. FIRST(T') = {} 11. FIRST(F) = {} 12. First We apply rule 2 to T' and E' 13. FIRST(E) = {} 14. FIRST(E') = {} 15. FIRST(T) = {} 16. FIRST(T') = {} 17. FIRST(F) = {} 18. First We apply rule 3 to T' *FT' this rule tells us that we can add everything in First(*FT') into First(T') 19. Since First(*) useing rule 1 is * we can add * to First(T') 20. FIRST(E) = {} 21. FIRST(E') = {+,} 22. FIRST(T) = {} 23. FIRST(T') = {*,} 24. FIRST(F) = {} 25. First We apply rule 3 to T' *FT' this rule tells us that we can add everything in First(*FT') into First(T') 26. Since First(*) useing rule 1 is * we can add * to First(T') 27. FIRST(E) = {} 28. FIRST(E') = {+,} 29. FIRST(T) = {} 30. FIRST(T') = {*,} 31. FIRST(F) = {} 32. Two more productions begin with terminals F (E) and F id If we apply rule 3 to these we get... 33. FIRST(E) = {} 34. FIRST(E') = {+,} 35. FIRST(T) = {} 36. FIRST(T') = {*,}

37. FIRST(F) = {'(',id} 38. Next we apply rule 3 to T FT' once again this tells us that we can add First(FT') to First(T) 39. Since First(F) doesn't contain that means that First(FT') is just First(F) 40. FIRST(E) = {} 41. FIRST(E') = {+,} 42. FIRST(T) = {'(',id} 43. FIRST(T') = {*,} 44. FIRST(F) = {'(',id} 45. Lastly we apply rule 3 to E TE' once again this tells us that we can add First(TE') to First(E) 46. Since First(T) doesn't contain that means that First(TE') is just First(T) 47. FIRST(E) = {'(',id} 48. FIRST(E') = {+,} 49. FIRST(T) = {'(',id} 50. FIRST(T') = {*,} 51. FIRST(F) = {'(',id} 52. Doing anything else doesn't change the sets so we are done!

53. We want to make Follow sets so first we list the sets we need 54. FOLLOW(E) = {} 55. FOLLOW(E') = {} 56. FOLLOW(T) ={} 57. FOLLOW(T') = {} 58. FOLLOW(F) = {} 59. The First thing we do is Add $ to the start Symbol 'E' 60. FOLLOW(E) = {$} 61. FOLLOW(E') = {} 62. FOLLOW(T) ={} 63. FOLLOW(T') = {} 64. FOLLOW(F) = {} 65. Next we apply rule 2 to E' +TE' This says that everything in First(E') except for should be in Follow(T) 66. FOLLOW(E) = {$} 67. FOLLOW(E') = {} 68. FOLLOW(T) ={+} 69. FOLLOW(T') = {} 70. FOLLOW(F) = {} 71. Next we apply rule 3 to E TE' This says that we should add everything in Follow(E) into Follow(E') 72. FOLLOW(E) = {$} 73. FOLLOW(E') = {$}

74. FOLLOW(T) ={+} 75. FOLLOW(T') = {} 76. FOLLOW(F) = {} 77. Next we apply rule 3 to T FT' This says that we should add everything in Follow(T) into Follow(T') 78. FOLLOW(E) = {$} 79. FOLLOW(E') = {$} 80. FOLLOW(T) ={+} 81. FOLLOW(T') = {+} 82. FOLLOW(F) = {} 83. Now we apply rule 2 to T' *FT' This says that everything in First(T') except for should be in Follow(F) 84. FOLLOW(E) = {$} 85. FOLLOW(E') = {$} 86. FOLLOW(T) ={+} 87. FOLLOW(T') = {+} 88. FOLLOW(F) = {*} 89. Now we apply rule 2 to F (E) This says that everything in First(')') should be in Follow(E) 90. FOLLOW(E) = {$,)} 91. FOLLOW(E') = {$} 92. FOLLOW(T) ={+} 93. FOLLOW(T') = {+} 94. FOLLOW(F) = {*} 95. Next we apply rule 3 to E TE' This says that we should add everything in Follow(E) into Follow(E') 96. FOLLOW(E) = {$,)} 97. FOLLOW(E') = {$,)} 98. FOLLOW(T) = {+} 99. FOLLOW(T') = {+} 100. FOLLOW(F) = {*} 101. Next we apply rule 4 to E' +TE' This says that we should add everything in Follow(E') into Follow(T) (because First(E') contains ) 102. FOLLOW(E) = {$,)} 103. FOLLOW(E') = {$,)} 104. FOLLOW(T) = {+,$,)} 105. FOLLOW(T') = {+} 106. FOLLOW(F) = {*} 107. Next we apply rule 3 to T FT' This says that we should add everything in Follow(T) into Follow(T') 108. FOLLOW(E) = {$,)} 109. FOLLOW(E') = {$,)} 110. FOLLOW(T) = {+,$,)} 111. FOLLOW(T') = {+,$,)} 112. FOLLOW(F) = {*}

113. Finaly we apply rule 4 to T' *FT' This says that we should add everything in Follow(T') into Follow(F) 114. FOLLOW(E) = {$,)} 115. FOLLOW(E') = {$,)} 116. FOLLOW(T) = {+,$,)} 117. FOLLOW(T') = {+,$,)} 118. FOLLOW(F) = {*,+,$,)}

Rules for First Sets


1. 2. 3. 4. If X is a terminal then First(X) is just X! If there is a Production X then add to first(X) If there is a Production X Y1Y2..Yk then add first(Y1Y2..Yk) to first(X) First(Y1Y2..Yk) is either 1. First(Y1) (if First(Y1) doesn't contain ) 2. OR (if First(Y1) does contain ) then First (Y1Y2..Yk) is everything in First(Y1) <except for > as well as everything in First(Y2..Yk) 3. If First(Y1) First(Y2)..First(Yk) all contain then add to First(Y1Y2..Yk) as well.

Rules for Follow Sets


1. First put $ (the end of input marker) in Follow(S) (S is the start symbol) 2. If there is a production A aBb, (where a can be a whole string) then everything in FIRST(b) except for is placed in FOLLOW(B). 3. If there is a production A aB, then everything in FOLLOW(A) is in FOLLOW(B) 4. If there is a production A aBb, where FIRST(b) contains , then everything in FOLLOW(A) is in FOLLOW(B)

5. trailing(E) : 6. EE+T --In this the trailing is '+' 7. E+TF [using TTF] -- Here trailing is '' 8. E+T(E) [using F(E)] -- Here trailing is ')' 9. (from E+TF) E+Tid [using Fid] -- Here trailing is 'id' 10. Hence trailing(E) = {+,,),id} 11. So the lesson here is that you must use the right sentential forms also and not just the sentence to find out the trailing. 12. Note : EE+TE+TFE+FidT+ididF+idid are all right sentential forms of E as they have at least one non-terminal. 13. However id+idid is a sentence because it has no non-terminals and you must not use just the sentence to find out the trailing.

You might also like