This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 CMPSC 160 Translation of Programming Languages Lectures 6 and 7: LL(1) Parsing Construct the root node of the parse tree, label it with the start symbol, and set the currentnode 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 nonterminal 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 Topdown Parsing Algorithm 2 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 lookahead 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( β ) = {} 3 Slightly More Tricky Examples • Here is an example of FIRST sets where the first symbol in the production is a nonterminal • 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 nonterminal • If all the nonterminals 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 α ⇒ * ε...
View
Full
Document
This note was uploaded on 11/23/2010 for the course MATH 104b taught by Professor Ceniceros,h during the Spring '08 term at UCSB.
 Spring '08
 Ceniceros,H
 Numerical Analysis

Click to edit the document details