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

Info iconThis preview shows pages 1–7. 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
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 6: LL(1) Parsing As we discussed in the last lecture, in top-down parsing we generate the parse tree by expanding the parse tree from the root towards the leaves We expand the non-terminal symbols at the frontier of the parse tree from left to right by picking a production for each non-terminal When there is a terminal in the frontier of the parse tree, we check if it matches the input and then consume the input if it does The key is picking the right production while expanding a non-terminal That choice should be guided by the input string Top-down Parsing Predictive Parsing The main idea is to look ahead at the next token and use that token to pick the production that you should apply Predictive parsing technique is more general! Definition of FIRST sets This means that we have to find ALL tokens that can be at the beginning of a string that can be derived from X + X | - Y Here we can use the + and to decide which rule to apply x FIRST( ) iff 1) * x , for some ( NT T ) * and x T 2) * and x = ( * means a series of (0 or more) productions, ( NT T ) * means that is a possibly empty sequence of nonterminal and terminal symbols, and x T means that x is a terminal symbol) FIRST Sets Intuitively, FIRST(S) is the set of all terminals that we could possibly see when starting to parse S If we want to build a predictive parser, we need to make sure that the look-ahead token tells us with 100% confidence which production to apply In order for this to be true, anytime we have a production that looks like A | , we need to make sure that FIRST( ) is distinct from the FIRST( ) Distinct means that there is no element in FIRST( ) that is also in FIRST( ) or formally, that FIRST( ) FIRST( ) = {} Slightly More Tricky Examples Here is an example of FIRST sets where the first symbol in the production is a non-terminal In this case, we have to examine all possible terminals that could begin a sentence derived from S If we have an , then we need to look past the first non-terminal If all the non-terminals have in their first sets, then add to the first set S AB A x | y B 0 | 1 FIRST(S) = { x, y } S AB A x | y | B 0 | 1 FIRST(S) = { x, y, 0, 1 } S AB A x | y | B 0 | 1 | FIRST(S) = { x, y, 0, 1, } How to Generate FIRST Sets For a string of grammar symbols , define FIRST( ) as Set of tokens that appear as the first symbol in some string that derives from If * , then is in FIRST( ) To construct FIRST( X ) for a grammar symbol X , apply the following rules until no more symbols can be added to FIRST( X ) 1. If X is a terminal FIRST( X ) is { X } 2. If X is a production, then put in FIRST( X ) 3. If X is a non-terminal and X Y 1 Y 2 ... Y k is a production, then put...
View Full Document

Page1 / 29

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

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

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