9. S aSb
Grammar:S
Derivation of sentence
aabb :
S aSb aaSbb aabb
S aSb S 9
10. S aSb aaSbb aaaSbbb aaabbb
Other derivations:
S aSb aaSbb aaaSbbb
aaaaSbbbb aaaabbbb
10
11. Language of the grammar:
L = { “a boy runs”,
“a boy walks”,
“the boy runs”,
“the boy walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
11
13. More Notation
G V , T , P, S
Grammar
V : A finiteSet of variables
T : A finiteSet of terminal symbols
S : Start variable
P: Set of Production rules
13
V and T are assumed to be disjoint
14. Example
G S aSb
Grammar : S
G V , T , P, S
V {S } T {a, b}
P {S aSb, S
14
}
15. More Notation
Sentential Form:
A sentence that contains
variables and terminals
Example:
S aSb aaSbb aaaSbbb aaabbb
Sentential Forms sentence
15
16. *
S aaabbb
We write:
Instead of:
S aSb aaSbb aaaSbbb aaabbb
16
17. *
w1 wn
In general we write:
w1 w2 w3 wn
If:
17
37. Theorem
Languages
Regular
Generated by
Languages
Regular Grammars
37
38. Theorem - Part 1
Languages
Regular
Generated by
Languages
Regular Grammars
Any regular grammar generates
a regular language
38
39. Theorem - Part 2
Languages
Regular
Generated by
Languages
Regular Grammars
Any regular language is generated
by a regular grammar
39
40. Proof – Part 1
Languages
Regular
Generated by
Languages
Regular Grammars
The language L (G ) generated by
any regular grammar G is regular
40
41. The case of Right-Linear
Grammars
Let G be a right-linear grammar
We will prove: L(G ) is regular
L( M ) L(G )
Proof idea: We will construct NFA M
41
with L(M)=L(G)
42. Grammar G is right-linear
Example:
S aA | B
A aa B
B b B|a
42
43. Construct NFA M such that
every state is a grammar variable:
A special
S VF
final state
B
S aA | B
A aa B
B b B|a 43
50. NFA M Grammar
A G
a S aA | B
a
A aa B
S a B bB | a
VF
a
B
L( M ) L(G )
b
aaab * a b * a
50
51. In General
A right-linear grammar G
has variables: V0 ,V1,V2 ,
and productions: Vi a1a2 amV j
or
Vi a1a2 am
51
52. We construct the NFA M such that:
each variable Vicorresponds to a
node: V1 V3
V0
VF
V2 special
V4
final state
52
53. Vi a1a2 amV j
For each production:
we add transitions and intermediate
nodes
Vi a1 a2 ………
am Vj
53
54. For each production:
Vi a1a2 am
we add transitions and intermediate
nodes
Vi a1 a2 ………
am
VF
54
55. Resulting NFA M looks like this:
a9
a2 a4
a1 V1 V3
a3 a5
V0
a3 a4
VF
a8 a9
V2 a5
V4
It holds that: L(G ) L( M ) 55
56. The case of Left-Linear
Grammars
Let G be a left-linear grammar
We will prove: L (G ) is regular
Proof idea:
We will construct a right-linear
grammar G with R
L(G ) L(G )
56
57. Since G is left-linear grammar
the productions look like:
A Ba1a2 ak
A a1a2 ak
57
58. Left G
A Ba1a2 ak
linear
Construct right-linear grammar G
A Bv
Right
G A ak a2a1B
linear
R
A v B 58
60. R
L(G ) L(G )
It is easy to see that:
G
Since is right-linear, we have:
R L(G )
L(G ) L(G )
Regular Regular Regular
Language Language Language
60
61. Proof - Part 2
Languages
Regular
Generated by
Languages
Regular Grammars
Any regular language L is generated
by some regular grammar G
61
62. Any regular language L is generated
by some regular grammar G
Proof idea:
Let M be the NFA with L L(M ).
Construct from M a regular grammar G
such that L ( M ) L (G )
62
63. Since L is regular
there is an NFA M such that L L(M )
b
Example:
M a
a
q0 q1 q2
b
L ab * ab(b * ab) * q3
L L(M ) 63
64. Convert M to a right-linear grammar
b
M a
a
q0 q1 q2
b
q0 aq1
q3
64
70. Since G is right-linear grammar
G is also a regular grammar
with L(G ) L( M ) L
70
71. For any regular language one can
construct left linear as well as right
linear grammar.
71
72. A
a a
a VF
S a F → Ba
B B → Bb here F is
start symbol
S aA | B B → Aaa
b B→S
A aa B A → Sa
B bB | a S→ this is new rule
72
as S is now a final state
73. A → aB D → Cb
B → bB D→B
C→B a
B → aC
B →B b Now D is the
C→b D start variable
D→ B B → Aa
A→
D→
73
88. S aSb
S SS
S
L(G ) {w : na ( w) nb ( w),
and na (v) nb (v)
in any prefix v}
() ((( ))) (( )) 88
89. Definition: Context-Free
Grammars
Grammar
G (V , T , P, S )
Variables Terminal Start
symbols variable
Productions of the form:
A x
x is string of variables and terminals 89
91. Derivation Order
1. S AB 2. A aaA 4. B Bb
3. A 5. B
Leftmost derivation:
1 2 3 4 5
S AB aaAB aaB aaBb aab
Rightmost derivation:
1 4 5 2 3
S AB ABb Ab aaAb aab
91
92. S aAB
A bBb
B A|
Leftmost derivation:
S aAB abBbB abAbB abbBbbB
abbbbB abbbb
Rightmost derivation:
S aAB aA abBb abAb
abbBbb abbbb 92
94. Def:
G =(V,T,P,S)
An ordered tree is a derivation tree for G iff
1 The root is labeled S
2 every leaf has a label from T { }
3 Every interior vertex has a label from V
4 If a vertex has label A(variable) and its children
are labeled from (L to R)
a1,a2,…an then P must contain a production
A a1 a2 …an
5 A leaf labeled has no sibling.
94
95. Yield:
The string of terminals obtained by
reading the leaves of the tree from left
to right omitting any ’s encountered
is called yield of the tree.
95
96. Partial derivation tree
A tree that has properties 3,4,5 but 1
need not
And 2 is replaced by
V T { }
96
104. S AB aaAB
Partial derivation tree S
A B
a a A
104
105. sentential
S AB aaAB
form
Partial derivation tree S
A B
yield
a a A
aaAB
105
106. Sometimes, derivation order doesn’t matter
Leftmost:
S AB aaAB aaB aaBb aab
Rightmost:
S AB ABb Ab aaAb aab
S
Same derivation tree
A B
a a A B b
106
108. E E E | E E | (E) | a
a a a
E E E E a E a E E
a a E a a*a
E E
leftmost derivation
a E E
a a 108
109. E E E | E E | (E) | a
a a a
E E E E E E a E E E
a a E a a a
E E
leftmost derivation
E E a
a 109 a
110. 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 110 a
111. The grammarE 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
111
112. The grammarE E E | E E | (E) | a
is ambiguous:
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 112
113. Definition:
A context-free grammar G is ambiguous
if some string w L(G ) has:
two or more derivation trees
113
114. In other words:
A context-free grammar G is ambiguous
if some string w L(G ) has:
two or more leftmost derivations
(or rightmost)
114
115. Why do we care about ambiguity?
a a a
take a 2
E E
E E E E
a E E E E a
a a a 115 a
123. 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 123
124. Inherent Ambiguity
Some context free languages
have only ambiguous grammars
Example: n n m n m m
L {a b c } {a b c }
S S1 | S2 S1 S1c | A S2 aS2 | B
A aAb | B bBc |
124
125. n n n
The string a b c
has two derivation trees
S S
S1 S2
S1 c a S2
125
127. A parser knows the grammar
of the programming language
127
128. 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
128
129. 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
129