# Language and Grammar in Discrete mathematics

### Formal Language

A formal language can be described as a language that is specified with the help of a well-defined set of syntax rules.

### Formal Grammar

A formal grammar G can be described as a compact, precise definition of a Language L.

Now we will learn about some definitions so that we can clearly understand the grammar of phrase structure.

### Vocabulary

A vocabulary V or alphabet can be described as a finite non-empty set of elements, which is also known as the symbols.

### Word

A word over V can be described as a string containing the finite length of elements of V. Word is also known as a sentence.

### Empty String

The empty string can be described as the string which is used to have no symbols. This string is also known as the empty string, which can be indicated by the symbol λ.

### Set of Words and Set of Language

With the help of symbol V*, we can indicate the set of words over V. A language over V can be described as a subset of V*.

### Phrase Structure Grammar

A phrase structure grammar G can be described in the following way:

Where

V is used to indicate the vocabulary

T is used to indicate the subsets T of V, which is used to contain the terminal symbols.

S is used to indicate the start symbol from V

P is used to indicate a finite set of production

With the help of N, we can indicate the set V-T. The elements of N are also known as non-terminal symbols. The left side of every production in P will have a minimum of one non-terminal.

### Derivability

Suppose there is a phrase structure grammar G = (V, T, S, P). Suppose w0 = lz0r (concentration of l, z0, and r) and w1 = lz1r be string over V. If there is a production of G: zo → z1, in this case, w1 will be directly derivable from w1 and in other words it can be written as w0 ⇒ w1. If there are some strings w0, w1, w2, w3, ...., wn over V in such a way that w0 ⇒ w1, w1 ⇒ w2, w2 ⇒ w3, ...., wn-1 ⇒ wn, in this case, wn will be derivable from w0, and it can be written as w0 ⇒ wn. If there is a sequence of steps that are used to get wn from w0, then it will be known as derivation.

### Language Generated by G, L

Suppose there is a phrase structure grammar G = (V, T, S, P). With the help of symbol L(G), we can indicate the language generated by G. It is used to contain the set of all strings of terminals, and we can drive it from the starting state S. The language generated by G can be shown in one another way, which is described as follows:

L(G) = {w ∈ T* | S ⇒ w}

### Types of Grammar

There are various types of grammar and restrictions on production, which are described as follows:

TypeRestrictions on Productions w1 → w2
0There are no restrictions in type 0 grammar
1w1 = lAr and w2 = lwr, where A ∈ N, l, r, w ∈ (N ∪ T) ∗ and w ≠ λ; or w1 = S and w2 = λ as long as S is not on the right-hand side of another production.
2w1 = A, where A is used to indicate the non-terminal symbol.
3w1 = A and w2 = aB or w2 = a, where A ∈ N, B ∈ N, and a ∈ T; or w1 = S and w2 = λ

### Derivation Tree

With the help of context-free grammar, we can generate a derivation in language. We can represent the derivative tree graphically with the help of an ordered rooted tree, which is also known as the derivation or parse tree. The starting symbol of this tree can be represented with the help of root. The non-terminal symbols of the tree are used to indicate the internal vertices. The terminal symbols are indicated by the leaves of a tree. If there is a derivation that contains the production A → w, then the vertex that shows A will contain the children's vertices that are used to show each symbol in w, where w is used to indicate a word. The order of this process will be from left to right.

### Backus-Naur form (BNF)

The BNF can be described as the syntactic rules of many computer languages, such as Java. If there is a type 2 grammar, the production will have a single non-terminal symbol on the LHS. So in place of listing all the productions one by one, we will combine all those productions with the help of same non-terminal symbol on LHS into one statement. To indicate the production, we use the symbol →, but here we will use the symbol ::=. All the non-terminal symbols will be enclosed with the help of bracket (), and all the RHS of productions will be listed in the same statement, which will be separated with the help of bars. The following example can be used to show the BNF: