LL Parsing Objectives What Is LL(N) Parsing? What Is LL(N

LL Parsing Objectives What Is LL(N) Parsing? What Is LL(N

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

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us