Key Answers for Homework #7

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS5371 Theory of Computation
127 The Chomsky Hierarchy(review) Recursively Enumerable Sets Turing Machines Post System Markov Algorithms,  -recursive Functions Regular Expression.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
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.
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
Normal forms for Context-Free Grammars
1 Background Information for the Pumping Lemma for Context-Free Languages Definition: Let G = (V, T, P, S) be a CFL. If every production in P is of the.
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.
Syntactic Pattern Recognition Statistical PR:Find a feature vector x Train a system using a set of labeled patterns Classify unknown patterns Ignores relational.
Homework #7 Solutions. #1. Use the pumping lemma for CFL’s to show L = {a i b j a i b j | i, j > 0} is not a CFL. Proof by contradiction using the Pumping.
Chapter 12: Context-Free Languages and Pushdown Automata
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
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.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
1 Homework #7 (Models of Computation, Spring, 2001) Due: Section 1; April 16 (Monday) Section 2; April 17 (Tuesday) 2. Covert the following context-free.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Grammars CPSC 5135.
Lecture # 5 Pumping Lemma & Grammar
Section 12.4 Context-Free Language Topics
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
1 1. Let A ={r, p, i } and B = { w, o, r, l, d }. What does each of the following (a), (b) and (c) denote? Briefly explain in plain English. (a) A * B.
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.
Chapter 6 Properties of Regular Languages. 2 Regular Sets and Languages  Claim(1). The family of languages accepted by FSAs consists of precisely the.
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.
Pumping Lemma for CFLs. Theorem 7.17: Let G be a CFG in CNF and w a string in L(G). Suppose we have a parse tree for w. If the length of the longest path.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
CSCI 3130: Formal languages and automata theory Andrej Bogdanov The Chinese University of Hong Kong Limitations.
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.
Exercises on Chomsky Normal Form and CYK parsing
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
Computability Joke. Context-free grammars Parsing. Chomsky
lec02-parserCFG May 8, 2018 Syntax Analyzer
Closed book, closed notes
Context-Free Languages & Grammars (CFLs & CFGs)
Context-Free Grammars: an overview
Syntax Specification and Analysis
Complexity and Computability Theory I
7. Properties of Context-Free Languages
Lecture 22 Pumping Lemma for Context Free Languages
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Compiler Construction
PDAs Accept Context-Free Languages
FORMAL LANGUAGES AND AUTOMATA THEORY
Jaya Krishna, M.Tech, Assistant Professor
More on Context Free Grammars
Context-Free Languages
Definition: Let G = (V, T, P, S) be a CFL
CHAPTER 2 Context-Free Languages
فصل دوم Context-Free Languages
Finite Automata and Formal Languages
COSC 3340: Introduction to Theory of Computation
Properties of Context-Free Languages
Midterm (Models of Computation, Fall, 2000)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS21 Decidability and Tractability
Theory of Computation Lecture #
Applications of Regular Closure
lec02-parserCFG May 27, 2019 Syntax Analyzer
Limitations of context-free languages
Presentation transcript:

Key Answers for Homework #7 1. This problem is to clean up context-free grammar G shown below. Using the two algorithms presented in the class, we are going to eliminate all useless symbols from the following context-free grammar G. Answer the following two questions. (a) Using the algorithm in Lemma 1 for grammar G construct a context-free grammar G’ such that L(G) = L(G’) and every nonterminal symbol in G’ derives a string x (VT)*, where VT is set of terminal symbols of G. (b) Using the algorithm in Lemma 2 for grammar G’, the result of part (a), and construct a context-free grammar G’’ such that L(G) = L(G’’), and for each symbol X (which is either terminal or nonterminal symbol) in G’’, starting with the start symbol, it is possible to derive a string containing X. Your answer should include detailed procedure (steps of the algorithm) that you took to collect the information needed to get your answer. No grade will be given to an answer that only shows the final result. G: S BA | CDg B aEA A  DEd C  Ce D  Df |  E  Eg | g F  hF | h G cGCc I  cDEFab Answer: (a) OLDV = {}; NEWV = {D, E, F} OLDV = NEWV; NEWV = OLDV  {A, I} = {A, D, E, F, I}; OLDV = NEWV; NEWV = OLDV  {B} = {A, B, D, E, F, I}; OLDV = NEWV; NEWV = OLDV  {S} = {A, B, D, E, F, I, S}; OLDV = NEWV; NEWV = OLDV  { } = {A, B, D, E, F, I, S}; V´N = NEWV = {A, B, D, E, F, I, S}; // Set of nonterminal symbols which derive a string x (VT)* G’: S BA B aEA A  DEd D  Df |  E  Eg | g F  hF | h I  cDEFab

G’: S BA B aEA A  DEd D  Df |  E  Eg | g F  hF | h I  cDEFab Answer (b): Constructing a context-free grammar G’’ such that L(G) = L(G’’), and for each symbol X (which is either terminal or nonterminal symbol) in G’’, starting with the start symbol, it is possible to derive a string containing X. V´T = V´N = {}; // initialize with empty set V´N = V´N {S} V´T = V´T  {} = {} V´N = V´N {A, B} = {S, A, B} V´T = V´T  {} = {} V´N = V´N {D, E} = {S, A, B, D, E} V´T = V´T  {a, d} ={a, d} V´N = V´N  {} = {S, A, B, D, E} V´T = V´T  {f, g, } ={a, d, f, g,  } V´N = V´N  {} = {S, A, B, D, E} V´T = V´T  {} ={a, d, f, g,  } G’’: S BA B aEA A  DEd D  Df |  E  Eg | g

2. Covert the following context-free grammar into a Chomsky Normal Form. S  abABCde A  aAa B  ba C  cab Answer: Given rule Converted rules ----------------------- ------------------------------------------------------------------------------------------------------------- S  abABCde S  S1S2 S1  a S2  S3S4 S3  b S4  AS5 S5  BS6 S6  CS7 S7  S8S9 S8  d S9  e A  aAa A  A1A2 A1 a A2  AA3 A3  a B  ba B  B1B2 B1  b B2  a C  cab C  C1C2 C1  c C2  C3C4 C2  a C2  b

3. (a) Show that the following grammar is ambiguous. S  S + S | S * S | T T  a | b Answer: (a) For string a + b * a, we have the following two parse trees. S + T a b * (b) Let + be the addition and * be the multiplication. Then the above grammar generates mathematical expressions with the two operators on two variable a and b. Convert the above grammar to an unambiguous context-free grammar such that it generates the same language and * has higher precedence than +. Answer: S  S + A | A A  A * T | T T  a | b * Notice that this grammar restricts such that multiplications are done as a term of an addition, if any. (c) Convert the above grammar to an unambiguous context-free grammar such that - it generates the same language, - the two operators * and + have the same precedence, and - the evaluation is done right to left. Answer: S  T + S | T * S | T T  a | b * Notice that all the operators in a string are generated left to right according to the order of appearance.

4. The following syntax flow graph is a simplified definition of the Pascal’s syntax flow graph for < IF statement>. Assume that all the strings in a circle are terminal symbols. <IF-statement> IF ( <bool> ) then else <statement> c d a + b a - b (a) Convert this syntax flow graph for <If-statement> to a context-free grammar that generate the same language, and show that the grammar is ambiguous. Answer: In the following grammar, <…> denotes nonterminals, and others are terminals. <IF-statement> is starting nonterminal. <IF-statement>  IF(<bool>)then<statement> | IF(<bool>)then<statement>else<statement> <statement>  a – b | <IF-statement> | a + b <bool>  c | d This grammar is ambiguous because there are two parse trees for string w = IF(c) then IF (d) then a + b else a - b. (Ignore blanks in the string. They are inserted for readability.)

<IF-statement>  IF(<bool>)then<statement> | IF(<bool>)then<statement>else<statement> <statement>  a – b | <IF-statement> | a + b <bool>  c | d For simplicity, lets transform the above grammar as shown below with the nonterminals and terminals substituted as follows; <IF-statement> = S <bool> = B <statement> = A IF = i then = t else = e a + b = p a – b = m S  i(B)tA | i(B)tAeA A  m | S | p B  c | d Then, for string i(c)ti(d)tpem (which is IF (c) then IF (d) then a + b else a – b according to the original grammar) has two parse trees as follows. i ( B ) t A d p S i ( B ) t A e A m c S i ( B ) t A c i ( B ) t A e A p m d

(b) Convert your grammar to an unambiguous grammar that generates the same if statements. You should briefly describe your idea. Answer: We change the grammar such that else part, which is e, is always generated together with the nearest if part ( i in the grammar) located to the left. (Notice that S has recursion at the right end of its production.) S i ( B ) t S c i ( B ) t A e A p m d S  i(B)tS | i(B)tAeS | i(B)tA | i(B)tAeA A  p | m B  c | d String i(c)ti(d)tpem can only be generated as shown to the right.

5. Using the pumping lemma we want to prove that language L = {xxR | x {a, b, c}* } is not regular as follows. Complete the proof by answering the questions. Proof. Suppose that L is regular. Then L should satisfy the pumping lemma. Let n be the constant of the lemma. Clearly, all the following strings belong to L. (a) abccba (b) a100b100b100a100 (c ) an/2bn/2b n/2an/2 (d) anbnbnan Question 1: Which string will you choose for z of the lemma? Why the other strings cannot be a candidate for z? If none of the above strings can be a candidate for z, what is your choice from L? Answer: Strings in (a) and (b) cannot be a candidate for z because their length is fixed. For n greater than the length of these strings, the lemma is not applicable. Strings in (c ) and (d) are fine, because their length is not less than n. I will choose the string in (d), since there will be smaller number of cases to be analyzed than the string in (c ). Question 2: Let u, v, w {a, b, c}* be the strings of the lemma such that z = uvw. What will be in v? Shortly describe all possible contents of string v. Answer: Since |uv|  n, and |v|  1, string v contains some a’s in the prefix an of the string such that 1  |v|  n. Question 3: The pumping lemma says “For all i  0, string uviw  L.” How do you deal with this statement for your proof? Justify what you do. Answer: Since this statement uses universal quantifier for i, it is enough to show one value of i for which the statement is wrong. Thus, we choose i = 2. Clearly, uv2w L, since uv2w  xxR for any x {a, b, c}*

6. Which of the following language are regular. Prove your answer 6. Which of the following language are regular. Prove your answer. Your answer will be graded according to the logical completeness. L1 = {aibjak | i, j, k > 0 } L2 = {aibjai | i, j > 0 } Answer: Since aa*bb*aa* is a regular expression which denotes L1, it is regular. (Also you can show that L1 is regular by showing a regular grammar or FA whose language is L1. Language L2 is not regular. We use the pumping lemma for the proof. Suppose that L2 is regular. Then it should satisfy the pumping lemma for regular languages. Let n be the constant of the lemma, and let z = anban, which is in the language L2. Since | z |  n, according to the pumping lemma there exist u, v, w such that (i) z = uvw, (ii) |uv|  n, (iii) |v|  1, and (iv) for all i  0, z’ L2 By conditions (i) – (iii) above, v contains only a’s which belong to the prefix an of z, and 1  |v|  n. It follows that uv2w L2 . Thus language L2 does not satisfy the pumping lemma. We are in contradiction. L2 is not regular.

7. For a string x, let #a(x), #b(x), and #c(x) denote, respectively, the number of a’s, number of b’s and number of c’s in string x. Which of the following languages are context-free? Prove your answer. Your answer will be graded according to the logical completeness. L3 = { x | x {a,b,c}+, and #a(x) > #c(x) } L4 = { x | x {a,b,c}+, and #a(x) = #b(x) = #c(x) } L5 = {x | x {a,b,c}+, and #a(x) > #b(x) > #c(x) } Answer: L3 is context-free language because we can construct a PDA which recognizes this language as follows, where X  {a, c, A, Z0}. (b, Z0/Z0) (a, Z0/AZ0) (a, A/aA), (a, a/aa), (c, a/ ), (b, X/X) (c, A/) (c, Z0/cZ0), (c, c/cc), (a, c/ ), (b, X/X) start (c, Z0/cZ0)

We prove that language L4 = { x | x {a,b,c}+, and #a(x) = #b(x) = #c(x) } is not context-free. Suppose it is. Then the language should satisfy the pumping lemma for context-free languages. Let p be the constant of the lemma. Choose z = apbpcp. Then we can apply the same argument that we used for proving that {aibici | i  0 }is not context-free, and show that z’  L4. This language does not satisfy the pumping lemma. We are in contradiction. It is not context-free. We prove that language L5 = {x | x {a,b,c}+, and #a(x) > #b(x) > #c(x) } is not context-free using the pumping lemma for context-free languages. Suppose it is, and let z = ap+2bp+1cp. Since |vwx|  p and |vx|  1, v and x together contain at most two different symbols among a, b and c (see the illustration below). We have following two cases. When v and x contain a’s or b’s: clearly z’ = uv0wx0y  L5 When v and x contains b’s or c’s: obviously z’ = uv2wx2y  L5 L5 does not satisfy the pumping lemma. We are in contradiction. The language is not context-free. z = aaa . . . . . . .abb . . . . . .bcc . . . . . .c P+2 p+1 p u v w x y p