ArticleslgStudy

mathematics

Formal grammar

Formal grammar is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Formal grammar rather than just read about it. In short: A formal grammar is a set of symbols and the production rules for rewriting some of them into every possible string of a formal language over an alphabet. A grammar does not describe the meaning of the strings—only their form.

Formal grammar — main illustration
Formal grammar — illustration

Key takeaways

  • Formal grammar belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Formal grammar to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Formal grammar from memory before moving on to harder problems.

Reference excerpt

A formal grammar is a set of symbols and the production rules for rewriting some of them into every possible string of a formal language over an alphabet. A grammar does not describe the meaning of the strings—only their form. In applied mathematics, formal language theory is the discipline that studies formal grammars and languages. Its applications are found in theoretical computer science, theoretical linguistics, formal semantics, mathematical logic, and other areas. A formal grammar is a set of rules for rewriting strings, along with a "start symbol" from which rewriting starts. Therefore, a grammar is usually thought of as a language generator. However, it can also be used as the basis for a parser—a computer program that determines whether a given string is well formed. Formal grammars for parser generators are often written in metasyntax notations. To describe such parsers, formal language theory uses separate formalisms, known as automata theory. One result of automata theory is that it is not possible to design a recognizer for certain formal languages. Many languages have the meanings of their strings structured according to their syntax—a practice known as compositional semantics. In these cases, the first step to describing the meaning of a string is to break it down part by part and look at its analyzed form (known as its parse tree in computer science, and as its deep structure in generative grammar).

Introductory example A grammar mainly consists of a set of production rules, rewrite rules for transforming strings. Each rule specifies a replacement of a particular string (its left-hand side) with another (its right-hand side). A rule can be applied to each string that contains its left-hand side and produces a string in which an occurrence of that left-hand side has been replaced with its right-hand side. Unlike a semi-Thue system, which is wholly defined by these rules, a grammar further distinguishes between two kinds of symbols: nonterminal and terminal symbols; each left-hand side must contain at least one nonterminal symbol. It also distinguishes a special nonterminal symbol, called the start symbol. The language generated by the grammar is defined to be the set of all strings without any nonterminal symbols that can be generated from the string consisting of a single start symbol by (possibly repeated) application of its rules in whatever way possible. If there are essentially different ways of generating the same single string, the grammar is said to be ambiguous. In the following examples, the terminal symbols are a and b, and the start symbol is S.

Example 1 Suppose we have the following production 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 obtain the string aSb. If we then choose rule 1 again, we replace S with aSb and obtain the string aaSbb. 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 infinite set {anbabn∣n≥0}={ba,abab,aababb,aaababbb,…}, where ak is a repeated k times (and n in particular represents the number of times production rule 1 has been applied). This grammar is context-free (only single nonterminals appear as left-hand sides) and unambiguous.

Examples 2 and 3 Suppose the rules are these instead:

1. S→a 2. S→SS 3. aSa→b This grammar is not context-free due to rule 3 and it is ambiguous due to the multiple ways in which rule 2 can be used to generate sequences of Ss. However, the language it generates is simply the set of all nonempty strings consisting of as and/or bs. This is easy to see: to generate a b from an S, use rule 2 twice to generate SSS, then rule 1 twice and rule 3 once to produce b. This means we can generate arbitrary nonempty sequences of Ss and then replace each of them with a or b as we please. That same language can alternatively be generated by a context-free, nonambiguous grammar; for instance, the regular grammar with rules

1. S→aS 2. S→bS 3. S→a 4. S→b

Definition

The syntax of grammars In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s, a grammar G consists of the following components:

A finite set N of nonterminal symbols. A finite set Σ of terminal symbols that is disjoint from N. A finite set P of production rules, each rule of the form (Σ∪N)∗N(Σ∪N)∗→(Σ∪N)∗ where ∗ is the Kleene star operator and ∪ denotes set union. That is, each production rule maps from one string of symbols to another, where the first string (the "head") contains an arbitrary number of symbols provided at least one of them is a nonterminal. In the case that the second string (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 Λ, e or ϵ) in order to avoid confusion. Such a rule is called an erasing rule. A distinguished symbol S∈N that is the start symbol, also called the sentence symbol. A grammar is formally defined as the tuple (N,Σ,P,S). Such a formal grammar is often called a rewriting system or a phrase structure grammar in the literature.

Some mathematical constructs regarding formal grammars The operation of a grammar can be defined in terms of relations on strings:

Given a grammar G=(N,Σ,P,S), the binary relation ⇒G (pronounced as "G derives in one step") on strings in (Σ∪N)∗ is defined by: x⇒Gy⟺∃u,v,p,q∈(Σ∪N)∗:(x=upv)∧(p→q∈P)∧(y=uqv) the relation ⇒G∗ (pronounced as G derives in zero or more steps) is defined as the reflexive transitive closure of ⇒G a sentential form is a member of (Σ∪N)∗ that can be derived in a finite number of steps from the start symbol S; that is, a sentential form is a member of {w∈(Σ∪N)∗∣S⇒G∗w}. A sentential form that contains no nonterminal symbols (i.e. is a member of Σ∗) is called a sentence. the language of G, denoted as 𝑳(G), is defined as the set of sentences built by G. The grammar G=(N,Σ,P,S) is effectively the semi-Thue system (N∪Σ,P), rewriting strings in exactly the same way; the only difference is in that we distinguish specific nonterminal symbols, which must be replaced in rewrite rules, and are only interested in rewritings from the designated start symbol S to strings without nonterminal symbols.

… excerpt ends here. Continue reading the full article.

Illustrations

Formal grammar: Example of a simple formal grammar (left) with parsed sentence "the dog ate the bone" (right). Formal grammars consist of a set of non-terminal symbols, terminal symbols, production rules, and a designated start symbol.
Example of a simple formal grammar (left) with parsed sentence "the dog ate the bone" (right). Formal grammars consist of a set of non-terminal symbols, terminal symbols, production rules, and a designated start symbol.

Worked examples

Example 1 — a first encounter with Formal grammar

Start with the simplest possible case. Write down what Formal grammar claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Formal grammar before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Formal grammar ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Formal grammar

In research
Formal grammar appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Formal grammar in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Formal grammar is common in secondary-school and first-year university syllabi. It links to neighbouring topics Automata (computation), Formal languages, Grammar, so understanding it makes those chapters shorter.
In everyday life
Look for Formal grammar outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Formal grammar” →

How to study Formal grammar in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Formal grammar means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Formal grammar out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Formal grammar in simple terms?

A formal grammar is a set of symbols and the production rules for rewriting some of them into every possible string of a formal language over an alphabet. A grammar does not describe the meaning of the strings—only their form.

Why does Formal grammar matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Formal grammar?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Formal grammar.

Tags

  • Automata (computation)
  • Formal languages
  • Grammar
  • Mathematical linguistics
  • Mathematical logic
  • Syntax

Keep exploring