Lably
Skip to content

Formal grammar

Structure of a formal language

  • 9 min read
  • 4 chapters

Related on Lably

Through String (computer science), Semantics and Mathematical logic

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 function in computing that determines whether a given string belongs to the language or is grammatically incorrect. 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

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Language and linguistics