cs160-lec5

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

Info iconThis preview shows pages 1–5. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 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 2 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 Lets 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 3 Lets 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 Lets try x 2 * y : Note that doesnt 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 4 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

Page1 / 14

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

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

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