lecture 06 context-free grammars
TRANSCRIPT
-
8/18/2019 Lecture 06 Context-Free Grammars
1/23
TIC 2151 – Theory of
Computation
Lecture 6
Context-Free Grammars (CFG)
TIC 2151 – Theory of Computation
-
8/18/2019 Lecture 06 Context-Free Grammars
2/23
2
Lecture 6 – Outline
Introduction Formal description of a CFG
Ambiguous CFGs
Normal forms
-
8/18/2019 Lecture 06 Context-Free Grammars
3/23
3
Context-free grammars (CFG)
context-f ree grammars CFG, a more powerful method of describing
languages. Such grammars can describe certain features that have a
recursive structure, which makes them useful in a variety of applications.The collection of languages associated with context-free grammars are called
the context-f ree languages.
A language is context free if and only if some pushdown automaton recognizes
it.Languages that can be accepted by some PDA or generated by some CFG are
context-free.
Many useful languages are context-free, including most programming
languages, query languages, and markup languages.
We can establish a relationship between the regular languages and the context-
free languages. Because every regular language is recognized by a finite
automaton and every finite automaton is automatically a pushdown automaton
that simply ignores its stack, we now know that every regular language is also a
context-free language.
-
8/18/2019 Lecture 06 Context-Free Grammars
4/23
4
Context-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
5/23
5
The following is an example of a context-free grammar, which we call G.
Grammar G:
A => 0A1
A => B
B => #
derivation of a word
You may also represent the same information pictorially with a parse tree.
See next slide
Context-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
6/23
6
parse tr ees for the word 00#11
Formal description of a CFG
Formally written we have the grammar G:G = (V, S, R, S)
with V = {A,B}
S = {0,1,#}
R = {[A, 0A1], [A, B], [B, #]}
S = A
Context-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
7/23
7
G = ({S}, {a, b},R, S). The set of rules, R, is S => aSb | SS |
This grammar G generates strings such as abab, aaabbb, and aababb.
Give a parse trees for aaabbb and abab
Some
exercises
Give the formal definition for the following grammar an describe the
produced language: S => aSb | SS |
Context-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
8/23
8
Some
exercises
Give the formal definition for the following grammar and give parse trees for
a + a x a and (a + a) x a
Formal Definition
Context-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
9/23
9
Some
exercises
Give the formal definition for the following grammar and give parse trees for
a + a x a and (a + a) x a
Formal Definition
Context-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
10/23
10
Ambiguity
o Sometimes a grammar can generate the same string in several different
ways. Such a string will have several different parse trees and thus
several different meanings.
o This result may be undesirable for certain applications, such as programming languages, where a program should have a unique
interpretation.
o A grammar is called ambiguous if there is at least one word having two
different parse trees.
AmbiguityContext-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
11/23
11
Ambiguity
Consider the following rules of a grammar and give two different parse trees for
a + a x a
This grammar generates the string a + a x a ambiguously.
Sometimes when we have an ambiguous grammar we can find an
unambiguous grammar that generates the same language.
A grammar is called inherently ambiguous if there is no equivalent
unambiguous grammar to generates it. {aib jck | i=j or j=k} is inherently
ambiguous!
AmbiguityContext-free grammars (CFG)
-
8/18/2019 Lecture 06 Context-Free Grammars
12/23
12
When working with context-free grammars CFG, it is often convenient to
have them in simplified form. One of the simplest and most useful forms
is called the Chomsky normal form.
Remark:
any context-free grammar may be transformed to a CNF grammar expressing the
same language
Chomsky Normal Form (CNF)
-
8/18/2019 Lecture 06 Context-Free Grammars
13/23
13
Original CF grammar S => ASA | aB
A => B | S
B => b | e
1. Insert a new start symbol
S0 => S
S => ASA | aB
A => B | S
B => b | e
2. Remove the rule B => e
S0 => S
S => ASA | aB | a
A => B | S |
B => b
Converting a CFG to CNF
Chomsky Normal Form (CNF) CFG to CNF
Step 1. Add a new start variable S0 and the rule S0S,
where S was the original start variable. This change
guarantees that the start variable doesn’t occur on the
right-hand side of a rule
Step 2.we take care of all e-rules. We remove an e-ruleC , where C is not the start variable. Then for each
occurrence of an C on the right-hand side of a rule, we
add a new rule with that occurrence deleted. if R uCv
is a rule in which u and v are strings of variables and
terminals, we add rule R uv. The rule R uCvCw
causes us to add R
uvCw, R
uCvw, and R
uvw.If we have the rule R C, we add R unless we had
previously removed the rule R e .
We repeat these steps until we eliminate all e -rules not
involving the start variable.
Try to follow every step! Don t go to the next step, if you
still have problems with the one before!
-
8/18/2019 Lecture 06 Context-Free Grammars
14/23
14
3. Remove the rule A => e
S0 => S
S => ASA | aB | a | SA | AS | S
A => B | S
B => b
4. Remove the rule S => S
S0 => S
S => ASA | aB | a | SA | AS
A => B | S
B => b
5. Remove the rule S0 => SS0 => ASA | aB | a | SA | AS
S => ASA | aB | a | SA | AS
A => B | S
B => b
Chomsky Normal Form (CNF) CFG to CNF
Step 3 .Eliminate all unit rules of the form
CD. We repeat these steps until we
eliminate all unit rules.
Try to follow every step! Don t go to the next step, if you
still have problems with the one before!
-
8/18/2019 Lecture 06 Context-Free Grammars
15/23
15
6. Remove the rule A => B
S0 => ASA | aB | a | SA | ASS => ASA | aB | a | SA | AS
A => S | b
B => b
7. Remove the rule A => S
S0 => ASA | aB | a | SA | ASS => ASA | aB | a | SA | AS
A => b | ASA | aB | a | SA | AS
B => b
8. Insert new symbols for the last incorrect rules
S0 => AA1 | UB | a | SA | AS
S => AA1 | UB | a | SA | AS
A => b | AA1 | UB | a | SA | AS
B => b
A1 => SA
U => a
Chomsky Normal Form (CNF) CFG to CNF
Try to follow every step! Don t go to the next step, if you
still have problems with the one before!
Step 4. Take care of rules with more
than two terminals or variables.
convert all remaining rules into the
proper form.
-
8/18/2019 Lecture 06 Context-Free Grammars
16/23
16
Covert the following CFG into Chomsky Normal Form (CNF)
SOLUTION
1. elimination of e, we obtain: 2. elimination of the unit production we obtain
Chomsky Normal Form (CNF) CFG to CNF
3. convert all remaining rules into the
proper form. replace a by A, b by B
and c by C, we obtain
-
8/18/2019 Lecture 06 Context-Free Grammars
17/23
17
Our next problem:
I f we have a word w, how can we determine if it can be
generated by a context-free grammars CFG ?
CFG
-
8/18/2019 Lecture 06 Context-Free Grammars
18/23
18
S
C D
w1w2. . . . . . wk wk+1. . . wn
The Cocke-Younger-Kasami
Algorithm (CYK)
CYK – The Concept
For each word, there must be a rule of the form A BC, whereB produces the left part of the word and C the right part.
We replace the occurrence of BC with A.
This process is repeated, and should leave us with the start
variable S.
-
8/18/2019 Lecture 06 Context-Free Grammars
19/23
19
Given the following grammar
S => AB
A => ab | aAb
B => c | cB
and in Chomsky normal form
S => ABA => CD | CF
B => c | EB
C => a
D => b
E => c
F => AD
we build a table for aaabbbcc:
a a a b b b c c
C C C D D D E,B E,B
A B
F
A
F
A
S
S
If we find S here,
we knowaaabbbcc
is part of the language.
CYK – In Action Example (1)
-
8/18/2019 Lecture 06 Context-Free Grammars
20/23
20
a a a b b b c c
C C C D D D E,B E,BA B
F
A
F
A
S
S
This (darkgray) partis produced by B
As there is a rule S => AB,
we can fill S here.
This (gray) part
is produced by this A
CYK – In Action Example (1)
-
8/18/2019 Lecture 06 Context-Free Grammars
21/23
21
a a a b b b c c
A F
B E
C D
?
If there is one of the following
rules, so write the left side X in the box:
X => AD
X => BE
X => CF
If there are more than one, write all.
CYK – In Action
-
8/18/2019 Lecture 06 Context-Free Grammars
22/23
22
For the grammar:
we prove the membership of 00100111 and 01011100:
CYK – In Action Example (2)
S => SS | BA | BC
A => SCB => 0
C => 1
0 0 1 0 0 1 1 1
B B C B B C C C
S S
A
S
A
S
A
S
0 1 0 1 1 1 0 0
B C B C C C B B
S S
S
YES NO
-
8/18/2019 Lecture 06 Context-Free Grammars
23/23
23
Learning Outcomes
After completion of this lecture, you should be able to:
Draw parse trees for words generated from CFG.
Given the definition of a CFL, design a grammar (i.e. give the
grammar rules).
Convert a CFG into Chomsky Normal Form.
Apply the CYK algorithm to determine if a word is generated
form a CFG.