TIC 2151 - Theory of Computation: Context-Free Grammars (CFG)
TIC 2151 - Theory of Computation: Context-Free Grammars (CFG)
TIC 2151 - Theory of Computation: Context-Free Grammars (CFG)
Computation
Lecture 6
Context-Free Grammars (CFG)
Introduction
Formal description of a CFG
Ambiguous CFGs
Normal forms
2
Context-free grammars (CFG)
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.
3
Context-free grammars (CFG)
4
Context-free grammars (CFG)
derivation of a word
You may also represent the same information pictorially with a parse tree.
7
Some
Context-free grammars (CFG) 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
8
Some
Context-free grammars (CFG) 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
9
Context-free grammars (CFG) Ambiguity
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.
10
Context-free grammars (CFG) Ambiguity
Ambiguity
Consider the following rules of a grammar and give two different parse trees for
a+axa
Remark:
any context-free grammar may be transformed to a CNF grammar expressing the
same language
12
Chomsky Normal Form (CNF) CFG to CNF
Converting a CFG to CNF Try to follow every step! Don’t go to the next step, if you
still have problems with the one before!
Original CF grammar
S => ASA | aB Step 1. Add a new start variable S0 and the rule S0 S,
A => B | S where S was the original start variable. This change
B => b | e guarantees that the start variable doesn’t occur on the
right-hand side of a rule
1. Insert a new start symbol
S0 => S Step 2.we take care of all e-rules. We remove an e-rule
S => ASA | aB Ce, where C is not the start variable. Then for each
A => B | S occurrence of an C on the right-hand side of a rule, we
B => b | e add a new rule with that occurrence deleted. if R uCv
is a rule in which u and v are strings of variables and
2. Remove the rule B => e terminals, we add rule R uv. The rule R uCvCw
S0 => S causes us to add R uvCw, R uCvw, and R uvw.
S => ASA | aB | a If we have the rule R C, we add R e unless we had
A => B | S | e previously removed the rule R e .
B => b We repeat these steps until we eliminate all e -rules not
involving the start variable.
13
Chomsky Normal Form (CNF) CFG to CNF
4. Remove the rule S => S Step 3 .Eliminate all unit rules of the form
S0 => S CD. We repeat these steps until we
S => ASA | aB | a | SA | AS eliminate all unit rules.
A => B | S
B => b
14
Chomsky Normal Form (CNF) CFG to CNF
SOLUTION
1. elimination of e, we obtain: 2. elimination of the unit production we obtain
16
Our next problem: CFG
17
The Cocke-Younger-Kasami
Algorithm (CYK)
CYK – The Concept
S
C D
w1w2. . . . . . wk wk+1. . . wn
For each word, there must be a rule of the form A BC, where
B 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.
18
CYK – In Action Example (1)
19
CYK – In Action Example (1)
a a a b b b c c
C C C D D D E,B E,B This (darkgray) part
A B is produced by B
F
A
This (gray) part
F is produced by this A
A
20
CYK – In Action
a a a b b b c c
A F
B E
C D
21
CYK – In Action Example (2)
S S
A
S
YES NO
22
Learning Outcomes
23