Languages Strings
Languages Strings
1
Why Study the Theory of
Computation?
Implementations come and go.
Chapter 1 2
IBM 7090 Programming in the 1950’s
ENTRY SXA 4,RETURN
LDQ X
FMP A
FAD B Ax2 + Bx +C
XCA
FMP X
FAD C
STO RESULT
RETURN TRA 0
A BSS 1
B BSS 1
C BSS 1
X BSS 1
TEMP BSS 1
STORE BSS 1
END 3
Programming in the 1970’s
IBM 360 JCL (Job Control Language)
//MYJOB JOB (COMPRESS),
'VOLKER BANDKE',CLASS=P,COND=(0,NE)
//BACKUP EXEC PGM=IEBCOPY
//SYSPRINT DD SYSOUT=*
//SYSUT1 DD DISP=SHR,DSN=MY.IMPORTNT.PDS
//SYSUT2 DD DISP=(,CATLG),
DSN=MY.IMPORTNT.PDS.BACKUP,
// UNIT=3350,VOL=SER=DISK01,
// DCB=MY.IMPORTNT.PDS,
SPACE=(CYL,(10,10,20))
//COMPRESS EXEC PGM=IEBCOPY
//SYSPRINT DD SYSOUT=*
//MYPDS DD DISP=OLD,DSN=*.BACKUP.SYSUT1
//SYSIN DD *
COPY INDD=MYPDS,OUTDD=MYPDS
//DELETE2 EXEC PGM=IEFBR14
//BACKPDS DD DISP=(OLD,DELETE,DELETE),
DSN=MY.IMPORTNT.PDS.BACKUP
4
Guruhood
5
Why study this?
Science of Computing
•Mathematical Properties (problems &
algorithms) having nothing to do with
current technology or languages
•E.G. Alan Turing – died 1954
•Provides Abstract Structures
•Defines Provable Limits
– Like “Big Oh”
6
Goals
Study Principles of Problems themselves
•Does a solution exist?
– If not, is there a restricted variation?
•Can solution be implemented in fixed
memory?
•Is Solution efficient?
– Growth of time & memory with problem size?
•Are there equivalent groups of problems?
7
Applications of the Theory
• Programming languages, compilers, & context-free
grammars.
• FSMs (finite state machines) for parity checkers,
vending machines, communication protocols, &
building security devices.
• Interactive games as nondeterministic FSMs.
• Natural languages are mostly context-free. Speech
understanding systems use probabilistic FSMs.
• Computational Biology: DNA & proteins are strings.
• The undecidability of a simple security model.
• Artificial Intelligence: the undecidability of first-order
logic.
8
Languages and Strings
Chapter 2
9
Let's Look at Some Problems
int alpha, beta;
alpha = 3;
beta = (2 + 5) / 10;
(1) Lexical analysis: Scan the program; break it into variable
names, numbers, etc.
(2) Parsing: Create a tree that corresponds to the sequence of
operations to be executed, e.g.,
/
+ 10
2 5
(3) Optimization: Recognize, can skip first assignment since value
is never used; can precompute the arithmetic expression, since it
contains only constants.
(4) Termination: Determine if program is guaranteed to halt.
(5) Interpretation: Figure out what (if anything) useful program does.
10
Side Note:
What is a Good Definition?
11
Definition Example
• Define automobile
• A mode of transportation – NO
• A vehicle with 4 wheels that allows a person to
travel from on location to another – NO
• A mode of transportation by which I get to MSU
– NO
• All the statements are true so Why Not?
• Which on is the best? Why?
12
Definition Example
• Define automobile
• A personal motorized vehicle with 4 wheels that
allows a person to drive himself and others
from on location to another
– This is pretty good but does it completely distinguish
an automobile from other similar vehicles?
– What about a go cart? A pick-up truck?
The framework is
Language Recognition
NOTE: Pay particular attention to use of finite & infinite in all definitions!
14
Alphabet -
• An alphabet is a non-empty, finite set of
characters/symbols
– Use to denote an alphabet
• Examples
= { a, b }
= { 0, 1, 2 }
= { a, b, c,…z, A, B, … Z }
= { #, $, *, @, & }
15
Strings
• A string is a finite sequence, possibly
empty, of characters drawn from some
alphabet .
is the empty string
• * is the set of all possible strings over an
alphabet .
16
Example Alphabets & Strings
17
Functions on Strings
Length:
• |s| is the length of string s
• |s| is the number of characters in string s.
| | = 0
|1001101| = 7
#a(abbaaa) = 4.
18
More Functions on Strings
Concatenation: the concatenation of 2 strings s
and t is the string formed by appending t to s; written
as s||t or more commonly, st
Example:
If x = good and y = bye, then xy = goodbye
and yx = byegood
w0=
w k+1 = w k w
Examples:
a3 = aaa
(bye)2 = byebye
a0b3 = bbb
b2y2e2 = ??
if |w| = 0 then w R = w =
if |w| = 1 then w R = w
Example:
22
Concatenation & Reverse of Strings
Proof: By induction on |x|:
Examples:
25
The Suffix Relations
Examples:
26
Defining a Language
A language is a (finite or infinite) set of strings over a (finite)
alphabet .
27
Defining a Language
Two ways to define a language via a
Machine = Automaton
AKA – Computer Program
•Recognizer
•Generator
Which do we want? Why?
28
*
* is defined as the set of all possible
strings that can be formed from the
alphabet *
* is a language
29
* Example
Let = {a, b}
* = {, a, b,aa,ab,ba,bb,aaa,aab,… }
30
Defining Languages
Remember we are defining a set
Set Notation:
L = { w * | description of w}
L = { w {a,b,c}* | description of w}
32
Example Language Definitions
Let = {a, b}
•L = { w * : |w| < 5}
•L = { w * | w begins with b}
•L = { w * | #b(w) = 2}
•L = { w * | each a is followed by
exactly 2 b’s}
•L = { w * | w does not begin with a}
33
The Perils of Using English
L = {x#y: x, y {0, 1, 2, 3, 4, 5, 6, 7, 8,
9}* and, when x & y are viewed as
decimal representations of natural
numbers, square(x) = y}.
Examples:
3#9, 12#144
3#8, 12, 12#12#12
#
34
A Halting Problem Language
35
More Examples
L = {an : n 0}
L = {ba2n : n 0}
L = {bnan : n 0}
36
Enumeration
• Arbitrary order
37
Lexicographic Enumeration
{w {a, b}* : |w| is even}
{, aa, ab, bb, aaaa, aaab, …}
38
Cardinality of a Language
40
How Many Languages Are There?
Language functions
• Concatenation
• Kleene star
42
Concatenation of Languages
If L1 and L2 are languages over :
L1L2 = {w : s L1 & t L2 ϶ w = st }
Examples:
L1 = {cat, dog}
L2 = {apple, pear}
L1 L2 ={catapple, catpear, dogapple, dogpear}
L2 L1 ={applecat,appledog,pearcat,peardog}
43
Concatenation of Languages
{} is the identity for concatenation:
L{} = {}L = L
L=L=
44
Concatenating Languages Defined
Using Variables
L1 = {an: n 0}
L2 = {bn : n 0}
L1 L2 = {anbm : n, m 0}
L1L2 {anbn : n 0}
45
*
Kleene Star
L* - language consisting of 0 or more concatenations of
strings from L
L* = {} {w * : w = w1 w2 … wk, k 1 &
w1, w2, … wk L}
Examples:
L = {dog, cat, fish}
L* = {, dog, cat, fish, dogdog, dogcat,
dogfish,fishcatfish,fishdogdogfishcat, …}
~~~~~~~~~~~~
L1 = a* L2 = b*
What is a*? b*?
L1 L2 =
L2 L1 =
46
L1 L1 =
The + Operator
L+ = language consisting of 1 or more
concatenations of strings from L
L+ = L L*
L+ = L* - {} iff L
Explain this definition!!
When is L+?
47
Closure
• A set S is closed under the operation @ if
for every element x & y in S, x@y is also
an element of S
• A set S is closed under the operation @ if
for every element x S & y S, x@y S
• Examples
48
Semantics: Assigning Meaning to Strings
50
Uniqueness???
Hand
•Give me a hand.
•I smashed my hand in the door.
•Please hand me that book.
51
Uniqueness in CS???
• int x = 4; x++;
• int x = 4; ++x;
• int x = 4; x = x + 1;
• int x = 4; x=x - -1;
• int x = 5;
52
Semantic Interpretation
Functions
For formal languages:
• Programming languages
• Network protocol languages
• Database query languages
• HTML
• BNF
For other kinds of “natural” languages:
• DNA
Other computing
• Genetic algorithm solutions
• Other problem solutions 53