cs160-lec5 - CMPSC 160 Translation of Programming Languages...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 160 Translation of Programming Languages Lecture 5: Top Down Parsing Parsing Techniques Top-down parsers (LL(1), recursive descent parsers) • Start at the root of the parse tree from the start symbol and grow toward leaves (similar to a derivation) • Pick a production and try to match the input • Bad “pick” ⇒ may need to backtrack • Some grammars are backtrack-free (predictive parsing) Bottom-up parsers (LR(1), shift-reduce parsers) • Start at the leaves and grow toward root • We can think of the process as reducing the input string to the start symbol • At each reduction step, a particular substring matching the right-side of a production is replaced by the symbol on the left-side of the production • Bottom-up parsers handle a large class of grammars Construct the root node of the parse tree, label it with the start symbol, and set the current-node to root node Repeat until all the input is consumed (i.e., until the frontier of the parse tree matches the input string) 1 If the label of the current node is a non-terminal node A, select a production with A on its lhs and, for each symbol on its rhs, construct the appropriate child 2 If the current node is a terminal symbol: If it matches the input string, consume it (advance the input pointer) If it does not match the input string, backtrack 3 Set the current node to the next node in the frontier of the parse tree If there is no node left in the frontier of the parse tree and input is not consumed, then backtrack The key is picking the right production in step 1 • That choice should be guided by the input string Top-down Parsing Algorithm Example And the input: x – 2 * y Let’s use the expression grammar with correct precedence and associativity as an example 1 S → Expr 2 Expr → Expr + Term 3 | Expr- Term 4 | Term 5 Term → Term * Factor 6 | Term / Factor 7 | Factor 8 Factor → num 9 | id Let’s try x – 2 * y : Example S Expr Term + Expr Term Fact. < id, x > Rule Sentential Form Input - S ↑ 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 < id , x > + Term ↑ x – 2 * y < id , x > + Term x ↑ – 2 * y Example Let’s try x – 2 * y : Note that “ – ” doesn’t match “+” The parser must backtrack to here S Expr Term + Expr Term Fact. < id, x > Rule Sentential Form Input - S ↑ 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 < id , x > + Term ↑ x – 2 * y < id , x > + Term x ↑ – 2 * y Example Continuing with x – 2 * y : S Expr Term – Expr Term Fact. < id, x > Rule Sentential Form Input - S ↑ x – 2 * y 1 Expr ↑ x – 2 * y 3 Expr – Term ↑ x – 2 * y 4 Term – Term ↑ x – 2 * y 7 Factor – Term ↑ x – 2 * y 9 < id , x > – Term ↑ x – 2 * y- < id , x > – Term x ↑ – 2 * y - < id , x > – Term x – ↑ 2 * y Example Continuing with x – 2 * y :...
View Full Document

This note was uploaded on 02/19/2012 for the course ENGR 361 taught by Professor Drexel during the Spring '12 term at Bloomsburg.

Page1 / 25

cs160-lec5 - CMPSC 160 Translation of Programming Languages...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online