How to construct an LL1 parsing table - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

How to construct an LL1 parsing table

Description:

Suppose the rule being checked is A B, and B N. add every element in Follow (A) to Follow (B). Suppose the rule being checked is A B , and B N. if First ... – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 16
Provided by: ChungC7
Category:
Tags: bb | construct | ll1 | parsing | table

less

Transcript and Presenter's Notes

Title: How to construct an LL1 parsing table


1
How to construct an LL(1) parsing table?
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 terminal symbol (the left most one) that
appears in any possible string derived from ?.
If ? derives ?, then let ? be in First(?) .
First Sets


First(?) a (a ? T and ? a ? )
or ( ? a and a ?)
2
First-set Examples
  • S ? A S b
  • S ? C
  • A ? a
  • C ? c C
  • C ? ?
  • 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

3
Algorithm to find First Sets
N the set of non-terminal symbols T the set
of terminal symbols Let a ? T, A ? N, ?i ? ( N
? T )
  • 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(?)

4
More (?) algorithmic procedure
N the set of non-terminal symbols T the set
of terminal symbols
  • 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(?)

5
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 set of A, Follow
(A), is defined as follows Follow (A) every
terminal symbol (including ) that can appear to
the right of A in some sentential form derived
from the start symbol.
Follow(A) a S ?Aa ? and a ? T

6
Rules of 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 let First (?) ? Follow (B).
  • ? ? First (?) , then let
  • (First (?) - ?) ? Follow (A) ? Follow (B).

Ax ? ?Bx
Ax ? ?B?x
7
Follow-set Examples
  • S ? A S b
  • S ? C
  • A ? a
  • C ? c C
  • C ? ?
  • 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)

8
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 let First (?) ? 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.
9
Principle repeatedly updating until nothing will
further changed.
  • 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 in
    First (?) into Follow (B).
  • if ? ? First (?), then add every terminal
    symbol in First (?) and Follow(A) into Follow
    (B). (note ? ? T)

10
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, ?, Follow
(S) b, , First (cC) c, First (?)
?, Follow (C) b,
  • S ? A S b
  • S ? C
  • A ? a
  • C ? c C
  • C ? ?

11
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.
12
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

13
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
14
LL(1) Parsing Table
First Follow
(, id , ? (, id , ? (, id
E E T T F
), ), , ), , ), ,, ),
  • 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
5
6
6
6
7
8
15
Derive id(idid)
Parsing LL(1)_PTE, id 1
  • E
  • ? T E
  • F T E
  • id T E
  • id F T E
  • id ( E ) T E
  • id ( T E ) T E
  • id ( F T E ) T E
  • id ( id T E ) T E
  • id ( id E ) T E
  • id ( id TE ) T E
  • id ( id F T E ) T E
  • id ( id id T 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
Write a Comment
User Comments (0)
About PowerShow.com