You are on page 1of 10
from left to right. The second | ‘And the number 1 in the is! head) to predict the passe ‘¢ The data structures used by LL(1) are i) input buffer i) stack il) parsing table. «The LL(1) parser uses input buffer to store the input tokens. The stack is used to pola the left sentential form.qThe symbols in RES. of rule ae pushed into the stack in reverse order i.e. from right to left. Thus use of stack makes this algorithm | non-zecursive. The table is basically a two dimensional aray. The table haa row for non-terminal and column for terminals. The table can be represented as MIA.al ‘where A is a non-terminal and a is current input symbol. 's The parsing program reads top of the stack and a current input symbol. With the am ma ‘of parsing method is called table driven parsing algorithm. on of LL(1) parser can be defined by top of the stack and a ec tion is performed and the input is successfully parsed if the the halting configuration. tack is empty and next token is $ then it corresponds to successful jon of Predictive LL(1) Parser of predictive LL(1) parser is based on two very important functions and FOLLOW. of Predictive LL(1) parser we have to follow the following steps - of FIRST and FOLLOW function. 3-90. Sieg Parsing Table using FIRST and FOLLOW function, the help of Predictive Parsing Table. Compiler Design Step 2: Construct the Predictive Step 3 : Parse the input string with FIRST function TAIST(Q) is a set of terminal symbols that are first symbols appearing at Riis , derivation of c. If c.=oe then e is also in FIRST (a). Following are the rules used to compute the FIRST functions. 1. If the terminal symbol a the FIRST(a) = (a). 2. If there is a rule Xe then FIRST(X) = fe}- 43. For the rule A > X, Xp Xy -~%j FIRST(A) = (FIRSTOX,) U FIRSTOG) v FIRST(X, U FIRST). Where k X; Sn such that 1 TE’ is a rule in which the first symbol at RH. is T. Now T-» FT in which the asst symbol at RES. is F and there is a rule for Fas F > (B)|id. i FIRST(E) = FIRST(T) = FIRST(F) a ©) Foid | TFT —+ replace F by RHS. rule frIRST(E) = FIRST(D) = FIRST(® = ( (ia || ‘ie This can As Ef > *TE Ee by referring computation rule 2 The first symbol appearing at RHS. of production rule for E’ is added in the FIRST function. FIRST(T’)=(*, €) As TO" TE Ese ‘The first terminal symbol appearing at RH. of production rule for T’ is added in the FIRST function. Now we will compute FOLLOW function. FOLLOW(E) - 4) As there is a rule F-+ (E) the symbol ‘’ appears immediately to the right of B. Hence ‘will be in FOLLOW). ii) The computation rule is A + aBB we can map this rule with F + (E) then A =F, a=( B=E,p=). | FOLLOW(B) = FIRST() = FIRST() ) = { ) } FOLLOW¢€E) = { )} Since E is a start symbol, add $ to follow of E. teUOH =) ‘OLLowe? - 4) ETE’ the computational rule is A 0B - & A=£,a=T, B=’, fre then by computational rule 3 everything in ~ FOLLOW(A) is in FOLLOW(®) ic. everything in FOLLOW(E) is in FOLLOW(E’). * FOLLOWE) ={),$} ae, Syntax Anaya, 3-32 3 everything in Compiier Design ii) E’> +TE’ the computational rule is A 47, B = EY, Be then by FOLLOW(A) is in FOLLOW®) i. everything in ) is really following E. AE, a FOLLOWEE) = (8 1} We can observe in the given grammar that FOLLOW(T) - We have to observe two rules E >TE i) Consider E> TE’ we will map it with A >a BB A=Ea=¢B=T, ‘That is FOLLOW(1) = (FIRST(E) — €} {{re}-e} if) Consider E’> +TE’ we will map it with A >a BB Le. FOLLOW(E’) = FOLLOW(T) FOLLOW(T) = 0S) =v Ds) [FOLLOW FOLLOW(T) - Tor We will map this rule with A—aBB then A=T, FOLLOW(?) = FOLLOW() = {+.)$) Tor } We will map this rule with A—>aBB then) A: FOLLOW(T) = FOLLOW(D) = (+,)5) Hence|FOLLOW(T) = (+) 5} / Finally FOLLOW() = 98) ‘We can observe in the given grammar that + and ) are really following T. BB. computational rule FOLLOW(E) is in FOLLOW(), E> +E’ =E' by computational rule 2 FOLLOW() = (FIRST @) ~<} a=F, oF, BH TY, = A=F,0 = +,B=T,B =’ by computational rule 3 FOLLOW(A) = FOLLOW() Ber, B=e Bee’ then ther =) U +)S) ‘Compiler Design Input : The Context Free Grammar G. Output : Predictive Parsing table M. Algorithm : For the rule Aa of grammar G 1, For each a in FIRST(q) create entry M[A,a] = A — 0 where a is terminal symbol, 2. Fore in FIRST(q) create entry M[Ab] = A >a Where b is the symbols from FOLLOW(A). 3. If e is in FIRST(@) and $ is in FOLLOW(A) then create entry in the table MIA, $] = A >a. 4. All the remaining entries in the table M are marked as SYNTAX ERRO! Step 5: T>*FT" Ata A=Ta="FT FIRSTCFT) = {*) Hence M[T,*] = T > *FT” Step 6: Te Asa A=T,a=€ FOLLOW(T) = (+)S} Hence MIT’+] = Te MIr)]=T3e Ir’s] =Te Step 7: F>@) Aza A=Fa=@® FIRST((E)) = { (} Hence MIE,(J= F > (E) 8: Pid Ava A=Faz=id FIRST(id) = { id} Hence M[F,id] = F — id mplete table can be as shown below. eae string id + id + id § can be parsed using above table. At the initial the stack will contain start symbol E, in the input buffer input string is that the input is scanned from left to right and we always follow while parsing the input string, Also ata time only one input symbol action. Hence the name of this parser is LL(). The pond parser. The left recursion and ambiguous for LUA) parser,

You might also like