SlideShare a Scribd company logo
1 of 12
19-Jul-22 COMP36512 Lecture 8 1
Lecture 8: Top-Down Parsing
Parsing:
• Context-free syntax is expressed with a context-free
grammar.
• The process of discovering a derivation for some
sentence.
Today’s lecture:
Top-down parsing
Front-End
Source code Object code
Back-End
Lexical
Analysis
IR
Syntax
Analysis
19-Jul-22 COMP36512 Lecture 8 2
Recursive-Descent Parsing
• 1. Construct the root with the starting symbol of the grammar.
• 2. Repeat until the fringe of the parse tree matches the input string:
– Assuming a node labelled A, select a production with A on its left-hand-side
and, for each symbol on its right-hand-side, construct the appropriate child.
– When a terminal symbol is added to the fringe and it doesn’t match the
fringe, backtrack.
– Find the next node to be expanded.
The key is picking the right production in the first step: that choice should
be guided by the input string.
Example:
1. Goal  Expr 5. Term  Term * Factor
2. Expr  Expr + Term 6. | Term / Factor
3. | Expr – Term 7. | Factor
4. | Term 8. Factor  number
9. | id
19-Jul-22 COMP36512 Lecture 8 3
Example: Parse x-2*y
Rule Sentential Form Input
- Goal | x – 2*y
1 Expr | x – 2*y
2 Expr + Term | x – 2*y
4 Term + Term | x – 2*y
7 Factor + Term | x – 2*y
9 id + Term | x – 2*y
Fail id + Term x | – 2*y
Back Expr | x – 2*y
3 Expr – Term | x – 2*y
4 Term – Term | x – 2*y
7 Factor – Term | x – 2*y
9 id – Term | x – 2*y
Match id – Term x – | 2*y
7 id – Factor x – | 2*y
9 id – num x – | 2*y
Fail id – num x – 2 | *y
Back id – Term x – | 2*y
5 id – Term * Factor x – | 2*y
7 id – Factor * Factor x – | 2*y
8 id – num * Factor x – | 2*y
match id – num * Factor x – 2* | y
9 id – num * id x – 2* | y
match id – num * id x – 2*y |
Rule Sentential Form Input
- Goal | x – 2*y
1 Expr | x – 2*y
2 Expr + Term | x – 2*y
2 Expr + Term + Term | x – 2*y
2 Expr + Term + Term + Term | x – 2*y
2 Expr + Term + Term + … + Term | x – 2*y
Goal
Term
Expr
Expr Term
-
Term
x
Factor y
Factor
Factor
*
2
Other choices for expansion are possible:
Steps (one scenario from many)
•Wrong choice leads to non-termination!
•This is a bad property for a parser!
•Parser must make the right choice!
19-Jul-22 COMP36512 Lecture 8 4
Left-Recursive Grammars
• Definition: A grammar is left-recursive if it has a non-terminal
symbol A, such that there is a derivation AAa, for some
string a.
• A left-recursive grammar can cause a recursive-descent parser
to go into an infinite loop.
• Eliminating left-recursion: In many cases, it is sufficient to
replace AAa | b with A bA' and A' aA' | 
• Example:
Sum  Sum+number | number
would become:
Sum  number Sum'
Sum'  +number Sum' | 
19-Jul-22 COMP36512 Lecture 8 5
Eliminating Left Recursion
Applying the transformation to the Grammar of the Example in Slide 2 we get:
Expr  Term Expr'
Expr'  +Term Expr' | – Term Expr' | 
Term  Factor Term'
Term'  *Factor Term' | / Factor Term' | 
(Goal  Expr and Factor  number | id remain unchanged)
Non-intuitive, but it works!
General algorithm: works for non-cyclic, no -productions grammars
1. Arrange the non-terminal symbols in order: A1, A2, A3, …, An
2. For i=1 to n do
for j=1 to i-1 do
I) replace each production of the form AiAj with
the productions Ai 1  | 2  | … | k 
where Aj 1 | 2 | … | k are all the current Aj productions
II) eliminate the immediate left recursion among the Ai
19-Jul-22 COMP36512 Lecture 8 6
Where are we?
• We can produce a top-down parser, but:
– if it picks the wrong production rule it has to backtrack.
• Idea: look ahead in input and use context to
pick correctly.
• How much lookahead is needed?
– In general, an arbitrarily large amount.
– Fortunately, most programming language constructs
fall into subclasses of context-free grammars that
can be parsed with limited lookahead.
19-Jul-22 COMP36512 Lecture 8 7
Predictive Parsing
• Basic idea:
– For any production A  a | b we would like to have a distinct way of
choosing the correct production to expand.
• FIRST sets:
– For any symbol A, FIRST(A) is defined as the set of terminal symbols
that appear as the first symbol of one or more strings derived from A.
E.g. (grammar in Slide 5): FIRST(Expr' )={+,-,}, FIRST(Term' )={*,/,},
FIRST(Factor)={number, id}
• The LL(1) property:
– If Aa and Ab both appear in the grammar, we would like to have:
FIRST(a)FIRST(b) = . This would allow the parser to make a
correct choice with a lookahead of exactly one symbol!
The Grammar of Slide 5 has this property!
19-Jul-22 COMP36512 Lecture 8 8
Recursive Descent Predictive Parsing
(a practical implementation of the Grammar in Slide 5)
Main() TPrime()
token=next_token(); if (token=='*' or '/') then
if (Expr()!=false) token=next_token()
then <next_compilation_step> if (Factor()==false)
else return false; then result=false
else if (TPrime()==false)
Expr() then result=false
if (Term()==false) else result=true
then result=false else result=true
else if (EPrime()==false) return result
then result=false
else result=true Factor()
return result if (token=='number' or 'id')then
token=next_token()
EPrime() result=true
if (token=='+' or '-') then else
token=next_token() report syntax_error
if (Term()==false) result=false
then result=false return result
elseif (EPrime()==false)
then result=false
else result=true
else result=true /*  */
return result
Term()
if (Factor()==false)
then result=false
else if (TPrime()==false)
then result=false
else result=true
return result
No backtracking is needed!
check :-)
19-Jul-22 COMP36512 Lecture 8 9
Left Factoring
What if my grammar does not have the LL(1) property?
Sometimes, we can transform a grammar to have this property.
Algorithm:
1. For each non-terminal A, find the longest prefix, say a, common to
two or more of its alternatives
2. if a then replace all the A productions, Aab1|ab2|ab3|...|abn|,
where  is anything that does not begin with a, with AaZ |  and
Zb1|b2|b3|...|bn
Repeat the above until no common prefixes remain
Example: A  ab1 | ab2 | ab3 would become A  aZ and Z  b1|b2|b3
Note the graphical representation:
A
ab3
ab1
ab2
A
b3
b2
b1
aZ
19-Jul-22 COMP36512 Lecture 8 10
Example
(NB: this is a different grammar from the one in Slide 2)
Goal  Expr Term  Factor * Term
Expr  Term + Expr | Factor / Term
| Term – Expr | Factor
| Term Factor  number
| id
We have a problem with the different rules for Expr as well as those for Term. In
both cases, the first symbol of the right-hand side is the same (Term and Factor,
respectively). E.g.:
FIRST(Term)=FIRST(Term)FIRST(Term)={number, id}.
FIRST(Factor)=FIRST(Factor)FIRST(Factor)={number, id}.
Applying left factoring:
Expr  Term Expr´ FIRST(+)={+}; FIRST(–)={–}; FIRST()={};
Expr´ + Expr | – Expr |  FIRST(–) FIRST(+)  FIRST()= =
Term  Factor Term´ FIRST(*)={*}; FIRST(/)={/}; FIRST()={};
Term´ * Term | / Term |  FIRST(*) FIRST(/)  FIRST()= =
19-Jul-22 COMP36512 Lecture 8 11
Example (cont.)
Rule Sentential Form Input
- Goal | x – 2*y
1 Expr | x – 2*y
2 Term Expr´ | x – 2*y
6 Factor Term´ Expr´ | x – 2*y
11 id Term´ Expr´ | x – 2*y
Match id Term´ Expr´ x | – 2*y
9 id  Expr´ x | – 2*y
4 id – Expr x | – 2*y
Match id – Expr x – | 2*y
2 id – Term Expr´ x – | 2*y
6 id – Factor Term´ Expr´ x – | 2*y
10 id – num Term´ Expr´ x – | 2*y
Match id – num Term´ Expr´ x – 2 | *y
7 id – num * Term Expr´ x – 2 | *y
Match id – num * Term Expr´ x – 2* | y
6 id – num * Factor Term´ Expr´ x – 2* | y
11 id – num * id Term Expr´ x – 2* | y
Match id – num * id Term´ Expr´ x – 2*y |
9 id – num * id Expr´ x – 2*y |
5 id – num * id x – 2*y |
1. Goal  Expr
2. Expr  Term Expr´
3. Expr´ + Expr
4. | - Expr
5. | 
6. Term  Factor Term´
7. Term´ * Term
8. | / Term
9. | 
10. Factor  number
11. | id
The next symbol
determines each choice
correctly. No backtracking
needed.
COMP36512 Lecture 8 12
Conclusion
• Top-down parsing:
– recursive with backtracking (not often used in practice)
– recursive predictive
• Nonrecursive Predictive Parsing is possible too: maintain a stack
explicitly rather than implicitly via recursion and determine the
production to be applied using a table (Aho, pp.186-190).
• Given a Context Free Grammar that doesn’t meet the LL(1)
condition, it is undecidable whether or not an equivalent LL(1)
grammar exists.
• Next time: Bottom-Up Parsing
• Reading: Aho2, Sections 4.3.3, 4.3.4, 4.4; Aho1, pp. 176-
178, 181-185; Grune pp.117-133; Hunter pp. 72-93; Cooper,
Section 3.3.

More Related Content

Similar to lect08.ppt

1 arithmetic
1 arithmetic1 arithmetic
1 arithmetic
fyjordan9
 
5indiceslogarithms 120909011915-phpapp02
5indiceslogarithms 120909011915-phpapp025indiceslogarithms 120909011915-phpapp02
5indiceslogarithms 120909011915-phpapp02
Sofia Mahmood
 
Sept. 21, 2012
Sept. 21, 2012Sept. 21, 2012
Sept. 21, 2012
khyps13
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2
bweldon
 

Similar to lect08.ppt (20)

Ip 5 discrete mathematics
Ip 5 discrete mathematicsIp 5 discrete mathematics
Ip 5 discrete mathematics
 
Business Math Chapter 2
Business Math Chapter 2Business Math Chapter 2
Business Math Chapter 2
 
Software Metrics
Software MetricsSoftware Metrics
Software Metrics
 
1 arithmetic
1 arithmetic1 arithmetic
1 arithmetic
 
Vb scripting
Vb scriptingVb scripting
Vb scripting
 
Discrete Math IP4 - Automata Theory
Discrete Math IP4 - Automata TheoryDiscrete Math IP4 - Automata Theory
Discrete Math IP4 - Automata Theory
 
5indiceslogarithms 120909011915-phpapp02
5indiceslogarithms 120909011915-phpapp025indiceslogarithms 120909011915-phpapp02
5indiceslogarithms 120909011915-phpapp02
 
Primes & Composites, GCF
Primes & Composites, GCFPrimes & Composites, GCF
Primes & Composites, GCF
 
Ch2_p1.pdf
Ch2_p1.pdfCh2_p1.pdf
Ch2_p1.pdf
 
Applications of Stack
Applications of StackApplications of Stack
Applications of Stack
 
Exponents and Logs
Exponents and LogsExponents and Logs
Exponents and Logs
 
Sept. 21, 2012
Sept. 21, 2012Sept. 21, 2012
Sept. 21, 2012
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab Record
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2
 
Idea for ineractive programming language
Idea for ineractive programming languageIdea for ineractive programming language
Idea for ineractive programming language
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
Number system
Number systemNumber system
Number system
 

More from ssuser0be977

More from ssuser0be977 (8)

IBM list NM portal not updategbbbbbnnmhhh
IBM list NM portal not updategbbbbbnnmhhhIBM list NM portal not updategbbbbbnnmhhh
IBM list NM portal not updategbbbbbnnmhhh
 
lect23_optimization.ppt
lect23_optimization.pptlect23_optimization.ppt
lect23_optimization.ppt
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.ppt
 
wk1a-basicstats.ppt
wk1a-basicstats.pptwk1a-basicstats.ppt
wk1a-basicstats.ppt
 
wk1a-basicstats (2).ppt
wk1a-basicstats (2).pptwk1a-basicstats (2).ppt
wk1a-basicstats (2).ppt
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
11CS10033.pptx
11CS10033.pptx11CS10033.pptx
11CS10033.pptx
 
dsa1.ppt
dsa1.pptdsa1.ppt
dsa1.ppt
 

Recently uploaded

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 

lect08.ppt

  • 1. 19-Jul-22 COMP36512 Lecture 8 1 Lecture 8: Top-Down Parsing Parsing: • Context-free syntax is expressed with a context-free grammar. • The process of discovering a derivation for some sentence. Today’s lecture: Top-down parsing Front-End Source code Object code Back-End Lexical Analysis IR Syntax Analysis
  • 2. 19-Jul-22 COMP36512 Lecture 8 2 Recursive-Descent Parsing • 1. Construct the root with the starting symbol of the grammar. • 2. Repeat until the fringe of the parse tree matches the input string: – Assuming a node labelled A, select a production with A on its left-hand-side and, for each symbol on its right-hand-side, construct the appropriate child. – When a terminal symbol is added to the fringe and it doesn’t match the fringe, backtrack. – Find the next node to be expanded. The key is picking the right production in the first step: that choice should be guided by the input string. Example: 1. Goal  Expr 5. Term  Term * Factor 2. Expr  Expr + Term 6. | Term / Factor 3. | Expr – Term 7. | Factor 4. | Term 8. Factor  number 9. | id
  • 3. 19-Jul-22 COMP36512 Lecture 8 3 Example: Parse x-2*y Rule Sentential Form Input - Goal | x – 2*y 1 Expr | x – 2*y 2 Expr + Term | x – 2*y 4 Term + Term | x – 2*y 7 Factor + Term | x – 2*y 9 id + Term | x – 2*y Fail id + Term x | – 2*y Back Expr | x – 2*y 3 Expr – Term | x – 2*y 4 Term – Term | x – 2*y 7 Factor – Term | x – 2*y 9 id – Term | x – 2*y Match id – Term x – | 2*y 7 id – Factor x – | 2*y 9 id – num x – | 2*y Fail id – num x – 2 | *y Back id – Term x – | 2*y 5 id – Term * Factor x – | 2*y 7 id – Factor * Factor x – | 2*y 8 id – num * Factor x – | 2*y match id – num * Factor x – 2* | y 9 id – num * id x – 2* | y match id – num * id x – 2*y | Rule Sentential Form Input - Goal | x – 2*y 1 Expr | x – 2*y 2 Expr + Term | x – 2*y 2 Expr + Term + Term | x – 2*y 2 Expr + Term + Term + Term | x – 2*y 2 Expr + Term + Term + … + Term | x – 2*y Goal Term Expr Expr Term - Term x Factor y Factor Factor * 2 Other choices for expansion are possible: Steps (one scenario from many) •Wrong choice leads to non-termination! •This is a bad property for a parser! •Parser must make the right choice!
  • 4. 19-Jul-22 COMP36512 Lecture 8 4 Left-Recursive Grammars • Definition: A grammar is left-recursive if it has a non-terminal symbol A, such that there is a derivation AAa, for some string a. • A left-recursive grammar can cause a recursive-descent parser to go into an infinite loop. • Eliminating left-recursion: In many cases, it is sufficient to replace AAa | b with A bA' and A' aA' |  • Example: Sum  Sum+number | number would become: Sum  number Sum' Sum'  +number Sum' | 
  • 5. 19-Jul-22 COMP36512 Lecture 8 5 Eliminating Left Recursion Applying the transformation to the Grammar of the Example in Slide 2 we get: Expr  Term Expr' Expr'  +Term Expr' | – Term Expr' |  Term  Factor Term' Term'  *Factor Term' | / Factor Term' |  (Goal  Expr and Factor  number | id remain unchanged) Non-intuitive, but it works! General algorithm: works for non-cyclic, no -productions grammars 1. Arrange the non-terminal symbols in order: A1, A2, A3, …, An 2. For i=1 to n do for j=1 to i-1 do I) replace each production of the form AiAj with the productions Ai 1  | 2  | … | k  where Aj 1 | 2 | … | k are all the current Aj productions II) eliminate the immediate left recursion among the Ai
  • 6. 19-Jul-22 COMP36512 Lecture 8 6 Where are we? • We can produce a top-down parser, but: – if it picks the wrong production rule it has to backtrack. • Idea: look ahead in input and use context to pick correctly. • How much lookahead is needed? – In general, an arbitrarily large amount. – Fortunately, most programming language constructs fall into subclasses of context-free grammars that can be parsed with limited lookahead.
  • 7. 19-Jul-22 COMP36512 Lecture 8 7 Predictive Parsing • Basic idea: – For any production A  a | b we would like to have a distinct way of choosing the correct production to expand. • FIRST sets: – For any symbol A, FIRST(A) is defined as the set of terminal symbols that appear as the first symbol of one or more strings derived from A. E.g. (grammar in Slide 5): FIRST(Expr' )={+,-,}, FIRST(Term' )={*,/,}, FIRST(Factor)={number, id} • The LL(1) property: – If Aa and Ab both appear in the grammar, we would like to have: FIRST(a)FIRST(b) = . This would allow the parser to make a correct choice with a lookahead of exactly one symbol! The Grammar of Slide 5 has this property!
  • 8. 19-Jul-22 COMP36512 Lecture 8 8 Recursive Descent Predictive Parsing (a practical implementation of the Grammar in Slide 5) Main() TPrime() token=next_token(); if (token=='*' or '/') then if (Expr()!=false) token=next_token() then <next_compilation_step> if (Factor()==false) else return false; then result=false else if (TPrime()==false) Expr() then result=false if (Term()==false) else result=true then result=false else result=true else if (EPrime()==false) return result then result=false else result=true Factor() return result if (token=='number' or 'id')then token=next_token() EPrime() result=true if (token=='+' or '-') then else token=next_token() report syntax_error if (Term()==false) result=false then result=false return result elseif (EPrime()==false) then result=false else result=true else result=true /*  */ return result Term() if (Factor()==false) then result=false else if (TPrime()==false) then result=false else result=true return result No backtracking is needed! check :-)
  • 9. 19-Jul-22 COMP36512 Lecture 8 9 Left Factoring What if my grammar does not have the LL(1) property? Sometimes, we can transform a grammar to have this property. Algorithm: 1. For each non-terminal A, find the longest prefix, say a, common to two or more of its alternatives 2. if a then replace all the A productions, Aab1|ab2|ab3|...|abn|, where  is anything that does not begin with a, with AaZ |  and Zb1|b2|b3|...|bn Repeat the above until no common prefixes remain Example: A  ab1 | ab2 | ab3 would become A  aZ and Z  b1|b2|b3 Note the graphical representation: A ab3 ab1 ab2 A b3 b2 b1 aZ
  • 10. 19-Jul-22 COMP36512 Lecture 8 10 Example (NB: this is a different grammar from the one in Slide 2) Goal  Expr Term  Factor * Term Expr  Term + Expr | Factor / Term | Term – Expr | Factor | Term Factor  number | id We have a problem with the different rules for Expr as well as those for Term. In both cases, the first symbol of the right-hand side is the same (Term and Factor, respectively). E.g.: FIRST(Term)=FIRST(Term)FIRST(Term)={number, id}. FIRST(Factor)=FIRST(Factor)FIRST(Factor)={number, id}. Applying left factoring: Expr  Term Expr´ FIRST(+)={+}; FIRST(–)={–}; FIRST()={}; Expr´ + Expr | – Expr |  FIRST(–) FIRST(+)  FIRST()= = Term  Factor Term´ FIRST(*)={*}; FIRST(/)={/}; FIRST()={}; Term´ * Term | / Term |  FIRST(*) FIRST(/)  FIRST()= =
  • 11. 19-Jul-22 COMP36512 Lecture 8 11 Example (cont.) Rule Sentential Form Input - Goal | x – 2*y 1 Expr | x – 2*y 2 Term Expr´ | x – 2*y 6 Factor Term´ Expr´ | x – 2*y 11 id Term´ Expr´ | x – 2*y Match id Term´ Expr´ x | – 2*y 9 id  Expr´ x | – 2*y 4 id – Expr x | – 2*y Match id – Expr x – | 2*y 2 id – Term Expr´ x – | 2*y 6 id – Factor Term´ Expr´ x – | 2*y 10 id – num Term´ Expr´ x – | 2*y Match id – num Term´ Expr´ x – 2 | *y 7 id – num * Term Expr´ x – 2 | *y Match id – num * Term Expr´ x – 2* | y 6 id – num * Factor Term´ Expr´ x – 2* | y 11 id – num * id Term Expr´ x – 2* | y Match id – num * id Term´ Expr´ x – 2*y | 9 id – num * id Expr´ x – 2*y | 5 id – num * id x – 2*y | 1. Goal  Expr 2. Expr  Term Expr´ 3. Expr´ + Expr 4. | - Expr 5. |  6. Term  Factor Term´ 7. Term´ * Term 8. | / Term 9. |  10. Factor  number 11. | id The next symbol determines each choice correctly. No backtracking needed.
  • 12. COMP36512 Lecture 8 12 Conclusion • Top-down parsing: – recursive with backtracking (not often used in practice) – recursive predictive • Nonrecursive Predictive Parsing is possible too: maintain a stack explicitly rather than implicitly via recursion and determine the production to be applied using a table (Aho, pp.186-190). • Given a Context Free Grammar that doesn’t meet the LL(1) condition, it is undecidable whether or not an equivalent LL(1) grammar exists. • Next time: Bottom-Up Parsing • Reading: Aho2, Sections 4.3.3, 4.3.4, 4.4; Aho1, pp. 176- 178, 181-185; Grune pp.117-133; Hunter pp. 72-93; Cooper, Section 3.3.