Unrestricted Grammars

Post on 02-Feb-2016

151 views 7 download

Tags:

description

Unrestricted Grammars. Chapter 23. Grammars, SD Languages, and Turing Machines. SD Language. L. Grammar. Accepts. Turing Machine. Unrestricted Grammars. An unrestricted , or type 0 , or phrase structure grammar G is a quadruple ( V ,  , R , S ),where: - PowerPoint PPT Presentation

Transcript of Unrestricted Grammars

Unrestricted Grammars

Chapter 23

Grammars, SD Languages, and Turing Machines

SD Language

Grammar

Turing Machine

L

Accepts

Unrestricted Grammars An unrestricted, or type 0, or phrase structure grammar G is a quadruple

(V, , R, S), where:

● V is an alphabet,

● (the set of terminals) is a subset of V,

● R (the set of rules) is a finite subset of (V+ V*),

● S (the start symbol) is an element of V - .

The language generated by G is:

{w * : S G* w}.

Unrestricted Grammars

Example: AnBnCn = {anbncn, n 0}.

CFG attempt 1:

S A B CA aA | B bB | C cC |

Unrestricted Grammars

Example: AnBnCn = {anbncn, n 0}.

CFG attempt 2:

S a b S c | S

a b S c

a b S c

ababcc

Unrestricted Grammars

Example: AnBnCn = {anbncn, n 0}.

S aBScS Ba aBBc bcBb bb

Proof: ● Only strings in AnBnCn :

● All strings in AnBnCn :

Another Example

{w {a, b, c}* : #a(w) = #b(w) = #c(w)}

Another Example

{w {a, b, c}* : #a(w) = #b(w) = #c(w)}

S ABCSS AB BABC CBAC CABA ABCA ACCB BCA aB bC c

WW = {ww : w {a, b}*}

WW = {ww : w {a, b}*}

One idea:

1. Generate a string in wwR, plus delimiters

aaabbCbbaaa#

2. Reverse the second half.

WW = {ww : w {a, b}*}

S T# /* Generate the wall exactly once.T aTa /* Generate wCwR.T bTb T C C CP /* Generate a pusher PPaa aPa /* Push one character to the right

to get ready to jump.Pab bPa Pba aPb Pbb bPb Pa# #a /* Hop a character over the wall.Pb# #b C#

A Strong Procedural Feel

Unrestricted grammars have a procedural feel that is absentfrom most restricted grammars.

Derivations often proceed in phases. We make sure that the phases work properly by using nonterminals as flags that we’re in a particular phase.

It’s very common to have three main phases: ● Generate the right number of the various symbols. ● Move them around to get them in the right order. ● Clean up and get rid of nonterminals.

No surprise: unrestricted grammars are general computingdevices.

Equivalence of Unrestricted Grammars and Turing Machines

Theorem: A language is generated by an unrestrictedgrammar if and only if it is in SD.

Proof:

Only if (grammar TM): by construction of an NDTM.

If (TM grammar): by construction of a grammar thatmimics the behavior of a semideciding TM.

Given G, produce a Turing machine M that semidecides L(G).

M will be nondeterministic and will use two tapes:

Proof that Grammar Turing Machine

For each nondeterministic “incarnation”: ● Tape 1 holds the input. ● Tape 2 holds the current state of a proposed derivation.

At each step, M nondeterministically chooses a rule to try to apply and a position on tape 2 to start looking for the left hand side of therule. Or it chooses to check whether tape 2 equals tape 1. If anysuch machine succeeds, we accept. Otherwise, we keep looking.

a b a b 1 0 0 0 0 0 0

a S T a a b 1 0 0 0 0 0 0

Proof that Turing Machine Grammar

Build G to simulate the forward operation of a TM M:

The first (generate) part of G:Create all strings over * of the form:

w = # q000 a1 a1 a2 a2 a3 a3 #

The second (test) part of G simulates the execution of M on a particular string w. An example of a partially derived string:

# a 1 b 2 c c b 4 q001 a 3 #

Examples of rules:

q100 b b b 2 q101 a a q011 b 4 q011 a a b 4

The Last Step

The third (cleanup) part of G erases the junk if M ever reaches any of its accepting states, all of which will be encoded as A.

Rules:x x A A x /* Sweep A to the left.x, y #A x y x #A /* Erase duplicates.

#A#

An Unrestricted Grammar of Tabla Drumming

We say that G computes f iff:

w, v * (SwS G* v v = f(w))

Example: f(x) = succ(x)

S1S G* 11 S11S G* 111

Computing with Grammars

We say that G computes f iff:

w, v * (SwS G* v v = f(w))

Example: f(x) = succ(x)

S1S G* 11 S11S G* 111

Rules: S1 1S or 1S 11SS 1 S1 1

SS 1

Computing with Grammars

A function f is called grammatically computable iff there is a grammar G that computes it.

Theorem: A function f is computable iff it is grammatically computable.

In other words, iff a Turing machine can do it, so can a grammar.

Proof: uses two constructions, analogous to the onesused to prove that a language can be defined with anunrestricted grammar iff it is in SD.

Grammatically Computable

The valuen Functions

For any k, valuek(n) returns the nonnegative integer that is encoded, base k, by the string n.

Example:

value2(101) = 5.

f(n) = m, where value1(m) = 2value1(n).

G = ({S, 1}, {1}, R, S), where R =

Examples:

Input: S111S SS

Output:

Example of Computing with a Grammar

f(n) = m, where value1(m) = 2value1(n).

G = ({S, 1}, {1}, R, S), where R =

Examples:

Input: S111S SS

Output: 111111

Example of Computing with a Grammar

f(n) = m, where value1(m) = 2value1(n).

G = ({S, 1}, {1}, R, S), where R =

S1 11SSS

Examples:

Input: S111S SS

Output: 111111

Example of Computing with a Grammar

f(x) = x with extra blanks squeezed out.

Examples:

Input: SaabaabS

Output: aabaab

Input: SaabS

Output: aab

Another Example

f(x) = x with extra blanks squeezed out.

G = ({S, T, a, b, }, {a, b, }, R, S), where R =

SS /* In case x has no nonblank characters.S S /* Get rid of leading ’s. Sa aT /* All characters to the left of T will be correct. Sb bT Ta aT /* Sweep T across a’s and b’s.Tb bT T T /* Squeeze repeated ’s.Ta aT /* Once there is a single , sweep T past it and the first letter

after it.Tb bT TS /* The T rule will get rid of all but possibly one at the

end of the string.

TS /* If there were no trailing ’s, this rule finishes up.

Another Example

● Given a grammar G and a string w, is w L(G)? ● Given a grammar G, is L(G)? ● Given two grammars G1 and G2, is L(G1) = L(G2)? ● Given a grammar G, is L(G) = ?

Or, as languages:

● La = {<G, w> : w L(G)}. ● Lb = {<G> : L(G)}. ● Lc = {<G1, G2> : L(G1) = L(G2)}. ● Ld = {<G> : L(G) = }.

None of these questions is decidable.

Decision Problems for Unrestricted Grammars

Proof: Let R be a mapping reduction from:

A = {<M, w> : Turing machine M accepts w} to La:

R(<M, w>) = 1. From M, construct the description <G#> of a grammar G# such that L(G#) = L(M). 2. Return <G#, w>.

If Oracle decides La, then C = Oracle(R(<M, w>)) decides A. We have already defined an algorithm that implements R. C is correct:

● If <M, w> A : M(w) halts and accepts. w L(M). So w L(G#). Oracle(<G#, w>) accepts. ● If <M, w> A : M(w) does not accept. w L(M). So w L(G#). Oracle(<G#, w>) rejects.

But no machine to decide A can exist, so neither does Oracle.

La = {<G, w> : w L(G)} is not in D.

The Word Problem

Given two strings, w and v, and a rewrite system T, determine whether v can be derived from w using T.

A key application: Logical reasoning

Semi-Thue SystemsA semi-Thue system T is a pair (, R), where:

• is an alphabet,

• R (the set of rules) is a subset of + *.

Note that:

• There is no unique start symbol.

• There is no distinction between terminal and nonterminal symbols.

Theorem: The word problem for semi-Thue systems is undecidable.