0% found this document useful (0 votes)
34 views26 pages

Context-Free Grammars: CSE 211 (Theory of Computation)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 26

CSE 211

Context-Free Grammars
CSE 211 (Theory of Computation)

Atif Hasan Rahman

Assistant Professor
Department of Computer Science and Engineering
Bangladesh University of Engineering & Technology

Adapted from slides by


Dr. Muhammad Masroor Ali

Atif Hasan Rahman CFG


CSE 211 CFG

A CFG for palindromes

P→
P→0
P→1
P → 0P0
P → 1P1

Atif Hasan Rahman CFG


CSE 211 CFG

Formal definition of a Context-Free Grammar

A context-free grammar is a 4-tuple (V , Σ, R, S) or


(V , T , P, S), where
i V is a finite set called the variables
ii Σ or T is a finite set, disjoint from V , called the terminals
iii R or P is a finite set of rules or productions, with each
rule being a variable and a string of variables and terminals

variable → string of variables and terminals


iv S ∈ V is the start variable
The variable in the first rule if not explicitly mentioned

Atif Hasan Rahman CFG


CSE 211 CFG

Formal definition of a Context-Free Grammar

Context-free grammar for the palindromes

Gpal = {{P}, {0, 1}, R, P}

i {P} is the set of variables


ii { 0,1 } is the set of terminals
iii R is the set of rules
P →
P →0
P →1
P → 0P0
P → 1P1
iv P is the start variable

Atif Hasan Rahman CFG


CSE 211 CFG

CFG - an example

A CFG that represents a simplification of expressions in a


typical programming language
Restricted to the operators + and *
Identifiers (letters followed by zero or more letters and
digits) allow only the letters a and b and the digits 0 and 1
We need two variables in this grammar
E represents expressions.
It is the start symbol
Represents the language of expressions we are defining
I represents identifiers
Its language is actually regular
It is the language of the regular expression

(a + b)(a + b + 0 + 1)∗

Atif Hasan Rahman CFG


CSE 211 CFG

CFG - an example

Atif Hasan Rahman CFG


CSE 211 CFG

Derivation using a CFG

The sequence of substitutions to obtain a string is called a


derivation
The symbol =⇒ is used to denote yields or derives
* is used to denote zero or more steps
The inference that a ∗ (a + b00) is in the language of
variable E

Atif Hasan Rahman CFG


CSE 211 CFG

Leftmost and Rightmost Derivations

Leftmost derivation
At each step we replace the leftmost variable by one of its
production bodies
Rightmost derivation
At each step we replace the rightmost variable by one of its
production bodies

Atif Hasan Rahman CFG


CSE 211 CFG

Leftmost Derivation

Atif Hasan Rahman CFG


CSE 211 CFG

Rightmost Derivation

Atif Hasan Rahman CFG


CSE 211 CFG

Language of a grammar

If G = (V , Σ, R, S) is a CFG, the language of G denoted


L(G) is the set of terminal strings that have derivations
from the start symbol

If a language L is the language of some context-free


grammar, then L is said to be a context-free language or
CFL

Atif Hasan Rahman CFG


CSE 211 CFG

Parse Trees

Let G = (V , Σ, R, S) be a CFG, the parse trees for G are


trees with following conditions
i Each interior node is labeled by a variable in V
ii Each leaf is labeled by either a variable, a terminal, or 
However, if the leaf is labeled  then it must be the only child
of its parent
iii If an interior node is labeled A, and its children are labeled

X1 , X2 . . . Xk

respectively, from the left, then A → X1 X2 . . . Xk is a rule in


R.
Note that the only time one of the X ’s can be  is if that is the
label of the only child, and A →  is a rule of G

Atif Hasan Rahman CFG


CSE 211 CFG

Parse Tree - example

Atif Hasan Rahman CFG


CSE 211 CFG

Parse Tree - example

Atif Hasan Rahman CFG


CSE 211 CFG

Yield of a Parse Tree

If we look at the leaves of any parse tree and concatenate


them from the left, we get a string, called the yield of the
tree
Of special importance are those parse trees such that
i The yield is a terminal string, That is, all leaves are labeled
either with a terminal or with 
ii The root is labeled by the start symbol
These are the parse trees whose yields are strings in the
language of the underlying grammar
The language of a grammar is the set of yields of those
parse trees having the start symbol at the root and a
terminal string as yield

Atif Hasan Rahman CFG


CSE 211 CFG

Parse Tree - example

Atif Hasan Rahman CFG


CSE 211 CFG

Inference, Derivations, and Parse Trees

The following are equivalent


Recursive inference is a body to head process to infer
membership of a string in a language

Atif Hasan Rahman CFG


CSE 211 CFG

Inference, Derivations, and Parse Trees

Proofs use induction

Atif Hasan Rahman CFG


CSE 211 CFG

Designing CFGs

Design a CFG, for the alphabet Σ = {0, 1}, for the


language

{w|w contains at least three 1s}


100101
01100101
1111

00101000

Atif Hasan Rahman CFG


CSE 211 CFG

Designing CFGs

CFG for the language {w|w contains at least three 1s}


Three 1s separated, preceded, followed by any string
S → X 1X 1X 1X
X s need to generate any string over Σ = {0, 1}
X → 0X |1X |

Atif Hasan Rahman CFG


CSE 211 CFG

Designing CFGs

CFG for the language {w|w contains at least three 1s}


Three 1s separated, preceded, followed by any string
S → X 1X 1X 1X
X s need to generate any string over Σ = {0, 1}
X → 0X |1X |

Atif Hasan Rahman CFG


CSE 211 CFG

Designing CFGs

CFG for the language {w|w contains at least three 1s}


Three 1s separated, preceded, followed by any string
S → X 1X 1X 1X
X s need to generate any string over Σ = {0, 1}
X → 0X |1X |

Atif Hasan Rahman CFG


CSE 211 CFG

Designing CFGs

CFG for the language {w|w contains at least three 1s}


Three 1s separated, preceded, followed by any string
S → X 1X 1X 1X
X s need to generate any string over Σ = {0, 1}
X → 0X |1X |

Atif Hasan Rahman CFG


CSE 211 CFG

Designing CFGs

CFG for the language {w|w contains at least three 1s}


Three 1s separated, preceded, followed by any string
S → X 1X 1X 1X
X s need to generate any string over Σ = {0, 1}
X → 0X |1X |

Atif Hasan Rahman CFG


CSE 211 CFG

Designing CFGs

Designing CFGs require creativity


Some techniques are helpful
Some CFLs are the union of simpler CFLs. Construct a
grammar for each piece and combine them with a rule like

S → S1 |S2 | · · · |Sk

If the CFL happens to be regular and if you can construct a


DFA, it can be easily converted to a CFG
Make a variable Ri for each state qi in the DFA
Add the rule Ri → aRj if δ(qi , a) = qj is a transition
Add the rule Ri →  if qi is an accept state

Atif Hasan Rahman CFG


CSE 211 CFG

Exercise
Design a DFA for the language
{w|w contains at least three 1s} and convert it to CFG

Atif Hasan Rahman CFG

You might also like