0% found this document useful (0 votes)
20 views

Unit 2 - Session 7

The document discusses the computation of FIRST sets for context-free grammars. It defines what a FIRST set is, provides rules for computing FIRST sets, and gives examples of applying the rules to compute FIRST sets for various grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit 2 - Session 7

The document discusses the computation of FIRST sets for context-free grammars. It defines what a FIRST set is, provides rules for computing FIRST sets, and gives examples of applying the rules to compute FIRST sets for various grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

18CSC304J

COMPILER DESIGN

UNIT 2
SESSION 7
Topics that will be covered in this Session

• Computation of FIRST
• Problems related to FIRST
COMPUTATION OF
FIRST
FIRST and FOLLOW – Introduction

• The functions FIRST and FOLLOW help in the construction of both top-down and bottom-up
parsers, associated with a grammar G

• During top-down parsing, FIRST and FOLLOW allows us to choose which production to apply,
based on the next input symbol

• During panic-mode error recovery, sets of tokens produced by FOLLOW can be used as
synchronizing tokens
Computation of FIRST
Definition

• Let  be any set of grammar symbols

• FIRST() is defined as the set of terminals that begin strings derived from 

Rules to compute FIRST(X)

Apply the following rules until no more terminals or ε can be added to any FIRST set

1. If X is a terminal, then FIRST(X)={X}

2. If X is a nonterminal and X→Y1Y2…Yk is a production for some k≥1, then

• Add everything in FIRST(Y1) to FIRST(X) except ε

• If FIRST(Y1) has ε, then add everything in FIRST(Y2) to FIRST(X) except ε


• If FIRST(Yk-1) has ε, then add everything in FIRST(Yk) to FIRST(X) including ε

3. If X→ ε is a production, then add ε to FIRST(X)


PROBLEMS RELATED
TO FIRST
Example 1
• Consider the grammar

• Compute the function FIRST for all non terminals

FIRST(E) = FIRST(T)

FIRST(T) = FIRST(F)

FIRST(F) = { (, id } So, FIRST(E) = FIRST(T) = { (, id }

FIRST(E′) = { +,ε }

FIRST(T′) = { *,ε }
Example 2
• Consider the grammar

S→(L)|a

L→L,S|S

• Compute the function FIRST for all non terminals

FIRST(S) = { (, a )

FIRST(L) = FIRST(S) = { (, a )
Example 3
• Consider the grammar

S→cAd

A → a A′

A′ → b | ε

• Compute the function FIRST for all non terminals

FIRST(S) = { c )

FIRST(A) = { a )

FIRST(A′) = { b , ε }
Example 4
• Consider the grammar

S→L=R|R

L → * R | id

R→L

• Compute the function FIRST for all non terminals

FIRST(S) = FIRST(L) and FIRST(R)

FIRST(L) = { * , id )

FIRST(R) = FIRST(L) = { * , id }

Therefore FIRST(S) = { * , id }

You might also like