0% found this document useful (0 votes)
60 views1 page

First and Follow

This document defines and provides examples of FIRST and FOLLOW for parsing grammar rules. FIRST(X) is the set of terminals that can begin strings generated by non-terminal X. FOLLOW(X) is the set of terminals that can follow X. Formally, FIRST(α) is the first terminal of all strings derived from α, and FOLLOW(A) includes terminals after A when A is at the end of a rule or after what follows A when A is in the middle of a rule. An example grammar is provided where FIRST(S)={a} and FOLLOW(S)={b, $}.

Uploaded by

Sagar
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)
60 views1 page

First and Follow

This document defines and provides examples of FIRST and FOLLOW for parsing grammar rules. FIRST(X) is the set of terminals that can begin strings generated by non-terminal X. FOLLOW(X) is the set of terminals that can follow X. Formally, FIRST(α) is the first terminal of all strings derived from α, and FOLLOW(A) includes terminals after A when A is at the end of a rule or after what follows A when A is in the middle of a rule. An example grammar is provided where FIRST(S)={a} and FOLLOW(S)={b, $}.

Uploaded by

Sagar
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/ 1

FIRSTandFOLLOW(Neededforbuildingaparsetable)

ThesenoteswerecollectedbySamJ.Frazierbasedonthetwosourceslistedbelow.Thenoteswere
editedbyCarolZander.
a. Aho,A.etal.Compilers:Principles,Techniques,&Tools.2nded.Pearson:Boston,2007.
b. Zander,Carol.ClassLecture.UniversityofWashington,Bothell.Oct.26&28,2009.

InformalDefinition
ForthenonterminalX,FIRST(X)isthesetofterminalsinagrammarthatbeginanystringthatXcan
generate.Inotherwords,ifyouuseXtogenerateastring,whatterminalsstartthestring.
ForthenonterminalX,FOLLOW(X)isthesetofterminalsthatmayappearafterXinthegrammar.
Inotherwords,whengeneratingastring,whatterminalscouldfollowX.

FormalDefinition
,

FIRST()

FOLLOW(A)

and

(Aisinthemiddleofarule)

(Aisattheendofarule)

WhenAisinthemiddleofarule,itisfairlyclearthatwhatfollowsAiswhatisthefirstthingof .
Forthesecondpart,whenAisattheendofarule,hereisanexample:
B > ... A
X > ... BC
Y > ... Bd

UsingthefirstruleandplugginginforB,putstheAjustbeforetheC.
Ditto,putstheAjustbeforethed.BoththeCanddfollowB.

Example
Considerthisgrammar,G:
0. S>S$
1. S>aSbS
2. S>a
OurFIRSTandFOLLOWstatementsare:

FIRST(S)={a}
FOLLOW(S)={b,$}

You might also like