Before a machine can read text we must say exactly which symbols it may see and which sequences of symbols count as “words”. Formal languages give us this precise vocabulary. It is the same vocabulary used in compilers, network protocols and data validation.
Intuition
Think of the keys on a keyboard as the alphabet, anything you can type as a string, and the set of valid passwords for a website as a language. The rules “at least 8 characters, at least one digit” decide which strings belong to the language.
Definition 2.1 · Alphabet, string, language
An alphabet Σ \Sigma Σ is a finite, non-empty set of symbols (also called letters). A string (or word) over Σ \Sigma Σ is a finite sequence of symbols from Σ \Sigma Σ . The length ∣ w ∣ |w| ∣ w ∣ of a string w w w is the number of symbols in it. The null string λ \lambda λ is the string of length 0. A language over Σ \Sigma Σ is any set of strings over Σ \Sigma Σ .
Note
IGNOU writes the null string as ∧ \wedge ∧ ; Rosen writes λ \lambda λ ; many other books write ε \varepsilon ε . They all mean the empty string. Note that the null string λ \lambda λ is a string, while ∅ \emptyset ∅ is a language with no strings, and { λ } \{\lambda\} { λ } is a language with one string.
Examples of languages over Σ = { z } \Sigma = \{z\} Σ = { z }
Language Description Contains λ \lambda λ ? L 1 = { z n ∣ n ≥ 1 } = { z , z z , z z z , … } L_1 = \{z^n \mid n \geq 1\} = \{z, zz, zzz, \dots\} L 1 = { z n ∣ n ≥ 1 } = { z , z z , z z z , … } at least one z z z no L 2 = { z 2 n + 1 ∣ n ≥ 0 } = { z , z z z , … } L_2 = \{z^{2n+1} \mid n \geq 0\} = \{z, zzz, \dots\} L 2 = { z 2 n + 1 ∣ n ≥ 0 } = { z , z z z , … } odd length no L 3 = { z 2 n ∣ n ≥ 0 } = { λ , z z , z z z z , … } L_3 = \{z^{2n} \mid n \geq 0\} = \{\lambda, zz, zzzz, \dots\} L 3 = { z 2 n ∣ n ≥ 0 } = { λ , z z , z z z z , … } even length yes L 4 = { z p ∣ p prime } L_4 = \{z^p \mid p \text{ prime}\} L 4 = { z p ∣ p prime } prime length no
Here z n z^n z n means z z z written n n n times. Even a one-letter alphabet has infinitely many different languages.
Operations on strings
Definition 2.2 · Concatenation and reverse
The concatenation u v uv uv of strings u u u and v v v is u u u followed by v v v ; ∣ u v ∣ = ∣ u ∣ + ∣ v ∣ |uv| = |u| + |v| ∣ uv ∣ = ∣ u ∣ + ∣ v ∣ and u λ = λ u = u u\lambda = \lambda u = u u λ = λ u = u . The reverse of w = a 1 a 2 … a n w = a_1 a_2 \dots a_n w = a 1 a 2 … a n is w R = a n … a 2 a 1 w^R = a_n \dots a_2 a_1 w R = a n … a 2 a 1 . A palindrome is a string with w R = w w^R = w w R = w .
For u = a b u = ab u = ab and v = b a a v = baa v = baa : u v = a b b a a uv = abbaa uv = abbaa and v u = b a a a b vu = baaab v u = baaab . So concatenation is usually not commutative (although z m z n = z n z m = z m + n z^m z^n = z^n z^m = z^{m+n} z m z n = z n z m = z m + n over a one-letter alphabet).
A language need not be closed under concatenation: in L 2 L_2 L 2 (odd lengths), z z z ⋅ z z z z z = z 8 zzz \cdot zzzzz = z^8 z z z ⋅ z z z z z = z 8 has even length, so it is not in L 2 L_2 L 2 . In L 1 L_1 L 1 and L 3 L_3 L 3 , the concatenation of two words is again a word of the language.
Example 2.3 Palindromes and concatenation
Goal. Over Σ = { a , b } \Sigma = \{a, b\} Σ = { a , b } , check whether the concatenation of two palindromes is always a palindrome.
Step. List some palindromes: λ , a , b , a a , b b , a b a , b a b , a b b a , … \lambda, a, b, aa, bb, aba, bab, abba, \dots λ , a , b , aa , bb , aba , bab , abba , …
Step. Try u = a b a u = aba u = aba and v = b v = b v = b , both palindromes: u v = a b a b uv = abab uv = abab .
Step. Reverse: ( a b a b ) R = b a b a ≠ a b a b (abab)^R = baba \neq abab ( abab ) R = baba = abab , so u v uv uv is not a palindrome.
Step. Sometimes it works: u = a b a u = aba u = aba , v = a b a v = aba v = aba gives a b a a b a abaaba abaaba , which is a palindrome.
Result. The concatenation of two palindromes may or may not be a palindrome; a b a ⋅ b = a b a b aba \cdot b = abab aba ⋅ b = abab is a counterexample.
Correction · Palindrome example uses a letter outside the alphabet
Study material says: In §2.2.1, for PALINDROME over Σ = { a , b } \Sigma = \{a, b\} Σ = { a , b } , the example takes u = a b b a u = abba u = abba and v = a b b c b a v = abbcba v = abb c ba .
Correct version: v = a b b c b a v = abbcba v = abb c ba contains the letter c c c , which is not in Σ = { a , b } \Sigma = \{a, b\} Σ = { a , b } , and it is not a palindrome (a b b c b a abbcba abb c ba reversed is a b c b b a abcbba ab c bba ). A correct example is u = a b a u = aba u = aba , v = b v = b v = b : both are palindromes over { a , b } \{a, b\} { a , b } , but u v = a b a b uv = abab uv = abab is not.
Why: The example must use two words of the language PALINDROME over { a , b } \{a, b\} { a , b } ; otherwise it says nothing about that language.
Common mistake
Confusing λ \lambda λ , ∅ \emptyset ∅ and { λ } \{\lambda\} { λ } . λ \lambda λ is a string of length 0; ∅ \emptyset ∅ is a language with no strings; { λ } \{\lambda\} { λ } is a language containing exactly one string, the empty one.
Common mistake
Assuming concatenation is commutative. a b ⋅ b a = a b b a ab \cdot ba = abba ab ⋅ ba = abba but b a ⋅ a b = b a a b ba \cdot ab = baab ba ⋅ ab = baab . Order matters except for special cases like one-letter alphabets.
Key idea
A language is simply a set of strings over a finite alphabet; strings are combined by concatenation, which adds lengths but is usually not commutative.
TEE Exam note
Short questions: “Define alphabet, string, language, null string” (one line each with an example) and Ex.1: for u = a b a b b u = ababb u = ababb , v = b a a v = baa v = baa : u v = a b a b b b a a uv = ababbbaa uv = ababbbaa , v u = b a a a b a b b vu = baaababb v u = baaababb , u u = a b a b b a b a b b uu = ababbababb uu = ababbababb , v v = b a a b a a vv = baabaa v v = baabaa , u u v = a b a b b a b a b b b a a uuv = ababbababbbaa uuv = ababbababbbaa . Always count lengths as a check: ∣ u u v ∣ = 5 + 5 + 3 = 13 |uuv| = 5 + 5 + 3 = 13 ∣ uuv ∣ = 5 + 5 + 3 = 13 .
Key takeaways
Alphabet: finite set of symbols; string: finite sequence of symbols; language: set of strings.
λ \lambda λ (IGNOU: ∧ \wedge ∧ ) is the null string, of length 0.
∣ u v ∣ = ∣ u ∣ + ∣ v ∣ |uv| = |u| + |v| ∣ uv ∣ = ∣ u ∣ + ∣ v ∣ ; concatenation is not commutative in general.
A language may or may not be closed under concatenation.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice Which of the following is a language over Σ = { 0 , 1 } \Sigma = \{0, 1\} Σ = { 0 , 1 } ?
A { 0 , 1 , 2 } \{0, 1, 2\} { 0 , 1 , 2 } B { λ , 01 , 0011 } \{\lambda, 01, 0011\} { λ , 01 , 0011 } C { a , b } \{a, b\} { a , b } D 0101 0101 0101 Check answer
Show answer Answer: B. { λ , 01 , 0011 } \{\lambda, 01, 0011\} { λ , 01 , 0011 }
A language is a set of strings over Σ \Sigma Σ ; all three strings use only 0 and 1. Option 4 is a single string, not a set of strings.
Question 2 · multiple choice If u = a b b u = abb u = abb and v = b a v = ba v = ba , what is v u vu v u ?
Check answer
Show answer Answer: B. b a a b b baabb baabb
v u vu v u is v v v followed by u u u : b a ⋅ a b b = b a a b b ba \cdot abb = baabb ba ⋅ abb = baabb . Option 1 is u v uv uv .
Question 3 · multiple choice What is the length of λ ⋅ a b ⋅ λ ⋅ c \lambda \cdot ab \cdot \lambda \cdot c λ ⋅ ab ⋅ λ ⋅ c ?
A 5 B 4 C 3 D 0
Check answer
Show answer Answer: C. 3
λ \lambda λ has length 0, so the string is a b c abc ab c of length 3. Option 1 counts each λ \lambda λ as one symbol.
Question 4 · multiple choice Which language over { z } \{z\} { z } is NOT closed under concatenation?
Check answer
Show answer Answer: D. { z 2 n + 1 ∣ n ≥ 0 } \{z^{2n+1} \mid n \geq 0\} { z 2 n + 1 ∣ n ≥ 0 }
odd + odd = even, so z ⋅ z = z z z \cdot z = zz z ⋅ z = z z leaves the language. Option 2 is closed: even + even is even.
Question 5 · multiple choice Which string over { a , b } \{a, b\} { a , b } is a palindrome?
Check answer
Show answer Answer: A. a b b a a b b a abbaabba abbaabba
Reading a b b a a b b a abbaabba abbaabba backwards gives the same string. a b a b abab abab reversed is b a b a baba baba .
Question 6 · short answer
Over Σ = { a , b } \Sigma = \{a, b\} Σ = { a , b } , list all strings of length 2, and say how many strings of length n n n there are.
Show worked answer Length 2: a a , a b , b a , b b aa, ab, ba, bb aa , ab , ba , bb — 4 strings. For each of the n n n positions there are 2 choices of letter, so there are 2 n 2^n 2 n strings of length n n n (in general ∣ Σ ∣ n |\Sigma|^n ∣Σ ∣ n ).