Chapter 13
Chapter 13
Chapter 13
Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education.
Chapter Summary
Languages and Grammars
Finite-State Machines with Output
Finite-State Machines with No Output
Language Recognition
Turing Machines
Languages and Grammars
Section 13.1
Section Summary
Phrase-Structure Grammars
Types of Phrase-Structure Grammars
Derivation Trees
Backus-Naur Form
Introduction
Syntax (form of a sentence) vs. semantics (meaning of
a sentence)
The sentence the frog writes neatly is a valid sentence
according to the rules of English grammar. That is, it is
syntactically correct, even though it’s nonsensical
(unless we are talking about a fantasy world).
The sequence of words swims quickly mathematics is
not a valid sentence according to the rules of English
grammar.
Grammars
The rules that specify the syntactically correct
sentences of a natural language such as English are
complex.
Instead of studying natural languages, we can define
formal languages that have well-defined rules of
syntax.
These rules of syntax are important both in linguistics
(the study of natural languages) and in the study of
programming languages.
An Example Grammar
1. a sentence is made up of a noun phrase followed by a verb phrase;
2. a noun phrase is made up of an article followed by an adjective followed by a noun, or
3. a noun phrase is made up of an article followed by a noun;
4. a verb phrase is made up of a verb followed by an adverb, or
5. a verb phrase is made up of a verb;
6. an article is a, or
7. an article is the;
8. an adjective is large, or
9. an adjective is hungry; We use these rules to form valid sentences by making a series
10. a noun is rabbit, or
of replacements until no more rules can be used.
11. a noun is mathematician;
12. a verb is eats, or
13. a verb is hops;
14. an adverb is quickly, or
15. an adverb is wildly.
We will trace the transitions and outputs of the vending machine when a student puts in a dime followed by a quarter, receives 5
cents back, and then pushes the orange button and receives an orange juice.
The machine starts in state s0.
The first input is 10 cents, which changes the state to s2 and gives no output.
After the second input of 25 cents, the state changes to s6 and gives 5 cents as output.
The last input is the orange button, which changes the state back to s0 and gives an orange juice as output.
FSMs with Outputs
A finite-state machine M =(S, I, O, f, g, s0) consists of a finite set S of states, a finite input
alphabet I, a finite output alphabet O, a transition function f that assigns to each state and
input pair a new state, an output function g that assigns to each state and input pair an
output, and an initial state s0 .
A state table is used to represent the values of the transition function f and the output
function g for all (state, input).
Alternatively, a finite-state machine can be represented by a state diagram, which is a
directed graph with labeled edges. Each state is represented by a circle, and arrows labeled
with the input and output pair represent the transitions.
The state table and state diagram both represent the finite state machine with S=
{s0 ,s1 ,s2 ,s3}, I = {0, 1}, and O = {0, 1}.
Unit-delay Machine
An important element in many electronic devices is a unit-delay machine, which
produces as output the input string delayed by a specified amount of time, i.e.,
padded with an initial string of 0s.
How can a finite-state machine be constructed that delays an input string by
one unit of time, that is, produces as output the bit string 0x1x2…xk-1 given the
input bit string x1x2…xk-1?
A delay machine can be constructed that has 0 or 1 as possible inputs. The
machine has the start state s0. The transition from s0 produces an output of 0.
The machine is in state s1 if the previous input was a 1 and it produces 1 as
output for its next transition, and in state s2 if the previous input was 0 and it
produces an output of 0 for its next transition.
Addition Machine
We will construct a finite-state machine that adds two positive integers using their binary expansions.
Recall the conventional procedure to add (xn…x1x0)2 and (yn…y1y0)2 .
First, the bits x0 and y0 are added, producing a sum bit z0 and a carry bit c0. Next the bits x1 and y1 are added
together with the carry bit c0. This gives a sum bit z1 and a carry bit c1.
The procedure continues until the nth stage, where xn, yn and the previous carry cn-1 are added to produce the
sum bit zn and the carry bit cn, which is equal to the sum bit zn+1.
We can construct a finite state machine that uses just two states.
The start state s0 is used to remember that the previous carry is 0.
The other state s1 is used to remember that the previous carry is 1. (For simplicity, we assume that both xn
and yn are 0.)
The inputs are pairs ofbits. The transitions and the outputs are constructed from the sum of the two bits in
the input and the carry represented by the state.
For example, when the machine is in state s1 and receives 01 as input, the next state is s1 and the output is 0,
because the sum 0 + 1 + 1 =(10)2.
Finite-State Machines
with No Output
Section 13.3
Section Summary
Set of Strings
Finite-State Automata
Language Recognition by Finite-State Machines
Designing Finite-State Automata
Equivalent Finite-State Automata (not currently
included in overheads)
Nondeterministic Finite-State Automata
Set of Strings Stephen Cole Kleene
(1909-1994)
FSMs with no output, but with some states designated as accepting
states, are specifically designed for recognizing languages.
The concatenation of A and B, where A and B are subsets of V*,
denoted by AB, is the set of all strings of the form xy, where x is a
string in A and y is a string in B.
Let A = {0, 11} and B = {1, 10, 110}. Then
AB = {01, 010, 0110, 111, 110, 11110} and
BA = {10, 111, 100, 1011, 1100, 11011}
If A is a subset of V*, the Kleene closure of A, denoted by A*, is the set
consisting of arbitrarily long strings of elements of A. That is, A* = .
The Kleene closures of the sets A = {0}, B = {0,1} and C = {11} are
A* = {0n | n = 0, 1, 2, ….}, B* = V*, and C* = {12n | n = 0, 1, 2, ….}
Finite-State Automata (FSA)
A finite-state automaton M = (S, I, f, s0, F) consists of a finite set S of
states, a finite input alphabet I, a transition function f that assigns a
next state to every pair of state and input (so that f: S × I → S), an
initial or start state s0, and a subset F of S consisting of final (or
accepting) states.
FSAs can be represented using either state tables or state diagrams,
in which final states are indicated with a double circle.
The state diagram for the FSA M = (S, I, f, s0, F), where
S = {s0, s1, s2,s3}, I = {0, 1}, F = {s0,s3}, and the transition
diagram is in Table 1, is shown here.
Language Recognition by FSAs
A string x is said to be recognized (or accepted) by the machine M = (S, I, f, s0, F) if it
takes the initial state s0 to a final state, that is, f(s0, x). The language recognized (or
accepted) by M, denoted by L(M), is the set of all strings that are recognized by M.
Two finite-state automata are called equivalent if they recognize the same language.
Now consider the input string 0N0t1N = 0N+t1N . The string is not of the correct
form and so, it is not recognized by M.
Consequently, f(s0 ,0N+t1N) can not be a final state.
However, when we use the string 0N+t1N as input, we end up in the same state
as before, namely, s2N. The reason is that we go through the loop one more time.
This contradiction shows that {0n1n | n = 0, 1, 2, …} is not regular.
More Powerful Types of Machines
The main limitation of finite-state automata is their finite
amount of memory. This has led to the development of more
powerful types of machines.
Pushdown Automaton (PDA): includes a stack, which provides
unlimited memory. We can use a PDA to recognize {0n1n | n = 0, 1,
2, …}, but no PDA recognizes the set {0n1n2n | n = 0, 1, 2, …}.
Linear Bounded Automaton (LBA): More powerful than pushdown
automata. We can use a LBA to recognize {0n1n2n | n = 0, 1, 2, …},
but there are languages generated by phrase-structure grammars
that cannot be recognized by a LBA.
Turing Machine (TM): Yet more powerful machines (to be studied
in the next section) which can recognize all languages generated by
phrase-structure grammars.
Turing Machines
Section 13.5
Section Summary
Definition of Turing Machines
Using Turing Machines to Recognize Sets
Computing Functions with Turing Machines
Different Types of Turing Machines (not currently
included in overheads)
The Church-Turing Thesis
Computational Complexity, Computability, and
Decidability
Alan Mathison Turing
(1912-1954)
Introduction
Informally, a Turing machine consists of a control unit, which at any step is in one of
finitely many different states, together with a tape, infinite in both directions, which
is divided into cells.
Turing machines have read and write capabilities on the tape as the control unit
moves back and forth along this tape, changing states depending on the tape
symbol read.
Turing machines are more powerful than finite-state machines because they include
additional memory capability.
Turing machines are the most general models of computation; essentially they can
do whatever a computer can do.
Definition of Turing Machines (TM)
A Turing machine T = (S, I, f, s0) consists of
a finite set S ofstates,
an alphabet I containing the blank symbol B,
a partial function f from S × I to S × I ×{R,L}, and
a starting state s0.
For some (state, symbol) pairs the partial function f may be undefined, but for a pair for which it is
defined, there is a unique (state, symbol, direction) triple associated to this pair.
The five-tuples corresponding to the partial function in the definition of a TM are called the transition
rules of the machine.
At each step, the control unit reads the current tape symbol x. If the control unit is in state s and if the
partial function f is defined for the pair (s, x) with f(s, x) = (s′, x′, d), the control unit:
enters the state s′,
writes the symbol x′ in the current cell, erasing x, and
moves right one cell if d = R or moves left one cell if d = L.
This step is written as the five-tuple (s, x, s′, x′, d). Turing machines are defined by specifying a set of
such five-tuples. If the partial function f is undefined for the pair (s, x) then T will halt.
At the beginning of its operation a TM is assumed to be in the initial state s0 and to be positioned over
the leftmost nonblank symbol on the tape. This is the initial position of the machine.
A TM in Operation
Example: What is the final tape
when the TM T defined by the
seven five-tuples (s0, 0, s0,
0, R), (s0, 1, s1, 1, R), (s0, B, s3, B,
R), (s1, 0, s0, 0, R), (s1, 1, s2, 0, L),
(s1, B, s3, B, R), and (s2, 1, s3, 0, R)
is run on the tape shown here in
(a)?