Tutorial 05 -- CSC3130 : Formal Languages and Automata Theory Tu Shikui ( ) SHB 905, Office hour: Thursday 2:30pm-3:30pm 2008-10-06.

Slides:



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

Simplifying CFGs There are several ways in which context-free grammars can be simplified. One natural way is to eliminate useless symbols those that cannot.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
The Cocke-Younger-Kasami Algorithm*
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.
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.
Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu Department of Computer Science & Engineering.
CS5371 Theory of Computation
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Tutorial CSC3130 : Formal Languages and Automata Theory Tu Shikui ( ) SHB 905, Office hour: Thursday 2:30pm-3:30pm
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 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Tutorial CSC3130 : Formal Languages and Automata Theory Tu Shikui ( ) SHB 905, Office hour: Thursday 2:30pm-3:30pm
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
1 CSCI 3130: Formal Languages and Automata Theory Tutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
1 Module 32 Chomsky Normal Form (CNF) –4 step process.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Finite State Machines Data Structures and Algorithms for Information Processing 1.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Chapter 7 PDA and CFLs.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
Tutorial CSC3130 : Formal Languages and Automata Theory Haifeng Wan ( )
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 20, 2005.
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.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Pushdown Automata (PDAs)
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Ambiguity.
Lecture Pushdown Automata. stack stack head finite control tape head tape.
CS 44 – Jan. 29 Expression grammars –Associativity √ –Precedence CFG for entire language (handout) CYK algorithm –General technique for testing for acceptance.
CSCI 3130: Formal languages and automata theory Tutorial 4 Chin.
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.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
January 20, 2016CS21 Lecture 71 CS21 Decidability and Tractability Lecture 7 January 20, 2016.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Normal forms.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
1 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations pop, push, and nop. PDAs always.
Theory of Computation Automata Theory Dr. Ayman Srour.
Theory of Languages and Automata By: Mojtaba Khezrian.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
Lecture 16 Cocke-Younger-Kasimi Parsing Topics: Closure Properties of Context Free Languages Cocke-Younger-Kasimi Parsing Algorithm June 23, 2015 CSCE.
CSE 105 theory of computation
Ambiguity Parsing algorithms
Complexity and Computability Theory I
LR(1) grammars The Chinese University of Hong Kong Fall 2010
فصل دوم Context-Free Languages
CSE 105 theory of computation
ReCap Chomsky Normal Form, Theorem regarding CNF, examples of converting CFG to be in CNF, Example of an FA corresponding to Regular CFG, Left most and.
Normal forms and parsing
CSE 105 theory of computation
Normal Forms for Context-free Grammars
CSE 105 theory of computation
Presentation transcript:

Tutorial CSC3130 : Formal Languages and Automata Theory Tu Shikui ( ) SHB 905, Office hour: Thursday 2:30pm-3:30pm

Outline Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) Grammar Transformations Remove ε- production; Remove unit-production; Covert to Chomsky-Normal-Form (CNF) Cocke-Younger-Kasami (CYK) algorithm

Relations Context-free Languages L Context-free Grammars G Push-down Automata M L = L(G) L = L(M) L(G) = L(M)

Example (I) Given the following CFG S  X | Y X  aXb | aX | a Y  aYb | Yb | b (1) L(G) = ? (2) Design an equivalent PDA for it. Σ={a, b}

Example (I) --- solution: L(S) S  X | Y X  aXb | aX | a Y  aYb | Yb | b Try to write some strings generated by it: S  X  aXb  aaXbb  aaaXbb  aaaabb S  Y  aYb  aYbb  aaYbbb  aabbbb more a’s than b’s more b’s than a’s Observations: Start from S, we can enter two States X & Y, and X, Y are “independent”; In X state, always more a are generated; In Y state, always more b are generated. Ls = Lx U Ly Lx = { a i b j ; i>j } Lx = { a i b j ; i<j } L(S) = { a i b j ; i≠j }

Example (I) --- solution: PDA S  X | Y X  aXb | aX | a Y  aYb | Yb | b L(S) = { a i b j ; i≠j } PDA = NFA + a stack (infinite memory) = { a i b j ; i>j } U { a i b j ; i<j } A possible way: “divide and conquer” L x = { a i b j ; i>j } a,  /A q0q0 ,  /# ,/,/ q1q1 b,A/  b,#/# q2q2 q3q3 ,#/  L Y = { a i b j ; i<j } q’ 0 ,  /# ,/,/ q’ 1 ,A/  q’ 2 ,A/  q’ 3 ,#/  a,  /Ab,A/  ,  /  Combine both …

Example (II) Given the following language: (1) design a CFG for it; (2) design a PDA for it. L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…},  = {0, 1}

Example (II) -- solution: CFG L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…},  = {0, 1} Consider two extreme cases: (a). if j = i, then L 1 = { 0 i 1 j : i=j }; (b). if j = 2i, then L 2 = { 0 i 1 j : 2i=j }. S  0S1 S  ε S  0S11 S  ε If i ≤ j ≤ 2i, then randomly choose “red- rule” or “blue-rule” in the generation. “red-rule” “blue-rule” S  0S1 S  0S11 S  ε

Example (II) -- solution: CFG L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…},  = {0, 1} S  0S1 S  0S11 S  ε Need to verify L = L(G) G = 1). L(G) is a subset of L: The “red-rule” and “blue-rule” guarantee that in each derivation, the number of 1s generated is one or two times larger than that of 0s. So, L(G) is a subset of L. 2). L is a subset of L(G): For any w = 0 i 1 j, i ≤ j ≤ 2i, we use “red-rule” ( 2i - j ) times and then “blue-rule” ( j - i ) times, i.e., S =*=> 0 2i-j S1 2i-j =*=> 0 2i-j 0 j-i S1 2(j-i) 1 2i-j ==> 0 i 1 j = w

Example (II) -- solution: PDA L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…},  = {0, 1} Similar idea: “randomly choose two extreme cases” q0q0 ,  /# 0,  /X ,/,/ q1q1 q2q2 ,#/  1,X/  1,X/X 1,X/  q3q3

Example (III) Given the following language: (1). Design a CFG for it; (2). Design a PDA for it. L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, where the alphabet Σ= {a, b, c, d}

Example (III) – solution: CFG L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, Note that i + k = j + l ==> | i – j | = | l – k |. Assume n = i – j = l – k > 0, then i = n + j, l = n + k, and w = a i b j c k d l = a n a j b j c k d k d n w anan dndn ajbjajbj ckdkckdk ajaj bjbj ckck dkdk Three blocks come from the same template: N  x N x S  aSd | XY X  aXb | ε Y  cYd | ε S=*=>a n Sd n =*=> a n XYd n =*=>a n a j b j Yd n =*=>a n a j b j c k b k d n = a n+j b j c k b n+k (n+j) + k = j + (n+k)

Example (III) – solution: PDA L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, Main idea: (1)use X to record an a or c; use Y to record an b or d. (2)Compare #X and #Y: by cancellation. How to realize the comparison by cancellation? Action1 : Push an X, when a or c was read; Action2 : Pop an X (if any, otherwise push a Y), when b or d was read. Action3 : Pop an Y (if any, otherwise push an X), when a or c was read.

Example (III) – solution: PDA L = { a i b j c k d l : i,j,k,l=0,1,…; i+k=j+l }, Action1 : Push an X, when a or c was read; Action2 : Pop an X (if any, otherwise push a Y), when b or d was read. Action3 : Pop an Y (if any, otherwise push an X), when a or c was read. ,  /# q5q5 q1q1 a,  /X ,/,/ b,#/Y# q2q2 ,/,/ c,X/XX q3q3 ,/,/ q4q4 ,/,/ b,X/  b,Y/YY c,#/X# c,Y/  d,X/  d,#/Y# d,Y/YY

Outline Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) Grammar Transformations Remove ε- production; Remove unit-production; Covert to Chomsky-Normal-Form (CNF) Cocke-Younger-Kasami (CYK) algorithm

Remove ε-production Example: Remove ε-productions of the following grammar G: S  ABaC A  BC B  b | ε C  D | ε D  d

Remove ε-production S  ABaC | BaC | AaC | aC | ABa | Ba | Aa | a A  BC | C | B B  b | ε C  D | ε D  d Nullable variables: A, B, C AddDelete For A:S  BaC For B:S  AaC | aC A  C B  ε For C:S  ABa | Ba | Aa | a A  B C  ε For i = 1 to k For every production of the form A →  N i , add another production A →  If N i →  is a production, remove it

Remove unit-productions Example: Remove the unit-productions of the following grammar: S  S + T | T T  T * F | F F  (S) | a

Remove unit-productions Add Delete S  T For T  T*F S  T*F For T  F  a S  a S  S+T | T | T*F | a | (S) T  T*F | F | a | (S) F  (S) | a For T  F  (S) S  (S) T  F For F  a S  a For F  (S) S  (S) A 1 → A 2 →... → A k → A 1 A 1 → A 2 →... → A k →  A 1 → ,..., A k →  delete it and replace everything with A 1 Rule 1: Rule 2:

Convert CFG to Chomsky-Normal- Form (CNF) Example: Convert the following CFG to CNF: S  0AB A  0D | 1AD B  0 D  1

Convert CFG to Chomsky-Normal- Form (CNF) A → BcDE replace terminals with new variables A → BCDE C → c break up sequences with new variables A → BX 1 X 1 → CX 2 X 2 → DE C → c S  0AB A  0D | 1AD B  0 D  1 S  XAB; X  0 A  XD | YAD; Y  1

Convert CFG to Chomsky-Normal- Form (CNF) A → BcDE replace terminals with new variables A → BCDE C → c break up sequences with new variables A → BX 1 X 1 → CX 2 X 2 → DE C → c S  XAB X  0 A  XD |YAD Y  1 B  0 D  1 S  XN 1 ; N 1  AB A  YN 2 ; N 2  AD

Convert CFG to Chomsky-Normal- Form (CNF) A → BcDE replace terminals with new variables A → BCDE C → c break up sequences with new variables A → BX 1 X 1 → CX 2 X 2 → DE C → c S  XN 1 N 1  AB A  XD A  YN 2 N 2  AD X  0 Y  1 B  0 D  1

Outline Context-free Languages, Context-free grammars (CFG), Push-down Automata (PDA) Grammar Transformations Remove ε- production; Remove unit-production; Covert to Chomsky-Normal-Form (CNF) Cocke-Younger-Kasami (CYK) algorithm

Constructing the parse table Input : w = a 1 a 2 …a n ∈ Σ+ Output : The parse table T for w such that t ij contains A ⇔ A + ⇒ a i a i+1 …a i+j-1 Steps: Step(1): cell ( i,1 ) = { A | A→a i ∈ P, 1≤i≤n } Step(2): for 1 ≤ k < j, cell ( i,j ) = { A | for some k, A→BC ∈ P, B is in cell ( i,k ), C is in cell ( i+k, j-k ) } Step(3): repeat Step(2) for 1≤ i≤ n, 1≤ j ≤ n-i+1

Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24 cell 12, cell 33 cell 13, cell 42 cell 14, cell 51 j=4 cell 11, cell 23 cell 12, cell 32 cell 13, cell 41 cell 21, cell 33 cell 22, cell 42 cell 23, cell 51 j=3 cell 11, cell 22 cell 12, cell 31 cell 21, cell 32 cell 22, cell 41 cell 31, cell 42 cell 32, cell 51 j=2 cell 11, cell 21 cell 21, cell 31 cell 31, cell 41 cell 41, cell 51 j=1 A1  x1A1  x1 A2  x2A2  x2 A3  x3A3  x3 A4  x4A4  x4 A5  x5A5  x5 i=1i=2i=3i=4i=5 x1x1 x2x2 x3x3 x4x4 x5x5 Cell (i,k), Cell (i+k, j-k) k = 1, …, j-1

Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24: S, A cell 12, cell 33: S, A cell 13, cell 42: S, A cell 14, cell 51: S, A j=4 cell 11, cell 23: S, A cell 12, cell 32: S, A cell 13, cell 41: S, A cell 21, cell 33: A cell 22, cell 42: S, A cell 23, cell 51: -- j=3 cell 11, cell 22 : S cell 12, cell 31: A, S cell 21, cell 32: -- cell 22, cell 41: S cell 31, cell 42: S,A cell 32, cell 51: -- j=2 cell 11, cell 21: S, A cell 21, cell 31: A cell 31, cell 41: S cell 41, cell 51: S,A j=1 A 1  x 1 : AA 2  x 2 : SA 3  x 3 : AA 4  x 4 :AA 5  x 5 :S i=1i=2i=3i=4i=5 abaab S → AA | AS | b A → SA | AS | a Test: w = abaab

Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24: S, A cell 12, cell 33: S, A cell 13, cell 42: S, A cell 14, cell 51: S, A j=4 cell 11, cell 23: S, A cell 12, cell 32: S, A cell 13, cell 41: S, A cell 21, cell 33: A cell 22, cell 42: S, A cell 23, cell 51: -- j=3 cell 11, cell 22 : S cell 12, cell 31: A, S cell 21, cell 32: -- cell 22, cell 41: S cell 31, cell 42: S,A cell 32, cell 51: -- j=2 cell 11, cell 21: S, A cell 21, cell 31: A cell 31, cell 41: S cell 41, cell 51: S,A j=1 A 1  x 1 : AA 2  x 2 : SA 3  x 3 : AA 4  x 4 :AA 5  x 5 :S i=1i=2i=3i=4i=5 abaab S → AA | AS | b A → SA | AS | a Test: w = abaab Trace back to find a parse tree

Cocke-Younger-Kasami (CYK) algorithm j=5 cell 11, cell 24: S, A cell 12, cell 33: S, A cell 13, cell 42: S, A cell 14, cell 51: S, A j=4 cell 11, cell 23: S, A cell 12, cell 32: S, A cell 13, cell 41: S, A cell 21, cell 33: A cell 22, cell 42: S, A cell 23, cell 51: -- j=3 cell 11, cell 22 : S cell 12, cell 31: A, S cell 21, cell 32: -- cell 22, cell 41: S cell 31, cell 42: S,A cell 32, cell 51: -- j=2 cell 11, cell 21: S, A cell 21, cell 31: A cell 31, cell 41: S cell 41, cell 51: S,A j=1 A 1  x 1 : AA 2  x 2 : SA 3  x 3 : AA 4  x 4 : AA 5  x 5 :S i=1i=2i=3i=4i=5 abaab S → AA | AS | b A → SA | AS | a Test: w = abaab We can find another parse tree.

End of this tutorial! Thanks for coming!