SlideShare a Scribd company logo
1 of 24
Syntax Analysis
LL Parser
Objectives:
• Be able to understand LL parsing methods
• Be able to find First sets and Follow sets parsing
• Be able to check LL Parsing properties
Hierarchy of grammar classes
© Oscar Nierstrasz Parsing
LL(k):
— Left-to-right, Leftmost
derivation, k tokens
lookahead
LR(k):
— Left-to-right, Rightmost
derivation, k tokens
lookahead
SLR:
— Simple LR (uses “follow
sets”)
LALR:
— LookAhead LR (uses
“lookahead sets”)
Top-down versus bottom-up
• Top-down parser:
– starts at the root of derivation tree and fills in
– picks a production and tries to match the input
– may require backtracking
– some grammars are backtrack-free (predictive)
• Bottom-up parser:
– starts at the leaves and fills in
– starts in a state valid for legal first tokens
– as input is consumed, changes state to encode possibilities
(recognize valid prefixes)
– uses a stack to store both state and sentential forms
Predictive Parsing
• If a top down parser picks the wrong production, it
may need to backtrack
• Alternative is to look ahead in input and use
context to pick correctly
• Fortunately, large classes of CFGs can be parsed
with limited lookahead
• Most programming languages constructs fall in
those subclasses
4
Predictive Parsing
• Eliminate left recursion from grammar
• Left factor the grammar
• Compute FIRST and FOLLOW
• Two variants:
– Recursive (recursive-descent parsing)
– Non-recursive (table-driven parsing)
5
LL Parsing Methods
• LL parsing methods read the tokens from Left
to right and parse them top-down according
to a Leftmost derivation.
• To build the parsing table, we need the notion
of nullability and the two functions
– FIRST
– FOLLOW
Nullability
• A nonterminal A is nullable if
A * .
• Clearly, A is nullable if it has a production
A  .
• But A is also nullable if there are, for example,
productions
A  BC.
B  A | aC | .
C  aB | Cb | .
Nullability
• In other words, A is nullable if there is a
production
A  ,
or there is a production
A  B1B2…Bn,
where B1, B2, ..., Bn are nullable.
Nullability
• In the grammar
E  T E'
E'  + T E' | .
T  F T'
T'  * F T' | .
F  (E) | id | num
E' and T' are nullable.
• E, T, and F are not nullable.
Nullability
• In the grammar
S → aBDh
B → cC
C → bC / ∈
D → EF
E → g / ∈
F → f / ∈
Which symbol/s is/are null-able?
Predictive Parsing
FIRST Sets:
For some rhs a  G, define
FIRST(a) as the set of tokens
that appear as the first symbol
in some string that derives
from a.
11
Predictive Parsing
That is,
x  FIRST(a)
iff a * x g, for some g.
12
FIRST Set
• FIRST(a) = { the set of terminals that begin all
strings derived from a }
FIRST(a) = {a} if a  T
FIRST() = {}
FIRST(A) = Aa FIRST(a)
for Aa  P
FIRST(X1X2…Xk) =
if for all j = 1, …, i-1 :   FIRST(Xj) then
add non- in FIRST(Xi) to FIRST(X1X2…Xk)
if for all j = 1, …, k :   FIRST(Xj) then
add  to FIRST(X1X2…Xk)
13
Example: FIRST
• Let the grammar be
E  T E'
E'  + T E' | .
T  F T'
T'  * F T' | .
F  (E) | id | num
Predictive Parsing
FOLLOW(a)
is the set of all words in the
grammar that can legally
appear after an a.
15
FOLLOW
• FOLLOW(A) = { the set of terminals that can
immediately follow nonterminal A }
FOLLOW(A) =
for all (B  a A )  P do
add FIRST()-{} to FOLLOW(A)
for all (B  a A )  P and   FIRST() do
add FOLLOW(B) to FOLLOW(A)
for all (B  a A)  P do
add FOLLOW(B) to FOLLOW(A)
if A is the start symbol S then
add $ to FOLLOW(A)
16
Find First and Follow sets
CFG
expr  expr + term | term
term  term * factor | factor
factor  number | ( expr )
Reformed
EE+T/T
TT*F/F
F(E)/id
Left-recursion/Left-factoring removed
?
1-SAa
2-ABD
3-Bb
4- B 
5-Dd
6-D
First sets
First (S)={}
First(A)= {}
First(B)= {}
First(D)={}
Follow(S)={}
Follow(A)={}
Follow(B)={}
Follow(D)={}
N/T a b d $
S
A
B
D
1. CPF class id XY
2. Ppublic
3. P 
4. Ffinal
5. F 
6. Xextends id
7. X 
8. Yimplements I
9. Y 
10.Iid J
11.JI
12.J 
First©={ public, final,class }
First(P)={public, }
First(F)={final , }
First(X)={extends, }
First(Y)={implements, }
First(I)={id}
First(J)={id, }
Follow(C)={}
Follow(P)={}
Follow(F)={}
Follow(X)={}
Follow(Y)={}
Follow(I)={}
Follow(J)={}
class id publi
c
final ext impl
e
$
C
P
F
X
Y
I
J
LL(1) Grammar
• A grammar G is LL(1) if it is not left recursive and for
each collection of productions
A  a1 | a2 | … | an
for nonterminal A the following holds:
1. FIRST(ai)  FIRST(aj) =  for all i  j
2. if ai *  then
2.a. aj *  for all i  j
2.b. FIRST(aj)  FOLLOW(A) = 
for all i  j
20
Sif C then S else S | if C then S
Non-LL(1) Examples
21
Grammar Not LL(1) because:
S  S a | a Left recursive
S  a S | a FIRST(a S)  FIRST(a)  
S  a R | 
R  S |  For R: S *  and  * 
S  a R a
R  S | 
For R:
FIRST(S)  FOLLOW(R)  
Example Table
22
E  T ER
ER  + T ER | 
T  F TR
TR  * F TR | 
F  ( E ) | id
A  a
FIRST(
a)
FOLLOW(
A)
E  T ER ( id $ )
ER  + T
ER
+
$ )
ER   
T  F TR ( id + $ )
TR  * F
TR
*
+ $ )
TR   
F  ( E ) ( * + $ )
F  id id * + $ )
Example Table
23
E  T ER
ER  + T ER | 
T  F TR
TR  * F TR | 
F  ( E ) | id
id + * ( ) $
E E  T ER
E  T
ER
ER
ER  + T
ER
ER   ER  
T T  F TR
T  F
TR
TR TR  
TR  * F
TR
TR   TR  
F F  id
F  ( E
)
Summary
• Predictive parsing requires
– Removal of Left Recursion
– Left factored CFG
• LL 1 Parsing requires
– First sets
– Follow sets

More Related Content

Similar to compiler-lecture-6nn-14112022-110738am.ppt

Similar to compiler-lecture-6nn-14112022-110738am.ppt (20)

PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Left factor put
Left factor putLeft factor put
Left factor put
 
Ch4a
Ch4aCh4a
Ch4a
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 
Lecture10 syntax analysis_6
Lecture10 syntax analysis_6Lecture10 syntax analysis_6
Lecture10 syntax analysis_6
 
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.
 
Ch8b
Ch8bCh8b
Ch8b
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Parsing
ParsingParsing
Parsing
 
UNIT 2 (1).pptx
UNIT 2 (1).pptxUNIT 2 (1).pptx
UNIT 2 (1).pptx
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptx
 

Recently uploaded

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

compiler-lecture-6nn-14112022-110738am.ppt

  • 1. Syntax Analysis LL Parser Objectives: • Be able to understand LL parsing methods • Be able to find First sets and Follow sets parsing • Be able to check LL Parsing properties
  • 2. Hierarchy of grammar classes © Oscar Nierstrasz Parsing LL(k): — Left-to-right, Leftmost derivation, k tokens lookahead LR(k): — Left-to-right, Rightmost derivation, k tokens lookahead SLR: — Simple LR (uses “follow sets”) LALR: — LookAhead LR (uses “lookahead sets”)
  • 3. Top-down versus bottom-up • Top-down parser: – starts at the root of derivation tree and fills in – picks a production and tries to match the input – may require backtracking – some grammars are backtrack-free (predictive) • Bottom-up parser: – starts at the leaves and fills in – starts in a state valid for legal first tokens – as input is consumed, changes state to encode possibilities (recognize valid prefixes) – uses a stack to store both state and sentential forms
  • 4. Predictive Parsing • If a top down parser picks the wrong production, it may need to backtrack • Alternative is to look ahead in input and use context to pick correctly • Fortunately, large classes of CFGs can be parsed with limited lookahead • Most programming languages constructs fall in those subclasses 4
  • 5. Predictive Parsing • Eliminate left recursion from grammar • Left factor the grammar • Compute FIRST and FOLLOW • Two variants: – Recursive (recursive-descent parsing) – Non-recursive (table-driven parsing) 5
  • 6. LL Parsing Methods • LL parsing methods read the tokens from Left to right and parse them top-down according to a Leftmost derivation. • To build the parsing table, we need the notion of nullability and the two functions – FIRST – FOLLOW
  • 7. Nullability • A nonterminal A is nullable if A * . • Clearly, A is nullable if it has a production A  . • But A is also nullable if there are, for example, productions A  BC. B  A | aC | . C  aB | Cb | .
  • 8. Nullability • In other words, A is nullable if there is a production A  , or there is a production A  B1B2…Bn, where B1, B2, ..., Bn are nullable.
  • 9. Nullability • In the grammar E  T E' E'  + T E' | . T  F T' T'  * F T' | . F  (E) | id | num E' and T' are nullable. • E, T, and F are not nullable.
  • 10. Nullability • In the grammar S → aBDh B → cC C → bC / ∈ D → EF E → g / ∈ F → f / ∈ Which symbol/s is/are null-able?
  • 11. Predictive Parsing FIRST Sets: For some rhs a  G, define FIRST(a) as the set of tokens that appear as the first symbol in some string that derives from a. 11
  • 12. Predictive Parsing That is, x  FIRST(a) iff a * x g, for some g. 12
  • 13. FIRST Set • FIRST(a) = { the set of terminals that begin all strings derived from a } FIRST(a) = {a} if a  T FIRST() = {} FIRST(A) = Aa FIRST(a) for Aa  P FIRST(X1X2…Xk) = if for all j = 1, …, i-1 :   FIRST(Xj) then add non- in FIRST(Xi) to FIRST(X1X2…Xk) if for all j = 1, …, k :   FIRST(Xj) then add  to FIRST(X1X2…Xk) 13
  • 14. Example: FIRST • Let the grammar be E  T E' E'  + T E' | . T  F T' T'  * F T' | . F  (E) | id | num
  • 15. Predictive Parsing FOLLOW(a) is the set of all words in the grammar that can legally appear after an a. 15
  • 16. FOLLOW • FOLLOW(A) = { the set of terminals that can immediately follow nonterminal A } FOLLOW(A) = for all (B  a A )  P do add FIRST()-{} to FOLLOW(A) for all (B  a A )  P and   FIRST() do add FOLLOW(B) to FOLLOW(A) for all (B  a A)  P do add FOLLOW(B) to FOLLOW(A) if A is the start symbol S then add $ to FOLLOW(A) 16
  • 17. Find First and Follow sets CFG expr  expr + term | term term  term * factor | factor factor  number | ( expr ) Reformed EE+T/T TT*F/F F(E)/id Left-recursion/Left-factoring removed ?
  • 18. 1-SAa 2-ABD 3-Bb 4- B  5-Dd 6-D First sets First (S)={} First(A)= {} First(B)= {} First(D)={} Follow(S)={} Follow(A)={} Follow(B)={} Follow(D)={} N/T a b d $ S A B D
  • 19. 1. CPF class id XY 2. Ppublic 3. P  4. Ffinal 5. F  6. Xextends id 7. X  8. Yimplements I 9. Y  10.Iid J 11.JI 12.J  First©={ public, final,class } First(P)={public, } First(F)={final , } First(X)={extends, } First(Y)={implements, } First(I)={id} First(J)={id, } Follow(C)={} Follow(P)={} Follow(F)={} Follow(X)={} Follow(Y)={} Follow(I)={} Follow(J)={} class id publi c final ext impl e $ C P F X Y I J
  • 20. LL(1) Grammar • A grammar G is LL(1) if it is not left recursive and for each collection of productions A  a1 | a2 | … | an for nonterminal A the following holds: 1. FIRST(ai)  FIRST(aj) =  for all i  j 2. if ai *  then 2.a. aj *  for all i  j 2.b. FIRST(aj)  FOLLOW(A) =  for all i  j 20 Sif C then S else S | if C then S
  • 21. Non-LL(1) Examples 21 Grammar Not LL(1) because: S  S a | a Left recursive S  a S | a FIRST(a S)  FIRST(a)   S  a R |  R  S |  For R: S *  and  *  S  a R a R  S |  For R: FIRST(S)  FOLLOW(R)  
  • 22. Example Table 22 E  T ER ER  + T ER |  T  F TR TR  * F TR |  F  ( E ) | id A  a FIRST( a) FOLLOW( A) E  T ER ( id $ ) ER  + T ER + $ ) ER    T  F TR ( id + $ ) TR  * F TR * + $ ) TR    F  ( E ) ( * + $ ) F  id id * + $ )
  • 23. Example Table 23 E  T ER ER  + T ER |  T  F TR TR  * F TR |  F  ( E ) | id id + * ( ) $ E E  T ER E  T ER ER ER  + T ER ER   ER   T T  F TR T  F TR TR TR   TR  * F TR TR   TR   F F  id F  ( E )
  • 24. Summary • Predictive parsing requires – Removal of Left Recursion – Left factored CFG • LL 1 Parsing requires – First sets – Follow sets