This article is about a technical term in mathematics and computer science. For any formal type of language usage, seeLiterary language. For studies about natural languages, seeFormal semantics (natural language).
Structure of the syntactically well-formed, although thoroughly nonsensical, English sentence,"Colorless green ideas sleep furiously" (historical example fromChomsky 1957)
In computer science, formal languages are used, among others, as the basis for defining the grammar ofprogramming languages and formalized versions of subsets of natural languages, in which the words of the language represent concepts that are associated with meanings orsemantics. Incomputational complexity theory,decision problems are typically defined as formal languages, andcomplexity classes are defined as the sets of the formal languages that can beparsed by machines with limited computational power. Inlogic and thefoundations of mathematics, formal languages are used to represent the syntax ofaxiomatic systems, andmathematical formalism is the philosophy that all of mathematics can be reduced to the syntactic manipulation of formal languages in this way.
The field offormal language theory studies primarily the purelysyntactic aspects of such languages—that is, their internal structural patterns. Formal language theory sprang out of linguistics, as a way of understanding the syntactic regularities ofnatural languages.
Gottlob Frege attempted to realize Leibniz's ideas, through a notational system first outlined inBegriffsschrift (1879) and more fully developed in his 2-volume Grundgesetze der Arithmetik (1893/1903).[3] This described a "formal language of pure language."[4]
In the first half of the 20th century, several developments were made with relevance to formal languages.Axel Thue published four papers relating to words and language between 1906 and 1914. The last of these introduced whatEmil Post later termed 'Thue Systems', and gave an early example of anundecidable problem.[5] Post would later use this paper as the basis for a 1947 proof "that the word problem for semigroups was recursively insoluble",[6] and later devised thecanonical system for the creation of formal languages.
In 1907,Leonardo Torres Quevedo introduced a formal language for the description of mechanical drawings (mechanical devices), inVienna. He published "Sobre un sistema de notaciones y símbolos destinados a facilitar la descripción de las máquinas" ("On a system of notations and symbols intended to facilitate the description of machines").[7]Heinz Zemanek rated it as an equivalent to aprogramming language for the numerical control of machine tools.[8]
Noam Chomsky devised an abstract representation of formal and natural languages, known as theChomsky hierarchy.[9] In 1959John Backus developed the Backus-Naur form to describe the syntax of a high level programming language, following his work in the creation ofFORTRAN.[10]Peter Naur was the secretary/editor for the ALGOL60 Report in which he usedBackus–Naur form to describe the Formal part of ALGOL60.
Analphabet, in the context of formal languages, can be anyset; its elements are calledletters. An alphabet may contain aninfinite number of elements;[note 1] however, most definitions in formal language theory specify alphabets with a finite number of elements, and many results apply only to them. It often makes sense to use analphabet in the usual sense of the word, or more generally any finitecharacter encoding such asASCII orUnicode.
Aword over an alphabet can be any finite sequence (i.e.,string) of letters. The set of all words over an alphabet Σ is usually denoted by Σ* (using theKleene star). The length of a word is the number of letters it is composed of. For any alphabet, there is only one word of length 0, theempty word, which is often denoted by e, ε, λ or even Λ. By concatenation one can combine two words to form a new word, whose length is the sum of the lengths of the original words. The result of concatenating a word with the empty word is the original word.
In some applications, especially inlogic, the alphabet is also known as thevocabulary and words are known asformulas orsentences; this breaks the letter/word metaphor and replaces it by a word/sentence metaphor.
Given a non-empty set, aformal language over is asubset of, which is the set ofall possible finite-length words over. We call the setthe alphabet of. On the other hand, given a formal language over, a word iswell-formed if. Similarly, an expression iswell-formed if. Sometimes, a formal language over has a set of clear rules and constraints for the creation of all possible well-formed words from.
In computer science and mathematics, which do not usually deal withnatural languages, the adjective "formal" is often omitted as redundant. On the other hand, we can just say "a formal language" when its alphabet is clear in the context.
While formal language theory usually concerns itself with formal languages that are described by some syntactic rules, the actual definition of the concept "formal language" is only as above: a (possibly infinite) set of finite-length strings composed from a given alphabet, no more and no less. In practice, there are many languages that can be described by rules, such asregular languages orcontext-free languages. The notion of aformal grammar may be closer to the intuitive concept of a "language", one described by syntactic rules. By an abuse of the definition, a particular formal language is often thought of as being accompanied with a formal grammar that describes it.
The following rules describe a formal language L over the alphabet Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, =}:
Every nonempty string that does not contain "+" or "=" and does not start with "0" is in L.
The string "0" is in L.
A string containing "=" is in L if and only if there is exactly one "=", and it separates two valid strings of L.
A string containing "+" but not "=" is in L if and only if every "+" in the string separates two valid strings of L.
No string is in L other than those implied by the previous rules.
Under these rules, the string "23+4=555" is in L, but the string "=234=+" is not. This formal language expressesnatural numbers, well-formed additions, and well-formed addition equalities, but it expresses only what they look like (theirsyntax), not what they mean (semantics). For instance, nowhere in these rules is there any indication that "0" means the number zero, "+" means addition, "23+4=555" is false, etc.
For finite languages, one can explicitly enumerate all well-formed words. For example, we can describe a language L as justL = {a, b, ab, cba}. Thedegenerate case of this construction is theempty language, which contains no words at all (L = ∅).
However, even over a finite (non-empty) alphabet such as Σ = {a, b} there are an infinite number of finite-length words that can potentially be expressed: "a", "abb", "ababba", "aaababbbbaab", .... Therefore, formal languages are typically infinite, and describing an infinite formal language is not as simple as writingL = {a, b, ab, cba}. Here are some examples of formal languages:
L = Σ*, the set ofall words over Σ;
L = {a}* = {an}, wheren ranges over the natural numbers and "an" means "a" repeatedn times (this is the set of words consisting only of the symbol "a");
the set of syntactically correct programs in a given programming language (the syntax of which is usually defined by acontext-free grammar);
the set of inputs upon which a certainTuring machine halts; or
the set of maximal strings ofalphanumericASCII characters on this line, i.e., the set {the, set, of, maximal, strings, alphanumeric, ASCII, characters, on, this, line, i, e}.
Formal languages are used as tools in multiple disciplines. However, formal language theory rarely concerns itself with particular languages (except as examples), but is mainly concerned with the study of various types of formalisms to describe languages. For instance, a language can be given as
those strings for which somedecision procedure (analgorithm that asks a sequence of related YES/NO questions) produces the answer YES.
Typical questions asked about such formalisms include:
What is their expressive power? (Can formalismX describe every language that formalismY can describe? Can it describe other languages?)
What is their recognizability? (How difficult is it to decide whether a given word belongs to a language described by formalismX?)
What is their comparability? (How difficult is it to decide whether two languages, one described in formalismX and one in formalismY, or inX again, are actually the same language?).
Surprisingly often, the answer to these decision problems is "it cannot be done at all", or "it is extremely expensive" (with a characterization of how expensive). Therefore, formal language theory is a major application area ofcomputability theory andcomplexity theory. Formal languages may be classified in theChomsky hierarchy based on the expressive power of their generative grammar as well as the complexity of their recognizingautomaton.Context-free grammars andregular grammars provide a good compromise between expressivity and ease ofparsing, and are widely used in practical applications.
Certain operations on languages are common. This includes the standard set operations, such as union, intersection, and complement. Another class of operation is the element-wise application of string operations.
Examples: suppose and are languages over some common alphabet.
Theconcatenation consists of all strings of the form where is a string from and is a string from.
Theintersection of and consists of all strings that are contained in both languages
Thecomplement of with respect to consists of all strings over that are not in.
TheKleene star: the language consisting of all words that are concatenations of zero or more words in the original language;
Reversal:
Letε be the empty word, then, and
for each non-empty word (whereare elements of some alphabet), let,
Suchstring operations are used to investigateclosure properties of classes of languages. A class of languages is closed under a particular operation when the operation, applied to languages in the class, always produces a language in the same class again. For instance, thecontext-free languages are known to be closed under union, concatenation, and intersection withregular languages, but not closed under intersection or complement. The theory oftrios andabstract families of languages studies the most common closure properties of language families in their own right.[11]
Closure properties of language families ( Op where both and are in the language family given by the column). After Hopcroft and Ullman.
A compiler usually has two distinct components. Alexical analyzer, sometimes generated by a tool likelex, identifies the tokens of the programming language grammar, e.g.identifiers orkeywords, numeric and string literals, punctuation and operator symbols, which are themselves specified by a simpler formal language, usually by means ofregular expressions. At the most basic conceptual level, aparser, sometimes generated by aparser generator likeyacc, attempts to decide if the source program is syntactically valid, that is if it is well formed with respect to the programming language grammar for which the compiler was built.
Of course, compilers do more than just parse the source code – they usually translate it into some executable format. Because of this, a parser usually outputs more than a yes/no answer, typically anabstract syntax tree. This is used by subsequent stages of the compiler to eventually generate anexecutable containingmachine code that runs directly on the hardware, or someintermediate code that requires avirtual machine to execute.
Aformal system (also called alogical calculus, or alogical system) consists of a formal language together with adeductive apparatus (also called adeductive system). The deductive apparatus may consist of a set oftransformation rules, which may be interpreted as valid rules of inference, or a set ofaxioms, or have both. A formal system is used toderive one expression from one or more other expressions. Although a formal language can be identified with its formulas, a formal system cannot be likewise identified by its theorems. Two formal systems and may have all the same theorems and yet differ in some significant proof-theoretic way (a formula A may be a syntactic consequence of a formula B in one but not another for instance).
Aformal proof orderivation is a finite sequence of well-formed formulas (which may be interpreted as sentences, orpropositions) each of which is an axiom or follows from the preceding formulas in the sequence by arule of inference. The last sentence in the sequence is a theorem of a formal system. Formal proofs are useful because their theorems can be interpreted as true propositions.
Formal languages are entirely syntactic in nature, but may be givensemantics that give meaning to the elements of the language. For instance, in mathematicallogic, the set of possible formulas of a particular logic is a formal language, and aninterpretation assigns a meaning to each of the formulas—usually, atruth value.
The study of interpretations of formal languages is calledformal semantics. In mathematical logic, this is often done in terms ofmodel theory. In model theory, the terms that occur in a formula are interpreted as objects withinmathematical structures, and fixed compositional interpretation rules determine how the truth value of the formula can be derived from the interpretation of its terms; amodel for a formula is an interpretation of terms such that the formula becomes true.
^For example,first-order logic is often expressed using an alphabet that, besides symbols such as ∧, ¬, ∀ and parentheses, contains infinitely many elementsx0, x1, x2, … that play the role of variables.
Each category of languages, except those marked by a*, is aproper subset of the category directly above it.Any language in each category is generated by a grammar and by an automaton in the category in the same line.