Professional Documents
Culture Documents
1
Grammars
Grammars express languages
predicate verb 2
article a
article the
noun boy
noun dog
verb runs
verb walks 3
A derivation of “the boy walks”:
L = { “a boy runs”,
“a boy walks”,
“the boy runs”,
“the boy walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”, 6
Notation
noun boy
noun dog
Variable Terminal
Production
or
rule
Non-terminal
7
Another Example
Grammar:
S aSb
S
Derivation of sentence
ab :
S aSb ab
S aSb S
8
S aSb
Grammar: S
Derivation of sentence
aabb :
S aSb aaSbb aabb
S aSb S 9
S aSb aaSbb aaaSbbb aaabbb
Other derivations:
10
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
Language of the grammar
S aSb
S
n n
L {a b : n 0}
12
More Notation
G V , T , P, S
Grammar
V : A finiteSet of variables
G V , T , P, S
V {S } T {a, b}
P {S aSb, S }
14
More Notation
Sentential Form:
A sentence that contains
variables and terminals
Example:
S aSb aaSbb aaaSbbb aaabbb
Instead of:
S aSb aaSbb aaaSbbb aaabbb
16
*
w1 wn
In general we write:
w1 w2 w3 wn
If:
17
*
w w
By default:
18
Example
Grammar Derivations
S aSb *
S
S
*
S ab
*
S aabb
*
S aaabbb
19
Example
Grammar Derivations
S aSb
S aaSbb
S
aaSbb aaaaaSbbbbb
20
Another Grammar Example
G S Ab
Grammar : A aAb
A
Derivations:
22
More Derivations
S Ab aAbb aaAbbb aaaAbbbb
aaaaAbbbbb aaaabbbbb
S aaaabbbbb
S aaaaaabbbbbbb
n n
S a b b 23
Language of a Grammar
For a grammar G
with start variable S :
L(G ) {w : S w}
String of terminals
24
Example
G S Ab
For grammar : A aAb
A
n n
L(G ) {a b b : n 0}
n n
Since: S a b b
25
A Convenient Notation
A aAb
A aAb |
A
article a
article a | the
article the
26
Linear Grammars
27
Linear Grammars
Grammars with
at most one variable at the right side
of a production
S aSb S Ab
Examples: S A aAb
A
28
A Non-Linear Grammar
Grammar G : S SS
S
Why S SS
S aSb
What will happen if
S bSa
S S instead
L(G ) {w : na ( w) nb ( w)}
Number of a in string w 29
Another Linear Grammar
G SA
Grammar : A aB |
B Ab
n n
L(G ) {a b : n 0}
30
Right-Linear Grammars
string of
Example: terminals
S abS
S a
31
Left-Linear Grammars
string of
S Aab
Example: terminals
A Aab | B
Ba 32
Regular Grammars
33
Regular Grammars
Examples: G1 G2
S abS S Aab
S a A Aab | B
Ba 34
Observation
36
Theorem
Languages
Generated by
Regular Grammars
Regular
Languages
37
Theorem - Part 1
Languages
Generated by Regular
Languages
Regular Grammars
38
Theorem - Part 2
Languages
Generated by Regular
Languages
Regular Grammars
39
Proof – Part 1
Languages
Generated by Regular
Languages
Regular Grammars
40
The case of Right-Linear
Grammars
L( M ) L(G )
with L(M)=L(G)
Grammar Gis right-linear
Example:
S aA | B
A aa B
Bb B|a
42
Construct NFA M such that
every state is a grammar variable:
A special
S VF
final state
B
S aA | B
A aa B
Bb B|a 43
Add edges for each production:
a A
S VF
B
S aA
44
a A
S VF
B
S aA | B
45
A
a a
S a VF
B
S aA | B
A aa B
46
A
a a
S a VF
B
S aA | B
b
A aa B
B bB 47
A
a a
S a VF
a
B
S aA | B
b
A aa B
B bB | a 48
A
a a
S a VF
a
B
A right-linear grammar G
53
For each production: Vi a1a2 am
we add transitions and intermediate
nodes
Vi a1 a 2 ………
a m VF
54
Resulting NFA M looks like this:
a9
a2 a4
a1 V1 V3
a3 a5
V0
a3 a4
VF
a8 a9
V2 a5
V4
A Ba1a2 ak
A a1a2 ak
57
Left G
A Ba1a2 ak
linear
Construct right-linear grammar G
A Bv
Right A ak a2a1B
G
linear
R
Av B 58
Construct right-linear grammar G
Left A a1a2 ak
G
linear
Av
Right
G A ak a2a1
linear
R
Av 59
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
Proof - Part 2
Languages
Generated by Regular
Languages
Regular Grammars
61
Any regular language L is generated
by some regular grammar G
Proof idea:
Let M be the NFA with L L(M ).
b
L ab * ab(b * ab) * q3
L L(M ) 63
Convert M to a right-linear grammar
b
M a
a
q0 q1 q2
q0 aq1 b
q3
64
b
M a
a
q0 q1 q2
q0 aq1
b
q1 bq1 q3
q1 aq2
65
b
M a
a
q0 q1 q2
q0 aq1
q1 bq1 b
q1 aq2 q3
q2 bq3
66
L(G ) L( M ) L
G b
q0 aq1 M a
a
q0 q1 q2
q1 bq1
q1 aq2 b
q2 bq3 q3
q3 q1
q3 67
In General
a
For any transition: q p
Add production: q ap
Add production: qf
69
Since G is right-linear grammar
with L(G ) L( M ) L
70
For any regular language one can
construct left linear as well as right
linear grammar.
71
A
a a
a VF
S a F → Ba
B B → Bb here F is
S aA | B B → Aaa
start symbol
b
A aa B B→S
A → Sa
B bB | a S → this
72 is new rule
as S is now a final state
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
Language generated by both
aaab* a +b*a
74
75
76
77
Context-Free Languages
78
Context-Free Languages
R
n n
{a b } {ww }
Regular Languages
79
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
80
Context-Free Grammars
81
Example
A context-free grammar G: S aSb
S
A derivation:
n n
L(G ) {a b : n 0}
(((( ))))
83
A context-free grammar G: S aSa
S bSb
S
Another derivation:
R
L(G ) {ww : w {a, b}*}
85
Example
A derivation:
S SS aSbS abS ab
86
A context-free grammar G: S aSb
S SS
S
A derivation:
L(G ) {w : na ( w) nb ( w),
and na (v) nb (v)
in any prefix v}
() ((( ))) (( )) 88
Definition: Context-Free
Grammars
Grammar G (V , T , P, S )
A language L is context-free
if and only if
L L(G )
90
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
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
Derivation Trees
93
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
Yield:
95
Partial derivation tree
96
S AB A aaA | B Bb |
S AB
S
A B
97
S AB A aaA | B Bb |
S AB aaAB
S
A B
a a A
98
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
99
S AB A aaA | B Bb |
A B
a a A B b
100
S AB A aaA | B Bb |
A B
a a A B b
101
S AB A aaA | B Bb |
A B
yield
a a A B b aab
aab
102
Partial Derivation Trees
S AB A aaA | B Bb |
S AB
Partial derivation tree S
A B
103
S AB aaAB
A B
a a A
104
sentential
S AB aaAB
form
A B
yield
a a A
aaAB
105
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
Ambiguity
107
E E E | E E | (E) | a
a aa
E E E E a E a EE
a a E a a*a
E E
leftmost derivation
a E E
a a 108
E E E | E E | (E) | a
a aa
E E a
a 109 a
E E E | E E | (E) | a
a aa
Two derivation trees
E E
E E E E
a E E E E a
a a a 110 a
The grammar E E E | E E | (E) | a
is ambiguous:
E E
E E E E
a E E E E a
a a a a
111
The grammar E E E | E E | (E) | a
is ambiguous:
E E E a E a EE
a a E a a*a
113
In other words:
114
Why do we care about ambiguity?
a aa
take a2
E E
E E E E
a E E E E a
a a a 115 a
2 22
E E
E E E E
2 E E E E 2
2 2 2 116 2
2 22 6 2 22 8
6 8
E E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 117 2
Correct result: 2 22 6
6
E
2 4
E E
2 2
2 E E
2 2 118
• Ambiguity is bad for programming languages
119
We fix the ambiguous grammar:
E E E | E E | (E) | a
F a
E E T T T F T a T a T F
a F F a aF a aa
E a aa
E E T
E T
E T
T T F T T F
T F
F F a
F (E)
F a a a
121
Unique derivation tree
E a aa
E T
T T F
F F a
a a
122
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
Inherent Ambiguity
S S1 | S 2 S1 S1c | A S2 aS2 | B
A aAb | B bBc |
124
n n n
The string a b c
has two derivation trees
S S
S1 S2
S1 c a S2
125
Compiler
Lexical
parser
analyzer
input output
machine
program
code
126
A parser knows the grammar
of the programming language
127
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
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