First and Follow

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

ashwinichhipa

A topnotch WordPress.com site


Menu
Skip to content

Home About

Tag Archives: Compiler Design FIRST and FOLLOW set


6 Replies Hi everyone! Many of us feel Compiler Design as a difficult subject. They feel that there are many algorithms and if you dont follow the algorithms, you wont be able to solve the problem statements. Some of us just cant get whats going on in an algorithm, its like a tedious job to trace it and apply on the problem statement. Some of us feel difficulties in finding FIRST and FOLLOW sets of a given grammar, the base of Syntax Analysis, which allow us to choose which production to apply based on next input symbol. Here well see how anyone can find FIRST and FOLLOW sets in an easy way. (For Algorithms please refer any good reference book.) First and Follow sets will be found for every non-terminal. (FIRST of any terminal is the same terminal itself.) FIRST: FIRST is nothing but a set of symbols (terminals and epsilon) that can appear as the first symbol of any non-terminals production. LikeA > abB/Ce B > d

C > f/ In this if we want to find the FIRST of every non-terminal (A,B,C), then ( i ) FIRST (A)={a,f,e} ,but How?

See the production of A, it gives A > abB/Ce , now from A> abB, the first symbol is a so FIRST(A)={a}. Now from A > Ce ,the first symbol is C ,which has its own production, and can give either f or (epsilon). When C > f ,the start symbol of As production will be f (as A > fe) and when C > , the As production will be A > e ,i.e. A > e. Hence FIRST(A)={f,e}. So, FIRST(A)={a,f,e}. ( ii ) FIRST(B)={b} ( iii ) FIRST(C)={f, }. Similar way we can have production set in which As first symbol is B, Bs is C, Cs D and so on We will replace all the possible combinations. Now I hope that you have got a little idea of FIRST without knowing your algorithm (which you can find in any reference book). Now well see how to find FOLLOW set, which is a very big head ache for many of us. FOLLOW: Pre-requisite: You should know how to find FIRST set properly (so better practice some questions). If your FIRST set is wrong then everything will go wrong later. The FOLLOW set will contain all the symbols (terminals, $ symbol) which are following a nonterminal (or can follow indirectly). NOTE: FOLLOW of START symbol will always contain $ symbol. Steps( i ) Always check the right side of the productions for a non-terminal, whose FOLLOW set is being found. ( never see the left side ). ( ii ) (a) If that non-terminal (S,A,B) is followed by any terminal (a,b,*,+,(,)) , then add that terminal into FOLLOW set.

(b) If that non-terminal is followed by any other non-terminal then add FIRST of other nonterminal into FOLLOW set. (Because, that other non-terminal can be replaced by its FIRST set, which will follow our nonterminal). Example: In S > Aa/AB, (where A and B have their own productions), if we want to find the FOLLOW(A) then it will have {a,FIRST(B)}, because A is followed by a and B, so FOLLOW(A) will have a and FIRST(B) ,as a is a terminal and B is non-terminal. ( iii ) If our non-terminal is the last symbol, and nothing is following it or it is followed by any other non-terminal which can give directly or indirectly then add the FOLLOW of LEFT SIDE one non-terminals into our FOLLOW set. ExampleS > aSA/AB A > ab/cB B > f/ Here as we can see, in finding FOLLOW of A, in production S > aSA/AB , A is followed by nothing and followed by B, which gives , so we will add all the symbols of Follow(S) into FOLLOW(A). NOTE: Be careful in analyzing the productions, many things might appear indirectly. Now we will see an another grammar and try to find out the FIRST and FOLLOW set for itConsider a given grammar productions areS > AB A > a B > C/b C > D D > E E > a (Here terminals are-S,A,B,C,D,E) (non ternimals-a,b) (start symbol=S) {FOLLOW(A)=FOLLOW(S), . B > }

FIRSTFIRST(S)={a} FIRST(C)={a} FOLLOWFOLLOW(S)={$} FOLLOW(A)={a,b} FOLLOW(B)={$} FOLLOW(C)={$} FOLLOW(D)={$} FOLLOW(E)={$} (=FIRST(B)) (=FOLLOW(S)) (=FOLLOW(B)) (=FOLLOW(C)) (=FOLLOW(D)) FIRST(A)={a} FIRST(D)={a} FIRST(B)={a,b} FIRST(E)={a}

Hope this will be helpful for you, feel free to comment and ask any doubts regarding this. This entry was posted in Uncategorized and tagged Compiler Design, FIRST set, FOLLOW set, Syntax Analysis on March 12, 2013 by ashwinichhipa. The Twenty Twelve Theme. Blog at WordPress.com. Follow

Follow ashwinichhipa
Get every new post delivered to your Inbox.
subscribe 48392861 http://ashw inichh loggedout-follow 44f89c69d4 /tag/compiler-des

Sign me up

Powered by WordPress.com

You might also like