Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Context-free grammars
2. Parsing and ambiguity
3. Context-free grammars and programming languages
4. Tutorial questions
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
Context-free grammars
Recall regular grammar definition:
A grammar G = (V, T, S, P ) is called to be right-linear if all its
productions are of the form:
A xB, or
A x,
where A, B V and x T .
For example, grammar G:
A aB, B abA|b,
is a right-linear regular grammar, and L(G) = {(aab)n ab : n 0}.
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
Consider G: S aSb|SS|.
L(G) = {, ab, abab, aabbaabb, }
= {w {a, b} : na (w) = nb (w), na (v) nb (v),
v is any prefix of w}.
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
Derivation tree
Consider G:
S aAB
A bBb
B A|
S
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
S
S
S
S
SS,
aSb,
bSa,
.
(3) and (4) cannot lead to the generation of aabb, so give up them.
10
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
S
S
S
S
SS
SS
SS
SS
SSS,
aSbS
bSaS,
S,
S
S
S
S
aSb aSSb,
aSb aaSbb,
aSb abSab,
aSb ab,
11
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
Observation:
(1) Exhaustive search parsing is too tedious and inefficient;
(2) For some w 6 L(G), it is possible the parsing procedure never
stops.
Example.
S aaB
A bBb|
B Aa
Show string aabbabba is not in L(G).
In a context-free grammar G, removing productions
A and
AB
from G will make derivation much easier.
12
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
13
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
14
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
15
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
16
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
17
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
18
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
19
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
20
c
Lecture Note for 300121 Formal Languages and Automata
UWS
(Yan Zhang)
4 Tutorial questions
1. Exercises 3, 7(c), 8(h), and 19 on pages 133-134.
2. Exercises 1, 13 and 15 on pages 144-145.
Questions in the 3rd edition textbook
1. Exercises 3, 7(c), 8(h), and 18 on pages 133-135.
2. Exercises 1, 13 and 15 on page 145.
21