0% found this document useful (0 votes)
23 views

6 CFG

The document discusses context free grammars including their definition, examples of context free grammars, derivation of strings from grammars, parse trees, ambiguity in grammars, techniques for designing grammars including merging grammars and converting DFAs to CFGs, and putting grammars in Chomsky normal form.

Uploaded by

mythemyaseen6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

6 CFG

The document discusses context free grammars including their definition, examples of context free grammars, derivation of strings from grammars, parse trees, ambiguity in grammars, techniques for designing grammars including merging grammars and converting DFAs to CFGs, and putting grammars in Chomsky normal form.

Uploaded by

mythemyaseen6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

2.

1 Context free grammars


CONTEXT-FREE GRAMMARS

• Example context free grammar G1:

A → 0A1
A→B
B→#

• 3 Substitution rules (productions)


• Variables = {A, B}
• Terminals = {0, 1, #}
• Start variable = A
Derivation
• For example, grammar G1 generates the string 000#111.
A → 0A1
A→B
B →#

• The sequence of substitutions to obtain a string is called a derivation.


A derivation of string 000#111 in grammar G1 is
• A ⇒ 0A1 ⇒ 00A11 ⇒ 000A111 ⇒ 000B111 ⇒ 000#111.
Examples
• Can the following strings be derived from G1:

0#1 A ⇒ 0A1 ⇒ 0B1 ⇒ 0#1 A → 0A1


A→B
0#11 Cannot be derived. 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

into a single line


A → 0A1 | B

using the symbol “|” as an “or”.


Grammar G2
Examples
Strings in L(G2) include:
• a boy sees
• the boy sees a flower
• a girl with a flower likes the boy
Derivation of “a boy sees”
FORMAL DEFINITION OF A CONTEXT-FREE
GRAMMAR
More definitions
Examples
Example
Design a CFG for the following language:
L = {w | w is a properly nested parentheses}
(), (()), (()())(), ()()()() are in L
()), (()(), ))(( are not in L

G3 = ({S}, {(, )}, R, S). The set of rules, R, is

S → (S) | SS | ε
Examples
DESIGNING CONTEXT-FREE GRAMMARS
As with the design of finite automata the design of context-free
grammars requires creativity.

But there are some useful techniques


Technique I: Merging Grammars
Technique II: DFA to CFG
• You can convert any DFA into an equivalent CFG as follows.
• Make a variable Ri for each state qi of the DFA.
• Add the rule Ri → aRj to the CFG if δ(qi,a) = qj is a transition in the DFA.
• Add the rule Ri → ε if qi is an accept state of the DFA.
• Make R0 the start variable of the grammar, where q0 is the start state of the
machine
Technique II: DFA to CFG
E → 0E
E → 1O
O → 0O
O → 1E
O→ε

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

the girl touches the boy with the flower

(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.

You might also like