TL;DR
A formal language is a set of strings made from a fixed list of symbols, such as the letters 0 and 1. Its defining feature is that an exact rule decides, for any string, whether it belongs to the set or not. This gives programs and tools a precise meaning for words like "valid identifier" or "well-formed number". The idea is the base of compilers, search tools and input checking.
Builds on: Set Operations · Leads to: Context-Free Grammar, State Machine
Where it's used
Programming language lexers. Before a compiler understands a program, a lexer splits the source text into tokens such as names, numbers and operators. Each token type is a small formal language. A name, for example, is defined as a letter followed by any number of letters, digits or underscores. The lexer uses the rule to decide whether a run of characters is a name or an error.
Search tools such as grep. When you search a log file with a pattern like ERROR [0-9]+, the tool checks each line against a rule. The pattern describes a formal language, and the tool reports the lines whose text belongs to it. Patterns of this kind are the everyday face of formal languages.
Input checking and protocols. A web form that accepts only a five-digit postal code, or a network message that must start with a fixed header, is checking that its input belongs to a language. A server that rejects a malformed request has answered the question "is this string in my language?" with no.
How it works
Take a tiny instance. The alphabet is . The language is the set of strings that contain an even number of 1s. We will test the string .
- Start the count. Set a counter to 0. Nothing has been read yet, so the string is still undecided. Check: the counter is even, as it would be for an empty string.
- Read the first symbol, 1. It is a 1, so the counter goes up to 1. Check: one 1 so far, which is odd.
- Read the second symbol, 0. It is not a 1, so the counter stays at 1. Check: the number of 1s has not changed.
- Read the third symbol, 0. Again not a 1, so the counter stays at 1. Check: still one 1.
- Read the fourth symbol, 1. It is a 1, so the counter goes up to 2. Check: two 1s, which is even.
- Decide at the end. The string has no symbols left. The counter is even, so belongs to .
The rule is what defines the language. The set of members is usually infinite and is never written out in full. A language is fixed by a rule (or a machine that applies the rule), and the rule answers yes or no for every string.
flowchart LR
A["string w over the alphabet"] --> B{"rule: even number of 1s?"}
B -->|yes| C["w is in L"]
B -->|no| D["w is not in L"]
The math
Step 1 in symbols: strings and the alphabet
A string over is a finite sequence in which every belongs to . Its length is , the number of symbols. The empty string is the string with no symbols, so .
For each length , the strings of length form the set . Because each of the positions can hold any of the symbols, . For and , this gives strings.
The set of all strings over is . It contains and is infinite whenever is nonempty.
Step 2 in symbols: the rule as a property
A language is described by a property that a string may or may not have:
In the example, is "the number of 1s in is even". Membership is a yes-or-no question, a Function from strings to yes or no: exactly when is true.
Step 3 in symbols: combining strings
For strings and , the concatenation is the string that writes and then . Its length is . Concatenation of languages is . Union and intersection of languages are the ordinary set operations, because a language is a set.
Step 4 in symbols: the answer
Let be the number of 1s in . The property in the example is , read "the remainder when is divided by 2 is 0". For , , and the remainder of 2 divided by 2 is 0, so . The state machine in the state-machine view computes the same answer one symbol at a time, keeping only the remainder so far.
Worked examples
- The alphabet is , so .
- Strings of length exactly 2 are the set , which has members.
- Define . Test : it has length 2 and uses only and , so .
- Test : its length is 1, so .
- Test : its length is 0, so .
Interpretation: is a finite language. Its rule ("length exactly 2") is simpler than listing its members, but both describe the same set.
- Use the alphabet and the language from How it works.
- Test . Its 1s are in positions 1, 3 and 4, so there are three of them. Three is odd, so .
- Test again. It has two 1s, so .
- Test . It has zero 1s, and zero is even, so .
- Split into and . Each piece is a string, and concatenating them gives .
- Count the 1s in the two pieces: has one 1 and has two, for three in total. This matches the count of three from step 2, so the count is preserved by concatenation.
Interpretation: a rule about a count can be checked symbol by symbol, and the answer depends only on the count, not on the order of the symbols.
Take the alphabet of letters a to z, uppercase A to Z, digits 0 to 9, and the underscore. This has 63 symbols, and the ASCII set has 128 characters in total. An identifier is a string that starts with a letter or underscore and continues with letters, digits or underscores.
- Test . It starts with the letter , and the rest is , which uses only allowed symbols. So is an identifier.
- Test . It starts with an underscore and the rest uses only letters, so it is an identifier.
- Test . It starts with the digit 9, which the rule forbids as a first symbol. So is not an identifier, and a lexer reports an error.
- Test . The hyphen is not in the alphabet of allowed continuation symbols, so the string is not an identifier.
Try it: Is an identifier? Answer: Yes. It starts with the letter , and the remaining symbols , and are all digits, underscores or letters.
Common mistakes and tradeoffs
It is tempting to think a string is just a set of its symbols. In fact order matters, because and are different strings, even though they use the same symbols. Strings are sequences, and that is why the concatenation step in the math is not commutative.
It is tempting to think the empty string and the empty language are the same thing. In fact is a string of length 0, while is a language with no strings at all. The language contains exactly one string, and contains none.
It is tempting to think a language must be infinite or hard to describe. In fact finite languages are also languages, and the simplest one, , has no members. A finite language can be listed, which is often the clearest choice for a short set of keywords.
When to use the formal-language view: when you need an exact yes-or-no answer for every string, as in a compiler or a validator. When not to: natural human language is too irregular for a clean rule, so exact formal languages are used for programming and protocols, not for everyday speech. Listing the members works for finite sets, and a grammar or a machine works for infinite ones.
History
In the 1950s, researchers working on computing and on linguistics asked how to describe the set of "correct" sentences of a language. Noam Chomsky's 1956 paper defined classes of formal languages by the kind of rule that generates them, and this grew into the Chomsky hierarchy. Stephen Kleene's work on regular events, in the same decade, gave the star notation used here. Around 1960, the report on the ALGOL 60 programming language used a notation for grammar rules to define a programming language precisely, and the idea caught on because it let compilers check programs mechanically.
Recap
- An alphabet is a finite nonempty set of symbols, and a string is a finite sequence of them.
- is the set of all strings, including the empty string .
- A formal language is any subset of , and a rule (a property of strings) defines it.
- Membership is a yes-or-no question about one string, and a machine can answer it one symbol at a time.
- Next, read Context-Free Grammar to see how rules that generate strings define larger languages, and State Machine to see how a machine decides membership.
Check yourself
QuestionWhat is the difference between the empty string and the empty language?Show answer
QuestionOver the alphabet , how many strings have length exactly 3?Show answer
QuestionWhy can a rule about the number of 1s be checked one symbol at a time, while the order of the 1s and 0s does not matter for the answer?Show answer
QuestionA lexer treats `2fast` as an error. Using the identifier rule from Example 3, explain why this is correct, and say what a valid name would need to change.Show answer
Sources
- Wikipedia, "Formal language": https://en.wikipedia.org/wiki/Formal_language
- Wikipedia, "Chomsky hierarchy": https://en.wikipedia.org/wiki/Chomsky_hierarchy
- Noam Chomsky, "Three models for the description of language," IRE Transactions on Information Theory, 1956: https://doi.org/10.1109/TIT.1956.1056813