0% found this document useful (0 votes)
6 views

17 Context Free Languages With Examples

language explained

Uploaded by

survivor000111
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)
6 views

17 Context Free Languages With Examples

language explained

Uploaded by

survivor000111
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/ 74

Context-Free Languages

Course Instructor: Ms. Tayyaba


Zaheer

Book: Prof. Sipser-MIT


Slides: Prof. Busch - LSU
1
context-free grammar (CFG)

In formal language theory, a context-free grammar (CFG) is a grammar


in which every production rule is of the form

V→w
where V is a single nonterminal symbol, and w is a string of terminals
and/or nonterminals (possibly empty). The term "context-free" expresses
the fact that nonterminals can be rewritten without regard to the context
in which they occur. A formal language is context-free if some context-
free grammar generates it.

Context-free grammars play a central role in the description and design


of programming languages and compilers. They are also used for
analyzing the syntax of natural languages.

(Ref. http://en.wikipedia.org/wiki/Content_free_grammar )

2
context-sensitive grammar (CSG)
is a grammar where each production has the form
wAx → wyx,
where w and x are strings of terminals and nonterminals
and y is also a string of terminals.
In other words, the productions give rules saying "if you
see A in a given context, you may replace A by the
string y." It's an unfortunate that these grammars are
called "context-sensitive grammars" because it means
that "context-free" and "context-sensitive" are not
opposites, and it means that there are certain classes of
grammars that arguably take a lot of contextual
information into account but aren't formally considered
to be context-sensitive.
3
context-sensitive grammar (CSG)
A context-sensitive grammar (CSG) is a formal grammar in which the left-
hand sides and right-hand sides of any production rules may be
surrounded by a context of terminal and nonterminal symbols. Context-
sensitive grammars are more general than context-free grammars but still
orderly enough to be parsed by a linear bounded automaton.

The concept of context-sensitive grammar was introduced by Noam


Chomsky in the 1950s as a way to describe the syntax of natural language
where it is indeed often the case that a word may or may not be
appropriate in a certain place depending upon the context. A formal
language that can be described by a context-sensitive grammar is called a
context-sensitive language.

(Ref. http://en.wikipedia.org/wiki/Context-sensitive_grammar )

4
Recursively enumerable language
In mathematics, logic and computer science, a formal language is
called recursively enumerable (also recognizable, partially
decidable, semidecidable, Turing-acceptable or Turing-
recognizable) if it is a recursively enumerable subset in the set of all
possible words over the alphabet of the language, i.e., if there exists a
Turing machine which will enumerate all valid strings of the language.

Recursively enumerable languages are known as type-0 languages in the


Chomsky hierarchy of formal languages. All regular, context-free,
context-sensitive and recursive languages are recursively enumerable.

The class of all recursively enumerable languages is called RE.

(Ref. https://en.wikipedia.org/wiki/Recursively_enumerable_language )

5
Some Application Area of Context
Free Languages

• Natural Language understanding (AI)

• Formulation of New Programming


Languages

• Compiler Construction and Optimization

6
Context-Free Languages

n n R
{a b : n 0} {ww }

Regular Languages
a *b * ( a  b) *

7
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

8
Context-Free Grammars

9
Context-Free Grammar
(CFG) is a generator of the
Context-free language
(CFL)
&
A recognizer [Push down
automata (PDA)] will be an
acceptor

10
Grammars
Grammars express languages

Example: the English language


grammar
sentence  noun _ phrase predicate

noun _ phrase  article noun

predicate  verb
11
article  a
article  the

noun  cat
noun  dog

verb  runs
verb  sleeps
12
Derivation of string “the dog sleeps”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 the noun verb
 the dog verb
 the dog sleeps

13
Derivation of string “a cat runs”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 a noun verb
 a cat verb
 a cat runs

14
Language of the grammar:

L = { “a cat runs”,
“a cat sleeps”,
“the cat runs”,
“the cat sleeps”,
“a dog runs”,
“a dog sleeps”,
“the dog runs”,
“the dog sleeps” }
15
Productions Sequence of
Terminals (symbols)

noun  cat

sentence  noun_ phrase predicate

Variables Sequence of Variables

16
Another Example

Sequence of
terminals and variables

Grammar: S  aSb
S

Variable The right side


may be 

17
Grammar: S  aSb
S

Derivation of string ab :

S  aSb  ab

S  aSb S
18
Grammar: S  aSb
S

Derivation of string aabb :

S  aSb  aaSbb  aabb

S  aSb S
19
Grammar: S  aSb
S

Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb
20
Grammar: S  aSb
S

Language of the grammar:

n n
L {a b : n 0}

21
A Convenient Notation
*
We write: S  aaabbb
for zero or more derivation steps

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

22
*
In general we write: w1  wn

If: w1  w2  w3    wn

in zero or more derivation steps

*
Trivially: w  w
23
Example Grammar Possible Derivations
S  aSb *
S 
S *
S  ab
*
S  aaabbb

 
S  aaSbb aaaaaSbbbb
b

24
Another convenient notation:

S  aSb
S  aSb | 
S  

article  a
article  a | the
article  the
25
Formal Definitions

Grammar: G V , T , S , P 

Set of
variables
Set of Start Set of
terminals variable productions

26
Context-Free Grammar: G (V , T , S , P )

All productions in P are of the form

A s
Variable String of
variables and
terminals

27
Context-Free Grammar: G (V , T , S , P )
Consider the following example grammar
with 5 productions:

1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

28
Example of Context-Free Grammar
S  aSb | 
productions
P {S  aSb, S  }

G V , T , S , P 

V {S }
T {a, b} start variable
variables
terminals
29
Language of a Grammar:

For a grammarG S
with start variable

*
L(G ) {w : S  w, w  T *}

String of terminals or 

30
Example:

context-free grammar G : S  aSb | 

n n
L(G )  {a b : n  0}

Since, there is derivation



n n
Sa b for any n 0
31
Context-Free Language definition:
A languageL is context-free
if there is a context-free grammarG
L L(G )
with

32
Example:
n n
L  {a b : n  0}
is a context-free language
since context-free grammarG :
S  aSb | 

generates L(G ) L

33
Another Example

Context-free grammar G :
S  aSa | bSb | 
Example derivations:
S  aSa  abSba  abba
S  aSa  abSba  abaSaba  abaaba

R
L(G ) {ww : w {a, b}*}
Palindromes of even length
34
Another Example
Context-free grammar G :
S  aSb | SS | 
Example derivations:
S  SS  aSbS  abS  ab
S  SS  aSbS  abS  abaSb  abab

L(G ) {w : na ( w) nb ( w),


and na (v) nb (v)
Describes
in any prefix v}
matched
parentheses: () ((( ))) (( )) a (, b )
35
Derivation Order
and
Derivation Trees

36
Derivation Order

Consider the following example grammar


with 5 productions:

1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

37
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

Leftmost derivation order of string aab :

1 2 3 4 5
S  AB  aaAB  aaB  aaBb  aab

At each step, we substitute the


leftmost variable
38
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

Rightmost derivation order of string aab :

1 4 5 2 3
S  AB  ABb  Ab  aaAb  aab
At each step, we substitute the
rightmost variable
39
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

Leftmost derivation of aab :


1 2 3 4 5
S  AB  aaAB  aaB  aaBb  aab

Rightmost derivation of aab :


1 4 5 2 3
S  AB  ABb  Ab  aaAb  aab
40
Derivation Trees
onsider the same example grammar:

S  AB A  aaA |  B  Bb | 

And a derivation of aab :

S  AB  aaAB  aaABb  aaBb  aab

41
S  AB A  aaA |  B  Bb | 

S  AB
S

A B

yield AB

42
S  AB A  aaA |  B  Bb | 

S  AB  aaAB
S

A B

yield aaAB
a a A

43
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb
S

A B

a a A B b

yield aaABb
44
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb
S

A B

a a A B b

yield
 aaBb aaBb
45
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
Derivation Tree S
(parse tree)
A B

a a A B b
yield
  aab aab
46
Sometimes, derivation order doesn’t matter
Leftmost derivation:
S  AB  aaAB  aaB  aaBb  aab
Rightmost derivation:
S  AB  ABb  Ab  aaAb  aab
S

Give same A B
derivation tree
a a A B b

  47
Ambiguity

48
Grammar for mathematical expressions

E  E  E | E  E | (E) | a

Example strings:
(a  a)  a  (a  a  (a  a))

Denotes any number

49
E  E  E | E  E | (E) | a

E  E E  aE  aEE
E
 a  a E  a  a*a
E  E
A leftmost derivation
for a  a  a
a E  E

a a
50
E  E  E | E  E | (E) | a

E  EE  E EE  aEE


E
 a aE  a aa
E  E
Another
leftmost derivation
for a  a  a E  E a

a a
51
E  E  E | E  E | (E) | a

Two derivation trees


for a  a  a
E E

E  E E  E

a E  E E  E a

a a a a
52
take a 2

a  a  a 2  2  2

E E

E  E E  E

2 E  E E  E 2

2 2 2 2
53
Good Tree Bad Tree
2  2  2 6 2  2  2 8
6 Compute expression result 8
E using the tree E
2 4 4 2
E  E E  E
2 2 2 2
2 E  E E  E 2

2 2 2 2
54
Two different derivation trees
may cause problems in applications which
use the derivation trees:

• Evaluating expressions

• In general, in compilers
for programming languages

55
Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L(G ) which has:

two different derivation trees


or
two leftmost derivations

(Two different derivation trees give two


different leftmost derivations and vice-versa)
56
Example: E  E  E | E  E | (E) | a

this grammar is ambiguous since


string a  a  a has two derivation trees
E E

E  E E  E

a E  E E  E a

a a a a
57
E  E  E | E  E | (E) | a
his grammar is ambiguous also because
string a  a  a has two leftmost derivations
E  E E  aE  aEE
 a  a E  a  a*a

E  EE  E EE  aEE


 a aE  a aa
58
Another ambiguous grammar:

IF_STMT  if EXPR then STMT


| if EXPR then STMT else STMT

Variables Terminals

Very common piece of grammar


in programming languages
59
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT

if expr1 then STMT

if expr2 then stmt1 else stmt2

Two derivation trees


IF_STMT

if expr1 then STMT else stmt2

if expr2 then stmt1


60
In general, ambiguity is bad
and we want to remove it

Sometimes it is possible to find


a non-ambiguous grammar for a language

But, in general ιt is difficult to achieve this

61
A successful example:
Equivalent
Ambiguous
Non-Ambiguous
Grammar
Grammar
E  E E
E  E T |T
E  E E
T  T F | F
E  (E )
E  a F  (E ) | a

generates the same


language
62
E  E T  T T  F T  a T  a T  F
 a  F F  a aF  a aa
E
E  E T |T
E  T
T  T F | F
F  (E) | a T T  F

F F a
Unique
derivation tree
for a  a  a a a
63
An un-successful example:

n n m n m m
L {a b c }  {a b c }
n, m 0

L is inherently ambiguous:
every grammar that generates this
language is ambiguous

64
Example (ambiguous) grammar forL :

n n m n m m
L {a b c }  {a b c }

S  S1 | S 2 S1  S1c | A S 2  aS2 | B
A  aAb |  B  bBc | 

65
The string a nb nc n  L
has always two different derivation trees
(for any grammar)

For example
S S

S1 S2

S1 c a S2

66
References
• http://matt.might.net/articles/gr
ammars-bnf-ebnf
/
• https://
en.wikipedia.org/wiki/Chomsky_hierar
chy
Thank You!

74

You might also like