Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars.

Slides:



Advertisements
Similar presentations
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Advertisements

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.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
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.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Specifying Languages CS 480/680 – Comparative 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.
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.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Lecture 19 Naveen Z Quazilbash. Overview CNFs-Assignment Greibach Normal Forms.
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:
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
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.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
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”
Overview Mathematical Induction Derivations Recursive Inference Parse Trees Equivalence of Inference, Derivations, and Parse Trees.
1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars.
1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple.
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.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Module 29 Parse/Derivation Trees Ambiguous Grammars
Context-Free Languages. Regular Languages Context-Free Languages.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 5 Context-free Languages
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.
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
AUTOMATA THEORY. Chapter 05 CONTEX-FREE GRAMMERS AND LANGUAGES.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
CSE 3302 Programming Languages
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
5. Context-Free Grammars and Languages
CONTEXT-FREE LANGUAGES
Parsing & Context-Free Grammars
Formal Language & Automata Theory
G. Pullaiah College of Engineering and Technology
Fall Compiler Principles Context-free Grammars Refresher
CSE 105 theory of computation
Syntax Analysis Sections :.
Context-Free Languages
Relationship to Left- and Rightmost Derivations
5. Context-Free Grammars and Languages
Finite Automata and Formal Languages
Relationship to Left- and Rightmost Derivations
Theory of Computation Lecture #
CFGs: Formal Definition
Fall Compiler Principles Context-free Grammars Refresher
CSE 105 theory of computation
COSC 3340: Introduction to Theory of Computation
CSE 105 theory of computation
Presentation transcript:

Lecture 15 Naveen Z Quazilbash Ambiguity

Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars

S-grammars Simple grammars a.k.a s-grammars. Rules for s-grammars: Productions of the form A  sB 1 B 2 B 3 ….B n,where s is a symbol and B 1 B 2 B 3 ….B n are variables. For each variable A and symbol s, there is at most one production of the form: A  sB 1 B 2 B 3 ….B n

S-grammars For example, we can not have A  aBC A  aCD Because we must only have one “A  a…” production.

S-grammars Example of a simple grammar: S  aA|bBC A  aA|bb B  bB|aC C  cC|a w=bbacca Parsing: S=>bBC=>bbBC=>bbaC=>bbacC=>bbaccC=>bbacca

S-grammars We use leftmost derivation. At each step we know which production to apply, by looking at the next symbol in w. Remark: Thus, the derivation takes linear time; that is, the time is proportional to |w|.

Ambiguity

Some important notes Applications of CFG’s (like Parsers, DTDs and markup languages) often rely on the grammar to provide the structure of files. Tacit Assumption: A grammar uniquely determines a structure for each string in its language. Problem: However, that’s not always possible. Solution: Redesign the grammar!!

solution Options: 1. Redesign possibility 2. Redesign impossibility ~ Inherently ambiguous (beyond our scope) So, first, lets try to understand the problem now!

Ambiguity Example 1: S  SA | A A  aAb | ε w=aabb Give it lm and rm derivations and also draw parse trees for both lm and rm derivations.

Ambiguity lm: S=>SA=>AA=>aAbA=>aaAbbA=>aabbA=>aabb rm: S=>SA=>SaAb=>SaaAbb=>Saabb=>Aaabb=>aabb

Ambiguity Example 2: E  I E  E+E E  E*E E  (E) I  a|b|Ia|Ib|I0|I1 w=E+E*E Give it lm and rm derivations and also draw parse trees for both lm and rm derivations.

Ambiguity rm: E=>E+E=>E+E*E lm: E=>E*E=>E+E*E

Ambiguity in Grammars There is a significant difference between the two derivations. What’s that? Hence, this grammar is not a good one for providing unique structures. In particular, while it can give correct groupings to arithmetic expressions, it also gives them incorrect groupings.

Ambiguity in Grammars Lets have a look at another very important factor, i-e “what causes ambiguity?” Multiplicity of derivations? //discussion

Ambiguous Grammars and Unambiguous Grammars A CFG, G={V, T, P, S} is ambiguous if there is at least one string w in T* for which we can find two different parse trees, each with root labeled S and yield w. If each string has at most one parse tree, then the grammar is unambiguous.

Unambiguous Grammars - Avoiding ambiguity Next class