formal language this article is about a technical term in mathematics and computer science for related studies about natural languages see formal semantics linguistics for formal modes of speech in natural languages see register sociolinguistics structure of a syntactically wellformed although nonsensical english sentence historical example from chomsky 1957 in mathematics computer science and linguistics a formal language consists of words whose letters are taken from an alphabet and are wellformed according to a specific set of rules the alphabet of a formal language consist of symbols letters or tokens that concatenate into strings of the language each string concatenated from symbols of this alphabet is called a word and the words that belong to a particular formal language are sometimes called wellformed words or wellformed formulas a formal language is often defined by means of a formal grammar such as a regular grammar or contextfree grammar which consists of its formation rules the field of formal language theory studies primarily the purely syntactical aspects of such languagesthat is their internal structural patterns formal language theory sprang out of linguistics as a way of understanding the syntactic regularities of natural languages in computer science formal languages are used among others as the basis for defining the grammar of programming languages and formalized versions of subsets of natural languages in which the words of the language represent concepts that are associated with particular meanings or semantics in computational complexity theory decision problems are typically defined as formal languages and complexity classes are defined as the sets of the formal languages that can be parsed by machines with limited computational power in logic and the foundations of mathematics formal languages are used to represent the syntax of axiomatic systems and mathematical formalism is the philosophy that all of mathematics can be reduced to the syntactic manipulation of formal languages in this way history the first formal language is thought to be the one used by gottlob frege in his begriffsschrift 1879 literally meaning concept writing and which frege described as a formal language of pure thought axel thues early semithue system which can be used for rewriting strings was influential on formal grammars words over an alphabet an alphabet in the context of formal languages can be any set although it often makes sense to use an alphabet in the usual sense of the word or more generally a character set such as ascii or unicode the elements of an alphabet are called its letters an alphabet may contain an infinite number of elements however most definitions in formal language theory specify alphabets with a finite number of elements and most results apply only to them a word over an alphabet can be any finite sequence ie string of letters the set of all words over an alphabet is usually denoted by using the kleene 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 the empty 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 in logic the alphabet is also known as the vocabulary and words are known as formulas or sentences this breaks the letterword metaphor and replaces it by a wordsentence metaphor definition a formal language l over an alphabet is a subset of that is a set of words over that alphabet sometimes the sets of words are grouped into expressions whereas rules and constraints may be formulated for the creation of wellformed expressions in computer science and mathematics which do not usually deal with natural languages the adjective formal is often omitted as redundant while formal language theory usually concerns itself with formal languages that are described by some syntactical rules the actual definition of the concept formal language is only as above a possibly infinite set of finitelength strings composed from a given alphabet no more and no less in practice there are many languages that can be described by rules such as regular languages or contextfree languages the notion of a formal 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 equipped with a formal grammar that describes it examples the following rules describe a formal languagel over the alphabet 0123456789 every nonempty string that does not contain or and does not start with 0 is inl the string 0 is inl a string containing is inl if and only if there is exactly one and it separates two valid strings ofl a string containing but not is inl if and only if every in the string separates two valid strings ofl no string is inl other than those implied by the previous rules under these rules the string 234555 is inl but the string 234 is not this formal language expresses natural numbers wellformed additions and wellformed addition equalities but it expresses only what they look like their syntax not what they mean semantics for instance nowhere in these rules is there any indication that 0 means the number zero means addition 234555 is false etc constructions for finite languages one can explicitly enumerate all wellformed words for example we can describe a languagel as just lababcba the degenerate case of this construction is the empty language which contains no words at all l however even over a finite nonempty alphabet such as ab there are an infinite number of finitelength 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 writing lababcba here are some examples of formal languages l the set of all words over l a an where n ranges over the natural numbers and an means a repeated n 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 a contextfree grammar the set of inputs upon which a certain turing machine halts or the set of maximal strings of alphanumeric ascii characters on this line ie the set the set of maximal strings alphanumeric ascii characters on this line i e languagespecification formalisms 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 generated by some formal grammar those strings described or matched by a particular regular expression those strings accepted by some automaton such as a turing machine or finite state automaton those strings for which some decision procedure an algorithm that asks a sequence of related yesno questions produces the answer yes typical questions asked about such formalisms include what is their expressive power can formalism x describe every language that formalism y 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 formalism x what is their comparability how difficult is it to decide whether two languages one described in formalism x and one in formalism y or in x 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 of computability theory and complexity theory formal languages may be classified in the chomsky hierarchy based on the expressive power of their generative grammar as well as the complexity of their recognizing automaton contextfree grammars and regular grammars provide a good compromise between expressivity and ease of parsing and are widely used in practical applications operations on languages certain operations on languages are common this includes the standard set operations such as union intersection and complement another class of operation is the elementwise application of string operations examples suppose l1 and l2 are languages over some common alphabet the concatenation l1l2 consists of all strings of the form vw where v is a string from l1 and w is a string from l2 the intersection l1l2 of l1 and l2 consists of all strings that are contained in both languages the complement l of a language with respect to a given alphabet consists of all strings over the alphabet that are not in the language the kleene star the language consisting of all words that are concatenations of 0 or more words in the original language reversal let e be the empty word then ere and for each nonempty word wx1xn over some alphabet let wrxnx1 then for a formal language l lrwrw l string homomorphism such string operations are used to investigate closure 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 the contextfree languages are known to be closed under union concatenation and intersection with regular languages but not closed under intersection or complement the theory of trios and abstract families of languages studies the most common closure properties of language families in their own right closure properties of language families l 1 displaystyle l1 op l 2 displaystyle l2 where both l 1 displaystyle l1 and l 2 displaystyle l2 are in the language family given by the column after hopcroft and ullman operation regular dcfl cfl ind csl recursive re union w w l 1 w l 2 displaystyle wwin l1lor win l2 yes no yes yes yes yes yes intersection w w l 1 w l 2 displaystyle wwin l1land win l2 yes no no no yes yes yes complement w w l 1 displaystyle wwnot in l1 yes yes no no yes yes no concatenation l 1 l 2 w z w l 1 z l 2 displaystyle l1cdot l2wcdot zwin l1land zin l2 yes no yes yes yes yes yes kleene star l 1 w z w l 1 z l 1 displaystyle l1epsilon cup wcdot zwin l1land zin l1 yes no yes yes yes yes yes homomorphism yes no yes yes no no yes efree homomorphism yes no yes yes yes yes yes substitution yes no yes yes yes no yes inverse homomorphism yes yes yes yes yes yes yes reverse w r w l displaystyle wrwin l yes no yes yes yes yes yes intersection with a regular language w w l 1 w r r regular displaystyle wwin l1land win rrtext regular yes yes yes yes yes yes yes applications programming languages syntax programming languages and compiler compiler a compiler usually has two distinct components a lexical analyzer generated by a tool like lex identifies the tokens of the programming language grammar eg identifiers or keywords which are themselves expressed in a simpler formal language usually by means of regular expressions at the most basic conceptual level a parser usually generated by a parser generator like yacc attempts to decide if the source program is valid that is if it belongs to the programming language 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 yesno answer typically an abstract syntax tree this is used by subsequent stages of the compiler to eventually generate an executable containing machine code that runs directly on the hardware or some intermediate code that requires a virtual machine to execute formal theories systems and proofs this diagram shows the syntactic divisions within a formal system strings of symbols may be broadly divided into nonsense and wellformed formulas the set of wellformed formulas is divided into theorems and nontheorems theory mathematical logic and formal system in mathematical logic a formal theory is a set of sentences expressed in a formal language a formal system also called a logical calculus or a logical system consists of a formal language together with a deductive apparatus also called a deductive system the deductive apparatus may consist of a set of transformation rules which may be interpreted as valid rules of inference or a set of axioms or have both a formal system is used to derive 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 f s displaystyle mathcal fs and f s displaystyle mathcal fs may have all the same theorems and yet differ in some significant prooftheoretic way a formula a may be a syntactic consequence of a formula b in one but not another for instance a formal proof or derivation is a finite sequence of wellformed formulas which may be interpreted as sentences or propositions each of which is an axiom or follows from the preceding formulas in the sequence by a rule 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 interpretations and models formal semantics logic interpretation logic and model theory formal languages are entirely syntactic in nature but may be given semantics that give meaning to the elements of the language for instance in mathematical logic the set of possible formulas of a particular logic is a formal language and an interpretation assigns a meaning to each of the formulasusually a truth value the study of interpretations of formal languages is called formal semantics in mathematical logic this is often done in terms of model theory in model theory the terms that occur in a formula are interpreted as objects within mathematical structures and fixed compositional interpretation rules determine how the truth value of the formula can be derived from the interpretation of its terms a model for a formula is an interpretation of terms such that the formula becomes true combinatorics on words free monoid formal method grammar framework mathematical notation associative array string computer science citation footnotes general references a g hamilton logic for mathematicians cambridge university press 1978 isbn0521218381 seymour ginsburg algebraic and automata theoretic properties of formal languages northholland 1975 isbn0720425069 michael a harrison introduction to formal language theory addisonwesley 1978 john e hopcroft and jeffrey d ullman introduction to automata theory languages and computation addisonwesley publishing reading massachusetts 1979 isbn8178083477 rautenberg wolfgang 2010 a concise introduction to mathematical logic 3rd ed new york springer sciencebusiness media doi1010079781441912213 isbn9781441912206 grzegorz rozenberg arto salomaa handbook of formal languages volume iiii springer 1997 isbn3540614869 patrick suppes introduction to logic d van nostrand 1957 isbn0442080727 wikimedia commons has media related to formal languages hazewinkel michiel ed 2001 formal language encyclopedia of mathematics springer sciencebusiness media bv kluwer academic publishers isbn9781556080104 alphabet planetmath language planetmath university of maryland formal language definitions james power notes on formal language theory and parsing 29 november 2002 drafts of some chapters in the handbook of formal language theory vol 13 g rozenberg and a salomaa eds springer verlag 1997 alexandru mateescu and arto salomaa preface in vol1 pp vviii and formal languages an introduction and a synopsis chapter 1 in vol 1 pp139 sheng yu regular languages chapter 2 in vol 1 jeanmichel autebert jean berstel luc boasson contextfree languages and pushdown automata chapter 3 in vol 1 christian choffrut and juhani karhumki combinatorics of words chapter 6 in vol 1 tero harju and juhani karhumki morphisms chapter 7 in vol 1 pp 439510 jeaneric pin syntactic semigroups chapter 10 in vol 1 pp 679746 m crochemore and c hancart automata for matching patterns chapter 9 in vol 2 dora giammarresi antonio restivo twodimensional languages chapter 4 in vol 3 pp 215267 vteautomata theory formal languages and formal grammarschomsky hierarchygrammarslanguagesabstract machines type0 type1 type2 type3 unrestricted no common name contextsensitive positive range concatenation indexed linear contextfree rewriting systems treeadjoining contextfree deterministic contextfree visibly pushdown regular nonrecursive recursively enumerable decidable contextsensitive positive range concatenation indexed linear contextfree rewriting language treeadjoining contextfree deterministic contextfree visibly pushdown regular starfree finite turing machine decider linearbounded ptime turing machine nested stack thread automaton restricted tree stack automaton embedded pushdown nondeterministic pushdown deterministic pushdown visibly pushdown finite counterfree with aperiodic finite monoid acyclic finite each category of languages except those marked by a is a proper 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 vtemathematical logicgeneral formal language formation rule formal proof formal semantics wellformed formula set element class classical logic axiom rule of inference relation theorem logical consequence type theory symbol syntax theory systems formal system deductive system axiomatic system hilbert style systems natural deduction sequent calculus traditional logic proposition inference argument validity cogency syllogism square of opposition venn diagram propositional calculusand boolean logic boolean functions propositional calculus propositional formula logical connectives truth tables manyvalued logic predicate logic firstorder quantifiers predicate secondorder monadic predicate calculus naive set theory set empty set element enumeration extensionality finite set infinite set subset power set countable set uncountable set recursive set domain codomain image map function relation ordered pair set theory foundations of mathematics zermelofraenkel set theory axiom of choice general set theory kripkeplatek set theory von neumannbernaysgdel set theory morsekelley set theory tarskigrothendieck set theory model theory model interpretation nonstandard model finite model theory truth value validity proof theory formal proof deductive system formal system theorem logical consequence rule of inference syntax computability theory recursion recursive set recursively enumerable set decision problem churchturing thesis computable function primitive recursive function authority control gnd 40178481 ndl 00576869 