0
0
Compiler Designknowledge~20 mins

Context-free grammars in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CFG Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the Components of a Context-Free Grammar

Which of the following correctly lists the four components that make up a context-free grammar (CFG)?

AA set of terminals, a set of states, a start state, and a transition function
BA set of terminals, a set of non-terminals, a start symbol, and a set of production rules
CA set of terminals, a set of non-terminals, a set of states, and a transition function
DA set of terminals, a start symbol, a set of production rules, and a transition function
Attempts:
2 left
💡 Hint

Think about what defines the structure and rules of a language in CFG.

📋 Factual
intermediate
2:00remaining
Derivation in Context-Free Grammars

What does a derivation in a context-free grammar represent?

AA sequence of production rule applications to generate a string from the start symbol
BA sequence of states visited in a finite automaton
CA set of terminals that cannot be replaced further
DA function mapping terminals to non-terminals
Attempts:
2 left
💡 Hint

Consider how strings are formed from the start symbol using rules.

🔍 Analysis
advanced
2:00remaining
Identifying Ambiguity in a CFG

Given the grammar with productions:

S → S + S | S * S | ( S ) | a

Which statement is true about this grammar?

AThe grammar is ambiguous because it generates strings with unmatched parentheses
BThe grammar is unambiguous because it uses only terminals and no non-terminals
CThe grammar is unambiguous because each string has a unique parse tree
DThe grammar is ambiguous because the string 'a + a * a' has more than one parse tree
Attempts:
2 left
💡 Hint

Try to draw parse trees for the string 'a + a * a'.

Comparison
advanced
2:00remaining
Comparing Context-Free and Regular Grammars

Which of the following statements correctly distinguishes context-free grammars (CFGs) from regular grammars?

ARegular grammars allow production rules with multiple non-terminals on the left side
BRegular grammars can generate all languages that CFGs can generate
CCFGs can generate languages with nested structures, while regular grammars cannot
DCFGs are less powerful than regular grammars in language generation
Attempts:
2 left
💡 Hint

Think about the kinds of patterns each grammar type can describe.

Reasoning
expert
2:00remaining
Determining the Language Generated by a CFG

Consider the CFG with productions:

S → a S b | ε

What language does this grammar generate?

AAll strings with equal numbers of 'a's followed by equal numbers of 'b's
BAll strings with any number of 'a's followed by any number of 'b's
CAll strings with equal numbers of 'a's and 'b's in any order
DAll strings consisting only of 'a's
Attempts:
2 left
💡 Hint

Look at how the production adds 'a' and 'b' in pairs recursively.