0% found this document useful (0 votes)
19 views8 pages

First Follow

The document discusses the computation of FIRST and FOLLOW sets for context-free grammars. It provides three examples, showing the derivation rules, variables, terminals, and computation of FIRST and FOLLOW sets for each grammar. The examples are intended to demonstrate how to perform this analysis for parsing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

First Follow

The document discusses the computation of FIRST and FOLLOW sets for context-free grammars. It provides three examples, showing the derivation rules, variables, terminals, and computation of FIRST and FOLLOW sets for each grammar. The examples are intended to demonstrate how to perform this analysis for parsing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DATE : 4/3/2021

COMPUTATION OF FIRST AND FOLLOW

A->S|Ɛ |c First (A)->{a,c, Ɛ}


S->BDx| Ɛ FIRST(A)=FIRST(S)={FIRST(B), Ɛ}= {c,e, Ɛ}
B-> Ɛ FIRST (A)=FIRST(S)= {d,c, Ɛ}

D-> Ɛ FIRST(A)= FIRST(S)={x,c, Ɛ}


Compute FIRST AND

EXAMPLE 1:

1.Consider the Grammar


E->TE’
E -> E+T | T
E’->+TE’| ε
T -> T*F | F
T->FT’
F -> (E) | id Eliminated left recursion T’->*FT’| ε
F -> (E) | id
Variables or Non terminals={E,E’,T,T’,F}
Terminals ={+,*,(,),id, ε }

FIRST:
FIRST (E) = FIRST (T)=FIRST(F) ={ (, id}
FIRST (E’) = {+, ε}
FIRST (T) = FIRST(F)={ (, id }
FIRST (T’) = {*, ε}
FIRST (F) = { (, id }

FOLLOW: {Should not write epcilon)

FOLLOW (E) = {$, )}


FOLLOW (E’) = FOLLOW (E) = {$, ) }
FOLLOW (T) = FIRST (E’) = {+, $, ) }
FOLLOW (T’) = FOLLOW(T)= {+, $, ) }
FOLLOW (F) = FIRST (T’) = { *, ε }= { * , +, $, ) }
EXAMPLE 2:
Consider the Grammar 1.S->iEtSS’
S->iEtSS’|a 2.S->a
S’-> eS| ε (Eliminated left recursion)
3.S’-> eS| ε
E->b 4. E->b

FIRST COMPUTATION
FIRST(S) = { i , a}
FIRST(S’) = { e, ε }
FIRST (E) = { b}

FOLLOW COMPUTATION
FOLLOW (S) = { $ (FIRST(S’)}= {$, e, ε } =
{$,e}
FOLLOW (S’) = FOLLOW(S) = {$,e}
Construction of predictive parsing table

Non- Input Symbol


Terminal id + * ( ) $
E E->TE’ E->TE’
E’ E’->+TE’ E’-> ε E’-> ε
T T->FT’ T->FT’
T’ T’-> ε T’->*FT’ T’-> ε T’-> ε
F F->id F-> (E)
id pop
+ pop
* - - pop -
( - pop
) pop
$ Accept
EXAMPLE: 3

S-> ( L ) | a
L-> L , S | S

1. S-> ( L ) | a
2. L->SL’
3. L’->, S L’| ε

FIRST
FIRST (S) = { ( , a }
FIRST (L) = FIRST (S) = { (, a }
FIRST (L’) = { , ε }
FOLLOW
FOLLOW (S) = {$, FIRST(L’) } = { $, , ε }
Therefore FOLLOW (S) ={ $ , , )}
FOLLOW (L) ={ ) }
FOLLOW (L’) = FOLLOW (L) = { ) }

You might also like