Grammars
Grammars
Context-free G
Regular G
General grammars and TM
• General grammars (GG) and TM are
equivalent formalism
• Given a GG it is possible to build a TM that
recognizes the language generated by the
grammar
• Given a TM it is possible to define a GG that
generates the language accepted by the TM
• How?
From a GG to a TM (1)
Given a general grammar G=<VN, VT, P,
S>, let us construct a NDTM M, accepting
L(G):
• M has one memory tape
• The input string x is on the input tape
• The memory tape is initialized with S (better:
Z0S)
• The memory tape in general will contain a
string a ( V*)
• It is scanned searching the left part of some
production of P
• When one is found, (not necessarily the first one)
M operates a ND choice and the chosen is
substituted by the corresponding right part (if
From a GG to a TM (2)
• Following this procedure
cs=<qs, Z0> |-*- <qs,Z0>
• If and when the tape holds a string yVT*,
it is compared with x
• If they coincide, x is accepted
• otherwise this particular move sequence
does not lead to acceptance
Remarks
• Using a NDTM facilitates the construction but it
is not necessary
• It is necessary that, if x L(G), M might “try an
infinite number of ways”
• some of these might never terminate, without being
able (rightly) to conclude that x L(G), but not even
the opposite
Indeed the definition of acceptance requires
that M reaches an accepting configuration if and
only if x L, but does not requires that M
terminates its computation without accepting
(i.e., in a “rejecting state”) if x L
• Again the complement-problem and the asymmetry
between solving a problem in the positive or negative
sense.
From TM to GG (1)
Given a single tape TM M let us build a
general grammar G, that generates L(M):
• First, G generates all strings of the type
x$X, xVT*, X being a “copy of x” composed of
nonterminal symbols (e.g., for x = aba, x$X =
aba$ABA)
• G simulates the successive configurations of M
using the string on the right of $
• G is defined in a way such that it has a derivation
x$X * x if and only if x is accepted by M.
• The base idea is to simulate each move of M by
an immediate derivation of G
From TM to GG (2)
a B A C b
is represented
through the string
q $BqAC
q q’