Skip to content
VUBuddy

CS606 Short Notes Lecture 13

Compiler Construction PDF 38 KBUpdated 23 Oct 2026
Quiz CS606 Short Notes Lecture 13 PDF 38 KB
vubuddy.com
Type
Quiz
Course
CS606 Compiler Construction
Size
PDF 38 KB
Updated
23 Oct 2026
Free download 0 of 3 used today
Download free · 38 KB

You get 3 free files a day on the website. Add the Chrome extension for 30 a day.

Unlimited downloads in the Android app Get it free on Google Play

Preview text

Text from pages 1, 2, 3 of 4. Download the file for all 4 pages.

Lecture 13

Top-Down Parser

A top-down parser starts with the root of the parse tree. The root node is labeled with the goal (start) symbol of the grammar. The top-down parsing algorithm proceeds as follows:

  1. Construct the root node of the parse tree

Q2. Repeat until the fringe of the parse tree matches input string

  1. At a node labeled A, select a production with A on its lhs
  2. for each symbol on its rhs, construct the appropriate child
  3. When a terminal symbol is added to the fringe and it does not match the fringe, backtrack

The key is picking right production in step a. That choice should be guided by the input string. Let’s try parsing using this algorithm using the expression grammar.

x – 2 * y

input Goal ↑ x – 2 * y 1 expr ↑ x – 2 * y 2 expr + term ↑ x – 2 * y 4 term + term ↑ x – 2 * y ↑ x – 2 * y 9 <id,x> + term ↑ x – 2 * y 9 <id,x> + term x ↑ – 2 * y

This worked well except that “– ” does not match “+ ”. The parser made the wrong choice of production to use at step 2. The parser must backtrack and use a different production.

This time the “– ” and “–” matched. We can advance past “–” to look at “2 ”. Now, we need to expand “term” input Goal ↑ x – 2 * y 1 expr ↑ x – 2 * y 3 expr – term ↑ x – 2 * y 4 term – term ↑ x – 2 * y ↑ x – 2 * y 9 <id,x> – term ↑ x – 2 * y 9 <id,x> – term x ↑ – 2 * y The 2’s match but the expansion terminated too soon because there is still unconsumed input and there are no non-terminals to expand in the sentential form ⇒ Need to backtrack.

input

  • <id,x> – term x – ↑ 2 * y

5 <id,x> – term * factor x – ↑ 2 * y 7 <id,x> – factor * factor x – ↑ 2 * y 8 <id,x> – <num,2> * factor x – ↑ 2 * y

  • <id,x> – <num,2> * factor x – 2 ↑ * y
  • <id,x> – <num,2> * factor x – 2 * ↑ y

9 <id,x> – <num,2> * <id,y> x – 2 * ↑ y

  • <id,x> – <num,2> * <id,y> x – 2 * y ↑

This time the parser met with success. All of the input matched.

Left Recursion

Consider another possible parse:

Parser is using productions but no input is being consumed. x – 2 ↑ * y <id,x> – <num,2> x – ↑ 2 * y <id,x> – <num,2> x – ↑ 2 * y <id,x> – factor x – ↑ 2 * y <id,x> – term Sentential Form input P

↑ x – 2 * y expr +term +term +term +.... ↑ x – 2 * y expr +term +term +term ↑ x – 2 * y expr +term +term expr +term expr Goal Sentential Form

↑ x – 2 * y ↑ x – 2 * y ↑ x – 2 * y input P Top-down parsers cannot handle left-recursive grammars. Formally, a grammar is left recursive if ∃ A ∈ NT such that ∃ a derivation A ⇒* A α , for some string α ∈ (NT ∪ T)*.

Our expression grammar is left recursive. This can lead to non-termination in a top-down parser. Non-termination is bad in any part of a compiler! For a top-down parser, any recursion must be a right recursion. We would like to convert left recursion to right To remove left recursion, we can transform the grammar. Consider a grammar fragment:

A ? A α | β

where neither α nor β starts with A . We can rewrite this as:

A ? β A'

A' ? α A' | ε

where A' is a new non-terminal. This grammar accepts the same language but uses only right recursion. The expression grammar we have been using contains two cases of leftrecursion. Applying the transformation yields

expr term expr' expr' + term expr' – term expr'

term term'

ε

These fragments use only right recursion. They retain the original left associativity. A top-down parser will terminate using them.

All CS606 files →
CS606 Compiler Construction All CS606 handouts, past papers, quizzes and MCQs in one place. Open course page

Get more out of VUBuddy, for free

Android app Free on Google Play
  • Unlimited downloads
  • The explanation behind every MCQ answer
  • Study offline on slow data
Get the Android app
Chrome extension Free on the Chrome Web Store
  • 30 downloads a day instead of 3
  • Sign in and buy Pro Semester here
Add to Chrome

One Pro Semester membership works in both the extension and the app.