WELCOME TO A
JOURNEY TO
CS419

Dr. Hussien Sharaf
Computer Science Department

dr.sharaf@from-masr.com
FIRST


1.
2.
3.

Let X be a grammar symbol (a terminal or nonterminal or
), then the FIRST(X) can be computed by the following rules:
If X is terminal, then FIRST(X) is {X}.
If X → is a production, then add to FIRST(X).
If X is nontermianl and X → Y1 Y2 …… Yk is a production, then
place a in FIRST(X) if for some i, a is in FIRST(Yi), and is in
all of FIRST(Y1), ……., FIRST(Yi-1); that is, Y1 …...Yi-1 → . If is
in FIRST(Yj) for all j = 1, 2, ……., k, then add to FIRST(X).
For example ,everything in FIRST(Y1) is surely in FIRST(X). If
Y1 doesn’t derive , then we add nothing more to
FIRST(X), but if Y1 → , then we add FIRST(Y2) and so on.
Ali
Hussien

Dr. Hussien M. Sharaf

First(Ali)

Ali
2
FIRST SET EXAMPLE
Consider the following grammar:
E → TQ
Q → +TQ |
T → FR
R → *FR |
F → (E) | id
Then,
FIRST(E) = FIRST(T) = FIRST(F) = {(, id}.
FIRST(Q) = {+, }
FIRST(R) = {*, }


Dr. Hussien M. Sharaf

3
FOLLOW
To compute FOLLOW(A) for all nonterminals A, then
apply the following rules until nothing can be added to
any FOLLOW set:
1. Place $ in FOLLOW(S), where S is the start symbol and
$ is the input right endmarker.
i.e. $ ∈ FOLLOW(S)
2. If there is a production A → αBβ, then everything in
FIRST(β) except for is placed in FOLLOW(B).


i.e. (FIRST(β) ~

) ⊆ FOLLOW(B)

Ali Plays
Dr. Hussien M. Sharaf

Follow of Ali

Plays
4
FOLLOW
If there is a production A → αB, or [a production A
→ αBβ where FIRST(β) contains (i.e β → )], then
everything in FOLLOW(A) is in FOLLOW(B).
i.e. FOLLOW(A) ⊆ FOLLOW(B)
3.

Ali Plays
Dr. Hussien M. Sharaf

Follow of Ali

Plays
5
FOLLOW SET EXAMPLE
Consider again the previous grammar:
E → TQ
Q → +TQ |
First (‘)’) ⊆
T → FR
Follow(E) because
F → (E)
R → *FR |
E is the start
F → (E) | id
symbol
Then,
First(Q) ⊆ Follow(T)
Because 1. E → TQ
FOLLOW(E) = FOLLOW(Q) = { ) , $ }.
FOLLOW(T) = FOLLOW(R) ={ +, ), $ }
Follow(E) ⊆
Follow(T)
FOLLOW(F) = {*, +, ), $}


Because 2. Q →

Dr. Hussien M. Sharaf

First(Q) ⊆
Follow(T)
Because E → TQ

6
FOLLOW SET EXAMPLE
Consider again the previous grammar:
E → TQ
Q → +TQ |
T → FR
R → *FR |
F → (E) | id
FOLLOW(E) = FOLLOW(Q) = { ) , $ }.
FOLLOW(T) = FOLLOW(R) ={ +, ), $ }
FOLLOW(F) = { * , + , ) , $ }


First(R) ⊆ Follow(F)
Because T→ FR

Dr. Hussien M. Sharaf

First(R) ⊆ Follow(F)
Because 1. T→ FR
Follow(T) ⊆ Follow(F)
Because 2. R →

7
EXAMPLE 1

[ Alfred V. Aho, Compilers Principles, Techniques, and
Tools, 1986, Addison-Wesley, page 188]

Construct the parsing table for the following grammar:
1. E
TQ
2. Q
+TQ |
3. T → FR

4. R
5. F
E
Q

Dr. Hussien M. Sharaf

T
R
F

First Set
{(, id}
{+, }
{(, id}
{*, }
{(, id}

*FR |
(E) | id
Follow set
{$, )}
{$, )}
{+, ), $ }
{+, ), $}
{*, +, ), $}
8
EXAMPLE 1 (CONT.)
The parsing table for the previous grammar is:
1. E
TQ
2. Q
+TQ |
3. T → FR
id
E E

+

(
E

Q

id

Dr. Hussien M. Sharaf

$

TQ
Q

Q

R

R

T → FR

R
F

*FR |
(E) | id
)

+TQ

T → FR

R
F

*

TQ

Q
T

4. R
5. F

R

*FR
F

(E)
9
FIRST AND FOLLOW EXAMPLE
S →A
 A → BC | DBC
 B → bB ΄|
 B ΄→ bB΄|
 C →c|
 D →a|d
FIRST(S) = { a, b, c, d,
FIRST(A) = { a, b, c, d,
FIRST(B) = { b, }
FIRST(B ΄) = { b, }
FIRST(C) = { c, }
FIRST(D) = { a, d }


Dr. Hussien M. Sharaf

}
}

FOLLOW(S) = { $ }
FOLLOW(A) = { $ }
FOLLOW(B) = { c, $ }
FOLLOW(B ΄) ={ c, $ }
FOLLOW(C) = { $ }
FOLLOW(D) = { b, c, $ }
10
EXAMPLE 2 PAGE 156
The parsing table for the following grammar is:
1. S

If_Stat | other

2. If_Stat

if(Exp) S Else_part
if

S

If_Stat

S

3. Else_part

If_Stat S

other

4. Exp

else

else S |

true | false
true

false

$

other

If_Stat
if(exp) S
Else_part

Else_part

Exp

Dr. Hussien M. Sharaf

Else_part
else S

Else_part
exp
true

exp
false
11
EXAMPLE 3
The parsing table for the following grammar is:
1. E

TQ

6. R

*FR

2.
3.
4.
5.

+TQ
-TQ

7. R
8. R
9. F
10.F

/FR

Q
Q
Q
T

FR
+

-

*

/

E
Q

(

)

1
2

3

F
Dr. Hussien M. Sharaf

8

6

7

4

5
8

9

$

1

5
8

id

4

T
R

(E)
id

8
10
12
THANK YOU

Cs419 lec9 constructing parsing table ll1

  • 1.
    WELCOME TO A JOURNEYTO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2.
    FIRST  1. 2. 3. Let X bea grammar symbol (a terminal or nonterminal or ), then the FIRST(X) can be computed by the following rules: If X is terminal, then FIRST(X) is {X}. If X → is a production, then add to FIRST(X). If X is nontermianl and X → Y1 Y2 …… Yk is a production, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and is in all of FIRST(Y1), ……., FIRST(Yi-1); that is, Y1 …...Yi-1 → . If is in FIRST(Yj) for all j = 1, 2, ……., k, then add to FIRST(X). For example ,everything in FIRST(Y1) is surely in FIRST(X). If Y1 doesn’t derive , then we add nothing more to FIRST(X), but if Y1 → , then we add FIRST(Y2) and so on. Ali Hussien Dr. Hussien M. Sharaf First(Ali) Ali 2
  • 3.
    FIRST SET EXAMPLE Considerthe following grammar: E → TQ Q → +TQ | T → FR R → *FR | F → (E) | id Then, FIRST(E) = FIRST(T) = FIRST(F) = {(, id}. FIRST(Q) = {+, } FIRST(R) = {*, }  Dr. Hussien M. Sharaf 3
  • 4.
    FOLLOW To compute FOLLOW(A)for all nonterminals A, then apply the following rules until nothing can be added to any FOLLOW set: 1. Place $ in FOLLOW(S), where S is the start symbol and $ is the input right endmarker. i.e. $ ∈ FOLLOW(S) 2. If there is a production A → αBβ, then everything in FIRST(β) except for is placed in FOLLOW(B).  i.e. (FIRST(β) ~ ) ⊆ FOLLOW(B) Ali Plays Dr. Hussien M. Sharaf Follow of Ali Plays 4
  • 5.
    FOLLOW If there isa production A → αB, or [a production A → αBβ where FIRST(β) contains (i.e β → )], then everything in FOLLOW(A) is in FOLLOW(B). i.e. FOLLOW(A) ⊆ FOLLOW(B) 3. Ali Plays Dr. Hussien M. Sharaf Follow of Ali Plays 5
  • 6.
    FOLLOW SET EXAMPLE Consideragain the previous grammar: E → TQ Q → +TQ | First (‘)’) ⊆ T → FR Follow(E) because F → (E) R → *FR | E is the start F → (E) | id symbol Then, First(Q) ⊆ Follow(T) Because 1. E → TQ FOLLOW(E) = FOLLOW(Q) = { ) , $ }. FOLLOW(T) = FOLLOW(R) ={ +, ), $ } Follow(E) ⊆ Follow(T) FOLLOW(F) = {*, +, ), $}  Because 2. Q → Dr. Hussien M. Sharaf First(Q) ⊆ Follow(T) Because E → TQ 6
  • 7.
    FOLLOW SET EXAMPLE Consideragain the previous grammar: E → TQ Q → +TQ | T → FR R → *FR | F → (E) | id FOLLOW(E) = FOLLOW(Q) = { ) , $ }. FOLLOW(T) = FOLLOW(R) ={ +, ), $ } FOLLOW(F) = { * , + , ) , $ }  First(R) ⊆ Follow(F) Because T→ FR Dr. Hussien M. Sharaf First(R) ⊆ Follow(F) Because 1. T→ FR Follow(T) ⊆ Follow(F) Because 2. R → 7
  • 8.
    EXAMPLE 1 [ AlfredV. Aho, Compilers Principles, Techniques, and Tools, 1986, Addison-Wesley, page 188] Construct the parsing table for the following grammar: 1. E TQ 2. Q +TQ | 3. T → FR 4. R 5. F E Q Dr. Hussien M. Sharaf T R F First Set {(, id} {+, } {(, id} {*, } {(, id} *FR | (E) | id Follow set {$, )} {$, )} {+, ), $ } {+, ), $} {*, +, ), $} 8
  • 9.
    EXAMPLE 1 (CONT.) Theparsing table for the previous grammar is: 1. E TQ 2. Q +TQ | 3. T → FR id E E + ( E Q id Dr. Hussien M. Sharaf $ TQ Q Q R R T → FR R F *FR | (E) | id ) +TQ T → FR R F * TQ Q T 4. R 5. F R *FR F (E) 9
  • 10.
    FIRST AND FOLLOWEXAMPLE S →A  A → BC | DBC  B → bB ΄|  B ΄→ bB΄|  C →c|  D →a|d FIRST(S) = { a, b, c, d, FIRST(A) = { a, b, c, d, FIRST(B) = { b, } FIRST(B ΄) = { b, } FIRST(C) = { c, } FIRST(D) = { a, d }  Dr. Hussien M. Sharaf } } FOLLOW(S) = { $ } FOLLOW(A) = { $ } FOLLOW(B) = { c, $ } FOLLOW(B ΄) ={ c, $ } FOLLOW(C) = { $ } FOLLOW(D) = { b, c, $ } 10
  • 11.
    EXAMPLE 2 PAGE156 The parsing table for the following grammar is: 1. S If_Stat | other 2. If_Stat if(Exp) S Else_part if S If_Stat S 3. Else_part If_Stat S other 4. Exp else else S | true | false true false $ other If_Stat if(exp) S Else_part Else_part Exp Dr. Hussien M. Sharaf Else_part else S Else_part exp true exp false 11
  • 12.
    EXAMPLE 3 The parsingtable for the following grammar is: 1. E TQ 6. R *FR 2. 3. 4. 5. +TQ -TQ 7. R 8. R 9. F 10.F /FR Q Q Q T FR + - * / E Q ( ) 1 2 3 F Dr. Hussien M. Sharaf 8 6 7 4 5 8 9 $ 1 5 8 id 4 T R (E) id 8 10 12
  • 13.