LL Parsing Objectives What Is LL(N) Parsing? What Is LL(N
Total Page:16
File Type:pdf, Size:1020Kb
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 I I Use recursive-descent parsing to implement an LL parser. University of Illinois at Urbana-Champaign Explain how left-recursion and common prefixes defeat LL parsers. Department of Computer Science I Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? What Is LL(n) Parsing? I An LL parse uses a Left-to-right scan and produces a Leftmost derivation, using n tokens I An LL parse uses a Left-to-right scan and produces a Leftmost derivation, using n tokens of lookahead. of lookahead. I A.k.a. top-down parsing I A.k.a. top-down parsing Example Grammar: Syntax Tree: Example Grammar: Syntax Tree: S S S + EE S + EE → → E int E int → + E E E→ EE E EE →∗ →∗ Example Input: Example Input: + 2 * 3 4 + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? What Is LL(n) Parsing? I An LL parse uses a Left-to-right scan and produces a Leftmost derivation, using n tokens I An LL parse uses a Left-to-right scan and produces a Leftmost derivation, using n tokens of lookahead. of lookahead. I A.k.a. top-down parsing I A.k.a. top-down parsing Example Grammar: Syntax Tree: Example Grammar: Syntax Tree: S S S + EE S + EE → → E int + E E E int + E E E→ EE E→ EE →∗ →∗ Example Input: 2 Example Input: 2 * E E + 2 * 3 4 + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? What Is LL(n) Parsing? I An LL parse uses a Left-to-right scan and produces a Leftmost derivation, using n tokens I An LL parse uses a Left-to-right scan and produces a Leftmost derivation, using n tokens of lookahead. of lookahead. I A.k.a. top-down parsing I A.k.a. top-down parsing Example Grammar: Syntax Tree: Example Grammar: Syntax Tree: S S S + EE S + EE → → E int + E E E int + E E E→ EE E→ EE →∗ →∗ Example Input: 2 * E E Example Input: 2 * E E + 2 * 3 4 + 2 * 3 4 3 3 4 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 I Think of a production as a function definition. I Each function immediately checks the first token of the input string to see what to do next. I The LHS is the function being defined. Terminals on RHS are commands to consume input. I 1 getE [] = undefined I Nonterminals on RHS are subroutine calls. 2 getE ('*':xs) = 3 let e1,r1 = getE xs I For each production, make a function of type [String] -> (Tree,[String]). 4 e2,r2 = getE r1 I Input is a list of tokens. 5 in (ETimes e1 e2, r2) I Output is a syntax tree and remaining tokens. 6 getE .... -- other code follows I 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 Prefixes Common Prefixes Are Bad Left Recursion Is Bad E E A pair of rules rule like would confuse the function. I A rule like E E + E would cause an infinite loop. I → − EE → Which version of the rule should| − be used? 1 getE xx = 2 let e1,r1 = getE xx 1 getE ('-':xs) = ... -- unary rule 3 ('+':r2) = r1 2 getE ('-':xs) = ... -- binary rule 4 e2,r3 = getE r2 5 in (EPlus e1 e2, r3) I NB: Common prefixes must be for the same nonterminal. E.g., E x A and S x B do not count as common prefixes. → →.