Answer Questions about Exam2 problems

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

Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
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.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
SIMPLIFYING GRAMMARS Definition: A useless symbol of a context-free
Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki.
Chapter 4 Normal Forms for CFGs Chomsky Normal Form n Defn A CFG G = (V, , P, S) is in chomsky normal form if each rule in G has one of.
CS5371 Theory of Computation
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
MA/CSSE 474 Theory of Computation
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.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Pushdown Automata (PDA) Intro
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
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.
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
Grammars CPSC 5135.
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.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
Context-Free Grammars Chapter 11. Languages and Machines.
CS 3240: Languages and Computation Context-Free Languages.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
Context-Free Grammars Chapter Languages and Machines 2.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Context-Free Grammars Chapter 11. Languages and Machines.
Structure and Ambiguity Removing Ambiguity Chomsky Normal Form Pushdown Automata Intro (who is he foolin', thinking that there will be time to get to this?)
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Theory of Languages and Automata By: Mojtaba Khezrian.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Normal Forms (Chomsky and Greibach) Pushdown Automata (PDA) Intro PDA examples MA/CSSE 474 Theory of Computation.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
PROGRAMMING LANGUAGES
CONTEXT-FREE LANGUAGES
Context-Free Grammars: an overview
Context-Free Languages
Formal Language & Automata Theory
Context-free grammars, derivation trees, and ambiguity
Fall Compiler Principles Context-free Grammars Refresher
Introduction to Parsing (adapted from CS 164 at Berkeley)
Ambiguity Parsing algorithms
Syntax Specification and Analysis
Complexity and Computability Theory I
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Simplifications of Context-Free Grammars
Simplifications of Context-Free Grammars
Context-Free Grammars
Context-free Languages
CHAPTER 2 Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
MA/CSSE 474 Theory of Computation
Structure and Ambiguity
Theory of Computation Lecture #
Fall Compiler Principles Context-free Grammars Refresher
COSC 3340: Introduction to Theory of Computation
MA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation
Context-Free Languages
Presentation transcript:

Answer Questions about Exam2 problems MA/CSSE 474 Theory of Computation Answer Questions about Exam2 problems Removing Ambiguity Chomsky, Griebach Normal Forms (who is he foolin', thinking that there will be time to get to all of this?) Exam next Friday!

Recap: Derivations of The Smart Cat A left-most derivation is: S  NP VP  the Nominal VP  the Adjs N VP  the Adj N VP  the smart N VP  the smart cat VP  the smart cat V NP  the smart cat smells NP  the smart cat smells Nominal  the smart cat smells N  the smart cat smells chocolate A right-most derivation is: S  NP VP  NP V NP  NP V Nominal  NP V N  NP V chocolate  NP smells chocolate  the Nominal smells chocolate  the Adjs N smells chocolate  the Adjs cat smells chocolate  the Adj cat smells chocolate 

Recap: Ambiguity A grammar is ambiguous iff there is at least one string in L(G) for which G produces more than one parse tree*. For many applications of context-free grammars, this is a problem. Example: A programming language. If there can be two different structures for a string in the language, there can be two different meanings. Not good! I removed the previous slide: Derivation is Not Necessarily Unique Which is About regular languages. * Equivalently, more than one leftmost derivation, or more than one rightmost derivation.

Recap: Expression Grammar E  E + E E  E  E E  (E) E  id

Recap:Inherent Ambiguity Some CF languages have the property that every grammar for them is ambiguous. We call such languages inherently ambiguous. Example: L = {anbncm: n, m  0}  {anbmcm: n, m  0}.

Recap: Inherent Ambiguity L = {anbncm: n, m  0}  {anbmcm: n, m  0}. One grammar for L has these rules: S  S1 | S2 S1  S1c | A /* Generate all strings in {anbncm}. A  aAb |  S2  aS2 | B /* Generate all strings in {anbmcm}. B  bBc |  Consider any string of the form anbncn. It turns out that L is inherently ambiguous.

Recap: Ambiguity and undecidability Both of the following problems are undecidable*: Given a context-free grammar G, is G ambiguous? Given a context-free language L, is L inherently ambiguous? Informal definition of undecidable for the first problem: There is no algorithm (procedure that is guaranteed to always halt) that, given a grammar G, determines whether G is ambiguous.

But We Can Often Reduce Ambiguity We can get rid of: ● some  rules like B  , ● rules with symmetric right-hand sides, e.g., S  SS E  E + E ● rule sets that lead to ambiguous attachment of optional postfixes, such as if … else …. Next time!

A Highly Ambiguous Grammar S  SS S  (S)

Resolving the Ambiguity with a Different Grammar The biggest problem is the  rule. A different grammar for the language of balanced parentheses: S*   S*  S S  SS S  (S) S  () We'd like to have an algorithm for removing all -productions except for the case where  is actually in the language; then we introduce a new start symbol and add one -production whose left side is that new symbol.

Nullable Nonterminals Examples: S  aTa T   T  A B A   B   A nonterminal X is nullable iff either: (1) there is a rule X  , or (2) there is a rule X  PQR… and P, Q, R, … are all nullable.

Nullable Nonterminals A nonterminal X is nullable iff either: (1) there is a rule X  , or (2) there is a rule X  PQ… where P, Q, … are all nullable nonterminals. So we compute Nullable, the set of nullable nonterminals, as follows: 1. Set Nullable to the set of nonterminals that satisfy (1). 2. Repeat until an entire pass is made without adding anything to Nullable Evaluate all other nonterminals with respect to (2). If any nonterminal satisfies (2) and is not in Nullable, add it.

A General Technique for Getting Rid of -Rules Definition: a rule is modifiable iff it is of the form: P  Q, for some nullable nonterminal Q. removeEps(G: cfg) = 1. Let G = G. 2. Find the set Nullable of nullable nonterminals in G. 3. Repeat until G contains no modifiable rules that haven’t been processed: Given the rule P  Q, where Q  Nullable, add the rule P   if it is not already present and if    and if P  . 4. Delete from G all rules of the form X  . 5. Return G. Then L(G) = L(G) – {}

An Example G = {{S, T, A, B, C, a, b, c}, {a, b, c}, R, S), R = { S  aTa T  ABC A  aA | C B  Bb | C C  c |  } removeEps(G: cfg) = 1. Let G = G. 2. Find the set N of nullable nonterminals in G. 3. Repeat until G contains no modifiable rules that haven’t been processed: Given the rule P  Q, where Q  N, add the rule P   if it is not already present and if    and if P  . 4. Delete from G all rules of the form X  . 5. Return G. C, A, B, T are all nullable.

What If   L? atmostoneEps(G: cfg) = 1. G = removeEps(G). 2. If SG is nullable then /* i. e.,   L(G) 2.1 Create in G a new start symbol S*. 2.2 Add to RG the two rules: S*   S*  SG. 3. Return G.