World Library  
Flag as Inappropriate
Email this Article

Terminal and nonterminal symbols

Article Id: WHEBN0007689061
Reproduction Date:

Title: Terminal and nonterminal symbols  
Author: World Heritage Encyclopedia
Language: English
Subject: Literal, Context-free grammar, Regular grammar, LL parser, Fold (higher-order function)
Collection: Formal Languages, Pattern Matching
Publisher: World Heritage Encyclopedia

Terminal and nonterminal symbols

In computer science, terminal and nonterminal symbols are the lexical elements used in specifying the production rules constituting a formal grammar. 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.


  • Terminal symbols 1
  • Nonterminal symbols 2
  • Production rules 3
  • Example 4
  • References 5

Terminal symbols

Terminal symbols are literal symbols which may appear in the outputs of the production rules of a formal grammar and which cannot be changed using the rules of the grammar (this is the reason for the name "terminal").

For concreteness, consider a grammar defined by two rules:

  1. x can become xa
  2. x can become a

Here a is a terminal symbol because no rule exists which would change it into something else. (On the other hand, x has two rules that can change it, thus it is nonterminal.) A formal language 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 which 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 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 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 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,[1][2] a grammar G consists of the following components:

  • A finite set N of nonterminal symbols.
  • A finite set \Sigma of terminal symbols that is disjoint from N.
  • A finite set P 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 \cup denotes set union, so (\Sigma \cup N)^{*} represents zero or more symbols, and N 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—i.e., that it contains no symbols at all—it may be denoted with a special notation (often \Lambda, e or \epsilon) 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 . Such a formal grammar is often called a rewriting system or a phrase structure grammar in the literature.[3][4]


For instance, the following represents an integer (which may be signed) expressed in a variant of Backus–Naur form:

 ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
 ::= ['-']  {}

In this example, the symbols (-,0,1,2,3,4,5,6,7,8,9) are terminal symbols and and are nonterminal symbols.

Note: Strings with leading zeroes like "0056" or "0000" will be also in that language.


  1. ^ Chomsky, Noam (1956). "Three Models for the Description of Language".  
  2. ^ Chomsky, Noam (1957). Syntactic Structures. The Hague:  
  3. ^  
  4. ^  
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.