{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
CMPSC 160 Translation of Programming Languages Lecture 5: Top Down Parsing
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
Example Continuing with x 2 * y : S Expr Term Expr Term Fact.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern