6 CFG
6 CFG
6 CFG
A → 0A1
A→B
B→#
# A⇒B⇒#
Parse Tree
Example Parse Trees for Natural Languages
Example Parse Trees for Natural Languages
Language of the grammar.
• All strings generated in this way constitute the language of the
grammar. We write L(G1) for the language of grammar G1.
• Some experimentation with the grammar G1 shows us that L(G1) is:
A → 0A1
A→B
B→#
{0n#1n| n ≥ 0}
“|” symbol
For convenience when presenting a context-free grammar, we
abbreviate several rules with the same left-hand variable, such as
A → 0A1 and A → B
S → (S) | SS | ε
Examples
DESIGNING CONTEXT-FREE GRAMMARS
As with the design of finite automata the design of context-free
grammars requires creativity.
Example Derivation:
E ⇒ 0E ⇒ 00E ⇒ 001O ⇒ 0010O ⇒ 00101E ⇒ 001011O ⇒
001011
Ambiguity
Example Derivations
• E -> E + E | E x E | (E) | a
a+a
•E ⇒ E + E ⇒ a + E ⇒ a + a
((a + a) x a)
• E ⇒ (E) ⇒(E x E) ⇒ ((E) x E) ⇒ ((E+E) x E) ⇒ ((a + a) x a)
a+axa
•E ⇒ E + E ⇒ E + E x E ⇒ a + a x a
•E ⇒ E x E ⇒ E + E x E ⇒ a + a x a
Ambiguity
Ambiguity
(a) (b)
Leftmost derivation
• A derivation of a string w in a grammar G is a leftmost derivation if at
every step the leftmost remaining variable is the one replaced. The
derivation below is a leftmost derivation.
Ambiguity
Chomsky Normal Form
Theorem
Example
Example con’t.
Example con’t.
Example con’t.
Example con’t.