Complete the code to define the first step in LL(1) parsing table construction: computing the FIRST set of a grammar symbol.
def compute_first(symbol): if symbol is terminal: return {symbol} first_set = set() for production in productions[symbol]: first_set |= compute_first(production[0]) return first_set # The key operation to combine sets is [1]
The operator |= is used to add elements from one set to another in Python, which is essential when combining FIRST sets.
Complete the code to check if a production can derive epsilon (empty string) during FIRST set computation.
def can_derive_epsilon(production): for symbol in production: if epsilon not in compute_first(symbol): return [1] return True
If any symbol in the production cannot derive epsilon, the whole production cannot derive epsilon, so the function returns False.
Fix the error in the code that adds entries to the LL(1) parsing table for a production based on its FIRST set.
for terminal in first_set: if terminal != epsilon: parsing_table[non_terminal][[1]] = production
The key in the parsing table is the terminal symbol from the FIRST set, so terminal must be used as the index.
Fill both blanks to correctly add entries for epsilon productions in the LL(1) parsing table using FOLLOW sets.
if epsilon in first_set: for terminal in [1][non_terminal]: parsing_table[non_terminal][[2]] = production
When a production can derive epsilon, entries are added for all terminals in the FOLLOW set of the non-terminal. The key in the parsing table is the terminal symbol.
Fill all three blanks to complete the LL(1) parsing table construction code snippet that handles all productions.
for non_terminal in grammar: for production in grammar[non_terminal]: first_set = compute_first(production) for terminal in first_set: if terminal != [1]: parsing_table[non_terminal][terminal] = [2] if [3] in first_set: for terminal in follow_set[non_terminal]: parsing_table[non_terminal][terminal] = production
The code skips epsilon when adding entries from the FIRST set, adds the production as the value, and checks for epsilon presence to add entries from the FOLLOW set.
