# Introduction to Grammars – Tutorial

# Introduction to Grammars

n the literary sense of the term, grammars denote syntactical rules for conversation in natural languages. Linguistics have attempted to define grammars since the inception of natural languages like English, Sanskrit, Mandarin, etc.

The theory of formal languages finds its applicability extensively in the fields of Computer Science. **Noam Chomsky** gave a mathematical model of grammar in 1956 which is effective for writing computer languages.

## Grammar

A grammar **G** can be formally written as a 4-tuple (N, T, S, P) where –

**N**or**V**is a set of variables or non-terminal symbols._{N}**T**or**â**is a set of Terminal symbols.**S**is a special variable called the Start symbol, S â N**P**is Production rules for Terminals and Non-terminals. A production rule has the form Î± â Î², where Î± and Î² are strings on V_{N}âª â and least one symbol of Î± belongs to V_{N}.

### Example

Grammar G1 –

({S, A, B}, {a, b}, S, {S â AB, A â a, B â b})

Here,

**S, A,**and**B**are Non-terminal symbols;**a**and**b**are Terminal symbols**S**is the Start symbol, S â NProductions,

**P : S â AB, A â a, B â b**

### Example

Grammar G2 –

(({S, A}, {a, b}, S,{S â aAb, aA â aaAb, A â Îµ } )

Here,

**S**and**A**are Non-terminal symbols.**a**and**b**are Terminal symbols.**Îµ**is an empty string.**S**is the Start symbol, S â NProduction

**P : S â aAb, aA â aaAb, A â Îµ**

## Derivations from a Grammar

Strings may be derived from other strings using the productions in a grammar. If a grammar **G** has a production **Î± â Î²**, we can say that **x Î± y** derives **x Î² y** in **G**. This derivation is written as –

**x Î± y âG x Î² y**

### Example

Let us consider the grammar –

G2 = ({S, A}, {a, b}, S, {S â aAb, aA â aaAb, A â Îµ } )

Some of the strings that can be derived are –

S â __aA__b using production S â aAb

â a__aA__bb using production aA â aAb

â aaa__A__bbb using production aA â aAb

â aaabbb using production A â Îµ