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 DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CMPSC 160 Translation of Programming Languages Lecture 6: LL(1) Parsing As we discussed in the last lecture, in topdown parsing we generate the parse tree by expanding the parse tree from the root towards the leaves We expand the nonterminal symbols at the frontier of the parse tree from left to right by picking a production for each nonterminal 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 nonterminal – That choice should be guided by the input string Topdown 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 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( β ) = {} 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 α ⇒ * ε , 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 nonterminal and X → Y 1 Y 2 ... Y k is a production, then put...
View
Full Document
 Spring '12
 Drexel
 NUM, Formal grammar, LL, Recursive descent parser

Click to edit the document details