Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Fundamentals of Formal Languages

Recommended resources

Welcome to your first lesson in the Theory of Computation! This course explores the fundamental capabilities and limits of computers. To do this, we need a precise, mathematical way to describe computational problems. The framework we'll use throughout this course is built on the idea of formal languages.

This lesson introduces the three foundational concepts that everything else will be built upon: alphabets, strings, and languages. By the end, you'll be able to define these concepts using formal set-theoretic notation, which is the standard language of computer science theory. Your background in software development has given you a practical, intuitive understanding of these ideas—you work with strings, character sets (alphabets), and structured data (like JSON, which can be seen as a language) all the time. Our goal here is to formalize that intuition.

1. Alphabets: The Building Blocks

In any language, whether it's English, Python, or a formal language, we start with a set of basic symbols. In the theory of computation, we call this an alphabet.

An alphabet is a finite, non-empty set of symbols. We conventionally use the uppercase Greek letter Sigma, , to denote an alphabet.

The symbols themselves are considered atomic or indivisible. What they are doesn't matter as much as the fact that they are distinct elements in a set.

Common Examples:

  • The binary alphabet: . This is the fundamental alphabet of all digital computers.
  • The alphabet of lowercase English letters: .
  • The set of all ASCII characters can be an alphabet.
  • A unary alphabet: . While simple, it's useful for illustrating certain theoretical concepts.

For a rigorous, mathematical definition of an alphabet and its properties, please read the following short text.

Alphabet (formal languages)

The article 'Alphabet (formal languages)' on Grokipedia provides a clear, formal definition of what constitutes an alphabet in this context.

Please read the section titled 'Formal Definition'. Focus on the three key properties mentioned: it's a set, it's finite, and it's non-empty.


2. Strings: Sequences of Symbols

Once we have an alphabet, we can create strings by forming finite sequences of symbols from that alphabet. You're already intimately familiar with strings as a data type in programming. The formal definition aligns perfectly with that concept.

A string (or word) over an alphabet is a finite sequence of symbols from .

  • Example: If , then a, b, ab, baba, and aaabbb are all strings over .
  • The Empty String: A special, and very important, string is the empty string, which has zero symbols. It is denoted by (epsilon) or sometimes (lambda). The length of the empty string is 0.
  • String Length: The length of a string , denoted , is the number of symbols in it. For example, if , then . For the empty string, .

To get a more dynamic and example-driven explanation of these concepts, the following video is quite helpful.

STRINGS and LANGUAGES - Theory of Computation

This video from TrevTutor provides a clear, conversational introduction to strings, the empty string, and string length.

Watch the segments from the beginning to 01:36 and from 02:51 to 04:44. The first part covers alphabets and strings, and the second part details string length and the empty string.

Sets of Strings

Using our alphabet , we can define sets of strings of a specific length.

  • is the set of all strings over of length exactly .
  • (the set containing only the empty string).
  • if (the set of all strings of length 1).
  • if .

The most important set of strings is the set of all possible strings of any finite length over an alphabet . This is called the Kleene Star of , denoted . It is the universal set from which we will draw our languages.

A related concept is (pronounced "sigma plus"), which is the set of all non-empty strings.

Therefore, .

The image below provides a great visual summary of these concepts.

Powers of Alphabets
This diagram illustrates how sets of strings of specific lengths (\(\Sigma^k\)) are formed from a binary alphabet. The Kleene star (\(\Sigma^*\)) is then defined as the union of all these sets, representing the set of all possible strings.
Test your understanding!

Let .

  1. What is ?
  2. What is ? (the size of the set )
  3. Is ? Why or why not?
Show answer
  1. . It's the set of all strings of length 2.
  2. . In general, for an alphabet , . Here, .
  3. No, is not in . By definition, contains all strings of length 1 or greater. The empty string has length 0. It is, however, in .

3. Formal Languages: Sets of Strings

Now we arrive at the final and most crucial definition for this lesson. In the context of theory of computation, a "language" is not what we typically think of (like Python or English). Instead, it has a very simple and precise mathematical definition.

A language over an alphabet is any subset of .

That's it. A formal language is simply a set of strings. The strings in the set are said to be "in the language," and the strings not in the set are "not in the language."

Examples of Languages:

Let . The following are all languages over :

  • .
    • Strings in : , 1, 00, 1010, 111.
    • Strings not in : 0, 10, 000.
  • . This is set-builder notation for "the set of strings consisting of zeros followed by ones, for any non-negative integer ".
    • Strings in : (for ), 01 (for ), 0011 (for ), etc.
    • Strings not in : 0, 1, 010, 001.
  • . This is a finite language.
  • The empty language, . This language contains no strings.
  • The language containing only the empty string, . Note that this is different from the empty language!

Your work in AI and development often involves defining or validating specific data formats. For instance, the set of all valid JSON objects is a language over the alphabet of ASCII characters. The set of all valid Bitcoin transaction scripts is a language. This course will give you the tools to reason about the complexity of recognizing such languages.

To solidify these definitions, the following reading provides the formal, set-theoretic definitions for strings and languages.

Chapter 1 Basics of Formal Language Theory

This chapter from Jean Gallier's 'Introduction to the Theory of Computation' provides the precise, mathematical definitions we need.

Please read section 1.2, 'Alphabets, Strings, Languages' (pages 10-18). Start from 'Well, it is now time to be precise!'. Focus on Definition 1.2.2 (String), the definition of Σ*, and Definition 1.2.6 (Language). You can skim the parts on prefixes, suffixes, and ordering for now, as we will revisit them later.

Test your understanding!

Let and consider the language .

  1. Is this language finite or infinite?
  2. List all the strings in the language .
Show answer
  1. The language is finite. The condition limits the strings to lengths 0, 1, and 2. Since the alphabet is finite, there is a finite number of such strings.
  2. The strings in are:
    • Length 0:
    • Length 1: a, b
    • Length 2: aa, ab, ba, bb
      So, .

Conclusion

This lesson established the fundamental vocabulary for our study of computation. Let's recap the key takeaways:

  • An alphabet () is a finite, non-empty set of symbols.
  • A string is a finite sequence of symbols from an alphabet. The set of all possible strings over is denoted .
  • A formal language is any subset of .

This simple, hierarchical structure—alphabets define strings, and sets of strings form languages—is the foundation upon which the entire field is built. A "computational problem" can be framed as a question of language membership: given a language and a string , is in ? The rest of this course is about designing abstract machines (automata) that can answer this question for different kinds of languages and understanding the limits of what such machines can do.

In the next lesson, we will delve deeper into the properties of strings, exploring operations like concatenation and reversal. We will then use mathematical induction to prove some fundamental properties of these operations, giving you your first taste of the formal reasoning that is central to this field.