1 CSCI 3130: Formal Languages and Automata Theory Tutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering.

Slides:



Advertisements
Similar presentations
CSCI 3130: Formal Languages and Automata Theory Tutorial 5
Advertisements

Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
CSCI 3130: Formal languages and automata theory Tutorial 5
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
CSCI 2670 Introduction to Theory of Computing September 15, 2004.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
1 CSC 3130: Automata theory and formal languages Tutorial 4 KN Hung Office: SHB 1026 Department of Computer Science & Engineering.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
CS 310 – Fall 2006 Pacific University CS310 Decidability Section 4.1/4.2 November 10, 2006.
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
CS 490: Automata and Language Theory Daniel Firpo Spring 2003.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
CS5371 Theory of Computation Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong DFA minimization.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Tutorial CSC3130 : Formal Languages and Automata Theory Haifeng Wan ( )
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.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Ambiguity.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
CS 3240: Languages and Computation Context-Free Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
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.
Review for final pm. 2 Review for Midterm Induction – On integer: HW1, Ex 2.2.9b p54 – On length of string: Ex p53, HW2, HW3.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CS 203: Introduction to Formal Languages and Automata
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
Homework 4 due 11/25.
CSCI 3130: Formal languages and automata theory Tutorial 3 Chin.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong DFA minimization.
CSCI 2670 Introduction to Theory of Computing October 13, 2005.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Normal forms.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
CSCI 3130: Formal languages and automata theory Andrej Bogdanov The Chinese University of Hong Kong Decidable.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Formal Language & Automata Theory
CSE 105 theory of computation
CSCI 2670 Introduction to Theory of Computing
Context-Free Languages
Context-Free Grammars
Context-Free Languages
فصل دوم Context-Free Languages
Theory of Computation Lecture #
CSE 105 theory of computation
DFA minimization The Chinese University of Hong Kong Fall 2011
CSE 105 theory of computation
Presentation transcript:

1 CSCI 3130: Formal Languages and Automata Theory Tutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering

2 Agenda DFA Minimization –Algorithm –2 Examples Context Free Grammar (CFG) –Design –Parse Tree

3 DFA Minimization

4 We will show how to turn any DFA for L into the minimal DFA for L 0 1 … … … … qq qq qq q  q  q  q  q  q  q  q  qq qq qq qq

5 DFA Minimization After we design a DFA, there may be some redundant states –They have the same “function” –They are indistinguishable –No matter what happens in the future (inputs), they will have the same fate (accept/reject)! We can merge indistinguishable states into groups

6 Indistinguishable States DFA example from lecture note Indistinguishable states example: {q 0, q 00 } Explain: next few slides 0 1 qq qq qq q  q  q  q 

7 Indistinguishable States On continuation string (further input) “0” q 0  Reject q 00  Reject 0 1 qq qq qq q  q  q  q 

8 Indistinguishable States On continuation string “11” q 0  Accept q 00  Accept 0 1 qq qq qq q  q  q  q 

9 Indistinguishable States On continuation string “01011” q 0  Accept q 00  Accept 0 1 qq qq qq q  q  q  q 

10 Indistinguishable States On all continuation strings, {q 0, q 00 } have the same outcome q 0, q 00 are indistinguishable  Can be merged 0 1 qq qq qq q  q  q  q 

11 How to find? Infinitely many continuation states –A bit difficult to find indistinguishable states directly We do the reverse: –To recognize distinguishable states –Pair of states, s.t. there is a continuation string, causing them have different “fates” –(De Morgan’s Law, still remember?)

12 Distinguishable States For continuation string = “1” q 01  Accept q 10  Reject Distinguishable 0 1 qq qq qq q  q  q  q 

13 Distinguishable States For continuation string = “  ” q 10  Reject q 11  Accept Distinguishable 0 1 qq qq qq q  q  q  q 

14 Distinguishable States 2 states are distinguishable, if there is a continuation string, that bring them different fate!! (Accept / Reject) –Try all continuation strings? How? Idea: –For any pair of {Accept, Reject} states, they are distinguishable –For these pairs, walk k transitions backward, mark the obtained pairs as distinguishable

15 Repeat !! as long as you mark some pairs Finding (in)distinguishable states Rule 1: Rule 2: q q’ If q is accepting and q’ is rejecting Mark (q, q’) as distinguishable (x) x q1q1 q1’q1’ x x q2q2 q2’q2’ aa If (q 1, q 1 ’) are marked, Mark (q 2, q 2 ’) as distinguishable (x) Rule 3: Unmarked pairs are indistinguishable Merge them into groups

16 Example qq qq qq q  q  q  q  xxxxxx qq qq q  q  q  q  qq qq qq q  q   q 11 is distinguishable from all other states

17 Example qq qq qq q  q  q  q  xxxxxx qq qq q  q  q  q  qq qq qq q  q   Look at pair q , q Neither (q 0, q 00 ) nor (q 1, q 01 ) are distinguishable

18 Example qq qq qq q  q  q  q  xxxxxx qq qq q  q  q  q  qq qq qq q  q   Look at pair q , q (q 1, q 11 ) is distinguishable x

19 Example 1 x x x x x x xxxx qq qq q  q  q  q  qq qq qq q  q   After going thru the whole table once Now we make another pass xxx x 0 1 qq qq qq q  q  q  q 

20 Example qq qq qq q  q  q  q  x x x x x x xxxx qq qq q  q  q  q  qq qq qq q  q   In the second pass, nothing changes So we are ready to apply Rule 3 xxx x

21 Example 1 qq qq qq q  q  q  q  x x x x x x x x x x x x xx qq qq q  q  q  q  qq qq qq q  q   Merge unmarked pairs into groups Unmarked Cells  Pairs of Indistinguishable states Blue line links indistinguishable states

22 A B C Example 1 qq qq qq q  q  q  q  x x x x x x x x x x x x xx qq qq q  q  q  q  qq qq qq q  q   Merge unmarked pairs into groups A A A A A B A

23 Example qq qq qq q  q  q  q  A x A x A x x A x A x x B x x x A x x xx qq qq q  q  q  q  qq qq qq q  q  1 qq qq qCqC minimized DFA: A B C

24 Example qq qq qq q  q  q  q  qq qq q  q  q  q  qq qq qq q  q  xxxxx  q 10 is distinguishable from all other states

25 Example qq qq qq q  q  q  q  qq qq q  q  q  q  qq qq qq q  q  xxxxx xxxx xxx x xx  After going thru the whole table several times (See white board for simulation)

26 Example qq qq qq q  q  q  q  qq qq q  q  q  q  qq qq qq q  q  xxxxx xxxx xxx x xx  Merge unmarked pairs into groups

27 Why it works? For a pair of states, there is a continuation string that bring them different fates –Distinguishable Idea: For any pair of {Accept, Reject} states, they are distinguishable, mark them –These pairs have a length-0 continuation string that distinguishes them 1 step backward, mark them as distinguishable –These pairs have a length-1 continuation string that distinguishes them

28 Why it works? 1 further step backward, mark –These pairs have a length-2 continuation string that distinguishes them 1 further step backward, mark –These pairs have a length-3 continuation string that distinguishes them So-on …

29 Context Free Grammar

30 3) Production Rule Context-Free Grammar (Recap) A context free grammar is consisted of 4) Start Variable Another Production Rule 1) Variables 2) Terminal Another Terminal S  AB | ba A  aA | a B  b

31 Context-Free Grammar (Recap) A string is said to belong to the language (of the CFG) if it can be derived from the start variable Derivation Therefore, aab belongs to the language S  AB | ba A  aA | a B  b CFG Example = Apply Production Rule  AB  aAB  aaB  aab S

32 L = {w = 0 n 1 n : n is an positive integer} L is not a regular language –Proved by “Pumping Lemma” A Context-Free Grammar can describe it Thus, CFG is more general than regular expression –Recall: NFA  Regular Expression  DFA Why CFG? S  0S1 S  01

33 CFG Design Given a context-free language, design the CFG L = { ab-string, w : Number of a’s < Number of b’s } Some time for you to get into think… 1 min S  ? …

34 CFG Design (Con’t) Idea: Bottom-up –Shortest string in L : “b” –Given a string in L, we can expand it, s.t. it is still in L –i.e., Add terminals, while not violating the constraints

35 CFG Design (Con’t) Good Try: S  b S  bS | Sb S  abS | baS | bSa | aSb After adding 1 “b”, number of “b” is still greater than that of “a” Adding 1 “a” and 1 “b”, the difference between the numbers of “a” and “b” keep constant However, cannot parse strings like “aabbbaa” Explaination makes sense. But there is a counter example.

36 CFG Design (Con’t) Consider the answer: S  b S  SS S  SaS | aSS | SSa Base Case #b still > #a : #b ≥ #a + 1 : #a = 1 1 st S 2 nd S ‘a’ Explaination makes sense. But, is the grammar is correct? SUM  #b ≥ #a

37 A set of strings CFG Design (Con’t) After designing the grammar, G, you may have to prove (if required) that the language of this grammar is equivalent to the given language i.e., Prove that L(G) = L Proof: Part 1) L(G) ⊂ L Part 2) L ⊂ L(G) Due to time limit, I will not do this part

38 Parse Tree How to parse “aab” in this grammar? (Previous example) S  AB | ba A  aA | a B  b CFG Example S  AB  aAB  aaB  aab Derivation

39  AB  aAB  aaB  aab Parse Tree (Con’t) Idea: Production Rule = Node + Children Should be very intuitive to understand S Derivation S B b a A a A

40 Different parse trees! The grammar is ambiguous! Parse Tree (Con’t) Ambiguity: S S 2 1 A S S  S - S  1 | 2 | 3 SSSS String: CFG: 3 – 1 – 2 S S 3 2 S S S – (1 – 2)

41 Parse Tree (Con’t) Useful in programming language –CSC3180 Useful in compiler –CSC3120

42 End Thanks for coming! Any questions?