lec10 - CS415 Compilers Top Down vs Bottom Up parsing first...

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

View Full Document Right Arrow Icon
CS415 Compilers Top Down vs. Bottom Up parsing; first and follow sets; LL(1) parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture 10 2 cs415, spring 10 Parsing (Syntax Analysis) EAC Chapters 3.3
Background image of page 2
Lecture 10 3 cs415, spring 10 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production & try to match the input Bad “pick” may need to backtrack Some grammars are backtrack-free (predictive parsing) Bottom-up parsers (LR(1), operator precedence) Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state Start in a state valid for legal first tokens Bottom-up parsers handle a large class of grammars
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lecture 10 4 cs415, spring 10 Parsing Techniques: Top-down parsers LL(1), recursive descent S * lm x A β lm x δβ * lm x y x y S A β δ input: read left-to-right construct leftmost deriviation (forwards) 1 input symbol lookahead
Background image of page 4
Lecture 10 5 cs415, spring 10 Parsing Techniques: Top-down parsers LL(1), recursive descent S * lm x A β lm x δβ * lm x y x y S A β δ input: read left-to-right construct leftmost deriviation (forwards) 1 input symbol lookahead rule A δ
Background image of page 5

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

View Full DocumentRight Arrow Icon
Lecture 10 6 cs415, spring 10 Parsing Techniques: Bottom-up parsers LR(1), operator precedence input: read left-to-right construct rightmost deriviation (backwards) 1 input symbol lookahead x y S B α γ S * rm α B y rm αγ y * rm x y
Background image of page 6
Lecture 10 7 cs415, spring 10 Parsing Techniques: Bottom-up parsers LR(1), operator precedence input: read left-to-right construct rightmost deriviation (backwards) 1 input symbol lookahead x y S B α γ S * rm α B y rm αγ y * rm x y rule B γ
Background image of page 7

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

View Full DocumentRight Arrow Icon
Lecture 10 8 cs415, spring 10 A top-down parser starts with the root of the parse tree The root node is labeled with the goal symbol of the grammar Top-down parsing algorithm: Construct the root node of the parse tree Repeat until the fringe of the parse tree matches the input string 1 At a node labeled A, select a production with A on its lhs and, for each symbol on its rhs, construct the appropriate child 2 When a terminal symbol is added to the fringe and it doesn’t match the fringe, backtrack 3 Find the next node to be expanded (label NT) The key is picking the right production in step 1 That choice should be guided by the input string Generic Top-down Parsing
Background image of page 8
Lecture 10 9 cs415, spring 10 Remember the expression grammar? 1 Goal Expr 2 Expr + Term 3| – Term 4| Term 5 Term * Factor 6| / Factor 7| Factor 8 number 9| i d And the input x –2 * y Version with precedence
Background image of page 9

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

View Full DocumentRight Arrow Icon
Lecture 10 10 cs415, spring 10 Let’s try x –2 * y : Rule Sentential Form Input Example 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< i d , x > + Term x – 2 * y i d , x > + x – 2 * y Goal Expr Term + Expr Term Fact.
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 / 38

lec10 - CS415 Compilers Top Down vs Bottom Up parsing first...

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