11. 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” }
13. 13
More Notation
Grammar
( )SPTVG ,,,=
:V
:T
:S
:P
A finiteSet of variables
A finiteSet of terminal symbols
Start variable
Set of Production rules
V and T are assumed to be disjoint
38. 38
Theorem - Part 1
Languages
Generated by
Regular Grammars
Regular
Languages
⊆
Any regular grammar generates
a regular language
39. 39
Theorem - Part 2
Languages
Generated by
Regular Grammars
Regular
Languages
⊇
Any regular language is generated
by a regular grammar
40. 40
Proof – Part 1
Languages
Generated by
Regular Grammars
Regular
Languages
⊆
The language generated by
any regular grammar is regular
)(GL
G
41. 41
The case of Right-Linear
Grammars
Let be a right-linear grammar
We will prove: is regular
Proof idea: We will construct NFA
with L(M)=L(G)
G
)(GL
M
)()( GLML =
56. 56
The case of Left-Linear
Grammars
Let be a left-linear grammar
We will prove: is regular
Proof idea:
We will construct a right-linear
grammar with
G
)(GL
G′ R
GLGL )()( ′=
60. 60
It is easy to see that:
Since is right-linear, we have:
R
GLGL )()( ′=
)(GL ′ R
GL )( ′
G′
)(GL
Regular
Language
Regular
Language
Regular
Language
61. 61
Proof - Part 2
Languages
Generated by
Regular Grammars
Regular
Languages
⊇
Any regular language is generated
by some regular grammar
L
G
62. 62
Proof idea:
Let be the NFA with .
Construct from a regular grammar
such that
Any regular language is generated
by some regular grammar
L
G
M )(MLL =
M G
)()( GLML =
63. 63
Since is regular
there is an NFA such that
L
M )(MLL =
Example:
a
b
a
bλ
*)*(* abbababL =
)(MLL =
M
1q 2q
3q
0q
64. 64
Convert to a right-linear grammarM
a
b
a
bλ
M
0q 1q 2q
3q
10 aqq →
94. 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.
95. 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.
128. 128
The parser finds the derivation
of a particular input
10 + 2 * 5
Parser
E -> E + E
| E * E
| INT
E => E + E
=> E + E * E
=> 10 + E*E
=> 10 + 2 * E
=> 10 + 2 * 5
input
derivation
129. 129
10
E
2 5
E => E + E
=> E + E * E
=> 10 + E*E
=> 10 + 2 * E
=> 10 + 2 * 5
derivation
derivation tree
E E
E E
+
*