Classification of Grammars
Classification of Grammars
GRAMMAR
presented by
Nivea
CHOMSKY CLASSIFICATION
OF GRAMMARS
The Chomsky hierarchy is a system for classifying formal
grammars and languages in computer science and
linguistics.According to Noam Chomosky, there are four types
of grammars
TYPE 0: UNRESTRICTED GRAMMAR
Example
S → ACaB
Bc → acB
CB → DB
These grammars, known as phrase structure grammars, contain productions of
the form:
where both α and β can be strings of terminals (Ts) and non-terminals (NTs).
Such productions permit arbitrary substitution of strings during derivation or
reduction, hence they are not relevant to the specification of programming
languages.
TYPE 1: CONTEXT-SENSITIVE GRAMMAR
The productions must be in the form
αAβ→αγβ
where A ∈ N (Non-terminal)
and α, β, γ ∈ (T ∪ N)* (Strings of terminals and non-terminals)
count symbol in LHS must be less than or equal to RHS.
Example
AB → AbBc
A → bcA
B→b
These grammars are known as context-sensitive grammars because their productions specify
that derivation or reduction of strings can take place only in specific contexts. A Type-1
production has the form:
Thus, a string in a sentential form can be replaced by 'A' (or vice versa) only when it is
enclosed by the strings and these grammars are also not particularly relevant for
programming language (PL) specification since recognition of PL constructs is not context-
sensitive.
TYPE 2: CONTEXT-FREE GRAMMAR
Grammar Production for Type 2 is given by
A —> α
which can be applied independently of its context. These grammars are therefore
known as context-free grammars (CFGs). CFGs are ideally suited for programming
language specification. Two best-known uses of Type-2 grammars in PL
specification are the ALGOL-60 specification (Naur, 1963) and Pascal specification
(Jensen, Wirth, 1975).
TYPE 3: REGULAR GRAMMAR
Type-3 grammars must have a single non-terminal on the left-hand side and
a right-hand side consisting of a single terminal or single terminal followed
by a single non-terminal
The productions must be in the form X → a or X → aY
where X, Y ∈ N (Non terminal)
and a ∈ T (Terminal)
Example
X→ε
X → a | aY
Y→b
Type-3 grammars are characterized by productions of the form:
Note that these productions also satisfy the requirements of Type-2 grammars. The
specific form of the right-hand side alternatives—namely a single terminal (T) or a string
containing a single terminal and a single non-terminal (NT)—gives some practical
advantages in scanning. However, the nature of the productions restricts the expressive
power of these grammars.
OPERATOR GRAMMAR
An operator grammar is a grammar where none of the productions
contain two or more consecutive non-terminals in any right-hand side
alternative
Example
E->E+E/E*E/id
THANK YOU!!