0% found this document useful (0 votes)
52 views4 pages

SPCC Writeup 1

The document outlines the steps to compute the First and Follow sets of a context-free grammar, including removing ambiguity and left-recursion from the original grammar, and provides an example of finding the First and Follow sets to design predictive parsing tables for a modified grammar describing arithmetic expressions. The purpose is for students to learn how to analyze grammars and construct parsers using First and Follow sets.

Uploaded by

Dhyey Valera
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)
52 views4 pages

SPCC Writeup 1

The document outlines the steps to compute the First and Follow sets of a context-free grammar, including removing ambiguity and left-recursion from the original grammar, and provides an example of finding the First and Follow sets to design predictive parsing tables for a modified grammar describing arithmetic expressions. The purpose is for students to learn how to analyze grammars and construct parsers using First and Follow sets.

Uploaded by

Dhyey Valera
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/ 4

Experiment 01 : First () and Follow() Set

Learning Objective: Student should be able to Compute First () and Follow () set of given grammar.

Tools: Jdk1.8,Turbo C/C++, Python, Notepad++

Theory:

1.Algorithmto Compute FIRST as follows:

 Let a be a string of terminals and non-terminals.


 First (a) is the set of all terminals that can begin strings derived from a.

Compute FIRST(X) as follows:

a) if X is a terminal, then FIRST(X)={X}

b) if X is a production, then add to FIRST(X)

c) if X is a non-terminal and X Y1Y2...Ynis a production, add FIRST(Yi) to


FIRST(X) if the preceding Yjs contain in their FIRSTs

2. Algorithm to Compute FOLLOW as follows:

a) FOLLOW(S) contains EOF

b) For productions A B , everything in FIRST ( ) except goes into


FOLLOW (B)

c) For productions A B or A B where FIRST ( ) contains , FOLLOW(B)


contains everything that is in FOLLOW(A)

Original grammar:

E E+E
E E*E
E (E)
E id

This grammar is left-recursive, ambiguous and requires left-factoring. It needs to


be modified before we build a predictive parser for it:

Step 1: Remove Ambiguity.

E E+T
T T*F
F (E)
F id

Grammar is left recursive hence Remove left recursion:

E TE'

E' +TE'|
T FT'

T' *FT'|
F (E)
F id

Step 2: Grammar is already left factored.

Step 3: Find First & Follow set to construct predictive parser table:-

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

FIRST (E') = {+, }

FIRST (T') = {*, }

FOLLOW (E) = FOLLOW(E') = {$, )}


FOLLOW (T) = FOLLOW(T') = {+, $, )}

FOLLOW (F) = {*, +, $, )}

Example:
E TE'

E' +TE'|
T FT'

T' *FT'|
F (E)
F id

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

FIRST (E') = {+, }

FIRST (T') = {*, }

FOLLOW (E) = FOLLOW (E') = {$, )}

FOLLOW (T) = FOLLOW (T') = {+, $, )}

FOLLOW (F) = {*, +, $, )}

Application: To desige Top Down and Bottom up Parsers.

Design:
Result and Discussion:

Learning Outcomes: The student should have the ability to


LO1: Identify type of grammar G.
LO2: Define First () and Follow () sets.
LO3: FindFirst () and Follow () sets for given grammar G.
LO4: Apply First () and Follow () sets for designing Top Down and Bottom up Parsers

Course Outcomes: Upon completion of the course students will be able to analyze the analysis
and synthesis phase of compiler for writhing application programs and construct different parsers for
given context free grammars.

Conclusion:

For Faculty Use

Correction Formative Timely completion Attendance /


Parameters Assessmen of Practical [ 40%] Learning
t [40%] Attitude [20%]

Marks
Obtained

You might also like