You are on page 1of 22

Theory of Computation (With Automata Theory)

Context-Free Languages
Context-Free Grammars

Introduction

Finite automatons and regular


expressions are tools that are
used for describing regular
languages.

Context-free grammars are


more powerful tools because
they can also describe nonregular languages.

In linguistics, a grammar is a
system of rules by which
sentences are constructed by
putting together words of the
language.

Context-Free Languages

* Property of STI
Page 1 of 22

Theory of Computation (With Automata Theory)

Example of a simple grammar, G1,


in English:
1. <sentence>
<noun phrase> <predicate>
2. <noun phrase>
<article> <noun>
3. <predicate> <verb>
4. <article> a
5. <article> the
6. <noun> boy
7. <noun> girl

8. <verb> smiles
9. <verb> laughs

Context-Free Languages

* Property of STI
Page 2 of 22

Theory of Computation (With Automata Theory)

To form the sentence the girl


smiles:
<sentence> <noun phrase>
<predicate>
<sentence> <article> <noun>
<predicate>

<sentence> <article> <noun>


<verb>
<sentence> the <noun> <verb>
<sentence> the girl <verb>
<sentence> the girl smiles

Context-Free Languages

* Property of STI
Page 3 of 22

Theory of Computation (With Automata Theory)

In theory of computation, a
grammar is a system of rules
by
which
strings
are
constructed by putting together
symbols of the language.

Example: The grammar, G2,


for language L = {w {0,1}* w
= wR} is
1.
2.
3.
4.
5.

S
S0
S1
S 0S0
S 1S1

To form the string 1010101:


S 1S1
S 10S01
S 101S101
S 1010101
Context-Free Languages

* Property of STI
Page 4 of 22

Theory of Computation (With Automata Theory)

A context-free grammars is
simply a set of rules called
substitution
rules
or
productions.

The left-hand side of each


production has a symbol,
called a variable, followed by
an arrow and a string.

The right-hand side of each


production
has
a
string
composed of variables and
other
symbols
called
terminals.
Variables are
symbols that can be replaced
while terminals are symbols
that cannot be replaced.

One variable is assigned as


the start variable. It usually
appears on the left-hand side
of the first production.

Context-Free Languages

* Property of STI
Page 5 of 22

Theory of Computation (With Automata Theory)

By following the rules of a


grammar, each string of that
language can be generated.
The procedure is:
1. Write down the rule that
contains the start variable.
It is usually the first rule of
the grammar.
2. Find a variable on the
right-hand side of the rule
written in step 1 and find
another rule that starts with
that variable. Replace or
substitute the variable with
the right-hand side of that
rule.
3. Repeat step 2 until no
variables remain.
The sequence of substitutions
performed to obtain a string is
called a derivation.

Context-Free Languages

* Property of STI
Page 6 of 22

Theory of Computation (With Automata Theory)

For convenience, rules with the


same left-hand side variable may
be combined into a single rule with
their right-hand side strings
separated by a .

For grammar G1:


1. <sentence>
<noun phrase> <predicate>
2. <noun phrase>
<article> <noun>
3. <predicate> <verb>
4. <article> a the
5. <noun> boy girl
6. <verb> smiles laughs

For grammar G2:


1. S 0 1 0S0 1S1

Context-Free Languages

* Property of STI
Page 7 of 22

Theory of Computation (With Automata Theory)

All strings generated or derived


from a grammar G constitute the
language of that grammar and is
written as L(G).

For grammar G1:


L(G1) = {the girl smiles, the girl
laughs, the boy smiles,
the boy laughs, a girl
smiles, a girl laughs, a
boy smiles, a boy laughs}
For grammar G2:
L(G2) = {w

{0,1}* w = wR}

Any language that can be


generated by some context-free
grammar is called a context-free
language.

Context-Free Languages

* Property of STI
Page 8 of 22

Theory of Computation (With Automata Theory)

Formal Definition of Context-Free


Grammars

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

1. V is a
variables,

finite

set

of

2.

finite

set

of

is a
terminals,

3. R is a finite set of rules,


with each rule being a
variable on the left-hand
side and a string of
variables and/or terminals
on the right-hand side, and
4. S is the start variable.

Context-Free Languages

* Property of STI
Page 9 of 22

Theory of Computation (With Automata Theory)

Examples:
Given grammar G3 as
1. A 0A1

The formal
grammar G3 is

definition

of

G3 = (V, , R, S)
where
V = {A}
= {0, 1}
R = {A 0A1 }
S=A
Context-Free Languages

* Property of STI
Page 10 of 22

Theory of Computation (With Automata Theory)

Sample
derivations
grammar G3:

using

A 0A1
00A11
0011
A 0A1
00A11
000A111
000111
A 0A1
00A11
000A111
0000A1111
00001111
L(G3) = {0x1x x 0}

Context-Free Languages

* Property of STI
Page 11 of 22

Theory of Computation (With Automata Theory)

Given grammar G4 as
1. A B1
2. B 0B1

The formal
grammar G4 is

definition

of

G4 = (V, , R, S)
where
V = {A, B}
= {0, 1}

R = {A B1, B 0B1 )
S=A

Context-Free Languages

* Property of STI
Page 12 of 22

Theory of Computation (With Automata Theory)

Sample
derivations
grammar G4:

using

A B1
0B11
011
A B1
0B11
00B111
00111
A B1
0B11
00B111
000B1111
0001111
L(G4) = {0x1x+1 x 0}

Context-Free Languages

* Property of STI
Page 13 of 22

Theory of Computation (With Automata Theory)

Given grammar G5 as
1. A (A) AA

The formal
grammar G5 is

definition

of

G5 = (V, , R, S)

where:
V = {A}
= {( , )}
R = {A (A) AA }
S=A

Context-Free Languages

* Property of STI
Page 14 of 22

Theory of Computation (With Automata Theory)

Sample
derivations
grammar G5:

using

A (A)
()
A (A)
(AA)
((A)A)
((A)(A))
(( )(A))
(( )( ))

Context-Free Languages

* Property of STI
Page 15 of 22

Theory of Computation (With Automata Theory)

A (A)
(AA)
((A)A)
((A)(A))
(( )(A))
(( )(AA))
(( )((A)A))
(( )((A)(A)))
(( )(( )(A)))
(( )(( )( )))

L(G5) is the language of all


strings of properly nested
parentheses.

Context-Free Languages

* Property of STI
Page 16 of 22

Theory of Computation (With Automata Theory)

More on Derivations

Consider
the
following
grammar G6 whose rules are
1. S A1B
2. A 0A
3. B 0B 1B

The set of variables V = {S, A,


B}, the set of terminals = {0,
1}, and the start state is S.

In the process of deriving a


string, there will be situations
where there will be more than
one variable on the right-hand
side.

Context-Free Languages

* Property of STI
Page 17 of 22

Theory of Computation (With Automata Theory)

The derivation obtained by


substituting
the
leftmost
variable at each step is called
the leftmost derivation.
Example:

In deriving the string 00101


using leftmost derivation:
S A1B
0A1B
00A1B
001B
0010B
00101B
00101

Context-Free Languages

* Property of STI
Page 18 of 22

Theory of Computation (With Automata Theory)

The derivation obtained by


substituting
the
rightmost
variable at each step is called
the rightmost derivation.
Example:

In deriving the string 00101


using rightmost derivation:
S A1B
A10B
A101B
A101
0A101
00A101
00101

Context-Free Languages

* Property of STI
Page 19 of 22

Theory of Computation (With Automata Theory)

Example:
Consider grammar G7 with the
following rules:
1. S 0AB
2. A 1B1
3. B A
Give
the
leftmost
and
rightmost derivation of the
string 01111.

Leftmost
S 0AB

Context-Free Languages

Rightmost
S 0AB

01B1B

0A

01A1B

01B1

011B11B

01A1

01111B

011B11

01111

01111
* Property of STI
Page 20 of 22

Theory of Computation (With Automata Theory)

Parse Trees

The derivations obtained from


a context-free grammar can be
represented graphically using
a tree structure called parse
trees or derivation trees.

Parse trees give a visualization


of the entire derivation of a
string.

The variables occupy the


internal nodes of a tree with
the start variable being the
root of the tree. The terminals
occupy the leaf at the bottom

The children of an internal


node (variables) are the righthand side string of a rule used
to expand the variable.

Context-Free Languages

* Property of STI
Page 21 of 22

Theory of Computation (With Automata Theory)

Example: The parse tree for


the derivation of the string
00101 using grammar G6.

Context-Free Languages

* Property of STI
Page 22 of 22