Automata Theory CS411-2015F-13 Unrestricted Grammars David Galles Department of Computer Science University of San Francisco
13-0: Language Hierarchy Regular Languaes Regular Expressions Finite Automata Context Free Languages Context-Free Grammars Push-Down Automata Recusively Enumerable Languages?? Turing Machines
13-1: CFG Review G = (V,Σ,R,S) V = Set of symbols, both terminals & non-terminals Σ V set of terminals (alphabet for the language being described) R ((V Σ) V ) Set of rules S (V Σ) Start symbol
13-2: Unrestricted Grammars G = (V,Σ,R,S) V = Set of symbols, both terminals & non-terminals Σ V set of terminals (alphabet for the language being described) R (V (V Σ)V V ) Set of rules S (V Σ) Start symbol
13-3: Unrestricted Grammars R (V (V Σ)V V ) Set of rules In an Unrestricted Grammar, the left-hand side of a rule contains a string of terminals and non-terminals (at least one of which must be a non-terminal) Rules are applied just like CFGs: Find a substring that matches the LHS of some rule Replace with the RHS of the rule
13-4: Unrestricted Grammars To generate a string with an Unrestricted Grammar: Start with the initial symbol While the string contains at least one non-terminal: Find a substring that matches the LHS of some rule Replace that substring with the RHS of the rule
13-5: Unrestricted Grammars Example: Grammar for L = {a n b n c n : n > 0} First, generate (ABC) Next, non-deterministically rearrange string Finally, convert to terminals (A a, B b, etc.), ensuring that string was reordered to form a b c
13-6: Unrestricted Grammars Example: Grammar for L = {a n b n c n : n > 0} S ABCS S T C CA AC BA AB CB BC CT C T C c T C T B BT B T B b T B T A AT A T A a T A ǫ
13-7: Unrestricted Grammars S ABCS ABCABCS ABACBCS AABCBCS AABBCCS AABBCCT C AABBCT C c AABBT C cc AABBT B cc AABT B bcc AAT B bbcc AAT A bbcc AT A abbcc T A aabbcc aabbcc
13-8: Unrestricted Grammars S ABCS ABCABCS ABCABCABCS ABACBCABCS AABCBCABCS AABCBACBCS AABCABCBCS AABACBCBCS AAABCBCBCS AAABBCCBCS AAABBCBCCS AAABBBCCCS AAABBBBCCCT C AAABBBCCT C c AAABBBCT C cc AAABBBT C ccc AAABBBT B ccc AAABBT B bccc AAABT B bbccc AAAT B bbbccc AAAT A bbbccc AAT A abbbccc AT A aabbbccc T A aaabbbccc aaabbbccc
13-9: Unrestricted Grammars Example: Grammar for L = {ww : w a,b }
13-10: Unrestricted Grammars Example: Grammar for L = {ww : w a,b } Hints: What if we created a string, and then rearranged it (like (abc) a n b n c n )
13-11: Unrestricted Grammars Example: Grammar for L = {ww : w a,b } Hints: What if we created a string, and then rearranged it (like (abc) a n b n c n ) What about trying ww R
13-12: Unrestricted Grammars L = {ww : w a,b } S S Z S as A S bs B S ǫ AZ XZ AX XA BX XB ax aa bx ba BZ YZ AY YA BY YB ay ab by bb
13-13: Unrestricted Grammars L UG is the set of languages that can be described by an Unrestricted Grammar: L UG = {L : Unrestricted Grammar G,L[G] = L} Claim: L UG = L re To Prove: Prove L UG L re Prove L re L UG
13-14: L UG L re Given any Unrestricted Grammar G, we can create a Turing Machine M that semi-decides L[G]
13-15: L UG L re Given any Unrestricted Grammar G, we can create a Turing Machine M that semi-decides L[G] Two tape machine: One tape stores the input, unchanged Second tape implements the derivation Check to see if the derived string matches the input, if so accept, if not run forever
13-16: L UG L re To implement the derivation on the second tape: Write the initial symbol on the second tape Non-deterministically move the read/write head to somewhere on the tape Non-deterministically decide which rule to apply Scan the current position of the read/write head, to make sure the LHS of the rule is at that location Remove the LHS of the rule from the tape, and splice in the RHS
13-17: L UG L re Input Tape a a b b c c Work Tape S
13-18: L UG L re Input Tape a a b b c c Work Tape S
13-19: L UG L re Input Tape a a b b c c Work Tape ABC S
13-20: L UG L re Input Tape a a b b c c Work Tape ABC S
13-21: L UG L re Input Tape a a b b c c Work Tape ABCABC S
13-22: L UG L re Input Tape a a b b c c Work Tape ABCABC S
13-23: L UG L re Input Tape a a b b c c Work Tape ABACBC S
13-24: L UG L re Input Tape a a b b c c Work Tape ABACBC S
13-25: L UG L re Input Tape a a b b c c Work Tape AABCBC S
13-26: L UG L re Input Tape a a b b c c Work Tape AABCBC S
13-27: L UG L re Input Tape a a b b c c Work Tape AABBCC S
13-28: L UG L re Input Tape a a b b c c Work Tape AABBCC S
13-29: L UG L re Input Tape a a b b c c Work Tape AABBCCT C
13-30: L UG L re Input Tape a a b b c c Work Tape AABBCCT C
13-31: L UG L re Input Tape a a b b c c Work Tape AA BBCT c C
13-32: L UG L re Input Tape a a b b c c Work Tape AA BBCT c C
13-33: L UG L re Input Tape a a b b c c Work Tape AABB T C c c
13-34: L UG L re Input Tape a a b b c c Work Tape AABB T C c c
13-35: L UG L re Input Tape a a b b c c Work Tape AABB T B c c
13-36: L UG L re Input Tape a a b b c c Work Tape AABB T B c c
13-37: L UG L re Input Tape a a b b c c Work Tape AA B T B b c c
13-38: L UG L re Input Tape a a b b c c Work Tape AA B T B b c c
13-39: L UG L re Input Tape a a b b c c Work Tape AA T B b b c c
13-40: L UG L re Input Tape a a b b c c Work Tape AA T B b b c c
13-41: L UG L re Input Tape a a b b c c Work Tape AA T A b b c c
13-42: L UG L re Input Tape a a b b c c Work Tape AA T A b b c c
13-43: L UG L re Input Tape a a b b c c Work Tape A a b b T A c c
13-44: L UG L re Input Tape a a b b c c Work Tape A a b b T A c c
13-45: L UG L re Input Tape a a b b c c Work Tape T A a a b b c c
13-46: L UG L re Input Tape a a b b c c Work Tape T A a a b b c c
13-47: L UG L re Input Tape a a b b c c Work Tape a a b b c c
13-48: L UG L re Input Tape a a b b c c Work Tape a a b b c c
13-49: L re L UG Given any Turing Machine M that semi-decides the language L, we can create an Unrestricted Grammar G such that L[G] = L
13-50: L re L UG Given any Turing Machine M that semi-decides the language L, we can create an Unrestricted Grammar G such that L[G] = L Will assume that all Turing Machines accept in the same configuration: (h, ) Not a major restriction why?
13-51: L re L UG Given any Turing Machine M that semi-decides the language L, we can create an Unrestricted Grammar G such that L[G] = L Will assume that all Turing Machines accept in the same configuration: (h, ) Not a major restriction why? Add a tape erasing machine right before the accepting state, that erases the tape, leaving the read/write head at the beginning of the tape
13-52: L re L UG Given any Turing Machine M that semi-decides the language L, we can create an Unrestricted Grammar G such that L[G] = L Grammar: Generates a string Turing Machine: Works from string to accept state Two formalisms work in different directions Simulating Turing Machine with a Grammar can be difficult..
13-53: L re L UG Two formalisms work in different directions Simulate a Turing Machine in reverse! Each partial derivation represents a configuration Each rule represents a backwards step in Turing Machine computation
13-54: L re L UG Given a TM M, we create a Grammar G: Language for G: Everything in Σ M Everything in K M Start symbol S Symbols and
13-55: L re L UG Configuration (Q, uaw) represented by the string: uaqw For example, (Q, abc a) is represented by the string abcq a
13-56: L re L UG For each element in δ M of the form: ((Q 1,a),(Q 2,b)) Add the rule: bq 2 aq 1 Remember, simulating backwards computation
13-57: L re L UG For each element in δ M of the form: ((Q 1,a),(Q 2, )) Add the rule: Q 2 a aq 1
13-58: L re L UG For each element in δ M of the form: ((Q 1, ),(Q 2, )) Add the rule Q 2 Q 1 (undoing erasing extra blanks)
13-59: L re L UG For each element in δ M of the form: ((Q 1,a),(Q 2, )) Add the rule abq 2 aq 1 b For all b Σ
13-60: L re L UG For each element in δ M of the form: ((Q 1,a),(Q 2, )) Add the rule a Q 2 aq 1 (undoing moving to the right onto unused tape)
13-61: L re L UG Finally, add the rules: S h Q s ǫ ǫ
13-62: L re L UG If the Turing machine can move from w to h Then the Grammar can transform Q h to Q s w Then, remove Q s and to leave w
13-63: L re L UG Example: Create a Turing Machine that accepts (ab), halting in the configuration (h, ) (assume tape starts out as w)
13-64: L re L UG Example: Create a Turing Machine that accepts (ab), halting in the configuration (h, ) L yes R a b R R a L a,b yes
13-65: L re L UG a b q 0 (q 1, ) (q 1, ) (q 1, ) q 1 (q 2, ) (q h, ) q 2 (q 3, ) q 3 (q 2, ) (q 4, ) q 4 (q 5, ) (q 5, ) (q h, ) (q 4, ) q 5
13-66: L re L UG ((q 0,a),(q 1, )) aaq 1 aq 0 a abq 1 aq 0 b a Q 1 aq 0 a Q 1 aq 0
13-67: L re L UG ((q 0,b),(q 1, )) baq 1 bq 0 a bbq 1 bq 0 b b Q 1 bq 0 b Q 1 bq 0
13-68: L re L UG ((q 0, ),(q 1, )) aq 1 Q 0 a bq 1 Q 0 b Q 1 Q 0 Q 1 Q 0
13-69: L re L UG ((q 1,a),(q 2, )) aaq 2 aq 1 a abq 2 aq 1 b a Q 2 aq 1 a Q 2 aq 1
13-70: L re L UG ((q 1, ),(q h, )) h Q 1
13-71: L re L UG ((q 2,b),(q 3, )) baq 3 bq 2 a bbq 3 bq 2 b b Q 3 bq 2 b Q 3 bq 2
13-72: L re L UG ((q 3,a),(q 4, )) aaq 4 aq 3 a abq 4 aq 3 b a Q 4 aq 3 a Q 4 aq 3
13-73: L re L UG ((q 4,a),(q 5, )) Q 5 aq 4 ((q 4,b),(q 5, )) Q 5 bq 4 ((q 4, ),(q h, )) h Q 4 ((q 5, ),(q 4, )) Q 4 Q 5
13-74: L re L UG S h aq 1 Q 0 a b Q 3 bq 2 Q 0 ǫ bq 1 Q 0 b b Q 3 bq 2 ǫ Q 1 Q 0 aaq 4 aq 3 a aaq 1 aq 0 a Q 1 Q 0 abq 4 aq 3 b abq 1 aq 0 b aaq 2 aq 1 a a Q 4 aq 3 a Q 1 aq 0 abq 2 aq 1 b a Q 4 aq 3 a Q 1 aq 0 a Q 2 aq 1 Q 5 aq 4 baq 1 bq 0 a a Q 2 aq 1 Q 5 bq 4 bbq 1 bq 0 b h Q 1 h Q 4 b Q 1 bq 0 baq 3 bq 2 a Q 4 Q 5 b Q 1 bq 0 bbq 3 bq 2 b
13-75: L re L UG Generating abab S h h Q 4 Q 4 Q 5 Q 5 aq 4 aq 4 a Q 5 a Q 5 abq 4 abq 4 ab Q 5 ab Q 5 abaq 4 abaq 4 aba Q 5 aba Q 5 ababq 4
13-76: L re L UG Generating abab ababq 4 abab Q 3 abab Q 3 ababq 2 ababq 2 abaq 3 b abaq 3 b abq 2 ab abq 2 ab aq 1 bab aq 1 bab Q 0 abab Q 0 abab abab abab abab