cs160-lec6

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

This preview shows pages 1–7. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document
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

{[ snackBarMessage ]}

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
Ask a homework question - tutors are online