SlideShare una empresa de Scribd logo
1 de 13
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

Más contenido relacionado

La actualidad más candente

1.6 inverse function (optional)
1.6 inverse function (optional)1.6 inverse function (optional)
1.6 inverse function (optional)
math123c
 

La actualidad más candente (20)

Derivatives power point
Derivatives power point Derivatives power point
Derivatives power point
 
On Gr-Separation Axioms
 On Gr-Separation Axioms  On Gr-Separation Axioms
On Gr-Separation Axioms
 
Hw9
Hw9Hw9
Hw9
 
On Fuzzy  - Semi Open Sets and Fuzzy  - Semi Closed Sets in Fuzzy Topologic...
On Fuzzy  - Semi Open Sets and Fuzzy  - Semi Closed Sets in Fuzzy Topologic...On Fuzzy  - Semi Open Sets and Fuzzy  - Semi Closed Sets in Fuzzy Topologic...
On Fuzzy  - Semi Open Sets and Fuzzy  - Semi Closed Sets in Fuzzy Topologic...
 
Algorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and SortingAlgorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and Sorting
 
Operation on functions
Operation on functionsOperation on functions
Operation on functions
 
On Spaces of Entire Functions Having Slow Growth Represented By Dirichlet Series
On Spaces of Entire Functions Having Slow Growth Represented By Dirichlet SeriesOn Spaces of Entire Functions Having Slow Growth Represented By Dirichlet Series
On Spaces of Entire Functions Having Slow Growth Represented By Dirichlet Series
 
δ ˆ – Closed Sets in Ideal Topological Spaces
δ ˆ – Closed Sets in Ideal Topological Spacesδ ˆ – Closed Sets in Ideal Topological Spaces
δ ˆ – Closed Sets in Ideal Topological Spaces
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI)
 
Ejercicios 4
Ejercicios 4Ejercicios 4
Ejercicios 4
 
The Yoneda lemma and String diagrams
The Yoneda lemma and String diagramsThe Yoneda lemma and String diagrams
The Yoneda lemma and String diagrams
 
5.5 Injective and surjective functions. Dynamic slides.
5.5 Injective and surjective functions. Dynamic slides.5.5 Injective and surjective functions. Dynamic slides.
5.5 Injective and surjective functions. Dynamic slides.
 
Ring homomorphism
Ring homomorphismRing homomorphism
Ring homomorphism
 
Taylor's series
 Taylor's  series   Taylor's  series
Taylor's series
 
1.6 inverse function (optional)
1.6 inverse function (optional)1.6 inverse function (optional)
1.6 inverse function (optional)
 
Maximal and Minimal α -Open Sets
Maximal and Minimal α -Open SetsMaximal and Minimal α -Open Sets
Maximal and Minimal α -Open Sets
 
5.1 Defining and visualizing functions. Dynamic slides.
5.1 Defining and visualizing functions. Dynamic slides.5.1 Defining and visualizing functions. Dynamic slides.
5.1 Defining and visualizing functions. Dynamic slides.
 
Calculus II - 21
Calculus II - 21Calculus II - 21
Calculus II - 21
 
5.8 Permutations (dynamic slides)
5.8 Permutations (dynamic slides)5.8 Permutations (dynamic slides)
5.8 Permutations (dynamic slides)
 
Between α-closed Sets and Semi α-closed Sets
Between α-closed Sets and Semi α-closed SetsBetween α-closed Sets and Semi α-closed Sets
Between α-closed Sets and Semi α-closed Sets
 

Destacado

Compiler First Set Follow Set Brief
Compiler First Set Follow Set BriefCompiler First Set Follow Set Brief
Compiler First Set Follow Set Brief
sallypicnic
 

Destacado (20)

Cs419 lec5 lexical analysis using dfa
Cs419 lec5   lexical analysis using dfaCs419 lec5   lexical analysis using dfa
Cs419 lec5 lexical analysis using dfa
 
Lec4
Lec4Lec4
Lec4
 
Theory of computation Lec6
Theory of computation Lec6Theory of computation Lec6
Theory of computation Lec6
 
Cs419 lec3 lexical analysis using re
Cs419 lec3   lexical analysis using reCs419 lec3   lexical analysis using re
Cs419 lec3 lexical analysis using re
 
Cs419 lec4 lexical analysis using re
Cs419 lec4   lexical analysis using reCs419 lec4   lexical analysis using re
Cs419 lec4 lexical analysis using re
 
Cs419 lec12 semantic analyzer
Cs419 lec12  semantic analyzerCs419 lec12  semantic analyzer
Cs419 lec12 semantic analyzer
 
Cs419 lec7 cfg
Cs419 lec7   cfgCs419 lec7   cfg
Cs419 lec7 cfg
 
Compiler First Set Follow Set Brief
Compiler First Set Follow Set BriefCompiler First Set Follow Set Brief
Compiler First Set Follow Set Brief
 
Cs419 Compiler lec1&2 introduction
Cs419 Compiler lec1&2  introductionCs419 Compiler lec1&2  introduction
Cs419 Compiler lec1&2 introduction
 
Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Theory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemTheory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence Problem
 
Infos2014
Infos2014Infos2014
Infos2014
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Minimizing DFA
Minimizing DFAMinimizing DFA
Minimizing DFA
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 

Similar a Cs419 lec9 constructing parsing table ll1

Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
Gerwin Ocsena
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
Gerwin Ocsena
 
Steiner Tree Parameterized by Treewidth
Steiner Tree Parameterized by TreewidthSteiner Tree Parameterized by Treewidth
Steiner Tree Parameterized by Treewidth
ASPAK2014
 
Design and analysis of algorithm By Creative Developper
Design and analysis of algorithm By Creative DevelopperDesign and analysis of algorithm By Creative Developper
Design and analysis of algorithm By Creative Developper
OMKARVERMA12
 
A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...
Alexander Decker
 
A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...
Alexander Decker
 

Similar a Cs419 lec9 constructing parsing table ll1 (20)

Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
 
First() and Follow()
First() and Follow()First() and Follow()
First() and Follow()
 
Steiner Tree Parameterized by Treewidth
Steiner Tree Parameterized by TreewidthSteiner Tree Parameterized by Treewidth
Steiner Tree Parameterized by Treewidth
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 
Design and analysis of algorithm By Creative Developper
Design and analysis of algorithm By Creative DevelopperDesign and analysis of algorithm By Creative Developper
Design and analysis of algorithm By Creative Developper
 
Lambda calculus
Lambda calculusLambda calculus
Lambda calculus
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
 
A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...
 
A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...
 
5 top-down-parsers
5  top-down-parsers 5  top-down-parsers
5 top-down-parsers
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
Contra  * Continuous Functions in Topological Spaces
Contra   * Continuous Functions in Topological SpacesContra   * Continuous Functions in Topological Spaces
Contra  * Continuous Functions in Topological Spaces
 
Prerequisite for metric space
Prerequisite for metric spacePrerequisite for metric space
Prerequisite for metric space
 
1. I. Hassairi.pdf
1.  I. Hassairi.pdf1.  I. Hassairi.pdf
1. I. Hassairi.pdf
 
1. I. Hassairi.pdf
1.  I. Hassairi.pdf1.  I. Hassairi.pdf
1. I. Hassairi.pdf
 
CONTINUITY ON N-ARY SPACES
CONTINUITY ON N-ARY SPACESCONTINUITY ON N-ARY SPACES
CONTINUITY ON N-ARY SPACES
 
Operations on Functions.pdf
Operations on Functions.pdfOperations on Functions.pdf
Operations on Functions.pdf
 

Más de Arab Open University and Cairo University

Más de Arab Open University and Cairo University (16)

File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 
CS215 - Lec 5 record organization
CS215 - Lec 5  record organizationCS215 - Lec 5  record organization
CS215 - Lec 5 record organization
 
CS215 - Lec 4 single record organization
CS215 - Lec 4  single record organizationCS215 - Lec 4  single record organization
CS215 - Lec 4 single record organization
 
CS215 - Lec 2 file organization
CS215 - Lec 2   file organizationCS215 - Lec 2   file organization
CS215 - Lec 2 file organization
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Último (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Cs419 lec9 constructing parsing table ll1

  • 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2. 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
  • 3. 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
  • 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 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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