Professional Documents
Culture Documents
Theory Of Computation
Context-Free Languages
Context-Free Grammars
Formal Definition
Designing
Ambiguity
Pushdown Automata
Formal Definition
Mashiour Rahman
Assistant Professor
Computer Science Department
American International University-Bangladesh
Context-Free Languages
Context-Free Languages includes all regular
languages and many additional languages,
such as {0n1n | n 0}.
Additional memory is used to recognize such
languages.
Context-Free Grammars.
More powerful method of describing language.
Can describe certain features that have a
recursive structure.
CSC3113 Theory Of Computation
Mashiour Rahman
Context-Free Grammars 2
Context-Free Grammars
Following is an example of a context-free grammar
A 0A1
A 0A1 | B
AB
or
B#
B#
A grammar consists of a collection of Substitution (or Production) rules.
Each rule appears as a line in the grammar.
Each line has a variable and a string separated by an arrow.
Variable consists of a symbol.
represented as uppercase letters.
might include index or subscript.
One variable is designated the start variable, usually the left-hand side of the
topmost rule.
Several rules for the same left-hand variable can be written using | as an or.
CSC3113 Theory Of Computation
Mashiour Rahman
Context-Free Grammars 3
Context-Free Grammars
Use a grammar to describe a language by
generating each string of the language in the
following manner
1.
2.
3.
4.
5.
Mashiour Rahman
Context-Free Grammars 4
Formal Definition
A context-free grammar is a 4-touple (V, , R, S),
where
V is a finite set called variables,
is a finite set, disjoint from V, called the alphabet,
R is a finite set of rules, with each rule being a variable and a
strings of variables and terminals, and
SV is the start variable.
written as u v.
*
Mashiour Rahman
Context-Free Grammars 5
Designing
CFG that is union of simpler CFGs:
Break into simpler pieces and Construct individual grammars for
each piece.
Combine them into one grammar by putting all the rules together
and adding a new rule, S s1 | s2 || sk, where the
variables si are the start variables for the individual grammars.
Example:
Mashiour Rahman
Context-Free Grammars 6
Designing
CFG for regular language:
Construct a DFA M=(Q, , , q0, F) for the regular language.
Convert the DFA M into equivalent CFG as follows
Variable Ri for each state qiQ. Make R0 the start variable for the start state q0.
Add rule Ri aRj to the CFG for each (qi, a) = qj.
Example:
Language: A = {w | the sum of all the symbols in w is an even number }
DFA:
M=(Q, , , q0, F), where, Q={b0,b1}, q0=b0, F={b0}, ={0,1}
b0
b1
0
b0
b1
1
b1
b0
2
b0
b1
CFG:
Grammar G = (V, , R, R0), where, V = {R0, R1}, = {0, 1, 2}, and
R is the set of rules
R0 0R0 | 1R1 | 2R0
R1 0R1 | 1R0 | 2R1
CSC3113 Theory Of Computation
Mashiour Rahman
Context-Free Grammars 7
Designing
Language containing strings with two
substrings that are linked or depended on
each other by their number of appearances.
Use rule of the form R uRv.
This generates strings wherein the portion
containing the us corresponds to the portion
containing vs.
Example:
Language: {1n0n | n0}
CFG: S 1S0 |
CSC3113 Theory Of Computation
Mashiour Rahman
Context-Free Grammars 8
Designing
Language containing strings with certain structures appearing recursively
as part of other (or the same) structure:
Place the variable symbol generating the structure in the location of the
rules corresponding to where that structure may recursively appear.
Mashiour Rahman
Context-Free Grammars 9
EXPR
EXPR
EXPR
EXPR
EXPR
EXPR
EXPR
EXPR
Mashiour Rahman
EXPR
Context-Free Grammars 10
Ambiguity - Derivation
When we say that a grammar generates a string
ambiguously, we mean that the string has two different parse
trees, not two different derivations.
A derivation of string w in a grammar G is a leftmost
derivation if at every step the leftmost remaining variable is
the one replaced.
Then we can say, a string w is derived ambiguously in CFG
G if it has two or more different leftmost derivations.
Grammar G is ambiguous if it generates some string
ambiguously.
Some CFLs can only be generated by ambiguous grammars.
Such languages are called inherently ambiguous.
Example: {0i1j2k | i=j or j=k}
CSC3113 Theory Of Computation
Mashiour Rahman
Context-Free Grammars 11
Mashiour Rahman
Context-Free Grammars 12
Mashiour Rahman
Context-Free Grammars 13
Mashiour Rahman
Context-Free Grammars 14
Example
Original Grammar
S ASA | aB
A B | S
Add
Add
Remove
Remove
Remove
rule
ruleAU
S
S
A
B
A
B
S
a
1
0 SA
1
B b |
S0
B |
AA
S 11 |||UaB
ASA
AA
||aaa|||SA
SA
SA|||AS
AS
AS
1aB
S
AA
UaB
aaa ||| SA
|||AS
1 |
1B |
S
AA
|
|
SA
AS
ASA
|
aB
|
SA
AS | S
1
S
A b | AA1 | U1B | a | SA | AS
A
b
|
AA
1 |
A
B
b
|
S
ASA
| aB
aB || aa || SA
SA || AS
AS
A
B
|
S
|
B b
B
b
B
SA
B1 b
b |
A
U11
SA
a
Action: Add
Remove
Converting
new start
unit
rules
into
rules
symbol
of
proper
the
of the
form
form
form
R
RPQ
R
Pand Ru
CSC3113 Theory Of Computation
Mashiour Rahman
Context-Free Grammars 15