Vous êtes sur la page 1sur 35

Formal Languages and Automata Theory 1

CSC 3130
Formal Languages and Automata Theory
Lecturer
Evangeline Young (SHB 916, 26098401, fyyoung@cse.cuhk.edu.hk)

Guest lecturer
Prof. C.K. Cheng from UCSD

Lecture Hour
Mon 4:30pm - 6:15pm
Fri 9:30am - 10:15am
(In September, we will only meet once a week: Mon 4:30pm 6:30pm)

Tutorial Hours
No tutorial in the first week

Text book
Introduction to Automata Theory, Languages and Computation, 3
rd
edition
John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
Addison Wesley


Formal Languages and Automata Theory 2
Formal Languages and Automata Theory
Planning Schedule
Week 1-4 : Introduction, Finite Automata, Regular Expression
Different Kinds of FA, Properties of Regular Sets
Quiz 1
Week 5-8 : Context-Free Grammars, Normal Forms
Pushdown Automata, Properties of Context-Free Languages
Quiz 2
Week 9-12 : Turing Machines, Undecidability
Chomsky Hierarchy, LR(k) Grammars, Complexities
Final Examination
(Chap. 2-4)
(Chap. 5-7)
(Chap. 8-10)
Formal Languages and Automata Theory 3
Formal Languages and Automata Theory
Assessment Scheme
Quiz 1 (12.5%)
Quiz 2 (12.5%)
Final Examination (40%)
Homework (25%)
Class Participation (10%)

Plagiarism in homework, quiz or examination will result in failing of the
whole course.
Formal Languages and Automata Theory 4
Topics
Automata Theory
Grammars and Languages
Complexities
Formal Languages and Automata Theory 5
Why Automata Theory?
To study abstract computing devices which are
closely related to todays computers. A simple
example of finite state machine:
off
on
start
1
1
There are many different kinds of machines.
Formal Languages and Automata Theory 6
Another Example
start
0
1
on
0
0
1
off
off
1
When will this be on?
Try 100, 1001, 1000, 111, 00,
Formal Languages and Automata Theory 7
Grammar and Languages
Grammars and languages are closely related
to automata theory and are the basis of
many important software components like:
Compilers and interpreters
Text editors and processors
Text searching
System verification
Formal Languages and Automata Theory 8
Complexities
Study the limits of computations. What
kinds of problems can be solved with a
computer? What kinds of problems can be
solved efficiently?

Can you write a program in C that can
check if another C program will terminate?
Formal Languages and Automata Theory 9
Preliminaries
Alphabets
Strings
Languages
Problems
Formal Languages and Automata Theory 10
Alphabets
An alphabet is a finite set of symbols.
Usually, use E to represent an alphabet.
Examples:
E = {0,1}, the set of binary digits.
E = {a, b, , z}, the set of all lower-case letters.
E = {(, )}, the set of open and close parentheses.
Formal Languages and Automata Theory 11
Strings
A string is a finite sequence of symbols
from an alphabet.
Examples:
0011 and 11 are strings from E = {0,1}
abc and bbb are strings from E = {a, b, , z}
(()(())) and )(() are strings from E = {(, )}
Formal Languages and Automata Theory 12
Strings
Empty string: c
Length of string: |0010| = 4, |aa| = 2, |c|=0
Prefix of string: aaabc, aaabc, aaabc
Suffix of string: aaabc, aaabc, aaabc
Substring of string: aaabc, aaabc, aaabc

Formal Languages and Automata Theory 13
Strings
Concatenation: e=abd, o=ce, eo=abdce
Exponentiation: e=abd, e
3
=abdabdabd, e
0
=c
Reversal: e=abd, e
R
= dba
E
k
= set of all k-length strings formed by the
symbols in E
e.g., E={a,b}, E
2
={ab, ba, aa, bb}, E
0
={c}
What is E
1
? Is E
1
different from E? How?

Formal Languages and Automata Theory 14
Strings
Kleene Closure E
*
= E
0
E
1
E
2
=
k>0
E
k

e.g., E={a, b}, E
*
= {c, a, b, ab, aa, ba, bb, aaa,
aab, abb, } is the set of all strings formed
by as and bs.
E
+
= E
1
E
2
E
3
=
k>0
E
k
i.e., E
*
without the empty string.


Formal Languages and Automata Theory 15
Languages
A language is a set of strings over an alphabet.
Examples:
E={(, )}, L
1
={(), )(, (())} is a language over E. The
set L
2
of all strings with balanced left and right
parentheses is also a language over E.
E={a, b, c, , z}, the set L of all legal English
words is a language over E.
The set {c} is a language over any alphabet.
What is the difference between | and {c}?
Formal Languages and Automata Theory 16
Languages
Other Examples:
E={0, 1}, L={0
n
1
n
| n>1} is a language over E
consisting of the strings {01, 0011, 000111, }
E={0, 1}, L = {0
i
1
j
| j>i>0} is a language over E
consisting of the strings with some 0s (possibly
none) followed by at least as many 1s.

Formal Languages and Automata Theory 17
Problems
In automata theory, a problem is to decide
whether a given string is a member of some
particular language.

This formulation is general enough to
capture the difficulty levels of all problems.
Formal Languages and Automata Theory 18
Finite Automata
( or Finite State Machines)
This is the simplest kind of machine.
We will study 3 types of Finite Automata:
Deterministic Finite Automata (DFA)
Non-deterministic Finite Automata (NFA)
Finite Automata with c-transitions (c-NFA)
Formal Languages and Automata Theory 19
Deterministic Finite Automata
(DFA)
We have seen a simple example before:

off
on
start
1
1
There are some states and transitions (edges)
between the states. The edge labels tell when
we can move from one state to another.
Formal Languages and Automata Theory 20
Definition of DFA
A DFA is a 5-tuple (Q, E, o, q
0
, F) where
Q is a finite set of states
E is a finite input alphabet
o is the transition function mapping Q E to Q
q
0
in Q is the initial state (only one)
F _ Q is a set of final states (zero or more)

Formal Languages and Automata Theory 21
Definition of DFA
For example:

off
on
start
1
1
Q is the set of states: {on, off}
E is the set of input symbols: {1}
o is the transitions: off 1 on; on 1 off
q
0
is the initial state: off
F is the set of final states (double circle): {on}

Formal Languages and Automata Theory 22
Definition of DFA
Another Example:
start
0
1
q
2

0
0
1
q
1

q
0

1
What are Q, E, o, q
0
and F in this DFA?

Formal Languages and Automata Theory 23
Transition Table
We can also use a table to specify the transitions.
For the previous example, the DFA is (Q,E,o,q
0
,F)
where Q = {q
0
,q
1
,q
2
}, E = {0,1}, F = {q
2
} and o is
such that
States
Inputs
0
1
q
0
q
1
q
2
q
1
q
0
q
2
q
0
q
1
q
0
Note that there is one transition only for each input
symbol from each state.
Formal Languages and Automata Theory 24
DFA Example
Consider the DFA M=(Q,E,o,q
0
,F) where Q =
{q
0
,q
1
,q
2
,q
3
}, E = {0,1}, F = {q
0
} and o is:
States
Inputs
0
1
q
0
q
1
q
2
q
3
q
2
q
2
q
1
q
1
q
3
q
3
q
0
q
0
q
0
q
2
q
3
q
1
1
1
1
1
0 0
0
0
Start
We can use a transition table or a transition diagram to specify
the transitions. What input can take you to the final state in M?
OR
Formal Languages and Automata Theory 25
Language of a DFA
Given a DFA M, the language accepted (or
recognized) by M is the set of all strings
that, starting from the initial state, will reach
one of the final states after the whole string
is read.

For example, the language accepted by the
previous example is the set of all 0 and 1
strings with even number of 0s and 1s.
Formal Languages and Automata Theory 26
Class Discussion
Start
q
0
q
1
Start
q
0
q
1
Start
q
0
q
1
q
2
0 0
1
1
0 1
1
0
1 0
0 0,1 1
What are the languages accepted by these DFA?
Formal Languages and Automata Theory 27
Class Discussion
Construct a DFA that accepts a language L
over E = {0, 1} such that:
(a) L contains 010 and 1 only.
(b) L is the set of all strings ending with 101.
(c) L is the set of all strings containing no
consecutive 1s nor consecutive 0s.

Formal Languages and Automata Theory 28
Non-deterministic FA (NFA)
For each state, zero, one or more transitions
are allowed on the same input symbol.
An input is accepted if there is a path
leading to a final state.
Formal Languages and Automata Theory 29
An Example of NFA
1
0
1
1
0 Start
q
0
q
1
q
2
In this NFA(Q,E,o,q
0
,F), Q = {q
0
,q
1
,q
2
}, E = {0,1},
F = {q
2
} and o is:
States
Inputs
0
1
q
0
q
1
q
2
|

{q
1
,q
2
}
{q
1
}
{q
0
}
{q
2
}
OR
|

Note that each transition can lead to a set of states,
which can be empty.
Formal Languages and Automata Theory 30
Language of an NFA
Given an NFA M, the language recognized by
M is the set of all strings that, starting from
the initial state, has at least one path reaching
a final state after the whole string is read.
Consider the previous example:
For input 101, one path is q
0
q
1
q
1
q
2
and the
other one is q
0
q
2
q
0
q
1
. Since q
2
is a final state,
so 101 is accepted. For input 1010, none of its
paths can reach a final state, so it is rejected.
Formal Languages and Automata Theory 31
More Examples of NFA
q
0
q
1
q
2
a
b
b
b
q
0
q
1
q
2
q
3
q
4
q
5
q
6
q
7
0-9
0-9
0-9
0-9
0-9
+,-
+,-
E .
0-9 0-9 0-9
0-9
0-9
Start
Start
Formal Languages and Automata Theory 32
Class Discussion
Consider the language L that consists of all
the strings over E = {0, 1} such that the
third last symbol is a 1,
(a) Construct a DFA for L.
(b) Construct an NFA for L.

Is NFA more powerful than DFA?
Formal Languages and Automata Theory 33
DFA and NFA
Is NFA more powerful than DFA?
NO! NFA is equivalent to DFA.

DFA NFA
Trivial
Constructive
Proof
Formal Languages and Automata Theory 34
Constructing DFA from NFA
Given any NFA M=(Q,E,o,q
0
,F) recognizing a
language L over E, we can construct a DFA
N=(Q,E ,o,q
0
,F) which also recognizes L:
Q = set of all subsets of Q
e.g., if Q = {q
0
, q
1
}, Q = {{}, {q
0
}, {q
1
}, {q
0
, q
1
}}
q
0
= {q
0
}
F = set of all states in Q containing a final state of M
o({q
1
,q
2
, q
i
}, a) = o(q
1
,a) o(q
2
,a) ... o(q
i
,a)

a state in N a state in N
Formal Languages and Automata Theory 35
An Example of NFA DFA
Consider a simple NFA:


Construct a corresponding DFA:
0
1
1
Start
0 1
q
0
q
1
{q
0
}
{q
0
, q
1
}
{q
1
}
{}
Start
1
0
1
0
1,0 1,0

Vous aimerez peut-être aussi