1 / 26

Chapter 3 Context-Free Grammars and Parsing Ambiguous Grammar

Chapter 3 Context-Free Grammars and Parsing Ambiguous Grammar. Gang S. Liu College of Computer Science & Technology Harbin Engineering University. Ambiguous Grammar. exp → exp op exp | ( exp ) | number op → + | - | *.

Download Presentation

Chapter 3 Context-Free Grammars and Parsing Ambiguous Grammar

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Context-Free Grammars and Parsing Ambiguous Grammar Gang S. Liu College of Computer Science & Technology Harbin Engineering University Samuel2005@126.com

  2. Ambiguous Grammar • exp → exp op exp | (exp) | number • op → + | - | * • The string 34 – 3 * 42 has two different parse trees and syntax trees. * - 42 34 3 - 34 * 3 42 A grammar that generates a string with two distinct parse trees is called an ambiguous grammar. Samuel2005@126.com

  3. Two Ways to Deal with Ambiguity • Disambiguating rule • State a rule that specifies in each ambiguous case which of the trees is correct. • It corrects the ambiguity without changing and complicating the grammar. • Syntactic structure of the language is not given by the grammar alone. • Change grammar into a form that forces the construction of a correct parse tree. • In both cases we must decide which of the trees are correct. Samuel2005@126.com

  4. Ambiguous Grammar • exp → exp op exp | (exp) | number • op → + | - | * • The string 34 – 3 * 42 has two different parse trees and syntax trees. * - 42 34 3 - 34 * 3 42 √ × A grammar that generates a string with two distinct parse trees is called an ambiguous grammar. Samuel2005@126.com

  5. Precedence • Some operations have precedence over other operations. • Multiplication has precedence over addition. • To handle the precedence of operations in the grammar, we group the operators into groups of equal precedence. • We must write a different rule for each precedence. Samuel2005@126.com

  6. Example exp → exp op exp | (exp) | number op →+ | - | * * - 42 34 3 - 34 * 3 42 √ × exp → exp addop exp | term addop →+ | - term → term mulop term | factor mulop →* factor →(exp) | number Samuel2005@126.com

  7. (34 – 3) – 42 = –11 34 – (3 – 42) = 73 Ambiguous 34 – 3 – 42 √ × (34 – 3) – 42 Samuel2005@126.com

  8. Associativity • Some operation (like subtraction) are left associative. • A series of subtraction operations is performed from left to right. • exp → exp addop exp | term • Recursion on both sides of the operator allows either side to match repetitions of the operator in a derivation. • exp → exp addop term | term • The right recursion is replaced with the base case, forcing the repetitive matches on the left side • Left recursion makes addition and subtraction left associative. Samuel2005@126.com

  9. Example 34 - 3 - 42has a unique parse tree Samuel2005@126.com

  10. Example exp → exp addop term | term addop →+ | - term → term mulop factor | factor mulop →* factor →(exp) | number 34- 3* 42has a unique parse tree exp exp addop term term - term mulop factor factor factor * number number number Samuel2005@126.com

  11. Dangling else Problem statement → if-stmt | other if-stmt → if (exp)statement | if(exp) statementelsestatement exp → 0 | 1 • if (0) if (1) other else other has two parse trees with two meaning if (0) if (1) other else otherand if (0) if (1) other else other √ × Samuel2005@126.com

  12. if (0) if (1) other else other Samuel2005@126.com

  13. if (0) if (1) other else other Samuel2005@126.com

  14. Dangling else Problem statement → if-stmt | other if-stmt → if (exp)statement | if(exp) statementelsestatement exp → 0 | 1 • if (0) if (1) other else other has two parse trees with two meaning if (0) if (1) other else otherand if (0) if (1) other else other • Modify the grammar. • Disambiguating rule: the most closely nested rule. Samuel2005@126.com

  15. Dangling else Problem if (0) if (1) other else other statement → matched-stmt | unmatched-stmt matched-stmt →if (exp) matched-stmt else matched-stmt | other unmatched-stmt → if (exp)statement | if(exp) matched-stmtelseunmatched-stmt exp → 0 | 1 Samuel2005@126.com

  16. Inessential Ambiguity • Sometimes a grammar may be ambiguous and yet always produce unique abstract syntax trees. • Example: ( a + b ) + c = a + ( b + c ) Samuel2005@126.com

  17. Extended BNF Notation (EBNF) • { }repetitions A → β {α} and A → {α} β Samuel2005@126.com

  18. Left and Right Recursion • Left recursive grammar: • A → A α | β • Equivalent to β α* • A → β {α} • Right recursive grammar: • A → αA | β • Equivalent to α * β • A → {α} β Samuel2005@126.com

  19. Extended BNF Notation (EBNF) • { }repetitions A → β {α} and A → {α} β • [ ] optional constructs statement → if-stmt | other if-stmt → if (exp)statement | if(exp) statementelsestatement exp → 0 | 1 statement → if-stmt | other if-stmt → if (exp)statement[elsestatement] exp → 0 | 1 Samuel2005@126.com

  20. Syntax Diagrams • Graphical representations for visually representing EBNF rules are called syntax diagrams. • They consist of boxes representing terminals and nonterminals, arrowed lines representing sequencing and choices, and nonterminal labels for each diagram representing the grammar rule defining that nonterminal. • A round or oval box is used to indicate terminals in a diagram, while a square or rectangular box is used to indicate nonterminals. Samuel2005@126.com

  21. Syntax Diagrams(cont) • As an example, consider the grammar rule • factor → ( exp ) | number • This is written as a syntax diagram in the following way: Samuel2005@126.com

  22. Syntax Diagrams(cont) • Syntax diagrams are written from the EBNF rather than the BNF, so we need diagrams representing repetition and optional constructs. Given a repetition such as • A → { B } • The corresponding syntax diagram is usually drawn as follow: Samuel2005@126.com

  23. Syntax Diagrams(cont) • An optional construct such as • A → [ B ] • Is drawn as: Samuel2005@126.com

  24. Example exp → exp addop term | term addop →+ | – term → term mulop factor | factor mulop →* factor →(exp) | number exp → term { addop term } addop →+ | – term → factor { mulop factor } mulop →* factor →(exp) | number Samuel2005@126.com

  25. Example statement → if-stmt | other if-stmt → if (exp)statement | if(exp) statementelsestatement exp → 0 | 1 statement → if-stmt | other if-stmt → if (exp)statement[elsestatement] exp → 0 | 1 Samuel2005@126.com

  26. Homework Samuel2005@126.com

More Related