Formal and Natural LanguagesΒΆ
Natural languages are the languages that people speak, such as English, Spanish, and French. They were not designed by people (although people try to impose some order on them); they evolved naturally.
- Formal languages are languages that are designed by people for specific applications.
- For example, the notation that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols.
- Chemists use a formal language to represent the chemical structure of molecules.
And most importantly:
Programming languages are formal languages that have been designed to express computations.
Formal languages tend to have strict rules about syntax. For example, 3+3=6
is a syntactically correct mathematical statement, but 3=+6$
is not.
H2O is a syntactically correct chemical name, but 2Zz is
not.
- Syntax rules come in two flavors, pertaining to tokens and structure.
Tokens are the basic elements of the language, such as words, numbers, parentheses, commas, and so on.
- Rules for the structure of a statement— that is, the way the tokens are arranged.
- The statement
3=+6$
is structurally illegal because you can’t place a plus sign immediately after an equal sign.
- The statement
When you read a sentence in English or a statement in a formal language, you have to figure out what the structure of the sentence is (although in a natural language you do this subconsciously). This process is called parsing.
Once you have parsed a sentence, you can figure out what it means, or the semantics of the sentence.
Although formal and natural languages have many features in common — tokens, structure, syntax, and semantics — there are many differences:
- ambiguity
- any statement has exactly one meaning, regardless of context.
- redundancy
- Natural languages are often verbose. Formal languages are less redundant and more concise.
- literalness
- Formal languages mean exactly what they say.
In some ways, the difference between formal and natural language is like the difference between poetry and prose, but more so:
- poetry
- Words are used for their sounds as well as for their meaning, and the whole poem together creates an effect or emotional response. Ambiguity is not only common but often deliberate.
- prose
- The literal meaning of words is more important, and the structure contributes more meaning. Prose is more amenable to analysis than poetry but still often ambiguous.
- program
- The meaning of a computer program is unambiguous and literal, and can be understood entirely by analysis of the tokens and structure.
Here are some suggestions for reading programs (and other formal languages).
Remember that formal languages are much more dense than natural languages, so it takes longer to read them.
The structure is very important, so it is usually not a good idea to read from top to bottom, left to right.
Instead, learn to parse the program in your head, identifying the token and interpreting the structure.
- The details matter.
- Little things like spelling errors and bad punctuation, which you can get away with in natural languages, can make a big difference in a formal language.