0
0
Compiler Designknowledge~10 mins

Left factoring in Compiler Design - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to identify the common prefix in the grammar productions.

Compiler Design
common_prefix = [1] if production1.startswith([1]) and production2.startswith([1]) else ""
Drag options to blanks, or click blank then click option'
A"a"
B"ab"
C"b"
D"abc"
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the entire string "ab" as the prefix without checking both productions.
Selecting a character not at the start of both productions.
2fill in blank
medium

Complete the code to extract the suffix after the common prefix in a production.

Compiler Design
suffix = production[len([1]):]
Drag options to blanks, or click blank then click option'
A"a"
B"ab"
C"abc"
D"b"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong prefix length causing incorrect slicing.
Not slicing the string at all.
3fill in blank
hard

Fix the error in the code that performs left factoring on two productions.

Compiler Design
if production1.startswith(common_prefix) and production2.startswith(common_prefix):
    new_production = common_prefix + [1]
Drag options to blanks, or click blank then click option'
A"X"
B"(X)"
C"[X]"
D"{X}"
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses or brackets which are not part of the grammar notation here.
Leaving the blank empty.
4fill in blank
hard

Fill both blanks to complete the left factoring transformation for two productions.

Compiler Design
factored_productions = {
    [2]: [common_prefix + [1]],
    [1]: [suffix1, suffix2]
}
Drag options to blanks, or click blank then click option'
A"X"
B"Y"
C"S'"
D"common_prefix"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same symbol for both blanks.
Using the original common prefix as a key instead of a non-terminal.
5fill in blank
hard

Fill all three blanks to complete the dictionary comprehension that performs left factoring on a grammar dictionary.

Compiler Design
left_factored = { [1]: [prod[len([2]):] for prod in prods] for [3], prods in grammar.items() if prods[0].startswith(common_prefix) }
Drag options to blanks, or click blank then click option'
Acommon_prefix + "X"
Bcommon_prefix
Cnon_terminal
Dprod
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable names in the comprehension.
Not slicing the productions correctly.