Lecture 08
Lecture 08
Lecture 08
PARSING
Introduction
2
TOP-DOWN PARSIN
G
Creating a top-down parser
5
An example follows.
Creating a top-down parser
(Cont.)
6
FIRST FOLLOW
S -> ABCDE {a,b,c} {$}
A -> a|€ {a, €} {b,c}
B -> b| € {b, €} {c}
C->c {c} {d,e,$}
D -> d | € {d, €} {e,$}
E -> e| € {e, €} {$}
FIRST and FOLLOW Example 1
12
FIRST FOLLOW
S -> Bb | Cd {a,b,c,d} {$}
B -> aB| € {a, € } {b}
C -> cC | € {c, €} {d}
FIRST and FOLLOW Example 2
13
FIRST FOLLOW
E -> TE’ {id,(} {$,)}
E’ -> +TE’|€ {+, €} {$,)}
T -> FT’ {id,(} {+,$,)}
T’ -> * FT’|€ {*, €} {+,$,)}
F -> id|(E) {id,(} {*,+,$,)}
FIRST and FOLLOW Example 3
14
FIRST FOLLOW
S->ACB|CbB|Ba {d,g,h,€,b,a} {$}
A -> da|BC {d,g,h, €} {h,g,$}
B-> g| € {g, €} {$,a,h,g}
C->h| € {h, €} {g,$,b,h}
15
T T->FT’ T->FT’
T’ T’->€ T’->*FT’ T’->€ T’->€
F16 F->id F->(E)
How does parsing table help
17
in parsing process?
S->(S) | € $ S
NT Input symbol
$ ) S (
( ) $
S S->(S) S->€ S->€ $ ) S
$ ) ) S (
( ( ) ) $ $ ) ) S
$ ) )
$ )
- ( id ) $
Expr 1 2 3
ExprTail 4 5 5
Var 6
VarTail 8 7 8 8
Exercise 2
Given the grammar:
S → i E t S S’ | a
S’ → e S | λ
E → b
22
Exercise 2 Solution
23
First(S) = {i, a}
First(S’) = {e, λ}
First (E) = {b}
S 2 1
S’ 3/4 4
E 5