Lably
Skip to content

Production (computer science)

Method of symbol substitution

  • 2 min read
  • 1 chapter

Related on Lably

Through Computer science, Recursion (computer science) and Cartesian product

In an unrestricted grammar, a production is of the form u→v, where u and v are arbitrary strings of terminals and nonterminals, and u may not be the empty string. If v is the empty string, this is denoted by the symbol ϵ, or λ (rather than leaving the right-hand side blank). So productions are members of the cartesian product

V∗NV∗×V∗=(V∗∖Σ∗)×V∗,

where V:=N∪Σ is the vocabulary, ∗ is the Kleene star operator, V∗NV∗ indicates concatenation, ∪ denotes set union, and ∖ denotes set minus or set difference. If we do not allow the start symbol to occur in v (the word on the right side), we have to replace V∗ by (V∖{S})∗ on the right side of the cartesian product symbol.

The other types of formal grammar in the Chomsky hierarchy impose additional restrictions on what constitutes a production. Notably in a context-free grammar, the left-hand side of a production must be a single nonterminal symbol. So productions are of the form:

N→(N∪Σ)∗

Grammar generation

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. This stops when a string containing only terminals is obtained. 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 different ways of generating this single string, then the grammar is said to be ambiguous.

For example, assume the alphabet consists of a and b, with the start symbol S, and we have the following rules:

1. S→aSb
2. S→ba

then we start with S, and can choose a rule to apply to it. If we choose rule 1, we replace S with aSb and obtain the string aSb. If we choose rule 1 again, we replace S with aSb and obtain the string aaSbb. This process is repeated until we only have symbols from the alphabet (i.e., a and b). If we now choose rule 2, we replace S with ba and obtain the string aababb, and are done. We can write this series of choices more briefly, using symbols: S⇒aSb⇒aaSbb⇒aababb. The language of the grammar is the set of all the strings that can be generated using this process: {ba,abab,aababb,aaababbb,…}.

More in Computing and communication