





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






In the field of computer science, the word “language” has a precise and easily-stated definition that is extremely broad, encompassing everything from familiar programming languages to very abstract problems of computer science. Simple languages also show up in numerous familiar contexts such as word processors, text editors, and search engines.
To distinguish the mathematically-defined languages that we will study in the context of computer science from those we employ in communicating to fellow humans, we refer to the former as formal languages and the latter as natural languages.
This chapter introduces the basic concepts needed to study formal languages, which are used to frame many problems of computer science. As we shall see, many fundamental problems can be viewed as language recognition problems. In fact, our first view of automata, or abstract machines, shall be as language recognizers. The books by Lewis and Papadimitiou [17], Sipser [21], Sudkamp [23], Howie [11], and Linz [18] are recommended for further reading on these topics. We start by defining alphabets and strings, which will be the building blocks of formal languages.
An alphabet is a non-empty finite set whose elements are referred to as letters, symbols, or characters. We will typically denote an alphabet by the symbol Σ, or by explicitly listing its letters as elements of a set, such as {a, b, c}. A string is a finite (possibly zero-length) sequence of symbols over some alphabet, denoted by juxtaposing the symbols in the sequence. For instance, aardvark is a string over the standard 26-letter alphabet used in the west, and ab#aa is a string over any alphabet containing the symbols “a”, “b”, and “#.”
If u and v both denote strings, then u ◦ v denotes the concatenation of u and v, which is the string obtained by juxtaposing the letters of u and the letters of v. If u = abc, and v = xyz, then u ◦ v is the string abcxyz. We will frequently omit the concatenation operator “◦” and simply write uv for
the concatenation of strings u and v, which is the same convention commonly employed for denoting multiplicative operations. As a special case of string concatenation, we will often write “au” to denote the letter a, viewed as a one-letter string, concatenated with the string u.
If Σ is an alphabet and w is a string over Σ, then |w| denotes the total number of letters in w. A useful extension of this notation is |w|x, by which we shall mean the number of occurrences of the letter x in the string w. Hence, |abbaa|a = 3. By w[i] we mean the ith symbol of w, with w[1] being the first letter of the string, and by wR^ we mean the string consisting of the same sequence of symbols as w, but in reverse order. We could define wR^ to be the string such that
wR[k] = w[ |w| − k + 1 ],
for k = 1, 2 ,... , |w|. If a string v consists of a contiguous sequence of the symbols found in another string w, then we refer to v as a substring of w. That is, if for some k ≥ 0 and m ≥ 0, v[i] = w[i + k] for i = 1, 2 ,... , m, then v is a substring of w. The empty string is the string consisting of zero symbols. We denote the empty string by ε, which we shall always assume is a symbol that is distinct from any other symbol in the alphabet under consideration. If Σ = {a, b}, then ε, a, aaaa, and abbaab are all strings over Σ. For any string w, both ε and w are always considered to be substrings.
A symbol or a string with a natural number exponent, written as an, denotes the string consisting of n concatenated copies of a; this operation is referred to as iterated concatenation. By definition, a^0 = ε, the empty string. Let Σ denote any alphabet. Then Σ∗^ is the Kleene Star of Σ, which is the set of all strings over Σ. That is,
Σ∗^ def = {c 1 c 2 · · · ck | k ∈ N, ci ∈ Σ for each i}. (4.1)
As special cases, observe that Σ∗^ always contains the empty string, ε, all the singleton strings, which are simply the symbols of Σ interpreted as strings of length one, and all the singletons raised to all positive integer powers. If Σ = {a, b}, then
Σ = {ε, a, b, aa, ab, ba, bb, aaa, aab, aba,.. .}. (4.2)
It is easy to see that Σ∗^ is a countably infinite set for any alphabet Σ. One way to see this is to observe that
Σ∗^ = Σ 0 ∪ Σ 1 ∪ Σ 2 ∪ · · ·
where Σk consists of all strings over Σ that consist of exactly k symbols. Since Σk is finite, and therefore countable, for all k, Σ∗^ is the countable union of countable sets. It follows that Σ∗^ is countable for any alphabet Σ.
Both string reversal and iterated concatenation can be conveniently defined using inductive defini- tions, as shown below.
Definition 9 For any w ∈ Σ∗, the reversal wR^ is defined inductively by
εR^ = ε, (au)R^ = uRa,
where a ∈ Σ and u ∈ Σ∗^ are such that w = au.
where the entries of the set are shown in lexicographic order with respect the the obvious alphabetical ordering of the letters in Σ. As we have defined it, lexicographic ordering is not the same as standard “dictionary” ordering. In a dictionary the word “aardvark” is listed before the word “emu.” However, “emu” is first lexicographically because it has fewer letters. This is an important difference when dealing with infinite collections of strings, since dictionary ordering of the set {a, b}∗^ would place all elements of the set {ε, a, aa, aaa, aaaa,.. .} before the first string containing a “b,” which fails to define a one-to-one correspondence between {a, b}∗^ and { 0 , 1 , 2 ,.. .}.
A language (over a given alphabet) is simply a set of strings; the set may be finite, infinite, or even empty. That is, if Σ is an alphabet, then every subset L ⊆ Σ∗^ is a language over Σ. The elements of any language can be put into lexicographic order in the same way that Σ∗^ can be ordered, as described above. The following sets are examples of languages over the alphabet {a, b}.
L 1 = { a, abb, aaaa} L 2 = { an^ | n ∈ N is prime} L 3 = { bnanbm^ | n, m ∈ N and n = m mod 3 } L 4 = The set of all w ∈ Σ∗^ with at most three a’s L 5 = { an^ | n ∈ N and ∃ x, y, z ∈ N − { 0 } such that xn^ + yn^ = zn}
The languages L 1 and L 5 are finite sets, whereas L 2 , L 3 , and L 4 are countably infinite. Although language L 5 is today known to be {a, aa}, because Fermat’s last theorem has been proven, it illustrates how the definition of a language (even over a trivial alphabet) can encode the answers to deep questions of number theory.
Since languages are sets, one can define new languages with set operations. For example, given any languages L 1 and L 2 over an alphabet Σ, the sets L 1 ∪ L 2 and L 1 ∩ L 2 are also languages over Σ. Another means of defining a new language from a given one is with the Kleene star operator. If L is a language over Σ, then L∗^ is the set of all strings formed by concatenating (joining together) a finite number of strings from L. That is,
L∗^ = {w 1 w 2 · · · wk | k ∈ N and wi ∈ L for i = 1, 2 ,... k} ,
where string concatenation is denoted by juxtaposing the symbols representing strings. Notice that the definition of Σ∗^ above is consistent with the definition of the Kleene star operator; it corresponds to the language obtained by taking the Kleene star of the language consisting of all strings of length one.
A language consisting of an infinite number of strings may be referred to as an infinite language if it is important to emphasize the cardinality of the language. A language consisting of a finite number of strings may similarly be referred to as a finite language. Observe that so long as a language L is non-empty (i.e. it contains at least one string), then L∗^ is an infinite language.
Using the concept of set cardinality we can place some very general limits on what can be expressed through symbolic representations, whether they be mathematical formulas, sentences in a natural
language such as English or French, or computer programs written in a programming language such as C, Lisp, or Java.
The languages L 1 ,... , L 5 in section 4.2 are all described using a finite number of symbols, such as “{”, “}”, “n”, and “m”, in addition to the symbols of the original alphabet {a, b}. Since any such description must be of finite length, we can view the definitions themselves as being strings in a meta-language; in this case a language that is a superset of both English and formal mathematics. This raises an interesting question: Are all languages (over a given alphabet) defined by some string in a suitable meta-language? We can answer this question negatively using only the tools of set theory.
Let M denote the alphabet of the meta-language and observe that the set M ∗^ of all strings in this language is countably infinite. While the set of all strings over Σ is also countably infinite, the set of all languages over Σ has the cardinality of 2Σ
∗ , which is uncountable. Since |M ∗| <
∣, it
follows that there cannot be a surjection from M ∗, or any other language over M , onto the set of all languages over Σ; there simply are not enough strings in the meta-language to go around. We must conclude that, regardless of the meta-language used, there must exist languages over Σ that have no finite description.
Since languages are sets, all the the traditional set operations can be used to form new languages from existing languages. For example, if L and L′^ are languages over a common alphabet Σ, then so are the sets L ∪ L′, L ∩ L′, L − L′, L∗, L, and L ◦ L′, where
L − L′^ def = {w ∈ L | w 6 ∈ L′} L def = Σ∗^ − L L∗^ def = {w 1 w 2 · · · wn | n ∈ N, wi ∈ L} L ◦ L′^ def = {uv | u ∈ L, v ∈ L′} LR^ def =
wR^ | w ∈ L
The operations denoted by “∪”, “∩”, “−”, and the bar are simply the standard set operations of union, intersection, difference, and complementation, respectively. The sets denoted by L∗^ and L ◦ L′^ are called the Kleene star and the concatenation, respectively. The Kleene star operator creates a new set of strings by concatenating every finite sequence of original strings; by definition, this includes the empty string ε, which is the concatenation of zero strings. The Kleene star operator applied to an alphabet, Σ∗, as defined earlier, is simply a special case of the operator defined here; that is, we can view Σ as the set of all singleton strings over Σ.
Let Σ = { 0 , 1 , a, b}, and let L 1 and L 2 be the following trivial languages over Σ:
L 1 = { 00 , 1 a, a, aa} L 2 = {a, ab, a 01 }
then
L 1 − L 2 = { 00 , 1 a, aa} L 2 ∗^ = {ε, a, aa, aaa, ab, aab, abab, aba, aa 01 , ab 01 ,.. .} L 1 ◦ L 2 = { 00 a, 00 ab, 00 a 01 , 1 aa, 1 aab, 1 aa 01 ,.. .} L 1 = { 0 , 1 , b, 11 , bb, 01 , 0 a, 0 b, 10 , 11 , 1 b, 000 ,.. .}.
regular expressions for encoding languages over any given alphabet Σ, we first introduce an expanded alphabet:
Σ̂ def = Σ ∪ { “(”, “)”, “∗”, “|”, “∅” }
This alphabet extends the original alphabet by adding several special symbols, which we shall assume are distinct from the symbols in Σ. Next, we give an inductive definition of the regular expressions, which form a language RΣ over the extended alphabet Σ. These rules precisely define thê form, or syntax, of the regular expressions.
Moreover, only strings that can be constructed by the applications of these rules are in RΣ. Since the strings of any language are of finite length (by definition), each element of RΣ must result from the application of only a finite number of these rules. We now define a function
R : RΣ → 2 Σ
∗
that associates each string in RΣ with a (possibly infinite) language over Σ. That is,
R( a regular expression over Σ ) =̂ the corresponding regular language over Σ.
Hence, the frunction R provides the meaning, or semantics, of each regular expression by mapping it to the language it represents. We define the function R inductively with the following collection of rules, which are exactly analogous to the rules used to form the regular expressions themselves. These rules define the meaning, or semantics, of the regular expressions. Here “a” denotes a symbol in Σ, and “u” and “v” denote strings in RΣ.
Note that the arguments to the function R above are to be interpreted as strings over the alphabet Σ, while the objects on the right hand sides are to be interpreted as sets and operations on sets.̂ Thus, in rule 1 above, the “∅” on the left denotes a symbol in Σ, while on the right it denoteŝ the empty set. Similarly, the “∗” on the left denotes a symbol in Σ, while on the right it denoteŝ
the Kleene star operator. It is by virtue of multiple interpretations of the same symbols that the function R provides the semantics of regular expressions; that is, it provides the meaning of certain strings over ̂Σ.
It is possible to interpret any regular expression as a set of strings by directly applying the function R defined above. For example, let Σ = {a, b} and consider the regular expression ((a)∗(b)∗). To interpret this string as a language, we simply apply the rules defining R, starting with a rule that is applicable to the entire string. Thus, we have
R( ((a)∗(b)∗) ) = R( (a)∗^ ) ◦ R( (b)∗^ ) = R( a )∗^ ◦ R( b )∗ = {a}∗^ ◦ {b}∗ = {an^ | n ≥ 0 } ◦ {bn^ | n ≥ 0 } =
anbk^ | n ≥ 0 , k ≥ 0
As a second example, we’ll derive the meaning of the expression (a | ∅∗)(aa | bb)∗. Here we shall drop some of the parentheses where the meaning is clear without them.
R( (a | ∅∗)(aa | bb)∗^ ) = R( a | ∅∗^ ) ◦ R( (aa | bb)∗^ ) = (R(a) ∪ R(∅∗)) ◦ R( aa | bb )∗ = ({a} ∪ ∅∗) ◦ (R(aa) ∪ R(bb))∗ = ({a} ∪ {ε}) ◦ ((R(a) ◦ R(a)) ∪ (R(b) ∪ R(b)))∗ = {a, ε} ◦ (({a} ◦ {a}) ∪ ({b} ∪ {b}))∗ = {a, ε} ◦ ({aa} ∪ {bb})∗ = {a, ε} ◦ {aa, bb}∗ =
akw 1 w 2 · · · wn | k ∈ { 0 , 1 } , n ∈ N, wi ∈ {aa, bb}
Both the definition of regular expressions and the definition of R used parentheses liberally to avoid confusion. However, as we saw above, the parentheses are frequently redundant. This follows from that fact that both union and concatenation operations are associative. Consequently, it is customary to eliminate the unnecessary parentheses, making the following identifications
((ab)c) = abc ((a | b) | c) = a | b | c (a)∗^ = a∗
Of course, not all parentheses are redundant, as the following examples demonstrate.
(a | b)c 6 = a | bc (a | b)∗^6 = a | b∗ (ab)∗^6 = ab∗
We call L ⊆ Σ∗^ a regular language if and only if it can be represented as a regular expression over Σ. Note that both the empty language ∅ and the language consisting of only the empty string, {ε}, are regular languages. (Be sure to understand that these languages are distinct.) The empty language is regular because ∅ is explicitly included in the extended alphabet Σ and given the semantics of̂ the empty set by rule 1 defining R above. The language {ε} is regular because R((∅)∗) = ∅∗^ = {ε}; that is, the empty string is an element of L∗^ for any language L, including the empty language ∅.