50 likes | 138 Views
Learn how to construct a predictive parser table using the FIRST and FOLLOW sets. This lecture revisits the algorithm for table construction, detailing the process step by step with an error-free example table.
 
                
                E N D
Lecture # 14 Algorithm for table construction of Predictive Parser
FIRST (Revisited) • FIRST() = { the set of terminals that begin all strings derived from } • FIRST(a) = {a} if a  T • FIRST() = {} • FIRST(A) = A FIRST() for A  P • FIRST(X1X2…Xk) = if for all j = 1, …, i-1 :   FIRST(Xj) then add non- in FIRST(Xi) to FIRST(X1X2…Xk) if for all j = 1, …, k :   FIRST(Xj) then add  to FIRST(X1X2…Xk)
FOLLOW • FOLLOW(A) = { the set of terminals that can immediately follow nonterminalA }FOLLOW(A) = • for all (B  A )  P doadd FIRST()\{} to FOLLOW(A) • for all (B  A )  Pand   FIRST() doadd FOLLOW(B) to FOLLOW(A) • for all (B  A)  P doadd FOLLOW(B) to FOLLOW(A) • ifA is the start symbol Sthen add $ to FOLLOW(A)
Constructing an LL(1) Predictive Parsing Table for each production A  do for each a FIRST() do add A  to M[A,a]enddoif  FIRST() thenfor each b FOLLOW(A) do add A  to M[A,b]enddoendifenddoMark each undefined entry in M error
Example Table E T E’E’ +TE’|  T F T’T’ *FT’|  F ( E ) | id