All Topics  
Formal grammar

 

   Email Print
   Bookmark   Link






 

Formal grammar



 
 
In formal language theory, grammars, also called formal grammars or generative grammar
Generative grammar

In theoretical linguistics, generative grammar refers to a particular approach to the study of syntax. A generative grammar of a language attempts to give a set of rules that will correctly predict which combinations of words will form grammatical sentences....
s, are a formalism used to describe formal language
Formal language

A formal language is a set of words, i.e. finite string of letters, or symbols. The inventory from which these letters are taken is called the alphabet over which the language is defined....
s – i.e. set
Set

A set is a collection of distinct objects, considered as an object in its own right. Sets are one of the most fundamental concepts in mathematics....
s of strings
String (computer science)

In computer programming and some branches of mathematics, a string is an ordered sequence of symbols. These symbols are chosen from a predetermined set or alphabet....
 over some alphabet
Alphabet (computer science)

In computer science, an alphabet is a, usually finite, set of characters or digits. The most common alphabet is , the binary alphabet. A finite String is a finite sequence of characters from an alphabet; for instance a binary string is a string drawn from the alphabet ....
. In other words, a grammar describes which of the possible sequences of symbols (strings) in a language constitute syntactically valid words or statements in that language, but it does not describe anything else about a language, such as its semantics
Semantics

Semantics is the study of meaning in communication. The word is derived from the Greek language word s??a?t???? , "significant", from s??a??? , "to signify, to indicate" and that from s??a , "sign, mark, token"....
 (i.e.






Discussion
Ask a question about 'Formal grammar'
Start a new discussion about 'Formal grammar'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In formal language theory, grammars, also called formal grammars or generative grammar
Generative grammar

In theoretical linguistics, generative grammar refers to a particular approach to the study of syntax. A generative grammar of a language attempts to give a set of rules that will correctly predict which combinations of words will form grammatical sentences....
s, are a formalism used to describe formal language
Formal language

A formal language is a set of words, i.e. finite string of letters, or symbols. The inventory from which these letters are taken is called the alphabet over which the language is defined....
s – i.e. set
Set

A set is a collection of distinct objects, considered as an object in its own right. Sets are one of the most fundamental concepts in mathematics....
s of strings
String (computer science)

In computer programming and some branches of mathematics, a string is an ordered sequence of symbols. These symbols are chosen from a predetermined set or alphabet....
 over some alphabet
Alphabet (computer science)

In computer science, an alphabet is a, usually finite, set of characters or digits. The most common alphabet is , the binary alphabet. A finite String is a finite sequence of characters from an alphabet; for instance a binary string is a string drawn from the alphabet ....
. In other words, a grammar describes which of the possible sequences of symbols (strings) in a language constitute syntactically valid words or statements in that language, but it does not describe anything else about a language, such as its semantics
Semantics

Semantics is the study of meaning in communication. The word is derived from the Greek language word s??a?t???? , "significant", from s??a??? , "to signify, to indicate" and that from s??a , "sign, mark, token"....
 (i.e. what the strings mean).

The branch of mathematics that is concerned with the properties of formal grammars and languages and the relationships between them is called formal language theory. Its applications are found in computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, linguistics
Linguistics

Linguistics is the science study of natural language. Linguistics encompasses a number of sub-fields. An important topical division is between the study of language structure and the study of Meaning ....
, formal semantics, and other areas.

A grammar consists of a set of string rewriting rules with an assigned start symbol; the language described is the set of strings that can be generated by applying these rules arbitrarily, starting with the start symbol.

Therefore, a grammar is usually thought of as a language generator; but it can also be used as the basis for a recognizer that determines for any given string whether it is grammatical (i.e. belongs to the language). Whether this can effectively be done depends on the type of grammar used. To describe such recognizers, formal language theory uses separate formalisms, known as automata
Automata theory

In theoretical computer science, automata theory is the study of abstract machines and problems which they are able to solve. Automata theory is closely related to formal language theory as the automata are often classified by the class of formal languages they are able to recognize....
.

The process of recognizing a string by constructing a combination of applications of rules that generate it is known as parsing
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
. Most languages have very compositional semantics, i.e. the meaning of their utterances is structured according to their syntax
Syntax

In linguistics, syntax is the study of the principles and rules for constructing Sentence s in natural languages. In addition to referring to the discipline, the term syntax is also used to refer directly to the rules and principles that govern the sentence structure of any individual language, as in "the Irish syntax"....
; therefore, the first step to describing the meaning of an utterance in language is to analyze it and look at its analyzed form (known as its parse tree
Parse tree

A parse tree or concrete syntax tree is an tree that represents the syntax structure of a string according to some formal grammar. In a parse tree, the interior nodes are labeled by nonterminals of the grammar, while the leaf nodes are labeled by terminal symbol of the grammar....
 in computer science, and as its deep structure
Deep structure

In Linguistics In linguistics, and especially the study of syntax, the deep structure of a linguistic expression is a theoretical construct that seeks to unify several related structures....
 in generative grammar
Generative grammar

In theoretical linguistics, generative grammar refers to a particular approach to the study of syntax. A generative grammar of a language attempts to give a set of rules that will correctly predict which combinations of words will form grammatical sentences....
).

Introductory example


A grammar mainly consists of a set of rules for transforming strings. (If it only consisted of these rules, it would be a semi-Thue system
Semi-Thue system

In computer science and mathematics a Semi-Thue system is a type of term rewriting system. It is named after the Norwegian mathematician Axel Thue, who introduced systematic treatment of string rewriting systems in the early 20th century....
.) To generate a string in the language, one begins with a string consisting of only a single start symbol, and then successively applies the rules (any number of times, in any order) to rewrite this string. The language consists of all the strings that can be generated in this manner. Any particular sequence of legal choices taken during this rewriting process yields one particular string in the language. If there are multiple ways of generating the same single string, then the grammar is said to be ambiguous
Ambiguous grammar

In computer science, a formal grammar is said to be an ambiguous grammar if there is some string that it can generate in more than one way . A language is inherently ambiguous if it can only be generated by ambiguous grammars....
.

For example, assume the alphabet consists of and , the start symbol is and we have the following rules:

1.
2.


then we start with , and can choose a rule to apply to it. If we choose rule 1, we obtain the string . If we choose rule 1 again, we replace with and obtain the string . This process can be repeated at will until all occurrences of S are removed, and only symbols from the alphabet remain (i.e., and ). For example, if we now choose rule 2, we replace with and obtain the string , and are done. We can write this series of choices more briefly, using symbols: . The language of the grammar is the set of all the strings that can be generated using this process: .

Formal definition


The syntax of grammars

In the classic formalization of generative grammars first proposed by Noam Chomsky
Noam Chomsky

Avram Noam Chomsky is an United States linguistics, philosopher, cognitive science, political activist, author, and lecturer. He is an Institute Professor emeritus and professor emeritus of linguistics at the Massachusetts Institute of Technology....
 in the 1950s, a grammar G consists of the following components:
  • A finite set of nonterminal symbols.
  • A finite set of terminal symbols that is disjoint
    Disjoint sets

    In mathematics, two Set are said to be disjoint if they have no element in common. For example, and are disjoint sets....
     from .
  • A finite set of production rules, each rule of the form
where is the Kleene star
Kleene star

In mathematical logic and computer science, the Kleene star is a unary operation, either on Set of string or on sets of symbols or characters....
 operator and denotes set union
Union (set theory)

In set theory, the term Union refers to a set operation used in the convergence of set elements to form a resultant set containing the elements of both sets....
. That is, each production rule maps from one string of symbols to another, where the first string contains at least one nonterminal symbol. In the case that the second string is the empty string
Empty string

In computer science and formal language theory, the empty string is the unique string of String #Formal_theory zero. It is denoted with "?" or sometimes ?....
 – that is, that it contains no symbols at all – in order to avoid confusion, the empty string is often denoted with a special notation, often ( or ).
  • A distinguished symbol that is the start symbol.
A grammar is formally defined as the ordered quad-tuple . Such a formal grammar is often called a rewriting system or a phrase structure grammar
Phrase structure grammar

Phrase structure grammar has several different common meanings.In mathematics, in the area of formal language theory, it is often used as a synonym for context-sensitive grammar, which uses phrase structure rules or rewrite rules....
 in the literature.

The semantics of grammars


The operation of a grammar can be defined in terms of relations on strings:
  • Given a grammar , the binary relation (pronounced as "G derives in one step") on strings in is defined by:
  • the relation (pronounced as G derives in zero or more steps) is defined as the transitive closure
    Transitive closure

    In mathematics, the transitive closure of a binary relation R on a Set X is the smallest transitive relation on X that contains R....
     of
  • the language of , denoted as , is defined as all those strings over that can be generated by starting with the start symbol and then applying the production rules in until no more nonterminal symbols are present; that is, the set .


Note that the grammar is effectively the semi-Thue system
Semi-Thue system

In computer science and mathematics a Semi-Thue system is a type of term rewriting system. It is named after the Norwegian mathematician Axel Thue, who introduced systematic treatment of string rewriting systems in the early 20th century....
 , rewriting strings in exactly the same way; the only difference is in that we distinguish specific nonterminal symbols which must be rewritten in rewrite rules, and are only interested in rewritings from the designated start symbol to strings without nonterminal symbols.

Example

For these examples, formal languages are specified using set-builder notation
Set-builder notation

In set theory and its applications to logic, mathematics, and computer science, set-builder notation is a mathematical notation for describing a Set by stating the properties that its members must satisfy....
.


Consider the grammar where , , is the start symbol, and consists of the following production rules:

1.
2.
3.
4.


Some examples of the derivation of strings in are:



This grammar defines the language where denotes a string of n consecutive 's. Thus, the language is the set of strings that consist of 1 or more 's, followed by the same number of 's, followed by the same number of 's.

The Chomsky hierarchy

When Noam Chomsky
Noam Chomsky

Avram Noam Chomsky is an United States linguistics, philosopher, cognitive science, political activist, author, and lecturer. He is an Institute Professor emeritus and professor emeritus of linguistics at the Massachusetts Institute of Technology....
 first formalized generative grammars in 1956, he classified them into types now known as the Chomsky hierarchy
Chomsky hierarchy

Within the field of computer science, specifically in the area of formal languages, the Chomsky hierarchy is a containment hierarchy of classes of formal grammars....
. The difference between these types is that they have increasingly strict production rules and can express fewer formal languages. Two important types are context-free grammar
Context-free grammar

In formal language theory, a context-free grammar is a formal grammar in which every Production rule is of the formwhere V is a single nonterminal symbol, and w is a string of Terminal and nonterminal symbolss and/or nonterminals ....
s
(Type 2) and regular grammar
Regular grammar

Strictly regular grammars In computer science, a right regular grammar is a formal grammar such that all the production rules in P are of one of the following forms:...
s
(Type 3). The languages that can be described with such a grammar are called context-free language
Context-free language

In formal language theory, a context-free language is a formal language generated by some context-free grammar. The set of all context-free languages is identical to the set of languages accepted by pushdown automaton....
s
and regular language
Regular language

In theoretical computer science, a regular language is a formal language that satisfies the following equivalent properties:* it can be accepted by a deterministic finite state machine...
s
, respectively. Although much less powerful than unrestricted grammar
Unrestricted grammar

In formal language theory, an unrestricted grammar is a formal grammar on which no restrictions are made on the left and right sides of the grammar's productions....
s (Type 0), which can in fact express any language that can be accepted by a Turing machine
Turing machine

Turing machines are basic abstract symbol-manipulating devices which, despite their simplicity, can be adapted to simulate the logic of any computer algorithm....
, these two restricted types of grammars are most often used because parser
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
s for them can be efficiently implemented. For example, all regular languages can be recognized by a finite state machine
Finite state machine

A finite state machine or finite state automaton or simply a state machine, is a model of behavior composed of a finite number of state s, transitions between those states, and actions....
, and for useful subsets of context-free grammars there are well-known algorithms to generate efficient LL parser
LL parser

An LL parser is a Top-down parsing parser for a subset of the context-free grammars. It parses the input from Left to right, and constructs a Context-free grammar#Derivations and syntax trees of the sentence ....
s and LR parser
LR parser

In computer science, an LR parser is a parser for context-free grammars that reads input from Left to right and produces a Context-free grammar#Derivations and syntax trees....
s to recognize the corresponding languages those grammars generate.

Context-free grammars

A context-free grammar
Context-free grammar

In formal language theory, a context-free grammar is a formal grammar in which every Production rule is of the formwhere V is a single nonterminal symbol, and w is a string of Terminal and nonterminal symbolss and/or nonterminals ....
 is a grammar in which the left-hand side of each production rule consists of only a single nonterminal symbol. This restriction is non-trivial; not all languages can be generated by context-free grammars. Those that can are called context-free languages.

The language defined above is not a context-free language, and this can be strictly proven using the pumping lemma for context-free languages
Pumping lemma for context-free languages

The pumping lemma for context-free languages, also known as the Yehoshua Bar-Hillel lemma, is a lemma that gives a property that all context-free languages have....
, but for example the language (at least 1 followed by the same number of 's) is context-free, as it can be defined by the grammar with , , the start symbol, and the following production rules:

1.
2.


A context-free language can be recognized in time (see Big O notation
Big O notation

In mathematics, big O notation describes the asymptotic analysis of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions....
) by an algorithm such as Earley's algorithm. That is, for every context-free language, a machine can be built that takes a string as input and determines in time whether the string is a member of the language, where is the length of the string. Further, some important subsets of the context-free languages can be recognized in linear time using other algorithms.

Regular grammars

In regular grammar
Regular grammar

Strictly regular grammars In computer science, a right regular grammar is a formal grammar such that all the production rules in P are of one of the following forms:...
s, the left hand side is again only a single nonterminal symbol, but now the right-hand side is also restricted. The right side may be the empty string, or a single terminal symbol, or a single terminal symbol followed by a nonterminal symbol, but nothing else. (Sometimes a broader definition is used: one can allow longer strings of terminals or single nonterminals without anything else, making languages easier to denote
Syntactic sugar

Syntactic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its Function but make it "sweeter" for humans to use....
 while still defining the same class of languages.)

The language defined above is not regular, but the language (at least 1 followed by at least 1 , where the numbers may be different) is, as it can be defined by the grammar with , , the start symbol, and the following production rules:



All languages generated by a regular grammar can be recognized in linear time by a finite state machine
Finite state machine

A finite state machine or finite state automaton or simply a state machine, is a model of behavior composed of a finite number of state s, transitions between those states, and actions....
. Although, in practice, regular grammars are commonly expressed using regular expression
Regular expression

In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters....
s, some forms of regular expression used in practice do not strictly generate the regular languages and do not show linear recognitional performance due to those deviations.

Other forms of generative grammars

Many extensions and variations on Chomsky's original hierarchy of formal grammars have been developed, both by linguists and by computer scientists, usually either in order to increase their expressive power or in order to make them easier to analyze or parse
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
. Some forms of grammars developed include:

  • Tree-adjoining grammar
    Tree-adjoining grammar

    Tree-adjoining grammar is a grammar formalism defined by Aravind Joshi. Tree-adjoining grammars are somewhat similar to context-free grammars, but the elementary unit of rewriting is the tree rather than the symbol....
    s increase the expressiveness of conventional generative grammars by allowing rewrite rules to operate on parse tree
    Parse tree

    A parse tree or concrete syntax tree is an tree that represents the syntax structure of a string according to some formal grammar. In a parse tree, the interior nodes are labeled by nonterminals of the grammar, while the leaf nodes are labeled by terminal symbol of the grammar....
    s instead of just strings.
  • Affix grammar
    Affix grammar

    An affix grammar is a kind of formal grammar; it is used to describe the syntax of languages, mainly computer languages, using an approach based on how natural language is typically described....
    s and attribute grammar
    Attribute grammar

    An Attribute grammar is a formal way to define Attribute for the productions of a formal grammar, associating these attributes to values. The evaluation occurs in the nodes of the abstract syntax tree, when the language is processed by some parser or compiler....
    s allow rewrite rules to be augmented with semantic attributes and operations, useful both for increasing grammar expressiveness and for constructing practical language translation tools.


Analytic grammars

Though there is a tremendous body of literature on parsing
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
 algorithms, most of these algorithms assume that the language to be parsed is initially described by means of a generative formal grammar, and that the goal is to transform this generative grammar into a working parser. Strictly speaking, a generative grammar does not in any way correspond to the algorithm used to parse a language, and various algorithms have different restrictions on the form of production rules that are considered well-formed.

An alternative approach is to formalize the language in terms of an analytic grammar in the first place, which more directly corresponds to the structure and semantics of a parser for the language. Examples of analytic grammar formalisms include the following:

  • The Language Machine directly implements unrestricted analytic grammars. Substitution rules are used to transform an input to produce outputs and behaviour. The system can also produce which shows what happens when the rules of an unrestricted analytic grammar are being applied.
  • Top-down parsing language
    Top-down parsing language

    Top-Down Parsing Language is a type of analytic grammar formal grammar developed by Alexander Birman in the early 1970s in order to study formally the behavior of a common class of practical top-down parsing that support a limited form of backtracking....
     (TDPL): a highly minimalist analytic grammar formalism developed in the early 1970s to study the behavior of top-down parsers
    Top-down parsing

    Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis....
    .
  • Link grammar
    Link grammar

    Link grammar is a theory of syntax by Davy Temperley and Daniel Sleator which builds relations between pairs of words, rather than constructing constituents in a Parse tree hierarchy....
    s: a form of analytic grammar designed for linguistics
    Linguistics

    Linguistics is the science study of natural language. Linguistics encompasses a number of sub-fields. An important topical division is between the study of language structure and the study of Meaning ....
    , which derives syntactic structure by examining the positional relationships between pairs of words.
  • Parsing expression grammar
    Parsing expression grammar

    A parsing expression grammar, or PEG, is a type of analytic grammar formal grammar that describes a formal language in terms of a set of rules for recognizing string in the language....
    s (PEGs): a more recent generalization of TDPL designed around the practical expressiveness needs of programming language
    Programming language

    A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
     and compiler
    Compiler

    A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
     writers.


See also


External links