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

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

View Full Document Right Arrow Icon
1 Chapter 4: Top-Down Parsing
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 .
Background image of page 2
Input String : C C C lm lm lm
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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) +
Background image of page 6
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 β αα α α
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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' | ε
Background image of page 9

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

View Full DocumentRight Arrow Icon
How about left recursion occurred for derivation with more than two steps? e.g.,
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online