Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Context-Free Grammars

4 Sets

V – Variables

Σ – Terminals

P or R – Production or Rules

S – Start Variable/Symbol

A -> x

Comment:

 A is a variable
 X is any combo of terminals and variables.

Problem 1:

Want: 0011

G = {(S), (0,1), (S -> 0S1 | ɛ)}

S => 0S1 => 00S11 -> 0011

Note:

 The => symbol called yields.


 The underlined called derivations, S => … => x ∈ Σ* this means from S we apply a bunch
of rules and then we get something in the form x where this is in sigma star.
 Σ* means set of all possible strings including empty strings.
 x ∈ Σ* means that x is a string composed of zero or more symbols from the
alphabet/terminal Σ.
Problem 2:

G = {(S,A,B), (a,b), (S -> AB | ɛ, A -> aSB, B -> b | SA)}

S => AB

Note:

 Leftmost derivation always replace leftmost variable.


 Every derivation has an equivalent left most derivation.
 Derivation in CFG means replacing variable with non-terminals/terminals.

Problem 3:

G = {(S), (), (S - > SS | ɛ)}

S => ɛ

S => SS => S => ɛ

Note:

 Both of them are different leftmost derivations, we called it ambiguous grammar


because the string that grammar makes can be achieved with two ways of derivations.
 S -> ɛ this grammar is unambiguous.

Questions:

1) Can we “remove” ambiguity from a CFG?


Answer: No in general.
2) Are there “inherently ambiguous” CFLs? Every CFG is ambiguous. Are there languages
that every grammar is ambiguous?
Answer: Yes

Problem 4:

L = (0 U 1)*

Solution:
G = {(S), (), (S -> ɛ | S0 | S1)}

Answer: The created CFG can be empty, starts with 0 or 1, and ends with 0 or 1.

Problem 5:

L = { 0n1m or 0*1*: n, m >= 0 }

Note:

 All leftside should be 0, then rightside must be 1.

Solution:

G = {(S,P), (0,1), (S -> 0S | P, P -> 1P | ɛ)}

Want = 001

S => 0S => 00S => 001P => 001

Problem 6:

L = { palindromes over {0, 1} }

Note:

 If you see 1 at the beginning then the end must be 1, same with 0.

G = {(S), (0,1), (S -> 0S0 | S -> 1S1 | 1 | 0 | ɛ)}


Problem 7:

La = L 1 U L 2

Lb = L 1 • L2

Lc = L 1*

Note:

 L1, L2 are CFLs


 G1, G2 are CFGs
 S1,S2 are start variables

Answer:

La = S -> S1 | S2

Lb = S -> S1S2

Lc = S -> ɛ | S1S

Problem 8:

L = { aibjck : i != j }

G = {(S, Ai<j, Ai>j B, C, X), (a,b,c), (S -> Ai<jC | Ai>jC, Ai<j -> aAi<jb | bB, B -> bB | ɛ, C -> cC | ɛ, Ai>j ->
aAi>jb | aX, X -> aX | ɛ)}

S -> Ai<jC | Ai>jC

Ai<j -> aAi<jb | bB

B -> bB | ɛ

C -> cC | ɛ

Ai>j -> aAi>jb | aX

X -> aX | ɛ
Problem 9:

L = { 0n1m2m3n : n, m > 0 }

S -> 0S3 | X

X -> 1x2 | ɛ

Problem 10:

L = { 0n1n2m3m : n, m >= 0 }

S -> X1X2

X1 -> 0X11 | ɛ

X2 -> 2X23 | ɛ

Problem 11:

L = { non-palindromes over { 0, 1} }

S -> 0S0 | 1S1 | 0X1 | 1X0

X -> 0X | 1X | ɛ

Chomsky Normal Form


Important steps:

1. Ensure start variable not on RHS.


2. Eliminate ɛ.
3. Eliminate unit rules (A -> B) one variable on RHS.
4. Ensure RHS is all variables or single terminal (S0 - > ɛ).
5. Reduce “long” RHS’s (>= 3 variables) to 2.

You might also like