0% found this document useful (0 votes)
29 views60 pages

4 Context Free Grammar

Uploaded by

liwase6772
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)
29 views60 pages

4 Context Free Grammar

Uploaded by

liwase6772
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/ 60

Context-Free Languages

Dr. Ayman Ali 1


CFG’s are useful in many applications

•Describing syntax of programming


languages

•Parsing

•Structure of documents, e.g.XML


Context-Free Languages

Context-Free Pushdown
Grammars Automata

Dr. Ayman Ali 3


Context-Free Grammars

Dr. Ayman Ali 4


Grammars
Grammars express languages

Example: the English language grammar

sentence  noun _ phrase predicate

noun _ phrase  article noun

predicate  verb
Dr. Ayman Ali 5
article  a
article  the

noun  cat
noun  dog

verb  runs
verb  sleeps
Dr. Ayman Ali 6
Derivation of string “the dog walks”:

sentence  noun _ phrase predicate


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

Dr. Ayman Ali 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

Dr. Ayman Ali 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” }
Dr. Ayman Ali 9
Productions
Sequence of
Terminals (symbols)

noun  cat

sentence  noun _ phrase predicate

Variables Sequence of Variables

Dr. Ayman Ali 10


Another Example

Sequence of
terminals and variables

Grammar: S  aSb
S 
Variable The right side
may be 

Dr. Ayman Ali 11


Grammar: S  aSb
S 

Derivation of string ab :
S  aSb  ab

S  aSb S 
Dr. Ayman Ali 12
Grammar: S  aSb
S 

Derivation of string aabb :

S  aSb  aaSbb  aabb

S  aSb S 
Dr. Ayman Ali 13
Grammar: S  aSb
S 

Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb
Dr. Ayman Ali 14
Grammar: S  aSb
S 

Language of the grammar:

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

Dr. Ayman Ali 15


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

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

Dr. Ayman Ali 16


*
In general we write: w1  wn

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

*
Trivially: w  w
Dr. Ayman Ali 17
Example Grammar Possible Derivations
S  aSb *
S 
S  *
S  ab
*
S  aaabbb

 
S  aaSbb  aaaaaSbbbb b

Dr. Ayman Ali 18


Another convenient notation:

S  aSb S  aSb | 
S 

article  a article  a | the


article  the
Dr. Ayman Ali 19
Formal Definitions

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

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

Dr. Ayman Ali 20


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

All productions in P are of the form

As
Variable String of
variables and
terminals

Dr. Ayman Ali 21


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
Dr. Ayman Ali 22
More Example

Consider following rule


S  aSa | bSb | 
Show the abbbba derivations:
S  aSa  abSba  abba
S  aSa  abSba  abbSbba  abbbba

Dr. Ayman Ali 23


Another Example
Context-free grammar G:
S  aSb | SS | 
Example derivations: ab, abab
S  SS  aSbS  abS  ab
S  SS  aSbS  abS  abaSb  abab
Derivation Order
and
Derivation Trees

Dr. Ayman Ali 25


Derivation Order

Consider the following example grammar


with 5 productions:

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

Dr. Ayman Ali 26


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
Dr. Ayman Ali 27
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
Dr. Ayman Ali 28
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
Dr. Ayman Ali 29
Derivation Trees
Consider the same example grammar:

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

And a derivation of aab :

S  AB  aaAB  aaABb  aaBb  aab

Dr. Ayman Ali 30


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

S  AB
S

A B

yield AB

Dr. Ayman Ali 31


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

S  AB  aaAB
S

A B

yield aaAB
a a A

Dr. Ayman Ali 32


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

S  AB  aaAB  aaABb
S

A B

a a A B b

yield aaABb
Dr. Ayman Ali 33
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb
S

A B

a a A B b

yield

Dr. Ayman Ali
aaBb  aaBb 34
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
Dr. Ayman Ali 35
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

Dr. Ayman Ali


  36
Ambiguity

Dr. Ayman Ali 37


Grammar for mathematical expressions

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

Example strings:

a  aa

Denotes any number

Dr. Ayman Ali 38


E  E  E | E  E | (E) | a
a  a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
Dr. Ayman Ali 39
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
Dr. Ayman Ali 40
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
Dr. Ayman Ali 41
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
Dr. Ayman Ali 42
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
Dr. Ayman Ali 43
Two different derivation trees
may cause problems in applications which
use the derivation trees:

• Evaluating expressions

• In general, in compilers
for programming languages

Dr. Ayman Ali 44


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)
Dr. Ayman Ali 45
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
Dr. Ayman Ali 46
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
Dr. Ayman Ali 47
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
Dr. Ayman Ali 48
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


Dr. Ayman Ali 49
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

Dr. Ayman Ali 50


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 )
F  (E ) | a
E a
generates the same
language
Dr. Ayman Ali 51
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
Dr. Ayman Ali 52
Compilers

Dr. Ayman Ali 53


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

Lexical parser
analyzer

input output

machine
program
code
Dr. Ayman Ali 55
A parser knows the grammar
of the programming language

Dr. Ayman Ali 56


Parser
PROGRAM  STMT_LIST
STMT_LIST  STMT; STMT_LIST | STMT;
STMT  EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }

EXPR  EXPR + EXPR | EXPR - EXPR | ID


IF_STMT  if (EXPR) then STMT
| if (EXPR) then STMT else STMT
WHILE_STMT while (EXPR) do STMT

Dr. Ayman Ali 57


The parser finds the derivation
of a particular input

derivation
Parser
input E -> E + E E => E + E
|E*E => E + E * E
10 + 2 * 5
| INT => 10 + E*E
=> 10 + 2 * E
=> 10 + 2 * 5

Dr. Ayman Ali 58


derivation tree
derivation
E

E => E + E E + E
=> E + E * E
=> 10 + E*E 10
E * E
=> 10 + 2 * E
=> 10 + 2 * 5 2 5

Dr. Ayman Ali 59


derivation tree

E machine code

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

2 5

Dr. Ayman Ali 60

You might also like