2 * 3 4 + + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers Objectives LL Parsing The topic for this lecture is a kind of grammar that works well with recursive-descent parsing. ◮ Classify a grammar as being LL or not LL. Dr. Mattox Beckman ◮ Use recursive-descent parsing to implement an LL parser. University of Illinois at Urbana-Champaign ◮ Explain how left-recursion and common prefjxes defeat LL parsers. Department of Computer Science Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. of lookahead. ◮ A.k.a. top-down parsing ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: Example Grammar: Syntax Tree: S S S → + E E S → + E E E → int E → int + E E E →∗ E E E →∗ E E Example Input: Example Input:
4 + 2 + 2 * 3 4 3 4 + 2 * + 2 * 3 * 3 4 Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. of lookahead. ◮ A.k.a. top-down parsing ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: Example Grammar: Syntax Tree: S S S → + E E S → + E E E → int E → int + E E + E E E →∗ E E E →∗ E E Example Input: Example Input: 2 2 * E E Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. of lookahead. ◮ A.k.a. top-down parsing ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: Example Grammar: Syntax Tree: S S S → + E E S → + E E E → int E → int + E E + E E E →∗ E E E →∗ E E Example Input: Example Input: 2 * E E 2 * E E 3 3 4
('+' : r2) = r1 let e1,r1 = getE xx e2,r3 = getE r2 in ( EPlus e1 e2, r3) in ( ETimes e1 e2, r2) e2,r2 = getE r1 let e1,r1 = getE xs Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers How to Implement It Things to Notice Interpreting a Production Key Point – Prediction ◮ Think of a production as a function defjnition. ◮ Each function immediately checks the fjrst token of the input string to see what to do next. ◮ The LHS is the function being defjned. ◮ Terminals on RHS are commands to consume input. 1 getE [] = undefined ◮ Nonterminals on RHS are subroutine calls. 2 getE ('*' : xs) = 3 ◮ For each production, make a function of type [String] -> (Tree,[String]) . 4 ◮ Input is a list of tokens. 5 ◮ Output is a syntax tree and remaining tokens. 6 getE .... -- other code follows ◮ Of course, you need to create a type to represent your tree. Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers Left Recursion Rules with Common Prefjxes Common Prefjxes Are Bad Left Recursion Is Bad ◮ A pair of rules rule like E → − E − E E would confuse the function. ◮ A rule like E → E + E would cause an infjnite loop. | Which version of the rule should be used? 1 getE xx = 1 getE ('-' : xs) = ... -- unary rule 2 3 2 getE ('-' : xs) = ... -- binary rule 4 ◮ NB: Common prefjxes must be for the same nonterminal. E.g., E → x A and S → x B do 5 not count as common prefjxes.
Recommend
More recommend