Basics of Formal Languages and Computability By Vijay...
Transcript of Basics of Formal Languages and Computability By Vijay...
![Page 1: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/1.jpg)
Basics of Formal Languages and Computability
By
Vijay Ganesh
![Page 2: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/2.jpg)
Last Class: The Structure (Stages/Phases) of a Compiler
Code Generation
Code Optimizations
Output Program
Semantic Analysis (Type Checking,…)
Lexical Analyzer
Parser Input Program
Tokens AST
Intermediate Representation (IR)
Three Address Format
Costa Busch - LSU
![Page 3: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/3.jpg)
• Formal Languages
• Computability • Concept of Undecidability
• Undecidability of program analysis
• Chomsky Hierarchy
Today’s Lecture
Costa Busch - LSU
![Page 4: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/4.jpg)
Language: a possibly infinite set of finite-length strings over a finite alphabet
String: a sequence (concatenation) of symbols
from the alphabet of the language Alphabet: Finite set of symbols/characters, e.g., Σ = {a,b,…,z} or {0,1}
Example:
Alphabet: Σ = {a,b} Strings: ε, a, b, aaa, ababa, abbb,….
We represent the set of all strings over Σ as Σ* Language: A subset of strings of Σ*
Formal Languages
Costa Busch - LSU
![Page 5: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/5.jpg)
• The language over unary alphabet {a}: {ε, a, aa, aaa,…}
• Finite Languages: The cardinality of such language is a finite number, e.g., The set of all numbers less than 100
• Most languages we study have infinite cardinality: e.g., the set of even numbers
• We will study classes of formal languages such as regular, context-free and context-sensitive languages that are crucial for understanding compiler construction
More Examples of Formal Languages
Costa Busch - LSU
![Page 6: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/6.jpg)
Consider a binary alphabet Σ = {a,b}
Typical Operations on Strings
Operation name Properties Example
Concatenation(s1,s2) Non-commutative, associative
a.b ≠b.a a.(b.c) = (a.b).c ε.a = a. ε = a
Prefix(s1) (Similarly define suffix)
Returns the prefix ab is a prefix of abba ba is a suffix of abba
Reverse(s1) Reverse(Reverse(S)) = S Reverse of ab is ba
Length(s1) Computes the number of chars in a string
Length(ab) =2 Length(ε) = 0
Costa Busch - LSU
![Page 7: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/7.jpg)
Typical Operations on Languages
Operation name Properties Representation
Union(L1, L2) Set union of the corresponding sets of strings
L1 U L2
Intersection(L1, L2) Set intersection of the corresponding sets of strings
L1 L2
Difference(L1,L2) Set difference of the corresponding sets of strings
L1 – L2
Complement(L1) Set complement of L1 Σ* - L1
Costa Busch - LSU
![Page 8: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/8.jpg)
Typical Operations on Languages
Operation name Properties Representation
Reverse(L) Reverse all strings in L {a, aab, abab}R = {a, baa, baba}
Concatenation(L1,L2) The set of strings which has a prefix from L1 and a suffix from L2
L1 = {a, aa,aaa,…} L2 = {b,bb,bbb,…} L1.L2 = {ab,abb,…aab,…}
Kleene Star(L) denoted as L*
The union of concatenations of strings in L1
(L)0 U (L)1 U (L)2 …
Complement(L) Set complement of L1 Σ* - L1
Costa Busch - LSU
![Page 9: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/9.jpg)
More on Reverse of a Language Definition: More Examples:
}:{ LwwL RR ∈=
}0:{
}0:{
≥=
≥=
nabL
nbaL
nnR
nn
Costa Busch - LSU
![Page 10: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/10.jpg)
More on Concatenation of Two Languages Definition: Example:
{ }2121 ,: LyLxxyLL ∈∈=
{ }{ }
{ }baaabababaaabbaaaab
aabbaaba
,,,,,
,,,
=
Costa Busch - LSU
![Page 11: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/11.jpg)
N-ary Concatenation of a Language Definition: Special case:
n
n LLLL =
{ } { }{ }{ }{ }bbbbbababbaaabbabaaabaaa
babababa,,,,,,,
,,,, 3 ==
{ }
{ } { }λ
λ
=
=
0
0
,, aaabbaa
L
Costa Busch - LSU
![Page 12: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/12.jpg)
}0:{ ≥= nbaL nn
}0,:{2 ≥= mnbabaL mmnn
2Laabbaaabbb∈
Example
Costa Busch - LSU
![Page 13: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/13.jpg)
Star-Closure (Kleene *) All strings that can be constructed from Definition: Example:
210* LLLL =
{ }
⎪⎪⎭
⎪⎪⎬
⎫
⎪⎪⎩
⎪⎪⎨
⎧
=
…,,,,,,,,
,,,
*,
abbbbabbaaabbaaabbbbbbaabbaa
bbabba
λ
L
Costa Busch - LSU
![Page 14: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/14.jpg)
Positive Closure Definition: 21 LLL =+
{ }⎪⎭
⎪⎬
⎫
⎪⎩
⎪⎨
⎧=+
…,,,,,,,,
,,,
abbbbabbaaabbaaabbbbbbaabbaa
bbabba
Costa Busch - LSU
![Page 15: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/15.jpg)
The * Operation on alphabets : the set of all possible strings from alphabet
*ΣΣ
{ }{ }…,,,,,,,,,*,
aabaaabbbaabaababaλ=Σ
=Σ
Costa Busch - LSU
![Page 16: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/16.jpg)
The + Operation on alphabets : the set of all possible strings from alphabet except
+ΣΣ
{ }{ }…,,,,,,,,,*,
aabaaabbbaabaababaλ=Σ
=Σ
λ−Σ=Σ+ *{ }…,,,,,,,, aabaaabbbaabaaba=Σ+
λ
Costa Busch - LSU
![Page 17: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/17.jpg)
{ }{ }
5|},,,,{|3|,,|
1||0|{}|
=
=
=
=
babaabbabbaaabaaa
λ
λ
Two special languages
Size of a language (number of elements):
}{ }{λ
Empty language Language with empty string
Costa Busch - LSU
![Page 18: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/18.jpg)
Languages are used to describe computational problems: For example, consider the PRIMES problem is “given a natural number X decide whether X is a PRIME?” • Very famous problem that was recently
shown to be in the complexity class P.
Connection between Formal Languages and Computation
Costa Busch - LSU
![Page 19: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/19.jpg)
All Computational problems can be described as Language or Set membership problems, i.e., “given a string X, and a language S, does there exist an algorithm to decide whether X belong to S?” The PRIMES problem can be equivalently written as a set membership problem over the language of strings over digits:
• Alphabet: {0,1,2,…,9} • Example Strings: 0, 100,.. • Language of PRIMES: {2,3,5,7,…}
Question: How can the compilation problem be described using the paradigm of language membership?
Connection between Formal Languages and Computation
Costa Busch - LSU
![Page 20: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/20.jpg)
Problem S: Computational problems can be described as Language or Set membership problems, i.e., “given a string X, and a language S, does there exist an algorithm/function/method to decide whether X belongs to S?” (Side Note: We can always reduce optimization problems to membership/decision problems.)
Solution to S: The algorithm C that takes as input any X and correctly decides whether X belongs to S is said to be a solution to the problem S. However, we need to be a bit more precise about what we mean by an algorithm C, and the possible outputs C can produce for a problem S.
Languages, Problems and Their Solutions
Costa Busch - LSU
![Page 21: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/21.jpg)
Languages and Turing Machines
Enter Alan Turing: • Proposes the Turing Machine (TM) • Establishes the existence of an Universal Turing Machine • Any computable function (or method or algorithm) can be
implemented as a “program” on TM
Solution to S in terms of Turing Machines: We say computational problem S has a solution if there exists a Turing Machine P that correctly decides for any string X whether X belongs to S. We need one some more precision in our description of P: • P can correctly say X in S and HALT • P can correctly say X is not in S and HALT • P can loop forever • (P can also have “bugs” and produce incorrect results. We will ignore
this case for now.) Costa Busch - LSU
![Page 22: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/22.jpg)
Decidability of Languages
Definition of Turing-acceptable languages: We say a language L is Turing-Acceptable if there exists a Turing Machine P that given any w determines if w belongs to L. We say P accepts L.
More precisely, for any string :
Lw∈ P halts in an accept state
Lw∉ P halts in a non-accept state or does not terminate
Costa Busch - LSU
![Page 23: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/23.jpg)
Decidability of Languages
Definition of decidable languages: We say a language L is decidable if there exists a Turing Machine P that given any w determines if w belongs to L or not.
More precisely, for any string :
Lw∈ P halts in an accept state
Lw∉ P halts in a non-accept state
Side Note: Languages that are not decidable are called undecidable.
Costa Busch - LSU
![Page 24: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/24.jpg)
Connect between Turing-acceptable and decidable languages
Theorem: Every decidable language is Turing-acceptable. However, not every Turing-acceptable language is decidable. For example, the halting problem. (Side note: Turing-acceptable languages are also called recursively-enumerable.)
Costa Busch - LSU
![Page 25: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/25.jpg)
Decidable
Turing-Acceptable
Non Turing-Acceptable L
L
Costa Busch - LSU
![Page 26: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/26.jpg)
A Turing Machine
...... ...... Tape
Read-Write head Control Unit
Costa Busch - LSU
![Page 27: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/27.jpg)
The Tape
...... ......
Read-Write head
No boundaries -- infinite length
The head moves Left or Right
Costa Busch - LSU
![Page 28: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/28.jpg)
...... ......
Read-Write head
The head at each transition (time step): 1. Reads a symbol 2. Writes a symbol 3. Moves Left or Right
Costa Busch - LSU
![Page 29: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/29.jpg)
...... ......
Example: Time 0
...... ...... Time 1
1. Reads 2. Writes
a a cb
a b k c
ak
3. Moves Left Costa Busch - LSU
![Page 30: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/30.jpg)
...... ...... Time 1
a b k c
...... ...... Time 2 a k cf
1. Reads 2. Writes
bf
3. Moves Right Costa Busch - LSU
![Page 31: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/31.jpg)
The Input String
...... ...... ◊ ◊ ◊ ◊
Blank symbol
head
◊a b ca
Head starts at the leftmost position of the input string
Input string
Costa Busch - LSU
![Page 32: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/32.jpg)
States & Transitions
1q 2qLba ,→
Read Write Move Left
1q 2qRba ,→
Move Right
Costa Busch - LSU
![Page 33: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/33.jpg)
Example:
1q 2qRba ,→
...... ...... ◊ ◊ ◊ ◊◊a b caTime 1
1qcurrent state
Costa Busch - LSU
![Page 34: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/34.jpg)
...... ...... ◊ ◊ ◊ ◊◊a b caTime 1
1q 2qRba ,→
...... ...... ◊ ◊ ◊ ◊◊a b cbTime 2
1q
2q
Costa Busch - LSU
![Page 35: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/35.jpg)
...... ...... ◊ ◊ ◊ ◊◊a b caTime 1
1q 2qLba ,→
...... ...... ◊ ◊ ◊ ◊◊a b cbTime 2
1q
2q
Example:
Costa Busch - LSU
![Page 36: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/36.jpg)
...... ...... ◊ ◊ ◊ ◊◊a b caTime 1
1q 2qRg,→◊
...... ...... ◊ ◊ ◊ ◊ga b cbTime 2
1q
2q
Example:
Costa Busch - LSU
![Page 37: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/37.jpg)
Determinism
1q
2qRba ,→
Allowed Not Allowed
3qLdb ,→
1q
2qRba ,→
3qLda ,→
No lambda transitions allowed
Turing Machines as originally defined are deterministic
Costa Busch - LSU
![Page 38: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/38.jpg)
Partial Transition Function
1q
2qRba ,→
3qLdb ,→
...... ...... ◊ ◊ ◊ ◊◊a b ca
1q
Example:
No transition for input symbol c
Allowed:
Costa Busch - LSU
![Page 39: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/39.jpg)
Halting
The machine halts in a state if there is no transition to follow or out of the state
Costa Busch - LSU
![Page 40: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/40.jpg)
Halting Example 1:
...... ...... ◊ ◊ ◊ ◊◊a b ca
1q
1q No transition from
HALT!!! 1q
Costa Busch - LSU
![Page 41: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/41.jpg)
Halting Example 2:
...... ...... ◊ ◊ ◊ ◊◊a b ca
1q
1q
2qRba ,→
3qLdb ,→
No possible transition from and symbol
HALT!!! 1q c
Costa Busch - LSU
![Page 42: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/42.jpg)
Accepting States
1q 2q Allowed
1q 2q
• The machine accepts and halts
Costa Busch - LSU
Not Allowed
![Page 43: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/43.jpg)
Acceptance
Accept Input If machine halts in an accept state
Reject Input
If machine halts in a non-accept state or If machine enters an infinite loop
string
string
Costa Busch - LSU
![Page 44: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/44.jpg)
Recall that: A language is decidable, if there is a Turing machine (decider) that accepts the language and halts on every input string
AM
A
Turing Machine
Input string
Accept
Reject
M
Decider for A
Decision On Halt:
Decidable Languages
YES
NO
Costa Busch - LSU
![Page 45: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/45.jpg)
Recall: A computational problem is decidable if the corresponding language is decidable
We also say that the problem is solvable
Costa Busch - LSU
![Page 46: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/46.jpg)
Halting Problem
Input: • Turing Machine M• String w
Question: Does halt while processing input string ?
Mw
Corresponding language:
} string input on halts that machine Turing a is :,{
wMwMHALTTM =
Costa Busch - LSU
![Page 47: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/47.jpg)
Theorem: (The halting problem is unsolvable)
Proof:
Suppose that is decidable; we will prove that every Turing-acceptable language is also decidable
TMHALT is undecidable
Basic idea:
A contradiction!
TMHALT
Costa Busch - LSU
![Page 48: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/48.jpg)
Theorem:
Proof:
Assume for contradiction that the halting problem is decidable;
(The halting problem is unsolvable) TMHALT is undecidable
we will obtain a contradiction using a diagonilization technique
The Diagonalization proof
Basic idea:
Costa Busch - LSU
![Page 49: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/49.jpg)
HM
w
YES M halts on w
M doesn’t halt on
wNO
Suppose that is decidable TMHALT
Decider for TMHALTwM,
Input string
Costa Busch - LSU
![Page 50: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/50.jpg)
HwM, 0q
acceptq
rejectq
Input string: YES
NO
Looking inside H
Decider for TMHALT
M halts on ? w
Costa Busch - LSU
![Page 51: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/51.jpg)
H0q
NO
aq bq
H ʹ′ Loop forever YES
wM,
Construct machine : H ʹ′
If halts on input Then Loop Forever Else Halt
M w
M halts on ? wacceptq
rejectq
Costa Busch - LSU
![Page 52: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/52.jpg)
Construct machine :
MM,Copy on tape H ʹ′
F
If halts on input MThen loop forever Else halt
M M
F
Costa Busch - LSU
F
![Page 53: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/53.jpg)
Run with input itself
Copy <F> on tape H ʹ′
If (F halts on input <F>) Then F loops forever on input <F>
Else (F does not halt <F>) F halts on input <F>
FF
END OF PROOF CONTRADICTION!!!
<F> <F,F>
Costa Busch - LSU
![Page 54: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/54.jpg)
We have shown:
Decidable
Undecidable TMHALT
Costa Busch - LSU
![Page 55: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/55.jpg)
We can actually show:
Decidable
Turing-Acceptable TMHALT
Costa Busch - LSU
![Page 56: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/56.jpg)
Turing machine that accepts :
wM,
1. Run on input
2. If halts on then accept
M w
M wwM,
TMHALT
is Turing-Acceptable TMHALT
Costa Busch - LSU
![Page 57: Basics of Formal Languages and Computability By Vijay Ganeshvganesh/TEACHING/S2014/ECE351/lecture… · Analysis (Type Checking,…) Lexical Analyzer Input Parser Program Tokens AST](https://reader033.fdocuments.net/reader033/viewer/2022060307/5f09b6b27e708231d4282973/html5/thumbnails/57.jpg)
• We can show that static analysis is Turing-acceptable, And consequently so is code optimization in general. • Informal Proof by reduction:
Let say that static analysis is decidable. It immediately follows that we can easily analyze any program and determines if it halts on an input. If so we have solved the halting problem. CONTRADICTION!!
Static Program Analysis is Undecidable
Costa Busch - LSU