Font size
WorksheetsContext-Free Grammar
Total questions: 98
Worksheet time: 49mins
What is a rule in a Context-Free Grammar (CFG)?
A rule can be applied only in a specific context
A rule can be applied anywhere the single non-terminal (variable) on the left side appears, regardless of its context (surrounding symbols)
A rule can be applied only if there are multiple variables on the left side
A rule can be applied only to terminal symbols
Rule Format: Fill in the blank for the rule format in a Context-Free Grammar (CFG): ____ → β (Only a single variable on the left side)
A → β
A B → β
a → β
A → aB
The language L = {aⁿbⁿ | n ≥ 1} requires an equal number of a's and b's. Which of the following strings belong to this language? Select all that apply.
ab
aabb
aaabbb
aabbb
abbb
Which of the following best describes the language L = anbn∣n≥1 ?
The language requires an equal number of a's and b's (e.g., ab, aabb, aaabbb).
The language requires more a's than b's.
The language requires more b's than a's.
The language requires only a's.
Fill in the blank: The CFG rules for the language L are: S → aSb S → __
ab
ba
aabb
bbaa
Fill in the blank: The parsing example for deriving 'aabb' is: S → aSb → a(ab)b → __
aabb
abab
aaab
abb
Why does the rule S → aSb work for generating strings in the language L = {a^n b^n | n ≥ 1}?
It always expands to aSb, generating a's and b's simultaneously and enforcing count equality.
It generates only a's.
It generates only b's.
It does not enforce count equality.
What is Chomsky Normal Form (CNF)?
A way to represent Regular Grammars
A highly restricted and standardized way to represent Context-Free Grammars (CFGs)
A method for parsing ambiguous grammars
A type of finite automaton
Chomsky Normal Form (CNF) enforces that every single production rule in the grammar must strictly follow one of two simple structures.
True
False
Which type of production in formal grammar requires a non-terminal (A) to derive exactly two non-terminals (B and C)?
Binary Production
Terminal Production
Phrase Production
Word Production
Fill in the blank: In Terminal Production, a non-terminal (A) must derive exactly ___ terminal symbol (a, a real word/character).
one
two
three
zero
What is the primary role of CNF?
To make rules more complex
To enable efficient and algorithmic parsing (understanding sentence structure) by a computer
To slow down parsing algorithms
To remove binary branching
Fill in the blank: The rules in Complex CFG are ______ and complex, while the rules in CNF are simple and uniform.
arbitrary
fixed
predictable
uniform
Which algorithm requires CNF to work and is used for efficient parsing?
CYK Algorithm (Cocke-Younger-Kasami)
Dijkstra's Algorithm
Merge Sort
Binary Search
CNF forces the Parse Tree to be Binary Branching (two branches always).
True
False
What is the original complex rule (Non-CNF) shown in the example?
S → open AB close
S → AB open close
S → open close AB
S → AB close open
According to the example, what is the difficulty in the original complex rule?
Mixing terminals with non-terminals and having more than two symbols on the right side.
Mixing only terminals.
Having only one symbol on the right side.
Mixing non-terminals only.
Fill in the blank: The CNF solution breaks the single complex rule down into ______ simple binary steps and two terminal steps.
three
four
five
two
Fill in the blank: S → ________
X_open Z_1
Y_closed Z_2
X_closed Z_3
Y_open Z_4
Fill in the blank: Z_1 → ________
A Z_2
B Z_3
C Z_4
D Z_5
Fill in the blank: Z_2 → ________
B X_close
A Y_open
C Z_far
D W_near
Fill in the blank: X_open → ________
open
close
start
exit
Fill in the blank: X_close → ________
close
open
exit
start
What is the result of the simplification process described?
The computer only has to check for simple binary pairs (X and Z_1) at every step, making the analysis structured and fast.
The computer must evaluate all possible combinations at each step, increasing complexity.
The process eliminates the need for binary analysis entirely.
The result is a random selection of binary pairs at each step.
Which of the following is NOT a valid rule in Chomsky normal form?
A) A → BC
B) A → a
C) A → B
D) S → ε (where S is the start variable)
In Chomsky normal form, 'a' is any _________.
terminal
non-terminal
production
variable
In Chomsky normal form, the rule S → ε is permitted if S is the start variable.
True
False
In Chomsky normal form, what do B and C represent in the rule A → BC?
B and C are any variables or terminals other than the start variable.
B and C are always terminal symbols.
B and C are always the start variable.
B and C are always epsilon (empty string).
Fill in the blank: The first step to convert any CFG to Chomsky normal form is to _________.
Add a new start symbol
Remove useless symbols
Eliminate left recursion
Convert to Greibach normal form
Fill in the blank: The second step to convert any CFG to Chomsky normal form is to eliminate ε rules of the form _________
A → ε
A → a
A → B
A → AB
Fill in the blank: The third step to convert any CFG to Chomsky normal form is to eliminate unit rules of the form _________
A → B
A → a
A → ε
A → BC
Fill in the blank: The fourth step to convert any CFG to Chomsky normal form is to convert remaining rules into proper form, which means _________?
2 non-terminal or 1 terminal
1 non-terminal or 2 terminals
3 non-terminals or 1 terminal
2 terminals or 1 non-terminal
According to the instructions for converting a CFG to Chomsky normal form, what is the new rule that should be created after adding a new start symbol?
S₀ → S
S → S₀
S₀ → ε
S → ε
In the process of converting a CFG to Chomsky normal form, which symbol is the start symbol?
S₀
S
CFG
Chomsky
What is the second step in converting a CFG to Chomsky normal form?
Eliminate all ε rules A → ε, where A is not the start variable
Add new rules with A deleted
Replace R → A with R → ε
Remove all terminal symbols
Fill in the blank: For each rule with an occurrence of A on the right-hand side, add a new rule with the ____ deleted.
A
left-hand side
terminal
start symbol
If we have R → A, what should we replace it with unless we had already removed R → ε?
R → ε
R → A
R → uAv
R → uv
Given the rule R → uAv, what does it become after eliminating ε rules?
R → uAv | uv
R → uAvAw | uvAw | uAvw | uvw
R → ε
R → A
Given the rule R → uAvAw, what does it become after eliminating ε rules?
R → uAvAw | uvAw | uAvw | uvw
R → uAv | uv
R → ε
R → A
What is the form of unit rules that need to be eliminated?
A → B
A → a
A → BC
A → ε
When converting a CFG to Chomsky normal form, what should you do for each rule B → u when eliminating unit rules?
Replace B → u with all rules of the form u → w in the grammar.
Remove B → u from the grammar without replacement.
Change B → u to B → ε.
Add a new non-terminal for each rule B → u.
Convert a CFG to Chomsky normal form: What is the process repeated until all unit rules have been replaced?
Repeat until all unit rules have been replaced.
Remove all terminal symbols from the rules.
Eliminate all left recursion in the grammar.
Add new start symbols to every production.
What is left to do in this step?
Complete the remaining task in this step.
Start a new project.
Review the previous step.
Skip to the final step.
Convert a CFG to Chomsky normal form Step 4: Convert remaining rules into proper form (2 variables or one terminal) Fill in the blanks for the rule transformation:
A → u₁A₁, A₁ → u₂A₂, ..., Aₖ₋₂ → uₖ₋₁uₖ
A → u₁u₂u₃, A₁ → u₄u₅, ..., Aₖ₋₂ → uₖuₖ₊₁
A → u₁A₁A₂, A₁ → u₂A₂A₃, ..., Aₖ₋₂ → uₖ₋₁Aₖ
A → u₁, A₁ → u₂, ..., Aₖ₋₂ → uₖ
What is the production rule for S in the given grammar?
S → S₁ | S₂
S → S₂ | S₁
S → S₁b | Ab
S → aAb | ab | ε
Fill in the blank: The production rule for S₁ is S₁ → _____
S₁b | Ab
S₁a | Aa
S₁c | Ac
S₁d | Ad
Which of the following is NOT a production for A?
aAb
ab
ε
S₁b
Fill in the blank: The production rule for S₂ is S₂ → _____
S₂ → S₂a | Ba
S₂ → aS₂ | aB
S₂ → aB | S₂a
S₂ → Ba | aS₂
What is the production rule for B in the given grammar?
B → bBa | ba | ε
B → S₂a | Ba
B → aAb | ab | ε
B → S₁b | Ab
Step 1: Add a new start symbol. Why is adding a new start symbol important in grammar transformation?
It helps to avoid ambiguity and ensures the original start symbol is preserved.
It makes the grammar more complex without any benefit.
It removes all non-terminals from the grammar.
It guarantees that the grammar becomes left-recursive.
Fill in the blank in the production rule: S₁ → S₁b | ___b
Ab
Bb
Sb
aB
Fill in the blank in the production rule: A → aAb | ab | ___
ε
a
b
A
Fill in the blank in the production rule: S₂ → S₂a | ___a
Ba
Sa
Aa
Ca
Fill in the blank: S₀ → ___
S
A
B
C
Fill in the blank: S → ___ | ___
S₁ | S₂
S | S
A | B
S₁ | B
Fill in the blank: S₁ → S₁b | Ab | ___
b
a
S₁a
Abc
Fill in the blank: A → aAb | ___
ab
aA
bA
aBb
Fill in the blank: S₂ → S₂a | Ba | ___
a
B
S₂
S
Fill in the blank: B → bBa | ___
ba
Bb
aB
bB
What is the purpose of Step 3 in the example?
Add new unit rules
Eliminate all unit rules
Add new non-terminals
Eliminate all productions
Step 4: Convert remaining rules to proper form. Fill in the blank: S₀ → ________
S₁b | Ab | b | S₂a | Ba | a
S₁a | Ab | a | S₂b | Ba | b
S₁b | Ab | a | S₂a | Ba | b
S₁a | Ab | b | S₂b | Ba | a
Step 4: Convert remaining rules to proper form. Fill in the blank: S → ________
S₁b | Ab | b | S₂a | Ba | a
S₁a | Aa | a | S₂b | Bb | b
S₁b | Aa | b | S₂a | Bb | a
S₁a | Ab | a | S₂b | Ba | b
Step 4: Convert remaining rules to proper form. Fill in the blank: S₁ → ________
S₁b | Ab | b
S₁a | Aa | a
S₁c | Ac | c
S₁d | Ad | d
Step 4: Convert remaining rules to proper form. Fill in the blank: A → ________
aAb | ab
aA | b
abA | a
Aab | b
Step 4: Convert remaining rules to proper form. Fill in the blank: S₂ → ________
S₂a | Ba | a
S₂a | Bb | b
S₂b | Ba | a
S₂a | Ba | b
Step 4: Convert remaining rules to proper form. Fill in the blank: B → ________
bBa | ba
Ba | bA
bA | Ba
bB | ab
Fill in the blank: S₀ → ________
S₁b | Ab | b | S₂a | Ba | a
S₁a | Bb | a | S₂b | Ab | b
S₂b | Ba | b | S₁a | Ab | a
Ab | S₁b | S₂a | Ba | b | a
Fill in the blank: S → ________
S₁b | Ab | b | S₂a | Ba | a
S₁a | Ab | a | S₂b | Ba | b
S₁c | Ac | c | S₂d | Bd | d
S₁d | Ad | d | S₂c | Bc | c
Fill in the blank: S₁ → ________
S₁b | Ab | b
S₁a | Aa | a
S₁c | Ac | c
S₁d | Ad | d
Fill in the blank: A → ________
aAb | ab
aB | ab
aAb | a
aA | ab
Fill in the blank: S₂ → ________
S₂a
S₂b
S₂c
S₂d
Fill in the blank: B → ________
bBa | ba
Ba | bA
bA | Ba
bB | a
Fill in the blank: A1 → ________
a
b
c
d
Fill in the blank: B1 → ________
b
a
c
d
What is the production rule for S₀ in the original grammar?
S₁B1 | AB1 | b | S₂A1 | BA1 | a
S₁b | Ab | b | S₂a | Ba | a
S₁B1 | AB1 | b
S₂A1 | BA1 | a
Fill in the blank: The production rule for A1 is ______.
a
b
c
d
After adding terminals, what is the main problem mentioned in the image regarding the variables?
Variables become undefined.
Variables are duplicated.
Variables are not initialized.
Variables are incorrectly named.
What is the production rule for B in the transformed grammar?
B1BA1 | B1A1
bBa | ba
B1BA1 | B1A1 | bBa | ba
ba
Fill in the blank: The production rule for B1 is ______.
b
a
c
d
Which rule in the original grammar produces the terminal 'a'?
A1 → a
A2 → b
A3 → c
A4 → d
Fill in the blank: S₀ → ______
S₁A1 | AB1 | b | S₂A1 | BA1 | a
S₁A2 | AB2 | c | S₂A2 | BA2 | d
S₁B1 | AA1 | e | S₂B1 | BB1 | f
S₁C1 | AC1 | g | S₂C1 | BC1 | h
Fill in the blank: S₁ → ______
S₁B1 | AB1 | b
S₁A1 | BB1 | a
S₁C1 | AC1 | c
S₁D1 | AD1 | d
Fill in the blank: A → ______
A1AB1 | A1B1
A1A | B1A1
A1B1 | AB1A1
A1B1A | 1A1B
Fill in the blank: S₂ → ______
S₂A1 | BA1 | a
S₂A2 | BA2 | b
S₂A3 | BA3 | c
S₂A4 | BA4 | d
Fill in the blank: B → ______
B1BA1 | B1A1
B2BA2 | B2A2
B1AB1 | B1A2
B2AB2 | B2A1
Fill in the blank: A1 → ______
a
b
c
d
Fill in the blank: B1 → ______
b
a
c
d
Fill in the blank: A2 → ______
A1A
A3B
B2A
C2A
Fill in the blank: B2 → ______
B1B
B3B
B2A
B4C
Fill in the blank: S0 → S1A1 | AB1 | b | S2A1 | BA1 | ____
a
bA1
A2
S1B
Fill in the blank: S1 → S1B1 | AB1 | ____
b
aB1
S1A
B1
Fill in the blank: A → A2B1 | ____
A1B1
A2B2
B1A2
B2A1
Fill in the blank: S2 → S2A1 | BA1 | ____
a
b
c
d
Fill in the blank: B → B2A1 | ____
B1A1
B2B1
A2B1
B1B2
Fill in the blank: A1 → ____
a
b
c
d
Fill in the blank: B1 → ____
b
a
c
d
Fill in the blank: A2 → ____
A1A
A3B
B2A
C2A
Fill in the blank: B2 → ____
B1B
B2B
B3B
BB2
