Beruflich Dokumente
Kultur Dokumente
Science (COSC-3302),
Lecture 4 (prepared after
Chapter 4 of Martins 2011
Stefan Andrei
textbook)
04/22/16
Course topics
1.1. Mathematical Tools and Techniques (Chapter 1)
1.2. Finite Automata and the Languages they Accept (Chapter 2)
1.3. Regular Expressions, Nondeterminism, and Kleenes Theorem (Chapter 3)
Exam 1
2.1. Context-Free Languages (Chapter 4)
2.2. Pushdown Automata (Chapter 5)
2.3. Context-Free and Non-Context-Free Languages (Chapter 6)
Exam 2
3.1. Turing Machines (Chapter 7)
3.2. Recursively Enumerable Languages (Chapter 8)
3.3. Undecidable Problems (Chapter 9)
3.4. Computable Functions (Chapter 10)
3.5. Introduction to Computational Complexity (Chapter 11)
Exam 3
04/22/16
Overview of Previous
Lecture
Regular Expressions, Nondeterminism, and
Kleenes Theorem
1.
2.
3.
4.
04/22/16
Context-Free Languages
1.
2.
3.
4.
5.
04/22/16
04/22/16
04/22/16
04/22/16
Context-Free Grammars:
Definitions and More
Definition: A context-free grammar (CFG) is a 4Examples
tuple G=(V, , S, P), where V and are disjoint
04/22/16
Context-Free Grammars:
Definitions and More
We will sometimes write to indicate a derivation
Examples
(contd.)
in a particular grammar
G
04/22/16
Context-Free Grammars:
Definitions and More
Definition: If
Examples
(contd.)
G = (V, , S, P) is a CFG,
the language generated by G is
L(G) = { x * | S G* x }
(S is the start variable, and x is a string of
terminals).
04/22/16
10
Context-Free Grammars:
Definitions and More
Consider AEqB = {x {a,b}* | n (x) = n (x)}
Examples
(contd.)
a
04/22/16
11
Context-Free Grammars:
Definitions and More
If a string x L starts with a, then the remainder is a
Examples
(contd.)
member of AEqB.
04/22/16
12
Context-Free Grammars:
Definitions and More
Theorem 4.9: If L and L are CFLs over , then
Examples
so are L L , (contd.)
L L , and L *.
1
1 2
04/22/16
13
04/22/16
14
04/22/16
V be Q,
S the initial state q0, and
P the set containing the production T aU for every
transition (T, a) = U in M and the production T for
every accepting state T of M.
Lecture 4 COSC 3302
15
04/22/16
16
Example
04/22/16
17
04/22/16
18
04/22/16
19
04/22/16
20
04/22/16
21
04/22/16
22
04/22/16
23
04/22/16
24
04/22/16
S1
if (
S1
S2
if (
S|
else
if (
S1 | OS
E
S1
else
S2
25
| (S) | a
G generates simple algebraic expressions.
One reason for ambiguity is that the relative
precedence of + and * has not been specified: a+a*a
could be interpreted as (a+a)*a or as a+(a*a).
In fact, S S + S causes ambiguity by itself, because
a+a+a could be interpreted as either (a+a)+a or a+
(a+a). Similarly for S S * S.
We might try to correct both problems by using the
productions:
S S + T | T
T T + F | F
(think of T as term and F as factor.)
04/22/16
26
04/22/16
27
04/22/16
28
29
04/22/16
30
Proof.
04/22/16
31
Examples
04/22/16
32
productions is similar.
We first identify pairs of variables (A, B) for
which
A * B (in this case we call B Aderivable); then for each such pair (A, B)
and each nonunit production B , we add
the production A .
Such pairs can be found as follows:
04/22/16
33
04/22/16
34
Example
04/22/16
35
04/22/16
36
productions.
The second step is to introduce for every terminal
symbol a new variable X and production X .
In every production, replace every terminal by its
new variable (except for the new productions
above).
Replace a production like A BACB by the
productions A BY1, Y1 AY2, Y2 CB, where Y1
and Y2 are new variables.
04/22/16
37
38
Summary
Context-Free Languages
1.
2.
3.
4.
5.
04/22/16
39
Reading suggestions
04/22/16
40
Coming up next
04/22/16
41
04/22/16
42