Automata Theory: CS411-2004F-13 Unrestricted Grammars
Automata Theory: CS411-2004F-13 Unrestricted Grammars
CS411-2004F-13
Unrestricted Grammars
David Galles
Department of Computer Science
University of San Francisco
13-0: Language Hierarchy
Regular
Languaes
Context Free
Languages
Regular Expressions
Finite Automata
Context-Free Grammars
Push-Down Automata
Recusively Enumerable
Languages
??
Turing Machines
13-1: CFG Review
G = (\. . 1. o)
\ = Set of symbols, both terminals & non-terminals
\ set of terminals (alphabet for the language
being described)
1 ((\ ) \
) Set of rules
o (\ ) Start symbol
13-2: Unrestricted Grammars
G = (\. . 1. o)
\ = Set of symbols, both terminals & non-terminals
\ set of terminals (alphabet for the language
being described)
1 (\
(\ )\
) Set of rules
o (\ ) Start symbol
13-3: Unrestricted Grammars
1 (\
(\ )\
) 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 1 = {c
n
/
n
c
n
: : 0}
First, generate (1C)
}
13-10: Unrestricted Grammars
Example: Grammar for 1 = {uu : u c. /
}
Hints:
What if we created a string, and then
rearranged it (like (c/c)
c
n
/
n
c
n
)
13-11: Unrestricted Grammars
Example: Grammar for 1 = {uu : u c. /
}
Hints:
What if we created a string, and then
rearranged it (like (c/c)
c
n
/
n
c
n
)
What about trying uu
R
...
13-12: Unrestricted Grammars
1 = {uu : u c. /
}
o o
2
o
co
/o
1
o
c
2 A2 12 1 2
A A 1 1
1A A1 11 1 1
cA cc c1 c/
/A /c /1 //
13-13: Unrestricted Grammars
1
UG
is the set of languages that can be described
by an Unrestricted Grammar:
1
UG
= {1 : Unrestricted Grammar
G. 1[G] = 1}
Claim: 1
UG
= 1
re
To Prove:
Prove 1
UG
1
re
Prove 1
re
1
UG
13-14: 1
lG
1
:c
Given any Unrestricted Grammar G, we can create
a Turing Machine ` that semi-decides 1[G]
13-15: 1
lG
1
:c
Given any Unrestricted Grammar G, we can create
a Turing Machine ` that semi-decides 1[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: 1
lG
1
:c
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: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S
13-18: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S
13-19: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S ABC
13-20: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S ABC
13-21: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S ABCABC
13-22: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S ABCABC
13-23: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S AB C A BC
13-24: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S AB C A BC
13-25: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S AA C B BC
13-26: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S AA C B BC
13-27: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S AA B B CC
13-28: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
S AA B B CC
13-29: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B CCT
C
13-30: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B CCT
C
13-31: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B C c T
C
13-32: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B C c T
C
13-33: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B c c T
C
13-34: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B c c T
C
13-35: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B c c T
B
13-36: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA B B c c T
B
13-37: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA b B c c T
B
13-38: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA b B c c T
B
13-39: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA b b c c T
B
13-40: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA b b c c T
B
13-41: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA b b c c T
A
13-42: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
AA b b c c T
A
13-43: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
A a b b c c T
A
13-44: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
A a b b c c T
A
13-45: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
a a b b c c T
A
13-46: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
a a b b c c T
A
13-47: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
a a b b c c
13-48: 1
lG
1
:c
a
...
...
a b b c c
Input Tape
Work Tape
a a b b c c
13-49: 1
:c
1
lG
Given any Turing Machine ` that semi-decides
the language 1, we can create an Unrestricted
Grammar G such that 1[G] = 1
13-50: 1
:c
1
lG
Given any Turing Machine ` that semi-decides
the language 1, we can create an Unrestricted
Grammar G such that 1[G] = 1
Will assume that all Turing Machines accept in
the same conguration: (/. >)
Not a major restriction why?
13-51: 1
:c
1
lG
Given any Turing Machine ` that semi-decides
the language 1, we can create an Unrestricted
Grammar G such that 1[G] = 1
Will assume that all Turing Machines accept in
the same conguration: (/. >)
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: 1
:c
1
lG
Given any Turing Machine ` that semi-decides
the language 1, we can create an Unrestricted
Grammar G such that 1[G] = 1
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
difcult ..
13-53: 1
:c
1
lG
Two formalisms work in different directions
Simulate a Turing Machine in reverse!
Each partial derivation represents a
conguration
Each rule represents a backwards step in
Turing Machine computation
13-54: 1
:c
1
lG
Given a TM `, we create a Grammar G:
Language for G:
Everything in
M
Everything in 1
M
Start symbol o
Symbols > and
13-55: 1
:c
1
lG
Conguration (Q. >ncu) represented by the string:
>ncQu
For example, (Q. > c/c c) is represented by the
string > c/cQ c
13-56: 1
:c
1
lG
For each element in
M
of the form:
((Q
1
. c). (Q
2
. /))
Add the rule:
/Q
2
cQ
1
Remember, simulating backwards computation
13-57: 1
:c
1
lG
For each element in
M
of the form:
((Q
1
. c). (Q
2
. 1))
Add the rule:
Q
2
c cQ
1
13-58: 1
:c
1
lG
For each element in
M
of the form:
((Q
1
. ). (Q
2
. 1))
Add the rule
Q
2
Q
1
,
halting in the conguration (/. >)
(assume conguration starts out as u)
13-64: 1
:c
1
lG
Example:
Create a Turing Machine that accepts (c/)
,
halting in the conguration (/. >)
R
a
R R
b
a
L yes
a,b
L yes
13-65: 1
:c
1
lG
c /
0
(
1
. ) (
1
. ) (
1
. )
1
(
2
. ) (
h
. )
2
(
3
. )
3
(
2
. ) (
4
. )
4
(
5
. ) (
5
. ) (
h
. )
5
(
4
. )
13-66: 1
:c
1
lG
((
0
. c). (
1
. ))
ccQ
1
cQ
0
c
c/Q
1
cQ
0
/
c Q
1
cQ
0
c Q
1
cQ
0
13-67: 1
:c
1
lG
((
0
. /). (
1
. ))
/cQ
1
/Q
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