compiling t echniques
play

Compiling T echniques Lecture 5: Introduction to Parsing - PowerPoint PPT Presentation

Compiling T echniques Lecture 5: Introduction to Parsing Christophe Dubach Overview Context Free Grammars Derivations and Parse T rees Ambiguity T op-Down Parsing Left Recursion Front End: Parser IR tokens Source Parser Scanner


  1. Compiling T echniques Lecture 5: Introduction to Parsing Christophe Dubach

  2. Overview Context Free Grammars Derivations and Parse T rees Ambiguity T op-Down Parsing Left Recursion

  3. Front End: Parser IR tokens Source Parser Scanner code Errors Checks the stream of words and their parts of speech (produced by the scanner) for grammatical correctness Determines if the input is syntactically well formed Guides checking at deeper levels than syntax Builds an IR representation of the code Think of this as the mathematics of diagramming sentences

  4. The Study of Parsing The process of discovering a derivation for some sentence Need a mathematical model of syntax — a grammar G Need an algorithm for testing membership in L(G) Need to keep in mind that our goal is building parsers, not studying the mathematics of arbitrary languages Roadmap Context-free grammars and derivations T op-down parsing: Recursive descent parsers LL(1) == L eft-to-right, L eftmost derivation, 1 token of lookahead Bottom-up parsing: Operator precedence parser LR(1) == L eft-to-right, R ightmost derivation, 1 token of lookahead

  5. Specifying Syntax with a Grammar Context-free syntax is specifjed with a grammar 1 SheepNoise → SheepNoise baa 2 baa | This grammar defjnes the set of noises that a sheep makes under normal circumstances It is written in a variant of Backus–Naur Form (BNF) Formally, a grammar G = (S,N,T,P) S is the start symbol N is a set of non-terminal symbols T is a set of terminal symbols or words P is a set of productions or rewrite rules (P:N→N∪T)

  6. Deriving Syntax We can use the SheepNoise grammar to create sentences: use the productions as rewriting rules And so on ... While it is cute, this example quickly runs out of intellectual steam ...

  7. A More Useful Grammar 1 Expr Expr Op Expr → 2 num | 3 id | 4 Op + → 5 - | 6 * | 7 / | this derivation represents x - 2 * y Such a sequence of rewrites is called a derivation Process of discovering a derivation is called parsing

  8. Derivations At each step, we choose a non-terminal to replace Difgerent choices can lead to difgerent derivations T wo derivations are of interest Leftmost derivation — replace leftmost NT at each step Rightmost derivation — replace rightmost NT at each step These are the two systematic derivations (We don’t care about randomly-ordered derivations!) The example on the preceding slide was a leftmost derivation Of course, there is also a rightmost derivation Interestingly, it turns out to be difgerent

  9. The T wo Derivations for x – 2 * y Leftmost derivation Rightmost derivation In both cases, id – num * id The two derivations produce difgerent parse trees The parse trees imply difgerent evaluation orders!

  10. Derivations and Parse Trees LEFTMOST DERIVATION G E E Op E x – Op E E 2 y * This evaluates as x – ( 2 * y )

  11. Derivations and Parse Trees RIGHTMOST DERIVATION G E E Op E y E Op E * x 2 – This evaluates as ( x – 2 ) * y

  12. Derivations and Precedence These two derivations point out a problem with the grammar: It has no notion of precedence , or implied order of evaluation T o add precedence Create a non-terminal for each level of precedence Isolate the corresponding part of the grammar Force the parser to recognise high precedence subexpressions fjrst For algebraic expressions Multiplication and division, fjrst (level one) Subtraction and addition, next (level two)

  13. Derivations and Precedence 1 Goal Expr → This grammar is slightly larger 2 Expr Expr + T erm → level • T two akes more rewriting to reach 3 Expr - T erm | some of the terminal symbols 4 T erm | 5 T erm T erm * Factor → level • Encodes expected precedence one 6 T erm / Factor | • Produces same parse tree 7 Factor | under leftmost & rightmost 8 Factor number → derivations 9 id | Let’s see how it parses x - 2 * y

  14. Derivations and Precedence G E – E T T T * F < id,y > F F < id,x > < num,2 > The rightmost derivation Its parse tree This produces x – ( 2 * y ), along with an appropriate parse tree. Both the leftmost and rightmost derivations give the same expression, because the grammar directly encodes the desired precedence.

  15. Ambiguous Grammars Our original expression grammar had other problems 1 Expr Expr Op Expr → 2 num | 3 id | 4 Op + → 5 - | 6 * | 7 / | difgerent choice than the fjrst time • This grammar allows multiple leftmost derivations for x - 2 * y • Hard to automate derivation if > 1 choice • The grammar is ambiguous

  16. T wo Leftmost Derivations for x – 2 * y The Difgerence: Difgerent productions chosen on the second step Both derivations succeed in producing x - 2 * y Original choice New choice

  17. Ambiguous Grammars If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous If a grammar has more than one rightmost derivation for a single sentential form, the grammar is ambiguous The leftmost and rightmost derivations for a sentential form may difger, even in an unambiguous grammar Classic example — the if-then-else problem 1 Stmt → if Expr then Stmt 2 if Expr then Stmt else Stmt | 3 OtherStmt | This ambiguity is entirely grammatical in nature

  18. Ambiguity if E 1 then if E 2 then S 1 else S 2 if if E 1 E 1 then else then production 1, then production 2, then production 2 S 2 production 1 if if E 2 E 2 else then then S 1 S 1 S 2 This sentential form has two derivations if E 1 then if E 1 then if E 2 then if E 2 then S 1 S 1 else else S 2 S 2

  19. Ambiguity Removing the ambiguity Must rewrite the grammar to avoid generating the problem Match each else to innermost unmatched if (common sense rule) 1 Stmt → WithElse 2 NoElse | 3 WithElse if Expr then WithElse else WithElse → 4 OtherStmt | 5 NoElse if Expr then Stmt → 6 if Expr then WithElse else NoElse | Intuition: a NoElse always has no else on its last cascaded else if statement With this grammar, the example has only one derivation

  20. 1 Stmt → WithElse 2 NoElse | Ambiguity 3 WithElse if Expr then WithElse else WithElse → 4 OtherStmt | 5 NoElse if Expr then Stmt → 6 if Expr then WithElse else NoElse | if E 1 then if E 2 then S 1 else S 2 This binds the else controlling S 2 to the inner if

  21. Deeper Ambiguity Ambiguity usually refers to confusion in the CFG (Context-Free Grammar) Consider the following case: a = f(17) In Algol-like languages, f could be either a function or an array In such cases, a context is required Need to track declarations Really a type issue, not context-free syntax Requires an extra-grammatical solution (not in the CFG) Must handle these with a difgerent mechanism Step outside the grammar rather than making it more complex

  22. Ambiguity - Final Word Ambiguity arises from two distinct sources • Confusion in the context-free syntax (if-then-else) • Confusion that requires context to resolve (overloading) Resolving ambiguity • T o remove context-free ambiguity, rewrite the grammar • T o handle context-sensitive ambiguity takes cooperation → Knowledge of declarations, types, … → Accept a superset of L(G) & check it by other means → This is a language design problem Sometimes, the compiler writer accepts an ambiguous grammar → Parsing techniques that “do the right thing” → i.e., always select the same derivation

  23. Parsing T echniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production & try to match the input Bad “pick” ⇒ may need to backtrack Some grammars are backtrack-free (predictive parsing) Bottom-up parsers (LR(1), operator precedence) Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state Start in a state valid for legal fjrst tokens Bottom-up parsers handle a large class of grammars

  24. T op-Down Parsing A top-down parser starts with the root of the parse tree The root node is labelled with the goal symbol of the grammar T op-down parsing algorithm: Construct the root node of the parse tree Repeat until the fringe of the parse tree matches the input string 1 At a node labelled A, select a production with A on its lhs and, for each symbol on its rhs, construct the appropriate child 2 When a terminal symbol is added to the fringe and it doesn’t match the fringe, backtrack 3 Find the next node to be expanded (label ∈ NT) • The key is picking the right production in step 1 → That choice should be guided by the input string

  25. 1 Goal Expr → 2 Expr Expr + T erm → Example 3 Expr - T erm | 4 T erm | 5 T erm T erm * Factor → 6 T erm / Factor | 7 Factor | 8 Factor number → 9 | id Let’s try x – 2 * y : Goal Expr Expr + T erm T erm Fact. < id,x > Leftmost derivation, choose productions in an order that exposes problems

  26. 1 Goal Expr → 2 Expr Expr + T erm → Example 3 Expr - T erm | 4 T erm | T erm 5 T erm * Factor → 6 T erm / Factor | 7 Factor | 8 Factor number → 9 | id Let’s try x – 2 * y : Goal Expr Expr + T erm T erm Fact. < id,x > This worked well, except that “–” doesn’t match “+” The parser must backtrack to here

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