1 / 16

How to construct an LL(1) parsing table ?

How to construct an LL(1) parsing table ?. S  A S b S  C A  a C  c C C  . LL(1) Parsing Table. The idea is easy:. What is the first terminal symbols you will see from deriving AS b , C , a , … and so on?. First Sets. N : the set of non-terminal symbols

shaina
Download Presentation

How to construct an LL(1) parsing table ?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to construct an LL(1) parsing table? • S A S b • S  C • A  a • C  c C • C   LL(1) Parsing Table The idea is easy: What is the first terminal symbols you will see from deriving ASb,C, a, … and so on? IT 327

  2. First Sets N : the set of non-terminal symbols T : the set of terminal symbols Let ω ( N  T )*. Definition: First set of ω, First(ω), is defined as follows: First(ω) = The set of every first (the left most) terminal symbol that appears in any possible string derived from ω. If ω derives , then let   First(ω); otherwise   First(ω); . * * First(ω) = { a | (a  T and ω a ω’ ) or ( ω a and a = )} IT 327

  3. S A S b • S  C • A  a • C  c C • C   First-set Examples • First() = {} • First(c C ) = {c} • First(a) = {a} • First(C)= First(c C )  First() = {c, } • First(ASb) = First(aSb) = {a} • First(S) = First(ASb)  First(C)= {a , c, } • First(Sb) = (First(S)- {})  First(b) = {a , b , c} IT 327

  4. N : the set of non-terminal symbols T : the set of terminal symbols Let a T, A N, ωi ( N  T )* Algorithm to find First Sets • First() = {} • First(a ω) = {a} • If A ω1 |ω2 ......... |ωn • First(A) = First(ω1 )  First(ω2)  .....  First(ωn) • Assume ω : •  First(A), • then First(Aω) = First(A) •  First(A), • then First(Aω) = (First(A) - {})  First(ω) IT 327

  5. N : the set of non-terminal symbols T : the set of terminal symbols More (?) algorithmic procedure • First(x): • if (x = ) return {}; • if (x = a ωand a  T) return {a}; • if (x = A , A N and A ω1 |ω2 ......... |ωn) • return First(ω1 )  First(ω2)  .....  First(ωn); • if (x = Aωand ω ) • if ( First(A)) • return First(A); • else • return (First(A) - {})  First(ω); IT 327

  6. Follow Sets N : the set of non-terminal symbols T : the set of terminal symbols let A, S  N and S is the start symbol Definition: Follow(A), the follow set of A, is defined as follows: Follow (A) = The set of every terminal symbol (including $) that can appear to the right of A in any possible sentential form derived from the start symbol. Follow(A) = { a | S ωAa ω’ and a  T} * IT 327

  7. Rules (recursive) for finding Follow Sets S …………Aa... Let a T, A, B N, and ,  ( N  T )* S$ • If S is the start symbol, then let $ Follow (S). • If A B, then let Follow (A)  Follow (B). • If A B  •  First () , then letFirst () Follow (B). •  First () , then let • (First () - {})  Follow (A)  Follow (B). …Ax…  … Bx… …Ax…  … Bx… IT 327

  8. S A S b • S  C • A  a • C  c C • C   Follow-set Examples • Follow (S) = {b, $} • S is the start symbol, $  Follow (S) • S A S b,b  Follow (S) • Follow (A ) = {a, b, c} • S A S b, First (S b) Follow (A). • First (S b) = (First (S)- {})  First (b) = {a , b , c} • Follow (C) = {b, $} • S  C, Follow (S) Follow (C) • C  c C, Follow (C) Follow (C) IT 327

  9. Convert rules(definitions) of follow sets into an algorithm. How? Let a T, A, B N, and ,  ( N  T )* S …………Aa... • If S is the start symbol, then let $ Follow (S). • If A B, then let Follow (A)  Follow (B). • If A B  •  First () , then letFirst () Follow (B). •  First () , then let • (First () - {})  Follow (A)  Follow (B). Check every rule and work on every Non-terminal symbols appearing in the right hand side of the production rules. IT 327

  10. Principle: repeatedly updating until nothing will further change. • Initialize Follow(X) =  for every X N; • Let Follow (S) = {$}. • Repeat the following procedure until every follow-set stops changing: Check every rule and work on every non-terminal symbol appeared in the right hand side of the rule. • If A B, and B N , then • add every element in Follow (A) into Follow (B). • If A B , and B N , then • if  First (), then add every element inFirst () into Follow (B). • if  First (), then add every terminal symbol in First () and Follow(A) into Follow (B).(note:   T) IT 327

  11. Construction of LL(1) parsing talbes For ever production rule A ω, do the following : • For every a First (ω) and a , put A ω into T [A, a] • If  First (ω) , then for every a Follow (A), • put A ω into T [A, a] First (ASb) = {a}, First (C) = {c, }, • S A S b • S  C • A  a • C  c C • C   1 2 2 2 Follow (S) = {b, $}, 3 First (a) = {a}, First (cC) = {c}, First () = {}, 5 4 5 Follow (C) = {b, $} IT 327

  12. Condition for being an LL(1) grammar: For every A N with A , A   and    • First ()  First () = . • If  First (), then First ()  Follow (A) = . Q: Can an LL(1) grammar be ambiguous? A: No. IT 327

  13. Another Example: • E T E’ • E’ + T E’ • E’  • T  F T’ • T’  * F T’ • T’  • F  ( E ) • F  id • E E + T • ET • T  T * F • T  F • F  ( E ) • F  id IT 327

  14. First and Follow Sets First (F) = { (, id } First (T’) = { *,  } First (T) = First (FT’) = { (, id } First (E’) = { +,  } First (E) = First (TE’) = {(, id } • E T E’ • E’ + T E’ • E’  • T  F T’ • T’  * F T’ • T’  • F  ( E ) • F  id Follow (E) Follow (E’) Follow (T) Follow (T’) Follow (F) {$ } {} {} {} {} {), $ } {} {+} {} {*} { ), $ } {), $ } { +,), $ } { +} { +,*} { ), $ } {), $ } { +,), $ } {+,), $ } {+ ,*, ), $ } initialization E to E’ and T T to T’ and F check those caused by first sets check those caused by the follow sets from the previous stage IT 327

  15. First Follow LL(1) Parsing Table E E’ T T’ F { (, id } {+,  } { (, id } {*,  } {(, id } { ), $ } {), $ } { +,), $ } {+,), $ } {+ ,*, ), $ } • E T E’ • E’ + T E’ • E’  • T  F T’ • T’  * F T’ • T’  • F  ( E ) • F  id 1 1 2 3 3 4 4 6 5 6 6 8 7 IT 327

  16. id*(id+id) The entry [E, id] in the Parsing table: LL(1)_PT[E, id] = 1 • E •  T E’ • F T’ E’ • idT’ E’ • id *F T’ E’ • id * (E )T’ E’ • id * (T E’ ) T’ E’ • id * (F T’ E’)T’ E’ • id * ( idT’ E’)T’ E’ • id * ( idE’)T’ E’ • id * ( id+TE’)T’ E’ • id * ( id +F T’ E’)T’ E’ • id * ( id + idT’ E’)T’ E’ • id * ( id + id E’)T’ E’ • id * ( id + id)T’ E’ • id * ( id + id)E’ • id * ( id + id) • E T E’ • E’ + T E’ • E’  • T  F T’ • T’  * F T’ • T’  • F  ( E ) • F  id left-most derivation IT 327

More Related