terminal and nonterminal symbols

{{short description|Categories of symbols in formal grammars}}

File:Terminal and non-terminal symbols example.png

In formal languages, terminal and nonterminal symbols are parts of the vocabulary under a formal grammar. Vocabulary is a finite, nonempty set of symbols. Terminal symbols are symbols that cannot be replaced by other symbols of the vocabulary. Nonterminal symbols are symbols that can be replaced by other symbols of the vocabulary, according to the production rules under the same formal grammar.Rosen, K. H. (2018). Discrete mathematics and its applications. McGraw-Hill. page 887.

In the context of formal language, the term vocabulary is more commonly known as alphabet. Nonterminal symbols are also called syntactic variables.

Terminal symbols

Terminal symbols can appear in the outputs of the production rules of a formal grammar. The process of applying the production rules successively to a start symbol might not terminate, but if it terminates when there is no more production rule can be applied, the output string will consist only of terminal symbols.

For example, consider a grammar defined by two rules. In this grammar, the symbol Б is a terminal symbol and Ψ is both a nonterminal symbol and the start symbol. The production rules for creating strings are as follows:

  1. The symbol Ψ can become БΨ
  2. 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. The rules implicitly define a formal language that contains countably infinite many finite-length words by the fact that we can apply the first rule any countable times as we wish. Diagram 1 illustrates a string that can be produced with this grammar.

File:Terminal and non-terminal symbols example number two.png

Nonterminal symbols

Nonterminal symbols, which are also called syntactic variables, are those symbols that can be replaced by other symbols of the vocabulary. A formal grammar includes a start symbol, which is a designated member of the set of nonterminal symbols. We can derive a set of strings of only terminal symbols by successively applying the production rules. The generated set is a formal language over the set of terminal symbols.

Context-free grammars 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. Context-free languages are the theoretical basis for the syntax of most programming languages.

Production rules

A grammar is defined by production rules (or just 'productions') that specify which symbols can replace which other symbols; these rules can 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 can be replaced, and a body, or right-hand side, which consists of a string that can replace it. Rules are often written in the form headbody; e.g., the rule ab specifies that a can be replaced by b.

In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s,{{Cite journal

| author = Chomsky, Noam

| title = Three Models for the Description of Language

| journal = IRE Transactions on Information Theory

| volume = 2

| issue = 3

| pages = 113–123

| year = 1956

| doi = 10.1109/TIT.1956.1056813

| s2cid = 19519474

}}{{Cite book

| author = Chomsky, Noam

| title = Syntactic Structures

| publisher = Mouton

| location = The Hague

| year = 1957

}} a grammar G consists of the following components:

  • A finite set {{mvar|N}} of nonterminal symbols.
  • A finite set {{math|Σ}} of terminal symbols that is disjoint from {{mvar|N}}.
  • A finite set {{mvar|P}} of production rules, each rule of the form

:: V^{*} N V^{*} \rightarrow V^{*}

:where V^{*} denotes the set of all possible finite-length strings over the vocabulary, V=\Sigma\cup N, by applying Kleene star operator on it. That is, each production rule replaces one string of symbols that contains at least one nonterminal symbol with another. In the case that the body consists solely of the empty string{{NoteTag|It contains no symbols at all.}}, it can be denoted with a special notation (often {{math|Λ}}, {{mvar|e}} or {{mvar|ε}}) 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 in the literature.{{cite book|first=Seymour|last=Ginsburg|authorlink=Seymour Ginsburg|title=Algebraic and automata theoretic properties of formal languages|publisher=North-Holland|pages=8–9|year=1975|isbn=0-7204-2506-9}}{{cite book|last=Harrison|first=Michael A.|authorlink=Michael A. Harrison|title=Introduction to Formal Language Theory|url=https://archive.org/details/introductiontofo00harr_425|url-access=limited|year=1978|pages=[https://archive.org/details/introductiontofo00harr_425/page/n14 13]|isbn=0-201-02955-3|location=Reading, Mass.|publisher=Addison-Wesley Publishing Company}}

Example

Backus–Naur form is a notation for expressing certain grammars. For instance, the following production rules in Backus-Naur form are used to represent an integer (which can be signed):

::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

::= ['-'] {}

In this example, terminal symbols are \{-,0,1,2,3,4,5,6,7,8,9\}, and nonterminal symbols are \{{{tag|digit|o}}, {{tag|integer|o}}\}.

{{NoteTag|This example supports strings with leading zeroes like "0056" or "0000", as well as negative zero strings like "-0" and "-00000".}}

Another example is:

:S -> cAd

:A -> a | ab

In this example, terminal symbols are \{\mathrm{a,b,c,d}\}, and nonterminal symbols are \{\mathrm{S,A}\}.

See also

Notes

{{NoteFoot}}

References