1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars.

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Advertisements

1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Context-Free Grammars
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Transparency No. P2C4-1 Formal Language and Automata Theory Part II Chapter 4 Parse Trees and Parsing.
Context-Free Grammars Lecture 7
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
Courtesy Costas Busch - RPI1 Context-Free Languages.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
1 Lecture 27 Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions If-then-else Statements.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 13 Parsing and Ambiguity. Given a string x and a CFG G = (V, Σ, R, S), determine whether x L(G) and if x L(G), find a derivation S * x. This problem.
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Syntax Analysis The recognition problem: given a grammar G and a string w, is w  L(G)? The parsing problem: if G is a grammar and w  L(G), how can w.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Chapter 5 Context-Free Grammars
PART I: overview material
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
CS 3240: Languages and Computation Context-Free Languages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 Chapter 4 Grammars and Parsing. 2 Context-Free Grammars: Concepts and Notation A context-free grammar G = (Vt, Vn, S, P) –A finite terminal vocabulary.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Module 29 Parse/Derivation Trees Ambiguous Grammars
Context-Free Languages. Regular Languages Context-Free Languages.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analyzer (Parser)
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
5. Context-Free Grammars and Languages
CONTEXT-FREE LANGUAGES
CS510 Compiler Lecture 4.
Fall Compiler Principles Context-free Grammars Refresher
Lecture 14 Grammars – Parse Trees– Normal Forms
Context-Free Languages
Relationship to Left- and Rightmost Derivations
Relationship to Left- and Rightmost Derivations
Theory of Computation Lecture #
CFGs: Formal Definition
Fall Compiler Principles Context-free Grammars Refresher
Presentation transcript:

1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars

2 Parse Trees uParse trees are trees labeled by symbols of a particular CFG.  Leaves: labeled by a terminal or ε. uInterior nodes: labeled by a variable. wChildren are labeled by the body of a production for the parent. uRoot: must be labeled by the start symbol.

3 Example: Parse Tree S -> SS | (S) | () S SS S)( () ()

4 Yield of a Parse Tree uThe concatenation of the labels of the leaves in left-to-right order wThat is, in the order of a preorder traversal. is called the yield of the parse tree. uExample: yield of is (())() S SS S)( () ()

5 Generalization of Parse Trees uWe sometimes talk about trees that are not exactly parse trees, but only because the root is labeled by some variable A that is not the start symbol. uCall these parse trees with root A.

6 Parse Trees, Leftmost and Rightmost Derivations uTrees, leftmost, and rightmost derivations correspond. uMeaning: 1.If there is a parse tree with root labeled A and yield w, then A =>* lm w. 2.If A =>* lm w, then there is a parse tree with root A and yield w.

Equivalance 7 Parse Tree Right most Der Left most Der. Deriv ation

8 Ambiguous Grammars uA CFG is ambiguous if there is a string in the language that is the yield of two or more parse trees. uExample: S -> SS | (S) | () uTwo parse trees for ()()() on next slide.

9 Example – Continued S SS SS () S SS SS ()()()()()

Example uE  E * E uE  E + E uW = E + E * E 10 E EE EE + E EE EE *+ *

11 Ambiguity uEquivalent definitions of “ambiguous grammar’’ are: 1.There is a string in the language that has two different leftmost derivations. 2.There is a string in the language that has two different rightmost derivations.

12 Ambiguity is a Property of Grammars, not Languages uFor the balanced-parentheses language, here is another CFG, which is unambiguous. B -> (RB | ε R -> ) | (RR B, the start symbol, derives balanced strings. R generates certain strings that have one more right paren than left.

13 Example: Unambiguous Grammar B -> (RB | ε R -> ) | (RR uConstruct a unique leftmost derivation for a given balanced string of parentheses by scanning the string from left to right.  If we need to expand B, then use B -> (RB if the next symbol is “(”; use ε if at the end. wIf we need to expand R, use R -> ) if the next symbol is “)” and (RR if it is “(”.

14 The Parsing Process Remaining Input: (())() Steps of leftmost derivation: B Next symbol B -> (RB | ε R -> ) | (RR

15 The Parsing Process Remaining Input: ())() Steps of leftmost derivation: B (RB Next symbol B -> (RB | ε R -> ) | (RR

16 The Parsing Process Remaining Input: ))() Steps of leftmost derivation: B (RB ((RRB Next symbol B -> (RB | ε R -> ) | (RR

17 The Parsing Process Remaining Input: )() Steps of leftmost derivation: B (RB ((RRB (()RB Next symbol B -> (RB | ε R -> ) | (RR

18 The Parsing Process Remaining Input: () Steps of leftmost derivation: B (RB ((RRB (()RB (())B Next symbol B -> (RB | ε R -> ) | (RR

19 The Parsing Process Remaining Input: ) Steps of leftmost derivation: B(())(RB (RB ((RRB (()RB (())B Next symbol B -> (RB | ε R -> ) | (RR

20 The Parsing Process Remaining Input: Steps of leftmost derivation: B(())(RB (RB(())()B ((RRB (()RB (())B Next symbol B -> (RB | ε R -> ) | (RR

21 The Parsing Process Remaining Input: Steps of leftmost derivation: B(())(RB (RB(())()B ((RRB(())() (()RB (())B Next symbol B -> (RB | ε R -> ) | (RR

Solving Ambiguity uIn an ideal world there should be an algorithm that removes ambiguity from a given CFG wUnfortunately NO uNo algorithm even to tell a CFG is ambiguous or not uSome context-free languages have only ambiguous CFGs 22

23 Inherent Ambiguity uIt would be nice if for every ambiguous grammar, there were some way to “fix” the ambiguity, as we did for the balanced-parentheses grammar. uUnfortunately, certain CFL’s are inherently ambiguous, meaning that every grammar for the language is ambiguous.

24 Example: Inherent Ambiguity uThe language {0 i 1 j 2 k | i = j or j = k} is inherently ambiguous. uIntuitively, at least some of the strings of the form 0 n 1 n 2 n must be generated by two different parse trees, one based on checking the 0’s and 1’s, the other based on checking the 1’s and 2’s.

25 One Possible Ambiguous Grammar S -> AB | CD A -> 0A1 | 01 B -> 2B | 2 C -> 0C | 0 D -> 1D2 | 12 A generates equal 0’s and 1’s B generates any number of 2’s C generates any number of 0’s D generates equal 1’s and 2’s And there are two derivations of every string with equal numbers of 0’s, 1’s, and 2’s. E.g.: S => AB => 01B =>012 S => CD => 0D => 012