ll parsing
play

LL Parsing Dr. Mattox Beckman University of Illinois at - PowerPoint PPT Presentation

Introduction LL Parsing Breaking LL Parsers LL Parsing Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Introduction LL Parsing Breaking LL Parsers Objectives The topic for this lecture is a


  1. Introduction LL Parsing Breaking LL Parsers LL Parsing Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science

  2. Introduction LL Parsing Breaking LL Parsers Objectives 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. ◮ Use recursive-descent parsing to implement an LL parser. ◮ Explain how left-recursion and common prefjxes defeat LL parsers.

  3. + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: S S → + E E E → int E →∗ E E Example Input:

  4. + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: S S → + E E E → int + E E E →∗ E E Example Input:

  5. + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: S S → + E E E → int + E E E →∗ E E Example Input: 2

  6. + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: S S → + E E E → int + E E E →∗ E E Example Input: 2 * E E

  7. + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: S S → + E E E → int + E E E →∗ E E Example Input: * E E 2 3

  8. + 2 * 3 4 Introduction LL Parsing Breaking LL Parsers What Is LL(n) Parsing? ◮ An LL parse uses a L eft-to-right scan and produces a L eftmost derivation, using n tokens of lookahead. ◮ A.k.a. top-down parsing Example Grammar: Syntax Tree: S S → + E E E → int + E E E →∗ E E Example Input: * E E 2 3 4

  9. Introduction LL Parsing Breaking LL Parsers How to Implement It Interpreting a Production ◮ Think of a production as a function defjnition. ◮ The LHS is the function being defjned. ◮ Terminals on RHS are commands to consume input. ◮ Nonterminals on RHS are subroutine calls. ◮ For each production, make a function of type [String] -> (Tree,[String]) . ◮ Input is a list of tokens. ◮ Output is a syntax tree and remaining tokens. ◮ Of course, you need to create a type to represent your tree.

  10. let e1,r1 = getE xs e2,r2 = getE r1 in ( ETimes e1 e2, r2) Introduction LL Parsing Breaking LL Parsers Things to Notice Key Point – Prediction ◮ Each function immediately checks the fjrst token of the input string to see what to do next. 1 getE [] = undefined 2 getE ('*' : xs) = 3 4 5 6 getE .... -- other code follows

  11. ('+' : r2) = r1 let e1,r1 = getE xx e2,r3 = getE r2 in ( EPlus e1 e2, r3) Introduction LL Parsing Breaking LL Parsers Left Recursion Left Recursion Is Bad ◮ A rule like E → E + E would cause an infjnite loop. 1 getE xx = 2 3 4 5

  12. Introduction LL Parsing Breaking LL Parsers Rules with Common Prefjxes Common Prefjxes Are Bad ◮ A pair of rules rule like E → − E − E E would confuse the function. | Which version of the rule should be used? 1 getE ('-' : xs) = ... -- unary rule 2 getE ('-' : xs) = ... -- binary rule ◮ NB: Common prefjxes must be for the same nonterminal. E.g., E → x A and S → x B do not count as common prefjxes.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend