# Chapter_4 - Chapter 4 Top-Down Parsing 1 Objectives of...

This preview shows pages 1–11. Sign up to view the full content.

1 Chapter 4: Top-Down Parsing

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct a parse tree for the input string starting from the root and creating the nodes of the parse tree in preorder .
Input String : C C C lm lm lm

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 1. with backtracking (making repeated scans of the input, a general form of top-down parsing) Methods: To create a procedure for each nonterminal. Approaches of Top-Down Parsing
e.g. S -> cAd A -> ab | a S( ) { if input symbol == ‘c’ A( ) { isave= input-pointer; { Advance(); if input-symbol == ‘a’ if A() { Advance(); if input-symbol == ‘d’ if input-symbol == ‘b’ { Advance(); { Advance(); return true; return true; } } } } return false; input-pointer = isave; } if input-symbol == ‘a’ { Advance(); return true; } else return false; } L = { cabd, cad } c a d

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Problems for top-down parsing with backtracking : (1) left-recursion (can cause a top-down parser to go into an infinite loop) Def. A grammar is said to be left-recursive if it has a nonterminal A s.t. there is a derivation A => A for some . (2) backtracking - undo not only the movement but also the semantics entering in symbol table. (3) the order the alternatives are tried (For the grammar shown above, try w = cabd where A -> a is applied first) +
7 With immediate left recursion: A -> A α | β ==> transform into A -> β A' A' -> α A' | ε A A α A α . A A α . β ===> A β A' α A' α A' . . A' ε Elimination of Left-Recursion β αα α α

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 e.g. E -> E + T | T T -> T * F | F F -> (E) | id After transformation: E -> TE ' E' -> +TE' | ε T -> FT ' T ' -> *FT' | ε F -> (E) | id
9 General form (with left recursion): A -> A α 1 | A α 2 | . .. | A α n | β 1 | β 2 | . .. | β m After transformation: ==> A -> β 1 A' | β 2 A' | . .. | β m A' A' -> α 1 A' | α 2 A' | . .. | α n A' | ε

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
How about left recursion occurred for derivation with more than two steps? e.g.,
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 35

Chapter_4 - Chapter 4 Top-Down Parsing 1 Objectives of...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online