Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

Department of Computer and Science Engineering (CSE)

UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE
ENGINEERING
Bachelor of Engineering
Theory of Computation (CST-353)

Topic: Context Free Language DISCOVER . LEARN .


EMPOWER
Department of Computer and Science Engineering (CSE)

Learning Objectives & Outcomes


Objective:
• To understand the concept of CFL, Parse tree.

Outcome:
• Student will understand the
 To know about CFG and its definition
Department of Computer and Science Engineering (CSE)

Context Free Grammar


• A context-free grammar (CFG) is a set of recursive
rewriting rules (or productions) used to generate
patterns of strings.
Context-free grammar G can be defined by four tuples as:
• G = (V, T, P, S)
Where,
• G is the grammar.
• V is the final set of a non-terminal symbol. Like A,B,C etc
• T is the final set of a terminal symbol. like; a, b, c,1 , 2 etc.
• P is a set of production rules.
• S is the start symbol which is used to derive the string.
Department of Computer and Science Engineering (CSE)

Examples of Context Free Grammar:

• {0n1n:n ≥ 0}{0n1n: n ≥ 0} is context-free because it is


generated by the context-free grammar ({S}, {0,1}, R,S)
({S}, {0,1}, P, S), where the set of rules, P, is as;

• S→0S1∣ε
(ε to denote the empty or null string.)
Department of Computer and Science Engineering (CSE)

Example #2
• Language of balanced paranthesis
e.g., ()(((())))((()))….
• CFG?

G:
S => (S) | SS | 

How would you “interpret” the string “(((()))()())” using this grammar?

5
Department of Computer and Science Engineering (CSE)

Example #3
• A grammar for L = {0m1n | m≥n}

• CFG?
G:
S => 0S1 | A
A => 0A | 

How would you interpret the string “00000111”


using this grammar?

6
Department of Computer and Science Engineering (CSE)

Applications of CFLs & CFGs


• Compilers use parsers for syntactic checking
• Parsers can be expressed as CFGs
1. Balancing paranthesis:
• B ==> BB | (B) | Statement
• Statement ==> …
2. If-then-else:
• S ==> SS | if Condition then Statement else Statement | if Condition then
Statement | Statement
• Condition ==> …
• Statement ==> …
3. C paranthesis matching { … }
4. Pascal begin-end matching
5. YACC (Yet Another Compiler-Compiler)

7
Department of Computer and Science Engineering (CSE)

Derivation trees

Derivation tree is a graphical representation for the


derivation of the given production rules for a given CFG. It is
the simple way to show how the derivation can be done to
obtain some string from a given set of production rules. The
derivation tree is also called a parse tree.

A parse tree contains the following properties:


•The root node is always a node indicating start symbols.
•The derivation is read from left to right.
•The leaf node is always terminal nodes.
•The interior nodes are always the non-terminal nodes.
Department of Computer and Science Engineering (CSE)

Example :
Production rules:
•E = E + E
•E = E * E
•E = a | b | c

Input
•a * b + c
Department of Computer and Science Engineering (CSE)

Step 1:

Step 2:
Department of Computer and Science Engineering (CSE)

Step 3:

Step 4:
Department of Computer and Science Engineering (CSE)

Step 5:
Department of Computer and Science Engineering (CSE)

Left-most & Right-most Derivation Styles


G:
E => E+E | E*E | (E) | F
F => aF | bF | 0F | 1F | 
Derive the string a*(ab+10) from G: E =*=>G a*(ab+10)
E E
==> E * E ==> E * E

==> F * E ==> E * (E)

Left-most ==> aF * E ==> E * (E + E)


Right-most
derivation: ==> a * E ==> E * (E + F)
derivation:
==> a * (E) ==> E * (E + 1F)

Always ==> a * (E + E) ==> E * (E + 10F)


Always
substitute ==> a * (F + E) ==> E * (E + 10)
substitute
leftmost ==> a * (aF + E) ==> E * (F + 10)
rightmost
==> a * (abF + E) ==> E * (aF + 10)
variable variable
==> a * (ab + E) ==> E * (abF + 0)

==> a * (ab + F) ==> E * (ab + 10)

==> a * (ab + 1F) ==> F * (ab + 10)

==> a * (ab + 10F) ==> aF * (ab + 10)

==> a * (ab + 10) ==> a * (ab + 10)

13
Department of Computer and Science Engineering (CSE)

Leftmost vs. Rightmost derivations

Q1) For every leftmost derivation, there is a rightmost


derivation, and vice versa. True or False?

True - will use parse trees to prove this

Q2) Does every word generated by a CFG have a leftmost


and a rightmost derivation?

Yes – easy to prove (reverse direction)

Q3) Could there be words which have more than one


leftmost (or rightmost) derivation?

Yes – depending on the grammar


14
Parse trees

15
Department of Computer and Science Engineering (CSE)

Parse Trees
• Each CFG can be represented using a parse tree:
– Each internal node is labeled by a variable in V
– Each leaf is terminal symbol
– For a production, A==>X1X2…Xk, then any internal node labeled A
has k children which are labeled from X1,X2,…Xk from left to right

Parse tree for production and all other subsequent productions:


A ==> X1..Xi..Xk A

X1 … Xi … Xk

16
Examples

Recursive inference
A
E + E
0 A 0
F F

Derivation
1 A 1
a 1

Parse tree for 0110


Parse tree for a + 1
G: G:
E => E+E | E*E | (E) | F A => 0A0 | 1A1 | 0 | 1 | 
F => aF | bF | 0F | 1F | 0 | 1 | a | b
17
Parse Trees, Derivations, and Recursive
Inferences
Production:
A ==> X1..Xi..Xk
A

Derivation
Recursive

X1 … Xi … Xk
inference

Left-most Parse tree


derivation

Derivation Right-most
Recursive
derivation
inference
18
Department of Computer and Science Engineering (CSE)

Interchangeability of different CFG representations


• Parse tree ==> left-most derivation
– DFS left to right
• Parse tree ==> right-most derivation
– DFS right to left
• ==> left-most derivation == right-most derivation
• Derivation ==> Recursive inference
– Reverse the order of productions
• Recursive inference ==> Parse trees
– bottom-up traversal of parse tree

19
Department of Computer and Science Engineering (CSE)

Outcomes

• Knowledge of applications of CFLs & CFGs


• Knowledge of CFG
• Knowledge of parse trees
Department of Computer and Science Engineering (CSE)

FAQ :

1. Find L(G),where P consists of S ® asb , s ® ab .


2. What are the closure properties of context- free
Language?
3. Write CFG for L(G) = {a m b n c p / m + n = p , p ³ 1}.
Department of Computer and Science Engineering (CSE)

REFERENCES :
• Martin J.C., “Introduction to Languages and Theory of
Computation”, Tata McGraw-Hill Publising Company
Limited, 3rd Edition.
• Daniel I.A. Cohen, “Introduction to computer Theory”, John
Wiley.
• K.L.P MISHRA, “Introduction to computer Theory” .
• https://youtu.be/NNSK4570Kho
• https://youtu.be/A_Zj-WaA2Lo
THANK YOU

University Institute of Engineering (UIE)

You might also like