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 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

Click to edit the document details