1 / 30

Unrestricted Grammars

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:

nuri
Download Presentation

Unrestricted Grammars

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unrestricted Grammars Chapter 23

  2. Grammars, SD Languages, and Turing Machines SD Language L Grammar Accepts Turing Machine

  3. Unrestricted Grammars • An unrestricted, or • type 0, or • phrase structure grammarG 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}.

  4. Unrestricted Grammars Example: AnBnCn = {anbncn, n 0}. CFG attempt 1: SA B C AaA |  BbB |  CcC | 

  5. Unrestricted Grammars Example: AnBnCn = {anbncn, n 0}. CFG attempt 2: Sab S c |  S ab S c ab S c  ababcc

  6. Unrestricted Grammars Example: AnBnCn = {anbncn, n 0}. SaBSc S BaaB Bcbc Bbbb Proof: ● Only strings in AnBnCn : ● All strings in AnBnCn :

  7. Another Example {w {a, b, c}* : #a(w) = #b(w) = #c(w)}

  8. Another Example {w {a, b, c}* : #a(w) = #b(w) = #c(w)} SABCS S ABBA BCCB ACCA BAAB CAAC CBBC Aa Bb Cc

  9. WW = {ww : w {a, b}*}

  10. WW = {ww : w {a, b}*} • One idea: • Generate a string in wwR, plus delimiters • aaabbCbbaaa# • Reverse the second half.

  11. 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 P Paa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# 

  12. A Strong Procedural Feel Unrestricted grammars have a procedural feel that is absent from 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 computing devices.

  13. Equivalence of Unrestricted Grammars and Turing Machines Theorem: A language is generated by an unrestricted grammar 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 that mimics the behavior of a semideciding TM.

  14. Proof that Grammar  Turing Machine Given G, produce a Turing machine M that semidecides L(G). M will be nondeterministic and will use two tapes: qa b a bqq q1 0 0 0 0 0 0q aS Taabq 1 0 0 0 0 0 0 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 the rule. Or it chooses to check whether tape 2 equals tape 1. If any such machine succeeds, we accept. Otherwise, we keep looking.

  15. 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 = # q qq000a1a1a2a2a3a3q q # The second (test) part of G simulates the execution of M on a particular string w. An example of a partially derived string: # q qa1b2ccb4q001a3 # Examples of rules: q100 b b  b 2 q101 a a q011 b 4  q011 a a b 4

  16. 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: xxAAx /* Sweep A to the left. x, y #Axyx #A /* Erase duplicates. #A# 

  17. An Unrestricted Grammar of Tabla Drumming

  18. Computing with Grammars We say that Gcomputesf iff: w, v* (SwSG* vv = f(w)) Example: f(x) = succ(x) S1SG* 11 S11SG* 111

  19. Computing with Grammars We say that Gcomputesf 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  11 SS  1S1  1 SS  1

  20. Grammatically Computable 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 ones used to prove that a language can be defined with an unrestricted grammar iff it is in SD.

  21. 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.

  22. Example of Computing with a Grammar f(n) = m, where value1(m) = 2value1(n). G = ({S, 1}, {1}, R, S), where R = Examples: Input: S111S SS Output:

  23. Example of Computing with a Grammar f(n) = m, where value1(m) = 2value1(n). G = ({S, 1}, {1}, R, S), where R = Examples: Input: S111S SS Output: 111111 

  24. Example of Computing with a Grammar f(n) = m, where value1(m) = 2value1(n). G = ({S, 1}, {1}, R, S), where R = S1 11S SS  Examples: Input: S111S SS Output:111111

  25. Another Example f(x) = x with extra blanks squeezed out. Examples: Input: SaaqbqqaaqqqqbqS Output: aaqbqaaqb Input: SqqaabS Output: aab

  26. Another Example f(x) = x with extra blanks squeezed out. G = ({S, T, a, b, q}, {a, b, q}, R, S), where R = SS /* In case x has no nonblank characters. SqS /* Get rid of leading q’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 TqqTq /* Squeeze repeated q’s. TqaqaT /* Once there is a single q, sweep T past it and the first letter after it. TqbqbT TqS /* The Tqq rule will get rid of all but possibly one q at the end of the string. TS  /* If there were no trailing q’s, this rule finishes up.

  27. Decision Problems for Unrestricted Grammars ● 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.

  28. La = {<G, w> : wL(G)} is not in D. 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.

  29. 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

  30. Semi-Thue Systems • A semi-Thue systemT 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.

More Related