cs160-lec6

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

This preview shows pages 1–4. 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 is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CMPSC 160 Translation of Programming Languages Lecture 6: LL(1) Parsing Construct the root node of the parse tree, label it with the start symbol, and set the current-node 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 non-terminal 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 Top-down 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 This technique is more general! Definition of FIRST sets This means that we have to examine ALL tokens that our productions could potentially start with X + X | - Y Here we can use the + and to decide which rule to apply x FIRST( ) iff * x , for some ( * means a series of (0 or more) productions) 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( ) = {} 3 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 ) If X is a terminal FIRST( X ) is { X } If X is a production, then is in FIRST(...
View Full Document

{[ snackBarMessage ]}

Page1 / 14

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

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

View Full Document
Ask a homework question - tutors are online