0% found this document useful (0 votes)
10 views75 pages

Context-Free Languages

The document provides an overview of context-free languages and grammars, explaining their structure and derivation processes. It includes examples of context-free grammars, derivation trees, and discusses the concept of ambiguity in grammars. The document emphasizes the importance of context-free languages in formal language theory and their applications in programming languages.

Uploaded by

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

Context-Free Languages

The document provides an overview of context-free languages and grammars, explaining their structure and derivation processes. It includes examples of context-free grammars, derivation trees, and discusses the concept of ambiguity in grammars. The document emphasizes the importance of context-free languages in formal language theory and their applications in programming languages.

Uploaded by

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

Context-Free Languages

1
Context-Free Languages

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

Regular Languages
a *b * ( a + b) *

2
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

3
Context-Free Grammars

4
Grammars
Grammars express languages

Example: the English language grammar

sentence → noun _ phrase predicate

noun _ phrase → article noun

predicate → verb
5
article → a
article → the

noun → cat
noun → dog

verb → runs
verb → sleeps
6
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

7
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

8
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” }
9
Productions Sequence of
Terminals (symbols)

noun → cat

sentence → noun _ phrase predicate

Variables Sequence of Variables

10
Another Example

Sequence of
terminals and variables

Grammar: S → aSb
S →

Variable The right side


may be 

11
Grammar: S → aSb
S →

Derivation of string ab :
S  aSb  ab

S → aSb S →
12
Grammar: S → aSb
S →

Derivation of string aabb :


S  aSb  aaSbb  aabb

S → aSb S →
13
Class Exercises
Grammar: S → aS | bS | a | b

Derive: abbab

Grammar: S → aA | bB
A → aS | a
B →bS | b

Derive: bbaaaa
14
Grammar: S → aSb
S →

Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb
15
Grammar: S → aSb
S →

Language of the grammar:

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

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

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

17
*
In general we write: w1  wn

If: w1  w2  w3    wn
in zero or more derivation steps

*
Trivially: w  w
18
Example Grammar Possible Derivations
S → aSb *
S 
S → *
S  ab
*
S  aaabbb

 
S  aaSbb  aaaaaSbbbbb

19
Another convenient notation:

S → aSb
S → aSb | 
S →

article → a
article → a | the
article → the
20
Formal Definitions

Grammar: G = (V , T , S , P )

Set of
variables
Set of Start Set of
terminal variable productions
symbols

21
Context-Free Grammar: G = (V , T , S , P )

All productions in P are of the form

A →s
Variable String of
variables and
terminals

22
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
23
Language of a Grammar:

For a grammar G with start variable S


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

String of terminals or 

24
Example:

context-free grammar G : S → aSb | 

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

Since, there is derivation



S a b n n
for any n 0
25
Context-Free Language:
A language L is context-free
if there is a context-free grammar G
with L = L(G )

26
Example:

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

is a context-free language
since context-free grammar G :
S → aSb | 

generates L(G ) = L

27
Another Example

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

L(G ) = {ww : w {a, b}*}


R

Palindromes of even length


28
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 =)
29
Derivation Order
and
Derivation Trees

30
Derivation Order

Consider the following example grammar


with 5 productions:

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

31
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
32
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
33
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
34
Derivation Trees
Consider the same example grammar:

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

And a derivation of aab :

S  AB  aaAB  aaABb  aaBb  aab

35
S → AB A → aaA |  B → Bb | 

S  AB
S

A B

yield AB

36
S → AB A → aaA |  B → Bb | 

S  AB  aaAB
S

A B

yield aaAB
a a A

37
S → AB A → aaA |  B → Bb | 

S  AB  aaAB  aaABb
S

A B

a a A B b

yield aaABb
38
S → AB A → aaA |  B → Bb | 
S  AB  aaAB  aaABb  aaBb
S

A B

a a A B b

yield
 aaBb = aaBb
39
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
40
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

  41
Ambiguity

42
Grammar for mathematical expressions

E → E + E | E  E | (E) | a

Example strings:
(a + a )  a + (a + a  (a + a ))

Denotes any number

43
E → E + E | E  E | (E) | a

a + aa

44
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
45
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
46
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
47
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
48
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
49
Two different derivation trees
may cause problems in applications which
use the derivation trees:

• Evaluating expressions

• In general, in compilers
for programming languages

50
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)
51
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
52
E → E + E | E  E | (E) | a
this 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
53
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
54
IF_STMT → If expr1 then STMT | if expr1 then STMT else stmt2
STMT → if expr2 then stmt1 else stmt2 | if expr2 then stmt1

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


55
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 we cannot do so

56
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
57
E → E +T |T
T →T F | F
F → (E) | a

Unique
derivation tree
for a + a  a
58
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

Unique F F a
derivation tree
for a + a  a a a
59
An un-successful example:

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

n, m  0

L is inherently ambiguous:

every grammar that generates this


language is ambiguous

60
Example (ambiguous) grammar for L:

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

S → S1 | S2 S1 → S1c | A S 2 → aS2 | B
A → aAb |  B → bBc | 

61
The string a n b n c n  L
has always two different derivation trees
(for any grammar)

For example
S S

S1 S2

S1 c a S2

62
Applications:
Compilers

63
Machine Code
Program Add v,v,0
v = 5; cmp v,5
if (v>5) jmplt ELSE
x = 12 + v; THEN:
while (x !=3) { Compiler
add x, 12,v
x = x - 3; ELSE:
v = 10; WHILE:
} cmp x,3
...... ...
64
Compiler

Lexical
parser
analyzer

input output

machine
program
code
65
A parser “knows” the grammar
of the programming language

66
The parser finds the derivation
of a particular input

derivation
Parser
input EE+E
E→E+E
E+E*E
10 + 2 * 5 |E*E
 10 + E*E
| INT
 10 + 2 * E
 10 + 2 * 5

67
derivation derivation tree

E
EE+E
E+E*E
E + E
 10 + E*E
 10 + 2 * E 10
 10 + 2 * 5 E * E

2 5

68
derivation tree

E machine code

E + E
mult a, 2, 5
10 add b, 10, a
E * E

2 5

69
Parsing examples

70
Parser
input
grammar derivation
string

71
Example:

Parser
S → SS derivation
input
S → aSb
aabb ?
S → bSa
S →

72
Exhaustive Search

S → SS | aSb | bSa | 

Phase 1: S  SS Find derivation of


S  aSb aabb
S  bSa
S 
All possible derivations of length 1
73
Final result of exhaustive search
(top-down parsing)
Parser
S → SS
input
S → aSb
aabb
S → bSa
S →
derivation

S  aSb  aaSbb  aabb


74
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

75

You might also like