HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, terminal and nonterminal symbols are the lexical elements used in specifying the production rules constituting a
formal grammar In formal language theory, a grammar (when the context is not given, often called a formal grammar for clarity) describes how to form strings from a language's alphabet that are valid according to the language's syntax. A grammar does not describe ...
. ''Terminal symbols'' are the elementary symbols of the language defined by a formal grammar. ''Nonterminal symbols'' (or ''syntactic variables'') are replaced by groups of terminal symbols according to the production rules. The terminals and nonterminals of a particular grammar are two
disjoint sets In mathematics, two sets are said to be disjoint sets if they have no element in common. Equivalently, two disjoint sets are sets whose intersection is the empty set.. For example, and are ''disjoint sets,'' while and are not disjoint. A ...
.


Terminal symbols

Terminal symbols are literal symbols that may appear in the outputs of the production rules of a formal grammar and which cannot be changed using the rules of the grammar. Applying the rules recursively to a source string of symbols will usually terminate in a final output string consisting only of terminal symbols. Consider a grammar defined by two rules. Using pictoric marks interacting with each other: # The symbol ר can become ди # The symbol ר can become д Here д is a terminal symbol because no rule exists which would change it into something else. On the other hand, ר has two rules that can change it, thus it is nonterminal. A
formal language In logic, mathematics, computer science, and linguistics, a formal language consists of words whose letters are taken from an alphabet and are well-formed according to a specific set of rules. The alphabet of a formal language consists of sym ...
defined or ''generated'' by a particular grammar is the set of strings that can be produced by the grammar ''and that consist only of terminal symbols''.


Nonterminal symbols

Nonterminal symbols are those symbols that can be replaced. They may also be called simply ''syntactic variables''. A formal grammar includes a ''start symbol'', a designated member of the set of nonterminals from which all the strings in the language may be derived by successive applications of the production rules. In fact, the language defined by a grammar is precisely the set of ''terminal'' strings that can be so derived.
Context-free grammar In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form :A\ \to\ \alpha with A a ''single'' nonterminal symbol, and \alpha a string of terminals and/or nonterminals (\alpha can be em ...
s are those grammars 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. These are exactly the languages that can be recognized by a non-deterministic
push down automaton In the theory of computation, a branch of theoretical computer science, a pushdown automaton (PDA) is a type of automaton that employs a stack. Pushdown automata are used in theories about what can be computed by machines. They are more capa ...
. Context-free languages are the theoretical basis for the syntax of most
programming languages A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
.


Production rules

A grammar is defined by production rules (or just 'productions') that specify which symbols may replace which other symbols; these rules may be used to generate strings, or to parse them. Each such rule has a ''head'', or left-hand side, which consists of the string that may be replaced, and a ''body'', or right-hand side, which consists of a string that may replace it. Rules are often written in the form ''head'' → ''body''; e.g., the rule ''a'' → ''b'' specifies that ''a'' can be replaced by ''b''. In the classic formalization of generative grammars first proposed by
Noam Chomsky Avram Noam Chomsky (born December 7, 1928) is an American public intellectual: a linguist, philosopher, cognitive scientist, historian, social critic, and political activist. Sometimes called "the father of modern linguistics", Chomsky i ...
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 from . * A finite set of ''production rules'', each rule of the form :: (\Sigma \cup N)^ N (\Sigma \cup N)^ \rightarrow (\Sigma \cup N)^ :where ^ is the Kleene star operator and denotes
set union In set theory, the union (denoted by ∪) of a collection of sets is the set of all elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other. A refers to a union of ze ...
, so (\Sigma \cup N)^ represents zero or more symbols, and means one ''nonterminal'' symbol. 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 body consists solely of the
empty string In formal language theory, the empty string, or empty word, is the unique string of length zero. Formal theory Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special c ...
, it may be denoted with a special notation (often , or ) in order to avoid confusion. * A distinguished symbol S \in N that is the ''start symbol''. A grammar is formally defined as the ordered quadruple \langle N, \Sigma, P, S\rangle. Such a formal grammar is often called a rewriting system or a
phrase structure grammar The term phrase structure grammar was originally introduced by Noam Chomsky as the term for grammar studied previously by Emil Post and Axel Thue (Post canonical systems). Some authors, however, reserve the term for more restricted grammars in th ...
in the literature.


Example

For instance, the following represents an integer (which may be signed) expressed in a variant of
Backus–Naur form In computer science, Backus–Naur form () or Backus normal form (BNF) is a metasyntax notation for context-free grammars, often used to describe the syntax of languages used in computing, such as computer programming languages, document format ...
: ::= '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ::= -' In this example, the symbols () are terminal symbols and and are nonterminal symbols. Another example is: :S -> cAd :A -> a , ab In this example, the symbols are terminal symbols and are nonterminal symbols.


See also

* Recursive grammar


Notes


References

{{reflist Formal languages Pattern matching