What is Grammar
Grammar is a finite set of rules for generating syntactically correct sentences or meaningful correct sentences. These rules are called Productions.
Each production rule is composed of two elements one is Variables and the other is Terminals.
- Variables are the symbols that are used to generate the sentences, but not the part of the sentences. Variables are represented by capital letters. Variables are also known as Non-Terminals.
- Terminals are the symbols that are the components of the sentences generated by the given grammar. Terminals are represented by small letters.
- Productions are the rules of Grammar that specify the substitution of variables. Productions are in the form of .
where is a Non-Terminal and is a Terminal
Formal definition of Grammar
Generally, Grammar G is represented by four tuples. G = (V, T, P, S)
Example:
Consider a Grammar G = (V, T, P, S) |
Types Of Grammar
According to Chomsky, there are four types of Grammar.
Type-0 Grammar
All the productions are in the form of
Where,
Epsilon is not allowed in
Type-1 Grammar
All the productions are in the form of
Where,
and
Epsilon is allowed neither in nor in and length of must be less than or equal to
Type-2 Grammar
All the productions are in the form of
Where,
and
Length of must be 1.
Type-3 Grammar
All the productions are in the form of
Where,
and
Length of must be 1. A grammar can only be Left Linear Grammar or Right Linear Grammar. Any type-3 grammar cannot be both LLG and RLG.
Note: Chomsky Hierarchy only works for grammar with no null production.