wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Context-Free Grammar

Total questions: 77

Worksheet time: 39mins

Name
Class
Date
1.

A rule in a CFG can be applied anywhere the single non-terminal (variable) on the left side appears, regardless of its context (surrounding symbols). Which of the following best describes a context-free grammar?

a)

A grammar where rules depend on surrounding symbols

b)

A grammar where rules can be applied regardless of context

c)

A grammar with no variables

d)

A grammar with only terminal symbols

2.

In a context-free grammar, the rule format is ______ → β (Only a single variable on the left side). Fill in the blank.

a)

A

b)

S

c)

α

d)

B

3.

Simple Example: Language L = {aⁿbⁿ | n ≥ 1}. This language requires an equal number of a's and b's. Which of the following strings belong to this language?

a)

ab

b)

aabb

c)

aaabbb

d)

All of the above

4.

What does the language L = anbnn1a^n b^n | n ≥ 1 require?

a)

An equal number of a's and b's

b)

More a's than b's

c)

More b's than a's

d)

Only a's

5.

Fill in the blank: The CFG rules for the language L = {a^n b^n | n ≥ 1} are: S → aSb S → ε\varepsilon

a)

ab

b)

a

c)

b

d)

ε

6.

Fill in the blank: In the parsing example for deriving aabb, the sequence of derivations is: S → aSb → a(ab)b → ____ Write the final derived string.

a)

aabb

b)

abab

c)

abbb

d)

aaab

7.

What is Chomsky Normal Form (CNF)?

a)

A way to represent Regular Grammars

b)

A highly restricted and standardized way to represent Context-Free Grammars (CFGs)

c)

A method for parsing natural languages

d)

A type of programming language

8.

Chomsky Normal Form (CNF) enforces that every single production rule in the grammar must strictly follow one of the two simple structures below.

a)

True

b)

False

9.

What is Binary Production in formal grammar?

a)

A non-terminal must derive exactly one terminal symbol

b)

A non-terminal must derive exactly two non-terminals

c)

A non-terminal must derive three non-terminals

d)

A non-terminal must derive a real word/character

10.

What is Terminal Production in formal grammar?

a)

A non-terminal must derive exactly two non-terminals

b)

A non-terminal must derive exactly one terminal symbol

c)

A non-terminal must derive three non-terminals

d)

A non-terminal must derive a phrase

11.

Fill in the blank: In Binary Production, a non-terminal (A) must derive exactly ___ non-terminals.

a)

two

b)

one

c)

three

d)

four

12.

Fill in the blank: In Terminal Production, a non-terminal (A) must derive exactly ___ terminal symbol.

a)

one

b)

two

c)

three

d)

zero

13.

What is the primary role of CNF?

a)

To make grammar rules more complex

b)

To enable efficient and algorithmic parsing (understanding sentence structure) by a computer

c)

To slow down parsing algorithms

d)

To remove binary branching

14.

In complex CFG, rules are ______ and complex. (e.g., A → aBCD or A → B)

a)

arbitrary

b)

simple

c)

fixed

d)

predictable

15.

In CNF, rules are ______ and uniform. (e.g., A → BC or A → a)

a)

simple

b)

complex

c)

ambiguous

d)

variable

16.

What does CNF force the Parse Tree to be?

a)

Single branching

b)

Binary branching

c)

Ternary branching

d)

No branching

17.

Which algorithm requires CNF to work?

a)

Dijkstra's Algorithm

b)

CYK Algorithm (Cocke-Younger-Kasami)

c)

Merge Sort

d)

Quick Sort

18.

Which of the following is NOT a valid rule in Chomsky normal form?

a)

A) A → BC

b)

B) A → a

c)

C) A → B

d)

D) S → ε (where S is the start variable)

19.

In Chomsky normal form, the rule S → ___ is permitted, where S is the start variable.

a)

ε

b)

a

c)

S

d)

AB

20.

In Chomsky normal form, 'a' must be a terminal and 'A', 'B', 'C' must be variables.

a)

True

b)

False

21.

A context-free grammar is in Chomsky normal form if every rule is of one of the following forms: A → BC, A → a. Which of the following forms is allowed in Chomsky normal form?

a)

A → BC

b)

A → a

c)

S → ε (where S is the start variable)

d)

All of the above

22.

What is the first step to convert any CFG to Chomsky normal form?

a)

Add a new start symbol

b)

Remove null productions

c)

Eliminate unit productions

d)

Convert terminals in rules to variables

23.

Which type of rules should be eliminated in the second step of converting any CFG to Chomsky normal form?

a)

ε rules of the form A → ε

b)

Unit rules of the form A → B

c)

Left-recursive rules of the form A → Aa

d)

Terminal rules of the form A → aB

24.

What is the third step in converting any CFG to Chomsky normal form?

a)

Eliminate unit rules of the form A → B

b)

Remove null productions

c)

Remove useless symbols

d)

Convert terminals in rules to non-terminals

25.

In the final step of converting any CFG to Chomsky normal form, what should the remaining rules be converted into?

a)

Proper form (2 non-terminal or 1 terminal)

b)

Unit productions (single non-terminal)

c)

Epsilon productions (empty string)

d)

Left-recursive form

26.

Step 1 in converting a CFG to Chomsky normal form is to add a new start symbol. What is the new rule that should be created?

a)

A) S₀ → S

b)

B) S → S₀

c)

C) S₀ → A

d)

D) S → A

27.

In converting a CFG to Chomsky normal form, the new start symbol added is ____ and it is not used in the original CFG.

a)

S₀

b)

S

c)

A

d)

X

28.

Convert a CFG to Chomsky normal form: What is the first step in eliminating all ε rules from a context-free grammar (CFG), where A is not the start variable?

a)

Eliminate all ε rules A → ε

b)

Add new rules with A deleted

c)

Replace R → A with R → ε

d)

Remove all terminal symbols

29.

Convert a CFG to Chomsky normal form: For each rule with an occurrence of A on the right-hand side, what should you do?

a)

Add a new rule with the A deleted

b)

Replace A with ε

c)

Remove the rule entirely

d)

Add a terminal symbol

30.

Convert a CFG to Chomsky normal form: Fill in the blank. If we have R → A, replace it with ______ unless we had already removed R → ε.

a)

R → ε

b)

R → A | ε

c)

R → A | R

d)

R → εA

31.

Convert a CFG to Chomsky normal form: Given the rule R → uAvAw, what are the possible new rules after eliminating A → ε?

a)

R → uAvAw | uvAw | uAvw | uvw

b)

R → uAvAw | uAw | vAw | vw

c)

R → uAvAw | uAvw | uvAw

d)

R → uAvAw only

32.

Step 3 in converting a CFG to Chomsky normal form is to eliminate all unit rules of the form A → B. What does this step require you to do?

a)

Replace each variable with a terminal

b)

Replace each alone variable with its value

c)

Remove all terminals

d)

Add new variables

33.

In the process of eliminating unit rules when converting a CFG to Chomsky normal form, for each rule B → u, what new rule should be added?

a)

A) A → B

b)

B) A → u

c)

C) B → A

d)

D) u → A

34.

Fill in the blank: Repeat the process of eliminating unit rules until ________ have been replaced.

a)

all unit rules

b)

some unit rules

c)

no unit rules

d)

the start symbol

35.

Convert a CFG to Chomsky normal form: What is the proper form for the remaining rules in Chomsky normal form?

a)

2 variables or one terminal

b)

3 variables or one terminal

c)

Only terminals

d)

Only variables

36.

Convert a CFG to Chomsky normal form: Fill in the blank: Replace each rule A → u₁u₂...uₖ, where k ≥ 3 and uᵢ is a variable or a terminal, with ____ rules.

a)

k-1

b)

k+1

c)

2k

d)

k

37.

Convert a CFG to Chomsky normal form: Which of the following is the correct sequence for replacing a rule A → u₁u₂...uₖ in Chomsky normal form?

a)

A) A → u₁A₁, A₁ → u₂A₂, ..., Aₖ₋₂ → uₖ₋₁uₖ

b)

B) A → u₁u₂, A₁ → u₃u₄

c)

C) A → u₁u₂u₃, A₁ → u₄u₅

d)

D) A → u₁, A₁ → u₂

38.

What is Chomsky Normal Form (CNF) in context-free grammar (CFG)?

a)

A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules satisfy the following conditions: 1) A non-terminal generating a terminal (e.g.; X→x), 2) A non-terminal generating two non-terminals (e.g.; X→YZ), 3) Start symbol generating ε (e.g.; S→ε).

b)

A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules generate only terminals.

c)

A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules generate only non-terminals.

d)

A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules generate only the start symbol.

39.

Which of the following is NOT a condition for Chomsky Normal Form (CNF)?

a)

A) A non-terminal generating a terminal (e.g.; X→x)

b)

B) A non-terminal generating two non-terminals (e.g.; X→YZ)

c)

C) A non-terminal generating three non-terminals (e.g.; X→XYZ)

d)

D) Start symbol generating ε (e.g.; S→ε)

40.

Consider the following grammars: G1 = {S→a, S→AZ, A→a, Z→z} G2 = {S→a, S→aZ, Z→a} Which grammar is in Chomsky Normal Form (CNF)?

a)

G1

b)

G2

41.

Why is the grammar G2 not in Chomsky Normal Form (CNF)?

a)

G2 is not in CNF as the production rule S→aZ contains terminal followed by non-terminal which does not satisfy the rules specified for CNF.

b)

G2 is not in CNF because all production rules contain only terminals.

c)

G2 is not in CNF as it has no start symbol.

d)

G2 is not in CNF because it contains only non-terminals in every production.

42.

If start symbol S is at the RHS of any production in the grammar, what new production should be created?

a)

S0 → S

b)

S → S0

c)

S0 → ε

d)

S → ε

43.

What should you do if a rule contains ε (null production)?

a)

Remove it by modifying other rules accordingly.

b)

Leave it as it is without any changes.

c)

Replace ε with a terminal symbol.

d)

Ignore the rule completely.

44.

If a rule has a single non-terminal on the RHS (e.g., A → B), what should you do?

a)

Remove the rule

b)

Replace it with B’s productions

c)

Add a new start symbol

d)

Remove all non-terminals

45.

What should be done with non-reachable or non-generating symbols from the grammar?

a)

Remove them from the grammar.

b)

Mark them as start symbols.

c)

Replace them with terminal symbols.

d)

Add them to the production rules.

46.

Step 3: Replace Terminals in Mixed Productions Eliminate terminals from RHS if they exist with other terminals or non-terminals. For example, the production rule X→xY can be decomposed as: Which of the above is the correct decomposition?

a)

A) X→ZY, Z→x

b)

B) X→YZ, Z→x

c)

C) X→xY, Y→Z

47.

Step 4: Reduce Productions with More Than Two Non-Terminals Eliminate RHS with more than two non-terminals. For example, the production rule X→XYZ can be decomposed as: Fill in the blanks: X→___, P→___

a)

X→PZ, P→XY

b)

X→XY, P→XZ

c)

X→YZ, P→XY

d)

X→PX, P→YZ

48.

What is the limitation of FA (Finite Automata) related to the fact that they can only 'remember' a bounded amount of information?

a)

They can process infinite memory

b)

They can only remember a limited amount of information

c)

They can solve all computational problems

d)

They have unbounded memory

49.

What is the simplest alteration that adds unbounded 'memory' to our machine?

a)

Adding a stack (as in a pushdown automaton) is the simplest alteration that adds unbounded memory.

b)

Increasing the number of states in the machine.

c)

Adding a finite tape to the machine.

d)

Allowing the machine to read input multiple times.

50.

A more powerful machine should be able to recognize which of the following languages?

a)

{0ⁿ1ⁿ : n ≥ 0}

b)

{0ⁿ1ᵐ : n, m ≥ 0}

c)

{0ⁿ1ⁿ2ⁿ : n ≥ 0}

d)

{0*1*}

51.

What does the tape alphabet represent in an NPDA diagram?

a)

A) The set of symbols that can be read from the tape

b)

B) The set of states in the automaton

c)

C) The set of stack operations

d)

D) The set of accept states

52.

In the NPDA diagram, what is the symbol for the stack alphabet?

a)

Σ

b)

Γ (gamma)

c)

δ

d)

ε

53.

Fill in the blank: The start state in an NPDA is the state where the computation _______.

a)

begins

b)

ends

c)

halts

d)

loops

54.

Which of the following correctly describes the transition label format in the NPDA diagram?

a)

(input symbol, stack symbol popped → stack symbol pushed)

b)

(state, input symbol → stack symbol)

c)

(stack symbol, input symbol → state)

d)

(input symbol, state → stack symbol)

55.

In the NPDA diagram, what does the transition '0, ε → 0' mean?

a)

On reading '0' from the tape and popping ε from the stack, push '0' onto the stack.

b)

On reading '0' from the tape and popping '0' from the stack, push ε onto the stack.

c)

On reading ε from the tape and popping '0' from the stack, push '0' onto the stack.

d)

On reading '0' from the tape and popping '0' from the stack, push '0' onto the stack.

56.

What does NPDA stand for in the context of Pushdown Automata?

a)

Nondeterministic Pushdown Automata

b)

Normal Pushdown Automata

c)

Non-Polynomial Deterministic Automata

d)

Numerical Pushdown Automata

57.

Which of the following is NOT a way to describe NPDA?

a)

Diagram

b)

Formal definition

c)

Truth table

d)

None of the above

58.

What does 'nondeterministic' mean in the context of pushdown automata?

a)

Only one possible next step

b)

Potentially several choices of 'next step'

c)

No choices of next step

d)

Always deterministic

59.

In NPDA operation, what does the 'read' component represent in the transition (read, pop, push)?

a)

read a from tape, or don’t read from tape if a = ε

b)

pop a symbol from the stack

c)

push a symbol onto the stack

d)

move the tape head to the right

60.

In NPDA operation, what does the 'pop' component represent in the transition (read, pop, push)?

a)

pop b from stack, or don’t pop from stack if b = ε

b)

push b onto stack, or don’t push if b = ε

c)

read b from input, or skip if b = ε

d)

move to next state if b = ε

61.

In NPDA operation, what does the 'push' component (c) represent in the transition (read, pop, push)?

a)

push c onto stack, or don’t push onto stack if c = ε

b)

pop c from stack, or don’t pop if c = ε

c)

read c from input, or skip if c = ε

d)

replace c with ε on stack

62.

Which of the following is true about the values of a, b, and c in NPDA operation?

a)

A) a ∈ (Σ ∪ {ε}), b, c ∈ (Γ ∪ {ε})

b)

B) a ∈ (Γ ∪ {ε}), b, c ∈ (Σ ∪ {ε})

c)

C) a, b, c ∈ (Σ ∪ {ε})

d)

D) a, b, c ∈ (Γ ∪ {ε})

63.

According to the formal definition, what does NPDA M = (Q, Σ, Γ, δ, q0, F) accept?

a)

A string w ∈ Σ*

b)

A string w ∈ Γ*

c)

A string w ∈ Q*

d)

A string w ∈ F*

64.

Which of the following is true for a string w to be accepted by an NPDA?

a)

w can be written as w1w2...wm ∈ (Σ ∪ {ε})*

b)

w can be written as w1w2...wm ∈ (Γ ∪ {ε})*

c)

w can be written as w1w2...wm ∈ (Q ∪ {ε})*

d)

w can be written as w1w2...wm ∈ (F ∪ {ε})*

65.

Fill in the blank: There exist states _____, r1, ..., rm for a string to be accepted by an NPDA.

a)

r0

b)

q0

c)

p0

d)

s0

66.

Which of the following is NOT a requirement for a string to be accepted by an NPDA?

a)

A) r0 = q0 and s0 = ε

b)

B) (ri+1, b) ∈ δ(ri, wi+1, a), where si = at, si+1 = bt for some t ∈ Γ*

c)

C) rm ∈ F

d)

D) s0 = q0 and r0 = ε

67.

For a string to be accepted by an NPDA, there must exist strings s0, s1, ..., sm in (Γ ∪ {ε})*.

a)

True

b)

False

68.

Q in the 6-tuple (Q, Σ, Γ, δ, q₀, F) of NPDA is a finite set called the _______.

a)

states

b)

transitions

c)

symbols

d)

inputs

69.

Σ in the 6-tuple (Q, Σ, Γ, δ, q₀, F) of NPDA is a finite set called the _______.

a)

tape alphabet

b)

state set

c)

transition function

d)

stack symbols

70.

Γ in the 6-tuple (Q, Σ, Γ, δ, q₀, F) of NPDA is a finite set called the _______.

a)

stack alphabet

b)

input alphabet

c)

state set

d)

final states

71.

δ: Q × (Σ ∪ {ε}) × (Γ ∪ {ε}) → ℘(Q × (Γ ∪ {ε})) is a function called the _______.

a)

transition function

b)

output function

c)

acceptance function

d)

state function

72.

q₀ in the 6-tuple (Q, Σ, Γ, δ, q₀, F) of NPDA is an element of Q called the _______.

a)

start state

b)

final state

c)

input symbol

d)

transition function

73.

F in the 6-tuple (Q, Σ, Γ, δ, q₀, F) of NPDA is a subset of Q called the _______.

a)

accept states

b)

start states

c)

transition states

d)

stack symbols

74.

Given the NPDA with Σ = {0, 1} and Γ = {0, 1, $}, and the state transitions shown in the diagram, which of the following languages does this NPDA accept?

a)

The set of all palindromes over {0, 1}

b)

The set of all strings with equal number of 0's and 1's

c)

The set of all strings of the form wwRww^R , where w ∈ {0,1}*

d)

The set of all strings ending with 1

75.

What is one of the new capabilities of a Pushdown Automata?

a)

Can multiply numbers

b)

Can push symbol onto stack

c)

Can sort data

d)

Can add numbers

76.

What is the function of the 'pop' operation in a Pushdown Automata?

a)

Add a symbol to the stack

b)

Remove a symbol from the stack

c)

Sort the stack

d)

Reverse the stack

77.

Fill in the blank: The three main components of a Pushdown Automata are finite control, input tape, and _________.

a)

stack

b)

queue

c)

register

d)

counter