Finite Automata C FLV 2
Finite Automata C FLV 2
2
Context Free Language
3
Context Free Language
4
Context-Free Languages
5
Context-Free Grammars
6
Informal Comments
7
CFG
A context-free grammar (CFG) G is a quadruple (V, Σ, R, S) where
V: a set of non-terminal symbols
Σ: a set of terminals (V ∩ Σ = Ǿ)
R: a set of rules (R: V → (V U Σ)*)
S: a start symbol.
➢ Terminals = symbols of the alphabet of the language being defined.
➢ Variables = nonterminals = a finite set of other symbols, each of
which represents a language.
➢ Start symbol = the variable whose language is the one being
defined.
➢ A production has the form variable -> string of variables and
terminals.
8
Informal Comments
9
Example: CFG for { 0n1n | n > 1}
➢ Productions:
➢ S -> 01
➢ S -> 0S1
➢ Basis: 01 is in the language.
➢ Induction: if w is in the language, then so is 0w1.
10
Example: Formal CFG
11
Derivations – Formalism
➢ To generate 000111
➢ 0S1 ------Rule 1
➢ 00S11 ------Rule 1
➢ 000111 ------Rule 2
12
Convention
➢ Convention:
➢ A, B, C,… are variables.
➢ a, b, c,… are terminals.
➢ …, X, Y, Z are either terminals or variables.
➢ …, w, x, y, z are strings of terminals only.
➢ , , ,… are strings of terminals and/or variables.
13
Derivations – Intuition
14
Example: Kleene Closure
15
Leftmost and Rightmost Derivations
16
Leftmost Derivations
17
Example: Leftmost
Derivations
➢ Balanced-parentheses grammmar:
➢ S -> SS | (S) | ()
➢ S =>lm SS =>lm (S)S =>lm (())S =>lm (())()
➢ Thus, S =>*lm (())()
➢ S => SS => S() => (S)() => (())() is a derivation, but not a leftmost
derivation.
18
Rightmost Derivations
19
Example: Rightmost Derivations
➢ Balanced-parentheses grammmar:
S -> SS | (S) | ()
➢ S =>rm SS =>rm S() =>rm (S)() =>rm (())()
➢ Thus, S =>*rm (())()
➢ S => SS => SSS => S()S => ()()S => ()()() is neither a rightmost nor a
leftmost derivation.
20
Example b + b x b
1. E→ E + E
2. E→ E x E
3. E→ b
4. E→ ∈
1. Derivation 2. Parsing tree
E➔E+E
E
➔ id + E
➔ id + E x E
E + E
➔ id + id x E
➔ id + id x id
b E x E
b
Note: rules to be followed when constructing parsing tree b
1. Root vertex = start symbol/non-terminal
2. Leaf vertex = terminals
3. intermediate vertex =non-terminal 21
Summary
22
Pushdown Automata and CFL
24
Components of PDA
A Pushdown Automata(PDA) has 3 components:
1) An input tape
2) A Finite Control Unit
3) A Stack with infinite size
25
Formal Definition of PDA
A Pushdown Automata (PDA) can be defined as -
M = (Q, Σ, Γ, δ, q0, Ζ, F) where
• Q is a finite set of states
• ∑ is a finite set of input symbols
• Γ is a finite stack alphabet (which can be pushed and
popped from stack)
• q0 is the start state
• Z is the start stack symbol
• F is the set of final/accepting states
• δ is a transition function which maps Q x {Σ ∪ ∈} x Γ into
Q x Γ*. In a given state, PDA will read input symbol and
stack symbol (top of the stack) and move to a new state
and change the symbol of stack.
26
Cont…
δ takes as argument a triple δ (q, a, X) where:
(i) q is a State in Q
(ii) a is either an Input Symbol in Σ or a= ∈
(iii)X is a Stack Symbol, that is a member of Γ
The output of δ is finite set of pairs (p, y)
where: p is a new state
y is a string of stack symbols that replaces X at the top of
the stack
Eg. If y= ∈ then the stack is popped
If y=X then the stack is unchanged
If y = YZ then X is replaced by Z and Y is pushed onto the
stack
27
Transition Operation
28
Example: L={0n1n|n>=0}
Start ∈, ∈→Z 1, 0→ ∈ ∈, Z→ ∈
q0 q1 q2 q3
29
Instantaneous Descriptions
30
Application of Pushdown
Automata(PDA)
31
Properties of CFLs
Closure Properties
Union
Concatenation
Kleene Star
Intersection with Regular Languages
Non-Closure Properties
Intersection with other CFLs
Complement
32
Applications of CFLs and PDAs
33
Advantages and Limitations
Advantages:
More expressive than regular languages.
Suitable for describing nested structures.
Limitations:
Cannot handle all possible languages (e.g., context-sensitive
languages).
34
35