Why Can't Finite State Machines Match Balanced Parentheses?
The pumping lemma for regular languages proves that finite state machines cannot match balanced parentheses because they lack unbounded memory. By assuming a finite state machine can recognize balanced parentheses, the pumping lemma forces a contradiction where repeating part of an input string creates an unbalanced result. This demonstrates that matching nested structures requires a computational model with more power, such as a pushdown automaton.
Understanding Finite State Machines and Regular Languages
In classical computing, a finite state machine (FSM), or finite automaton, is a mathematical model of computation with a strictly limited number of states. It reads an input string one symbol at a time and transitions between states based on the current symbol. The set of all languages that can be recognized by a finite state machine is called the class of regular languages.
Because an FSM has a fixed, finite number of states, it cannot count arbitrarily high. If an input string requires the machine to remember an unbounded number of previous symbols, the machine will eventually run out of unique states and be forced to repeat one.
The Pumping Lemma for Regular Languages
The pumping lemma is a formal property of all regular languages. It states that if a language \(L\) is regular, then there exists a constant \(p \ge 1\) (called the pumping length) such that any string \(s \in L\) with length \(|s| \ge p\) can be divided into three parts, \(s = xyz\), satisfying three conditions:
- \(|y| \ge 1\) (the middle section \(y\) is not empty).
- \(|xy| \le p\) (the first two sections together fit within the pumping length).
- For all integers \(i \ge 0\), the string \(xy^i z \in L\) (repeating \(y\) any number of times, including zero, keeps the string in the language).
Intuitively, if a string is at least as long as the number of states \(p\) in the FSM, the pigeonhole principle guarantees that the machine must visit at least one state more than once. The loop between those repeated visits corresponds to the substring \(y\), which can be repeated ("pumped") any number of times without changing the machine's acceptance of the string.
Proving Balanced Parentheses Are Not Regular
To prove that balanced parentheses cannot be matched by a finite state machine, we use a proof by contradiction. Consider the language of balanced parentheses \(L\), or a simplified subset of it:
\[L = \{ (^n )^n \mid n \ge 0 \}\]
This language consists of \(n\) opening parentheses followed by \(n\) closing parentheses. Assume, for the sake of contradiction, that \(L\) is regular. By the pumping lemma, there must be a pumping length \(p\).
Now, choose the specific string \(s = (^p )^p\). Clearly, \(s \in L\), and its length is \(|s| = 2p \ge p\). According to the pumping lemma, \(s\) can be split into \(s = xyz\) such that \(|xy| \le p\) and \(|y| \ge 1\).
Because \(|xy| \le p\) and the first \(p\) characters of \(s\) are all opening parentheses, both \(x\) and \(y\) must consist exclusively of opening parentheses. Let \(k = |y|\), where \(k \ge 1\).
Now, consider what happens when we pump \(y\) by setting \(i = 2\) (or \(i = 0\)). The resulting string is:
\[xy^2 z = (^{p+k} )^p\]
Since \(k \ge 1\), the number of opening parentheses \(p + k\) is strictly greater than the number of closing parentheses \(p\). Therefore, \(xy^2 z \notin L\).
This directly contradicts the third condition of the pumping lemma, which requires \(xy^i z \in L\) for all \(i \ge 0\). Because our assumption that \(L\) is regular led to a contradiction, the language of balanced parentheses cannot be regular, and no finite state machine can recognize it.