PS3 - Comments. Describe precisely the language accepted by this nondeterministic PDA.

Similar documents
Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

CSE 105 THEORY OF COMPUTATION

CT32 COMPUTER NETWORKS DEC 2015

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Multiple Choice Questions

1. [5 points each] True or False. If the question is currently open, write O or Open.

Limits of Computation p.1/?? Limits of Computation p.2/??

Specifying Syntax COMP360

COMP 330 Autumn 2018 McGill University

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

LL(1) predictive parsing

CSE 105 THEORY OF COMPUTATION

JNTUWORLD. Code No: R

Pushdown Automata. A PDA is an FA together with a stack.

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

Decision Properties for Context-free Languages

CS 44 Exam #2 February 14, 2001

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

QUESTION BANK. Unit 1. Introduction to Finite Automata

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

Ambiguous Grammars and Compactification

Lecture 4: Syntax Specification

Yet More CFLs; Turing Machines. CS154 Chris Pollett Mar 8, 2006.

CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Theory of Computations Spring 2016 Practice Final Exam Solutions

Models of Computation II: Grammars and Pushdown Automata

Definition 2.8: A CFG is in Chomsky normal form if every rule. only appear on the left-hand side, we allow the rule S ǫ.

T.E. (Computer Engineering) (Semester I) Examination, 2013 THEORY OF COMPUTATION (2008 Course)

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

CS525 Winter 2012 \ Class Assignment #2 Preparation

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

CS6160 Theory of Computation Problem Set 2 Department of Computer Science, University of Virginia

CSE 105 THEORY OF COMPUTATION

Chapter 3: Lexing and Parsing

Chapter 14: Pushdown Automata

From Theorem 8.5, page 223, we have that the intersection of a context-free language with a regular language is context-free. Therefore, the language

Theory of Computation


UNIT I PART A PART B

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Context-Free Grammars

Menu. Problem 5: PRIMES. Problem 5: PRIMES. Please read the PS2 Comments thoroughly! cs3102: Theory of Computation

Decidable Problems. We examine the problems for which there is an algorithm.

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Midterm I (Solutions) CS164, Spring 2002

CMSC 330, Fall 2009, Practice Problem 3 Solutions

1 Parsing (25 pts, 5 each)

Context Free Languages and Pushdown Automata

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

CS402 - Theory of Automata Glossary By

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

Turing Machine Languages

Final Course Review. Reading: Chapters 1-9

LL(1) predictive parsing

Context-Free Grammars

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Context-Free Grammars

PDA s. and Formal Languages. Automata Theory CS 573. Outline of equivalence of PDA s and CFG s. (see Theorem 5.3)

Theory of Computations Spring 2016 Practice Final

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

CS/B.Tech/CSE/IT/EVEN/SEM-4/CS-402/ ItIauIafIaAblll~AladUnrtel1ity

Computer Sciences Department

CIT3130: Theory of Computation. Regular languages

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Languages and Automata

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Final Exam 1, CS154. April 21, 2010

CS3102 Theory of Computation Problem Set 2, Spring 2011 Department of Computer Science, University of Virginia

CS311 / MATH352 - AUTOMATA AND COMPLEXITY THEORY

Languages and Compilers

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Theory of Programming Languages COMP360

Course Project 2 Regular Expressions

Theory Bridge Exam Example Questions Version of June 6, 2008

DHANALAKSHMI SRINIVASAN INSTITUTE OF RESEARCH AND TECHNOLOGY SIRUVACHUR, PERAMBALUR DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Regular Languages (14 points) Solution: Problem 1 (6 points) Minimize the following automaton M. Show that the resulting DFA is minimal.

THEORY OF COMPUTATION

Final Exam 2, CS154. April 25, 2010

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Formal Grammars and Abstract Machines. Sahar Al Seesi

Answer All Questions. All Questions Carry Equal Marks. Time: 20 Min. Marks: 10.

Qualifying Exam Theory

Non-deterministic Finite Automata (NFA)

CS402 - Theory of Automata FAQs By

III. Supplementary Materials

The Big Picture. Chapter 3

Lecture 7: Deterministic Bottom-Up Parsing

Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

Syntax Analysis Part I

CS103 Handout 16 Winter February 15, 2013 Problem Set 6

COP 3402 Systems Software Syntax Analysis (Parser)

An Interactive Approach to Formal Languages and Automata with JFLAP

Transcription:

University of Virginia - cs3102: Theory of Computation Spring 2010 PS3 - Comments Average: 46.6 (full credit for each question is 55 points) Problem 1: Mystery Language. (Average 8.5 / 10) In Class 7, we considered how the language would change if states q 1 and q 2 from the NPDA on slide 7 were merged, producing the NPDA not shown here but included in the PS3 PDF handout. Describe precisely the language accepted by this nondeterministic PDA. Answer: There are several ways to precisely describe a language. A good description makes it clear exactly what strings are in the language. One way to describe this language is by providing a context-free grammar that produces the language: S SS asa bsb ɛ Another way would be do describe it in English, but this is hard to do precisely: The language of all strings consisting of as and bs where the as and bs are balanced. We can consider each a to be either a ( or a ), and each b to be either a [ or a ]. If there is some way to assing the parentheses to the as and brackets to the bs that produces a well-balanced string (that is, follows the rules typical in programming languages for matching parentheses and brackets) the string is in the language. Challenge Bonus. Is there a deterministic PDA that recognizes the same language? Include a convincing proof that supports your answer. Answer: The language is inherently ambiguous, so there is no deterministic PDA that can recognize it. Note that the language is the Kleene closure of the language PS3C-1

of palindromes, but the language ww R cannot be recognized by a deterministic PDA. Problem 2: NPDAs and CFGs. (Average 8.5 / 10) Describe a NPDA that recognizes the language produced by this context-free grammar: S 1A A1 0B B0 ɛ A 0S1 1S0 CS SC B S11 1S1 11S C 01 10 Answer: Although it is possible to follow the mechanical construction used in the proof that NPDAs and CFGs are equivalent, this would be very painful and tedious, and exceedingly unlikely to do correctly by hand. A better approach is to try to understand the language produced by the CFG. It is the language of all strings with twice as many 1s as 0s. We can see this by examining the grammar rules. The variable S produces either the empty string, or 1A or A1, or 0B or B0. The A variable produces all arrangements of S with one 0 and one 1. So, 1A and A1 produce all arrangments where there is a 1 on one of the outsides and there are two 1s and one 0 with an S in any arrangment. For the 0B and B0 productions, B produces all arrangements of S and two 1s. For S B0 0B, produces all arrangements with two 1s and one 0 and S where there is a 0 on one of the outsides. Combined with the A productions, this produces all strings with twice as many 1s as 0s. Now we understand the language, producing an NPDA that recognizes is is similar to the NPDA we produces in class for recognizing the language with as many 1s as 0s. The difference is we need to push/pop two stack symbols for each 0, and only pop/push one for each 1. The tricky part is dealing with the case where we see a 0 but there is only one stack symbol to pop. We deal with this by adding an extra state that keeps track of the deficeit of one 1. PS3C-2

Problem 3: Context-Free Grammars. Provide a context-free grammar that recognizes the language: {w w {a, b} w contains more as than bs.} Use as few nonterminals as possible. Answer: See the answer to question 2.6a in the book (p. 132). Problem 4: Priming the Pump Redux. Prove the language, PRIMES, is not context-free: PRIMES = {1 n n is a prime number} Answer: Our answer is very similar to the question on Problem Set 2. Assume PRI MES is context-free with some pumping length p. We use the pumping lemma for context-free languages to obtain a contradiction. Choose s = 1 q where q is some prime number greater than p. (Note that we cannot choose s = 1 p since p might be composite. But, since there is no maximum prime, we know that for any p such a q must exist.) The pumping lemma for context-free languages requires that s can be broken into uvxyz with vy 1 and vxy p. Note that since the string is all 1s, however the pieces are divided, we can rearrange them without changing the string: PS3C-3

uvxyz = uxvyz = rtz where r = ux, and t = vy. Note that the two parts that can be pumped, vy are now in t, so pumping v i y i is the same as pumping t i. Thus, we have taken whatever 5-part division is selected, and converted it to the same 3-part division as needed in the regular languages pumping lemma. In the last problem set, we saw how to get a contridiction by setting i = q + 1. We have a contradiction, so PRIMES is not context-free. Problem 5: Closure Properties. a. Prove that the context-free languages are closed under concatenation. Answer: Given two context-free language, A and B, we can construct a contextfree grammar G AB that is the concatenation of the languages. Since A and B are context-free, there exist context-free grammars G A = (V A, Σ, R A, S A ) and G B = (V B, Σ, R B, S B ) that produce each language. First, rename the variables in G B so they are disjoint from the variables in G A. Then, construct G AB = (V A V B {S AB }, Σ, R A R B S AB S A S B, S AB ) We have added the rules S AB S A S B to the language. S A is the start variable for G A, so produces all strings in A; S B is the start variable for G B, so produces all strings in B. Thus, S AB produces all strings in the concatenation language AB. b. Prove that the intersection of a context-free language with a regular language is always a context-free language. Answer: We use proof by construction. Since the context-free language, A, is context-free there is some nondeterministic pushdown automaton, M A = (Q A, Σ, Γ, δ A, q 0A, F A ) that recognizes A. Since B is a regular language, there exists some DFA, M B = (Q B, Σ, δ B, q 0B, F B ) that recognizes B. We show how to construct a nondeterministic pushdown automata, M A B, that recognizes the language A B. The key point to observe is that M A B should accept a string if and only if both M A and M B would accept the string. So, the states of M A B need to keep track of both the state of M A and the state of M B. Thus, we label the states using pairs from Q A and Q B : M A B = (Q A Q B, Σ, Γ, δ AB, q 0A q 0B, F A F B ) The new δ AB : Q A Q B Σ ɛ Γ ɛ Q A Q B Γ ɛ is defined by: where (q an, γ an ) = δ A (q a, σ, γ). δ AB (q a, q b, σ, γ) = (q a n, δ B (q b, σ), γ an ) c. Prove that the context-free languages are not closed under intersection. PS3C-4

Answer: We prove by finding a counter-example. We show that there is some non-context-free language that is the intersection of two context-free languages. The non-context-free language is {a n b n c n n 0}. We proved in class that this language is not context-free. This language is the intersection of two context-free languages: {a n b n c n 0} and {a b m c m m 0}. (We use different variables for n and m to emphasize that there is no connection between the values in the two languages.) Problem 6: Parsing. Below is a slightly simplified excerpt from the actual Java grammar specification (from http://java.sun.com/docs/books/jls/third edition/html/syntax.html#18.1, Chapter 18). I have changed the syntax to match the context-free grammar notation used in Sipser and the class. Expression Expression1 OptAssignmentOperator OptAssignmentOperator ɛ AssignmentOperator Expression1 Expression1 Expression2 OptExpression1Rest OptExpression1Rest ɛ Expression1Rest Expression1Rest? Expression : Expression1 AssignmentOperator = Expression2 Expression3 OptExpression2Rest OptExpression2Rest ɛ Expression2Rest Expression2Rest InfixExpressionList InfixExpressionList ɛ InfixExpression InfixExpressionList InfixExpression InfixOp Expression3 InfixOp && == + Expression3 Primary SelectorList Primary ( Expression ) Identifier Literal SelectorList ɛ Selector SelectorList Selector [ Expression ]. Identifier The terminal Identifier is any valid Java identifier (see Section 3.8 of the Java Language Specification for the grammar for Identifiers) and Literal is any literal. a. Consider the following Java expression: true? false? true == true : false : false == false which evaluates to false. The Boolean values true, and false are Literals. The conditional expression, Expression pred? Expression consequent : Expression alternate, is evaluated by first evaluating Expression pred, which must evaluate to a boolean. If it evaluates to true, then the value of the conditional expression is the value obtained by evaluating Expression consequent (and Expression alternate is not evaluated). If it evaluates to false, then the value of the conditional expression is the value obtained by evaluating Expression alternate (and Expression consequent is not evaluated). PS3C-5

By adding only parentheses, transform it into a grammatical Java expression that evaluates to true. Answer: (true? false? true == true : false : false) == false The expression in parentheses evaluates to false+, so the whole expression evaluates to true+. b. Explain how to change the grammar rules so the original expression in the previous part evaluates to true. Your new grammar should produce exactly the same language as the original grammar. It is acceptable if your answer leads to an ambiguous grammar, as long as one possible parse of the expression in your grammar evaluates to true. (Challenge Bonus. Produce an unambiguous grammar for this question. It should produce exactly the same language as the original grammar, but the only possible parse of the expression evaluates to true.) Answer: This is a tricky one. Essentially, we want to change the grammar so the == gets bound after the?. To do this, we need to swap Expression3 and Expression1 in the grammar. To keep things simple, we do this by adding a production for Primary that produces the ConditionalExpression. We change the OptAssignmentOperator rule to be: OptAssignmentOperator ɛ AssignmentOperator Expression2 eliminating Expression1. Then, add a new rule for Primary: Primary Expression? Expression : Expression Note that this makes the grammar ambiguous, but one of the possible parses of the expression now corresponds to the parenthesize expression from the previous part (since we parenthesized Expression was a Primary, which can now be a conditional expression (without needing the parentheses. Finding a way to do this without making the grammar ambiguous is more challenging. PS3C-6