Professional Documents
Culture Documents
chapter-7-Context-Free Languages
chapter-7-Context-Free Languages
Context-Free Languages
& context-free Grammars
1
Context-Free Languages (CFL)
In order to cover this and other more complicated features we must enlarge
the family of languages, i.e. context-free languages. A context-free
language is generated by a context-free grammar.
Definition:
a grammar G=(V, T, S, P) is said to be context-free
if all productions in P have the form:
A→x where A V and x (V T )*
3
Context-Free Grammars (CFG)
a language L is said to be context-free if and only if there is
a context-free grammar G such that L=L(G).
4
Linear context-free Grammars
The grammar G=({S}, {a,b}, S, P) with productions:
S → aSa
S → bSb
S→
5
Nonlinear context-free Grammars
The grammar G=({S}, {a,b}, S, P) with productions:
S → aSb Ι SS Ι
There are many equivalent grammars: It is not easy to see if there are any
linear grammar equivalent to the grammar above.
6
Equivalent context-free Grammars
The language L={anbm : n ≠ m} is context free.
From this we see that two derivations yield the same sentence and use
the same productions. The difference is in the order in which the
productions are applied. To remove such irrelevant factors, we often
require that the variables be replaced in a specific order. 8
Leftmost and Rightmost derivation
• A derivation is said to be Leftmost if in each step the leftmost variable in
the sentential form is replaced
S => aAB => aA => abBb => abAb => abbBbb => abbbb
9
Parsing and Ambiguity
•We have concentrated on the generative aspects of grammars:
given a grammar G, we studied the set of strings that can be
derived using G
•We are also concerned with the analytical side of the grammar:
given a string w of terminals, we want to know whether or not w is in
L(G)
10
Parse Tree
A second way of showing derivations, independent of the order in which
productions are used, is by using a Parse Tree. A parse tree is an
ordered tree in which nodes are labeled with the left sides of productions
and in which the children of a node represent its corresponding right
sides. Beginning with the root, labeled with the start symbol and ending in
leaves that are terminals. A partial parse tree, every leaf has a label
from (V T).
S
S → SS Ι aSb Ι ab
S S
a S b a b
Poof:
first we show that for every sentential form of L(G) there is a corresponding
partial parse tree. by induction on the number of steps in the derivation.
S => u implies that there is a production S → u . Assume that for every
sentential form derivable in n steps, there is a corresponding partial parse tree.
Now any w derivable in n+1 steps must be such that:
Note: parse trees show which productions are used in obtaining a sentence, but
do not give the order of their application.
12
Ambiguous Grammars
• A CFG is ambiguous if there is a string in
the language that is the yield of two or
more parse trees.
S S
S S S S
S S a b a b S S
a b a b a b a b
14
Ambiguity, Left- and
Rightmost Derivations
• If there are two different parse trees,
they must produce two different leftmost
derivations by the construction given in
the proof.
• Conversely, two different leftmost
derivations produce different parse
trees by the other part of the proof.
• Likewise for rightmost derivations.
15
Ambiguity – Continued
• Thus, equivalent definitions of
“ambiguous grammar’’ are:
1. There is a string in the language that has
two different leftmost derivations.
2. There is a string in the language that has
two different rightmost derivations.
16
Ambiguity is a Property of
Grammars, not Languages
Next
symbol
28
Inherent Ambiguity
• It would be nice if for every ambiguous
grammar, there were some way to “fix” the
ambiguity, as we did for the balanced-
parentheses grammar.
• Unfortunately, some CFL’s are inherently
ambiguous, meaning that every grammar
for the language is ambiguous.
29
Example: Inherent Ambiguity
• The language {0i1j2k ; i = j or j = k} is
inherently ambiguous.
• Intuitively, at least some of the strings of
the form 0n1n2n must be generated by two
different parse trees, one based on
checking the 0’s and 1’s, the other based
on checking the 1’s and 2’s.
30
One Possible Ambiguous
Grammar
S -> AB | CD
A -> 0A1 | 01
A generates equal 0’s and 1’s
B -> 2B | 2
B generates any number of 2’s
C -> 0C | 0
C generates any number of 0’s
D -> 1D2 | 12
D generates equal 1’s and 2’s
32