0% found this document useful (0 votes)
32 views14 pages

TOC Chapter5

This document discusses context free grammars. It defines context free grammars using 4 tuples: variables, terminals, start symbol, and production rules. Production rules are used to recursively generate strings. Derivation trees can be used to represent strings generated from a grammar. Grammars can be ambiguous if more than one derivation tree exists for a string. Examples are provided for designing context free grammars to generate specific languages. Exercises related to context free grammars are listed at the end.

Uploaded by

Saadman Zilani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

TOC Chapter5

This document discusses context free grammars. It defines context free grammars using 4 tuples: variables, terminals, start symbol, and production rules. Production rules are used to recursively generate strings. Derivation trees can be used to represent strings generated from a grammar. Grammars can be ambiguous if more than one derivation tree exists for a string. Examples are provided for designing context free grammars to generate specific languages. Exercises related to context free grammars are listed at the end.

Uploaded by

Saadman Zilani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 5

Context Free Grammar

Lutfun Nahar Lota, Lecturer,CSE, IUT


What is Context Free Grammar
• A context free grammar is a notation for describing languages
• It is more powerful than finite automata or Regular Expressions, but
cannot define all possible
• Applications: can describe certain features that have a recursive structure,
used to compiler technology, describe document formats
• The languages generated by context free grammars are knowns as the
context free languages

08/08/2020 2
Context Free Grammar for Palindromes
•  Examples: madam, radar, madamimadam, 101
1. P
2. P 0
3. P 1
4. P 0P0
5. P 1P1

08/08/2020 3
Context Free Grammar
•Defined
  by 4 tuples- G = {V, S, P}
• V = Set of variables or non-terminal symbols, represents language
• = Set of terminal symbols that forms the strings of language
• S= Start Symbol
• P= Production Rule

08/08/2020 4
Production Rule
• Represents
  the recursive definition of a language
• Collection of substitution rules
• Used to generate patterns of strings
• Each production consists of-
• A variable, called head of the production
• The production symbol
• A string of zero or more terminals and variables
• Example- A aB or A
08/08/2020 5
Context Free Grammar (Example)
•• A  language that generates equal number of a’s and b’s in to form of
• G = { (S,A), (a,b), S, (S aAb, A aAb|)
S aAb
a aAb b “S aAb”
aa aAb bb “S aAb”
aaa bbb “S aAb”
aaa bbb

08/08/2020 6
Derivation Tree/Parse Tree
•  Derivation Tree/Parse Tree is an ordered rooted tree that graphically
represents the semantic information of strings derived from a context free
grammar. Three types of elements-
• Root Vertex- Must be labeled by the start symbol
• Vertex- Labeled by non-terminal symbols
• Leaves- Labeled by terminal symbols or
• G = {(S,A,B) (0,1), S, (S , A , B )}
08/08/2020 7
Parse Tree (Example)
•  G = {(S,A,B) (0,1), S, (S , A , B )}
S

0 B

0 A A

0 A A 𝜖
 

 
𝜖
  08/08/2020 8
Left and Right Derivation Tree
• Left Derivation Tree: is obtained by applying production to the leftmost
variable in each step.
• Right Derivation Tree: is obtained by applying production to the
rightmost variable in each step.

08/08/2020 9
Left and Right Derivation Tree
 For generating the string “aabaa” from the grammar
S aSS|, A
Left Derivation Tree Right Derivation Tree

S S

a S S a S S

a A S 𝜖
 
𝜖
 
a A S

b a a S S b a a S S

𝜖 𝜖
08/08/2020 10

       
Ambiguous Grammar
•  Ambiguous Grammar- if there exists two or more left derivation tree for a string
then it’s called Ambiguous Grammar.
• G = {(S), (a+b, +, *), S,(SS+S|S*S|a|b}

• = a+a+b
S S+S S S*S
a+S S + S *S
a+S* a+S*S
S
a+a*S a+a*b
aa +
+ aa ** bb aa +
+ aa ** bb

08/08/2020 11
Designing Context Free Grammar
• Construct
  a CFG for the language having any number of a’s
• ={a} RE = {a*}
• L = {, a, aa, aaa, …..}
• Production Rule: S aS, S
• Derive a String “aaaaaa”
S
aS
a aS
aa aS
aaa aS
aaaa aS
aaaaa aS
aaaaa aS
aaaaaa
08/08/2020 12
Designing Context Free Grammar
• L
  = { w (a,b)*}
• L = { aacaa, bcb, abcba, abbcbaa, ….}
• Production Rule: S aSa, S bSb, S
• Derive a String “abbcbba”
S
aSa
a bSb a
ab bSb ba
abb c bba

08/08/2020 13
Exercise
Solve the following exercises
5.1.1, 5.1.2, 5.1.5, 5.2.1,5.4.1,5.4.2,5.4.3, 5.4.4, 5.4.5,5.4.6, 5.4.7

08/08/2020 14

You might also like