You are on page 1of 15

CSC3113

Theory Of Computation

Context-Free Languages
Context-Free Grammars

Formal Definition

Designing

Ambiguity

Chomsky Normal Form

Pushdown Automata

Formal Definition

Equivalence with Context-Free Grammars

Mashiour Rahman
Assistant Professor
Computer Science Department
American International University-Bangladesh

Context-Free Languages
Context-Free Languages includes all regular
languages and many additional languages,
such as {0n1n | n 0}.
Additional memory is used to recognize such
languages.
Context-Free Grammars.
More powerful method of describing language.
Can describe certain features that have a
recursive structure.
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 2

Context-Free Grammars
Following is an example of a context-free grammar
A 0A1

A 0A1 | B

AB

or

B#

B#
A grammar consists of a collection of Substitution (or Production) rules.
Each rule appears as a line in the grammar.
Each line has a variable and a string separated by an arrow.
Variable consists of a symbol.
represented as uppercase letters.
might include index or subscript.

Strings consists of variables and/or other symbols called terminals.


terminals are analogous to input alphabet, represented as lowercase letters,
numbers, or special symbols.
might include index or subscript.

One variable is designated the start variable, usually the left-hand side of the
topmost rule.
Several rules for the same left-hand variable can be written using | as an or.
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 3

Context-Free Grammars
Use a grammar to describe a language by
generating each string of the language in the
following manner

1.

Write down the start variable, s.

2.

Choose a variable v from the right-hand side of


the rule for s.

3.

Find a rule that starts with (or left-hand side is) v.

4.

Replace v in rule s by right-hand side of rule v.

5.

Repeat from step 2 to 4 until no more variables


remains.

The sequence of substitutions to obtain a string is


called derivation. Derivation of string 000#111 in
the grammar
A 0A1 | B
B#

A 0A1 00A11 000A111 000B111 000#111


Using Parse Tree: Figure on the right
0

CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 4

Formal Definition
A context-free grammar is a 4-touple (V, , R, S),
where
V is a finite set called variables,
is a finite set, disjoint from V, called the alphabet,
R is a finite set of rules, with each rule being a variable and a
strings of variables and terminals, and
SV is the start variable.

If u, v, and w are strings of variables and terminals, and


A w is a rule of the grammar, we say that uAv yields uwv,
written uAv uwv.
If u = v or if a sequence u1, u2, , uk exists for k 0
and u u1 u2 uk v, we say u yields v,
*

written as u v.
*

The language of the grammar is {w* | S w}.


CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 5

Designing
CFG that is union of simpler CFGs:
Break into simpler pieces and Construct individual grammars for
each piece.
Combine them into one grammar by putting all the rules together
and adding a new rule, S s1 | s2 || sk, where the
variables si are the start variables for the individual grammars.

Example:

{strings containing n, n0, number of 0s and 1s}


= {1n0n | n0} {0n1n | n0}
{0n1n | n0} can be represented S1 0S11 |
{1n0n | n0} can be represented S2 1S20 |
{1n0n | n0} {0n1n | n0} can be represented
S S1 | S2
S1 0S11 |
S2 1S20 |
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 6

Designing
CFG for regular language:
Construct a DFA M=(Q, , , q0, F) for the regular language.
Convert the DFA M into equivalent CFG as follows
Variable Ri for each state qiQ. Make R0 the start variable for the start state q0.
Add rule Ri aRj to the CFG for each (qi, a) = qj.

Example:
Language: A = {w | the sum of all the symbols in w is an even number }
DFA:
M=(Q, , , q0, F), where, Q={b0,b1}, q0=b0, F={b0}, ={0,1}

b0
b1

0
b0
b1

1
b1
b0

2
b0
b1

CFG:
Grammar G = (V, , R, R0), where, V = {R0, R1}, = {0, 1, 2}, and
R is the set of rules
R0 0R0 | 1R1 | 2R0
R1 0R1 | 1R0 | 2R1
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 7

Designing
Language containing strings with two
substrings that are linked or depended on
each other by their number of appearances.
Use rule of the form R uRv.
This generates strings wherein the portion
containing the us corresponds to the portion
containing vs.

Example:
Language: {1n0n | n0}
CFG: S 1S0 |
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 8

Designing
Language containing strings with certain structures appearing recursively
as part of other (or the same) structure:
Place the variable symbol generating the structure in the location of the
rules corresponding to where that structure may recursively appear.

Example: G=(V,,R,EXPR), where, V={EXPR,TERM,FACTOR},


= {a, +, , (, )} and R is the rules
EXPR EXPR + TERM | TERM
TERM TERMFACTOR | FACTOR
FACTOR (EXPR) | a
Consider the string (a+a)a.
EXPR
TERM
TERMFACTOR
FACTORa
(EXPR)a
(EXPR + TERM)a
(FACTOR+FACTOR)a
(a+a)a
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 9

Ambiguity Parse Tree


If a grammar generates the same string in several different ways, we say that the
string is derived ambiguously in the grammar.
If a grammar generates some string ambiguously we say that the grammar is
ambiguous.
Example: Grammar G, EXPREXPR+EXPR|EXPREXPR|(EXPR)|a
Two parse trees for the string a+aa in G
EXPR

EXPR

EXPR

EXPR

EXPR

EXPR

EXPR

EXPR

EXPR

CSC3113 Theory Of Computation

Mashiour Rahman

EXPR

Context-Free Grammars 10

Ambiguity - Derivation
When we say that a grammar generates a string
ambiguously, we mean that the string has two different parse
trees, not two different derivations.
A derivation of string w in a grammar G is a leftmost
derivation if at every step the leftmost remaining variable is
the one replaced.
Then we can say, a string w is derived ambiguously in CFG
G if it has two or more different leftmost derivations.
Grammar G is ambiguous if it generates some string
ambiguously.
Some CFLs can only be generated by ambiguous grammars.
Such languages are called inherently ambiguous.
Example: {0i1j2k | i=j or j=k}
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 11

Chomsky Normal Form


It is often convenient to have CFGs in simplified
form. One such form is Chomsky normal form.
A context free grammar is in Chomsky normal form if
every rule is of the form
A BC
A a
where a is any terminal and A, B, and C are any
variables except that B and C may not be the start
variable.
In addition S is permitted, where S is the start
variable.
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 12

Convert any grammar G to Chomsky Normal Form


Add a new start symbol S0 and the new rule S0S,
where S was the original start symbol.
Eliminate all rules of the form A , where A is
not the start symbol.
Add rule Ruv for every rule of the form RuAv,
where u and v are strings of variables and terminals.
Add such rules for every occurrence of A. for
example, add RuvAw, RuAvw, Ruvw for the
rule of the form RuAvAw.
Add R for the rule of the form RA, unless we
have previously removed the rule R.
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 13

Convert any grammar G to Chomsky Normal Form


Eliminate all unit rules of the form A B.
Add rule A u for the rule of the form B u, unless this was
a unit rule previously removed.
Here u is a string of variables and terminals.

Convert remaining rules into proper form, RPQ and Ru.


We replace each rule of the form A u1u2uk with the rules
Au1A1, A1u2A2, A2u3A3, , Ak-2uk-1uk.
Here k 3 and each ui is a variable or terminal symbol, and
Ais are new variables.
If k 2, replace any terminal ui in the preceding rule(s) with the
new variable Ui and the rule Uiui.

The above procedure converts a Grammar to a chomsky


normal form. Next we will go through an example.
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 14

Example
Original Grammar
S ASA | aB
A B | S

Add
Add
Remove
Remove
Remove
rule
ruleAU
S
S
A
B
A
B
S
a
1
0 SA
1

B b |
S0
B |
AA
S 11 |||UaB
ASA
AA
||aaa|||SA
SA
SA|||AS
AS
AS
1aB
S

AA
UaB
aaa ||| SA
|||AS
1 |
1B |
S

AA
|
|
SA
AS
ASA
|
aB
|
SA
AS | S
1
S
A b | AA1 | U1B | a | SA | AS
A

b
|
AA
1 |
A

B
b
|
S
ASA
| aB
aB || aa || SA
SA || AS
AS
A

B
|
S
|

B b
B

b
B

SA
B1 b
b |
A
U11
SA
a

Action: Add
Remove
Converting
new start
unit
rules
into
rules
symbol
of
proper
the
of the
form
form
form
R
RPQ
R
Pand Ru
CSC3113 Theory Of Computation

Mashiour Rahman

Context-Free Grammars 15

You might also like