0% found this document useful (0 votes)
23 views97 pages

CFG V2

Uploaded by

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

CFG V2

Uploaded by

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

Content

At the end of this lecture, students should be


able to describe the following concepts:

Regular vs. Non-regular Languages


Context-Free Languages
Context-Free Grammars
Derivation Trees. Ambiguity
define some basic concepts
Applications
Push-Down Automata, PDA

1
Context-Free Languages

Based on C Busch, RPI, Models of Computation 2


Context-Free Languages

Context-Free Pushdown
Grammars Automata

3
Context-Free Grammars

4
Grammar
Formal Definition
G  V , T , S , P 
V : Set of variables
T : Set of terminal symbols
S : Start variable
P : Set of production rules
5
Repetition: Regular Grammars

Grammar G  (V , T , S , P )

Variables Terminal Start


symbols variables
Right or Left Linear Grammars. Productions of the form:

A  xB A  Bx or Cx

x is string of terminals
6
Definition: Context-Free Grammars

Grammar G  (V , T , S , P )

Variables Terminal Start


symbols variables

Productions of the form:


A x
x is string of variables and terminals
7
Derivations
• Grammar rules determine the legal strings of
token symbols by means of derivations

• A derivation is a sequence of replacements of


structure names by choices on the right-hand
sides of grammar rules

• A derivation begins with a single structure name


and ends with a string of token symbols
• At each step in a derivation, a single replacement
is made using one choice from a grammar rule
Derivations
• The example
exp  exp op exp | (exp) | number
op  + | – | *
Derive (4-3) * 5 from the expression above
• A derivation
(1) exp => exp op exp [exp  exp op exp]
(2) => exp op number [exp  number]
(3) => exp * number [op * ]
(4) => ( exp ) * number [exp ( exp ) ]
(5) =>{ exp op exp ) * number [exp  exp op exp}
(6) => (exp op number) * number [exp number]
(7) => (exp - number) * number [op  - ]
(8) => (number - number) * number [exp  number]
• Derivation steps use a different arrow from the arrow meta-symbol
in the grammar rules.
Symbols in rules
• Start symbol
– The most general structure is listed first in the
grammar rules.
• Non-terminals
– Structure names are also called non-terminals,
since they always must be replaced further on in a
derivation.
• Terminals
– Symbols in the alphabet are called terminals,
since they terminate a derivation.
– Terminals are usually tokens in compiler
applications.
Regular vs. Context-free Grammar
A regular grammar is either right or left linear, whereas
context free* grammar is any combination of terminals
and non-terminals.

Hence regular grammars are a subset of context-free


grammars. Grammar generating palindromes is not
regular:
S  ABA
A  something
B  something
*The name context-free grammar is explained by the property of productions that are
independent of the surrounding symbols. There are also context-sensitive grammars where
productions depend on the context (symbols that surround variables). 11
A palindrome
• A palindrome is a word, number, or other
sequence of characters which reads the
same backward as forward, such as madam
or racecar.

12
Example 1:

A context-free grammar G
S  aSb
S 
A derivation
S  aSb  aaSbb  aabb

13
A context-free grammar G
S  aSb
S 
Another derivation

S  aSb  aaSbb  aaaSbbb  aaabbb

14
S  aSb
S 

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

( ( ( ( ) ) ) )
15
Example 2:

A context-free grammar G
S  aSa
S  bSb
S 
A derivation
S  aSa  abSba  abba

16
A context-free grammar G
S  aSa
S  bSb
S 
Another derivation

S  aSa  aaSaa  aaaSaaa


 aaabSbaaa  aaabbaaa
17
S  aSa
S  bSb
S 

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

18
Example 3:

A context-free grammar G
S  aSb
S  SS
S 
A derivation
S  SS  aSbS  abS  ab

19
A context-free grammar G
S  aSb
S  SS
S 
A derivation
S  SS  aSbS  abS  abaSb  abab

20
S  aSb
S  SS
S 
L(G )  {w : na ( w)  nb ( w),
and na (v )  nb (v )
in any prefix v}

 ( )( ( ( ) ) ) ( ( ) )  21
Example 4:
Language L  {a nb m:n  m} is context - free.
For the case n  m :
S  AS1 ,
S1  aS1b|λ,
A  aA|a.
For the case n  m :
S  S1B ,
S1  aS1b|λ,
B  bB|b.
n  m: n  m:
S  AS1 , S  S1B ,
S1  aS1b|λ, S1  aS1b|λ,
A  aA|a. B  bB|b.

The grammar for the language L  {a nb m:n  m} is :

S  AS1|S1B
S1  aS1b|λ
A  aA|a
B  bB|b
Definition: Context-Free Grammars

Grammar G  (V , T , S , P )

Variables Terminal Start


symbols variables

Productions of the form:


A x
x is string of variables and terminals
24
Definition: Context-Free Languages

A language L is context-free

if and only if there is a grammar G with


L  L(G )

25
Advantages of context free grammar
• A grammar gives a precise, yet easy to
understand, syntactic specification of a
programming language.

• An efficient parser can be constructed


automatically from a properly designed
grammar.

• A grammar imparts a structure to a program


that is useful for its translation into object
code for the detection of errors.
26
Derivation Order
1. S  AB
2. A  aaA
3. A  
4. B  Bb
5. B  
Leftmost derivation
1 2 3 4 5
S  AB  aaAB  aaB  aaBb  aab
27
Derivation Order: Right/Left
1. S  AB
2. A  aaA
3. A  
4. B  Bb
5. B  
Rightmost derivation
1 4 5 2 3
S  AB  ABb  Ab  aaAb  aab
28
S  aAB
A  bBb
B  A|

Leftmost derivation

S  aAB  abBbB  abAbB  abbBbbB


 abbbbB  abbbb
29
S  aAB
A  bBb
B  A|

Rightmost derivation

S  aAB  aA  abBb  abAb


 abbBbb  abbbb
30
Derivation Trees

31
Derivation can be represented in a tree form

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

A B

32
S  AB A  aaA |  B  Bb | 
S  AB  aaAB

A B

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

S  AB  aaAB  aaABb
S

A B

a a A B b
34
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb
S

A B

a a A B b

 35
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
Derivation Tree S

A B

a a A B b

  36
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
Derivation Tree S

A B
yield

a a A B b aab
 aab
  37
Partial Derivation Trees

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

S  AB
Partial derivation tree
S

A B

38
S  AB  aaAB
Partial derivation tree

A B

a a A
39
S  AB  aaAB sentential form

Partial derivation tree


S
yield
A B aaAB

a a A
40
Sometimes, derivation order doesn’t matter
Leftmost:
S  AB  aaAB  aaB  aaBb  aab
Rightmost:
S  AB  ABb  Ab  aaAb  aab
S
The same derivation tree
A B

a a A B b

  41
Ambiguity

42
Definition

A context-free grammar G is ambiguous

if some string w L(G ) has


two or more derivation trees
(two or more leftmost/rightmost derivations).

43
E  E  E | E  E | (E) | a
a  a a derivation (* denotes multiplication)

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

a E  E leftmost derivation

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

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

E  E a
leftmost derivation
a a
45
E  E  E | E  E | (E) | a
a  aa
E E

E  E E  E

a E  E E  E a

a a a a
46
E  E  E | E  E | (E) | a
a  aa
Two derivation trees
E E

E  E E  E

a E  E E  E a

a a a a
47
The grammar
E  E  E | E  E | (E) | a
is ambiguous!
String a  a  a has two derivation trees
E E

E  E E  E

a E  E E  E a

a a a a
48
The grammar E  E  E | E  E | (E) | a
is ambiguous as the 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
49
Why do we care about ambiguity?
a  aa
E a2 E

E  E E  E

a E  E E  E a

a a a a
50
Why do we care about ambiguity?
2  22
E E

E  E E  E

2 E  E E  E 2

2 2 2 2
51
Why do we care about ambiguity?
6 2  22 8
E E
2 4 4 2
E  E E  E
2 2 2 2
2 E  E E  E 2

2 2 2 2
2  22  6 2  22  8 52
Correct result: 2  22  6
6
E
2 4
E  E
2 2
2 E  E

2 2
53
Ambiguity is bad
for programming languages

We want to remove ambiguity!

54
We fix the ambiguous grammar…
E  E  E | E  E | (E) | a
…by introducing parentheses ()
E  E T to indicate grouping, (precedence)
E T
T T F Non-ambiguous grammar
T F
F  (E)
F a 55
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 a  aa
E  T
E T
T T F T 
T F
T F
F
F  (E) F a
F a a
a 56
Unique derivation tree

E a  aa
E  T

T 
T F

F a
F

a
a 57
The grammar G : E  E T
E T
T T F
T F
F  (E)
F a
is non-ambiguous.
Every string w L(G ) has a unique
derivation tree. 58
Inherent Ambiguity
Some context free languages
have only ambiguous grammars!
n n m n m m
Example: L  {a b c }  {a b c }

S  S1 | S 2
S1  S1c | A S 2  aS 2 | B
A  aAb |  B  bBc | 
59
n n n
The string a b c
has two derivation trees

S S

S1 S2

S1 c a S2

60
n l n l n!
{a b c : n, l  0}{a : n  0}
Non-regular languages
Context-Free Languages
n n R
{a b } {ww }

Regular Languages

61
Applications:
Compilers

62
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
...... ...
63
Compiler

Lexical
parser
analyzer

input output

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

65
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
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
E E
 10 + 2 * 5 *

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
S  SS aabb
S  aSb
S  bSa
S 

74
Phase 2 S  SS | aSb | bSa | 
S  SS  SSS
S  SS  aSbS aabb
Phase 1 S  SS  bSaS
S  SS S  SS  S
S  aSb S  aSb  aSSb
S  aSb  aaSbb
S  aSb  abSab
S  aSb  ab 75
Phase 2 S  SS | aSb | bSa | 
S  SS  SSS
S  SS  aSbS aabb
S  SS  S
S  aSb  aSSb
S  aSb  aaSbb
Phase 3
S  aSb  aaSbb  aabb
76
Final result of exhaustive search
(top-down parsing)
Parser
S  SS
input
S  aSb
aabb
S  bSa
S 
derivation

S  aSb  aaSbb  aabb


77
Another use of context free grammars: Context Free Art
http://www.contextfreeart.org/index.html

78
Context Free Art

79
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

80
Pushdown Automata
PDAs

81
Pushdown Automaton - PDA
Input String

Stack

States

82
The Stack
A PDA can write symbols on a stack 
and read them later on. y
x
POP reading symbol
z
PUSH writing symbol
All access to the stack only on the top!
(Stack top is written leftmost in the string, e.g. yxz)
A stack is valuable as it can hold an unlimited
amount of information.
The stack allows pushdown automata to
recognize some non-regular languages.
83
The States

Pop old
Input Push new
reading
symbol writing
stack symbol
stack symbol

q1 a , b / c q2

84
q1 a, b / c q2
input
 a   a 

stack
b top c
h Replace h
e e
$ $
(An alternative is to start and finish with empty stack)
85
q1 a ,  / c q2

input
 a   a 

stack c
b top b
h Push h
e e
$ $
86
q1 a , b /  q2
input
 a   a 

stack
b top
h Pop h
e e
$ $
87
q1 a ,  / q2

input
 a   a 
stack
b top b
h No Change h
e e
$ $
88
Formal Definition
Pushdown Automaton is defined as 7-tuple

M  (Q, , ,  , q 0, z , F )
Final
States states

Input
Stack
alphabet start
Transition start
Stack state
function symbol
alphabet

89
Example 3.7 Salling: Time 0
A PDA for simple nested parenthesis strings

( ( ( ) ) ) 
Input Stack

(, / ( ), ( /

start
s ), (/  q end

90
Example 3.7 Time 1
Input
( ( ( ) ) ) (

Stack
(, / ( ), ( /

start
s ), (/  q end

91
Example 3.7 Time 2
Input (
( ( ( ) ) ) (

Stack
(, / ( ), ( /

start ), (/  q end
s

92
Example 3.7 Time 3
Input
(
( ( ( ) ) ) (
(

(, / ( ), ( / Stack

start
s ), (/  q end

93
Example 3.7 Time 4
Input
( ( ( ) ) ) (
(
(

(, / ( ), ( /
Stack

start ), (/  q end
s

94
Example 3.7 Time 5
Input
(
( ( ( ) ) )
(

(, / ( ), ( / Stack

start
s ), (/  q end

95
Example 3.7 Time 6
Input
( ( ( ) ) ) (

Stack
(, / ( ), ( /

start
s ), (/  q end

96
Example 3.7 Time 7
Input
( ( ( ) ) ) 
Stack

(, / ( ), ( /

start ), (/  q end
s

97

You might also like