- Type
- Quiz
- Course
- CS606 Compiler Construction
- Size
- PDF 38 KB
- Updated
- 23 Oct 2026
You get 3 free files a day on the website. Add the Chrome extensionSign in with the extension for 30 a day.
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:
- Construct the root node of the parse tree
Q2. Repeat until the fringe of the parse tree matches input string
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.
Related CS606 files
All CS606 files →- CS606 Highlighted Handouts Set 2 by Masters Highlighted PDF 5.7 MB
- CS606 Final Term Current Papers 2022 by Masters Final term PDF 2.3 MB
- CS606 Short Notes Lecture 6 Quiz PDF 26 KB
- CS606 Short Notes Lecture 9 Quiz PDF 26 KB
- CS606 Short Notes Lecture 1 Quiz PDF 73 KB
- CS606 Short Notes Lecture 16 Quiz PDF 33 KB
Get more out of VUBuddy, for free
- Unlimited downloads
- The explanation behind every MCQ answer
- Study offline on slow data
- 30 downloads a day instead of 3
- Sign in and buy Pro Semester here
One Pro Semester membership works in both the extension and the app.