One of the few lessons I distinctly remember from college was finite automata in my PL class. I really enjoyed exploring the concepts and writing a grep tool; we were supposed to write either a NFA or DFA processing application, but I decided to write both.<p>20 years later I got to apply some of the same ideas to a language processing application, and it was such a pleasure to actually use something conceptual like that. Made me briefly regret landing in more hybrid infrastructure/automation roles instead of pure software development.<p>Somewhere I may still have my copy of Preperata and Yeh that my professor recommended at the time for further reading. Like most of my books, it was never actually read, just sat around for years.
<i>we’ll ask, “What’s the simplest possible computing machine that can recognize balanced parentheses?”</i><p>A counter. That's the difference between theory and practice. Because in practice, everything is finite.
The counter is simply the stack depth without bothering with the actual stack. If the stack is empty when you encounter a closer then it's unbalanced. If the stack isn't empty when you reach the end of the input then the items in the stack are unbalanced.<p>If you have multiple kinds of brackets then you need the same number of counters. Each counter corresponds to the number of openers of that type currently on the stack. EDIT: this is wrong. Counters can't distinguish between [() and ([)<p>If you're writing a parser and you want to report the location of an unclosed opening bracket then you need the actual stack.
FWIW it's a fairly straightforward algorithm. In C++:<p><pre><code> bool balanced(const string& text, const string& open, const string& close) {
size_t length = text.size(), brackets = open.size();
assert(close.size() == brackets);
stack<char> buffer;
for (size_t index = 0; index < length; ++index) {
char ch = text[index];
for (size_t slot = 0; slot < brackets; ++slot) {
if (ch == open[slot])
buffer.push(ch);
else if (ch == close[slot]) {
if (buffer.empty() || buffer.top() != open[slot])
return false;
buffer.pop();
}
}
}
return buffer.empty();
}</code></pre>
You need the actual stack, I think, in the case of multiple types of openers without additional constraints, because if you just have raw counters you'd get tripped up by ([)] or similar.<p>So to generalise your point you need a counter for each transition to a different type of opener.<p>So (([])) needs only 2 counters, not 3.<p>You could constrain it further if certain types of openers are only valid in certain cases so you could exclude certain types of transitions.<p>EDIT:<p>([)] could indeed be handled by just additionally tracking the <i>current</i> open type. (([]]) is a better example, as it shows that to handle deeper nesting you need additional pieces of data that will grow at some rate (at most by the number of opens, possibly lower depending on which types can validly appear within which types)
Wouldn't two counters report "([)]" as being properly balanced?
No, there's an open [ when the ) is encountered. The problem is the other way around -- my algorithm would report [() as an error. Oops, back to the drawing board. Clearly no counting can tell the difference between [() and ([).
> Because in practice, everything is finite.<p>Indeed! <a href="https://neilmadden.blog/2019/02/24/why-you-really-can-parse-html-and-anything-else-with-regular-expressions/" rel="nofollow">https://neilmadden.blog/2019/02/24/why-you-really-can-parse-...</a>
you don't need a full counter. increment, decrement, and check_if_zero are enough. no need for get_value.
Yes. Actually, a more interesting example which does not complicate the statement (not the problem) too much is to check for nested parenthesis and brackets:<p>(([[()])) -> ok
((([](])) -> not ok<p>Hope OP gets this message.
<i>"But on a day-to-day basis, if asked to recognize balanced parentheses?"</i><p>On day-to-day basis you will never encounter this problem in pure form. As the consequence the solutions are not good for the day-to-day stuff.<p>Even if you only are only writting a verifier (which is already a bit unrealistic), you'll need to say something more than "not balanced". Probably rather something along the lines of "closing brace without a matching opening at [position]" or "[n] unclosed parentheses at <end of stream>" which rules out the simple recursive regex approach (counter still works).
The pictures of Brutalist architecture are awesome!
The proof of non-regularity is a bit convoluted. You can easily apply the pumping lemma there
What is some further reading y'all could recommend on formal languages?
That's what I learnt from as part of CS curriculum at MiMUW. Can recommend:
<a href="https://en.wikipedia.org/wiki/Introduction_to_Automata_Theory,_Languages,_and_Computation" rel="nofollow">https://en.wikipedia.org/wiki/Introduction_to_Automata_Theor...</a>
sipser's theory of computation
[dead]