chapter 3 Slides
chapter 3 Slides
Chapter Three
Formal Languages and Automata
Yoseph K, Instructor
[email protected]
Office Hours: Monday - Friday
Introduction
CLASSIFICATION OF GRAMMARS
A grammar is a set of rules for putting strings together and
so corresponds to a language.
A grammar G can be formally written as a 4-tuple (N, T, S,
P) where
N or V is a set of Non-terminal symbols
T or ∑ is a set of Terminal symbols
S is the Start symbol, S ∈ N
P is Production rules for Terminals and Non-terminals
Summary
Type-3 grammars generate regular languages.
Type-2 grammars generate context-free languages.
Type-1 grammars generate context-sensitive languages.
Type-0 grammars generate recursively enumerable languages.
Context-Free Grammar
Definition: A context-free grammar (CFG) consisting of a finite
set of grammar rules is a quadruple (N, T, P, S) where
N or V is a set of non-terminal symbols.
T is a set of terminals
P is a set of rules,
S is the start symbol.
Example
The grammar ({A}, {a, b, c}, P, A), P : A → aA, A → abc.
The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε
Cont’d
Example:-
for generating a language that generates equal number of a’s
& b’s in the form an bn , the CFG will be defined as
G={(S,A), (a,b), (S → aAb, A → aAb| ε)}
Solution:
S → aAb
→ aaAbb [by production rule of A →aAb]
→ aaaAbbb [by production rule of A →aAb]
→ aaabbb [by production rule of A → ε]
→ a3 b3 = an bn
Cont’d
Example2. Consider the grammar
S → AB (1)
A → C (2)
CB → Cb (3)
C → a (4)
where {a, b} are terminals, and {S, A, B, C} are non-terminals.
We can derive the phrase “ab” from this grammar in the following
way: S → AB, from (1)
→ CB, from (2)
→ Cb, from (3)
→ ab, from (4)
Generation of Derivation Tree
A derivation tree or parse tree is an ordered rooted tree that
graphically represents the semantic information a string derived from a
context-free grammar.
Representation Technique
Root vertex − Must be labeled by the start symbol.
Vertex − Labeled by a non-terminal symbol.
Leaves − Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree /
derivation tree will be as follows −
Cont’d
Q1. Language?
Q2 Derivations in Tree Form ?
Derivations in Tree Form
CFL Closure Property
Context-free languages are closed under −
Union
Concatenation
Kleene Star operation
[Union]
Let L1 and L2 be two context free languages. Then L1 ∪ L2 is also
context free
[Concatenation]
If L1 and L2 are context free languages, then L1L2 is also context
free.
[Kleene Star]
If L is a context free language, then L* is also context free.
Arithmetic expressions in a programming
language