# Initialize the parser with a grammar, a set of terminals, and a set of non-terminals
def __init__(self, grammar, terminals, non_terminals):
self.grammar = grammar
self.terminals = terminals
self.non_terminals = non_terminals
# Initialize the LL1 table
self.ll1_table = {}
# Fill the LL1 table with the FIRST and FOLLOW sets
self.fill_ll1_table()
# Fill the LL1 table with the FIRST and FOLLOW sets
def fill_ll1_table(self):
# Calculate the FIRST sets for each non-terminal
first_sets = self.calculate_first_sets()
# Calculate the FOLLOW sets for each non-terminal
follow_sets = self.calculate_follow_sets()
# Generate the LL1 table
for non_terminal in self.non_terminals:
for terminal in self.terminals.union({'$'}):
# Check if the non-terminal produces the terminal in its FIRST set
if terminal in first_sets[non_terminal]:
productions = [p for p in self.grammar[non_terminal] if p[0] == terminal]
# Add the productions to the LL1 table entry
self.ll1_table[(non_terminal, terminal)] = productions
# Check if the non-terminal produces the terminal in its FOLLOW set and the FIRST set is empty
elif terminal in follow_sets[non_terminal] and not first_sets[non_terminal]:
productions = [p for p in self.grammar[non_terminal] if p[0] == terminal]
# Add the productions to the LL1 table entry
self.ll1_table[(non_terminal, terminal)] = productions
# Add an error entry to the LL1 table
else:
self.ll1_table[(non_terminal, terminal)] = 'error'
# Calculate the FIRST sets for each non-terminal
def calculate_first_sets(self):
first_sets = {}
# Initialize the FIRST sets with empty sets for each non-terminal
for non_terminal in self.non_terminals:
first_sets[non_terminal] = set()
# Calculate the FIRST sets iteratively
while True:
has_changed = False
for non_terminal in self.non_terminals:
for production in self.grammar[non_terminal]:
# If the first symbol is a terminal, add it to the FIRST set
if production[0] in self.terminals:
if production[0] not in first_sets[non_terminal]:
first_sets[non_terminal].add(production[0])
has_changed = True
# If the first symbol is a non-terminal, add the FIRST set of that non-terminal to the current FIRST set
else:
for symbol in production:
if symbol == non_terminal:
break
if symbol in self.terminals:
if symbol not in first_sets[non_terminal]:
first_sets[non_terminal].add(symbol)
has_changed = True
break
if self.is_nullable(symbol):
for s in first_sets[symbol]:
if s not in first_sets[non_terminal]:
first_sets[non_terminal].add(s)
has_changed = True
else:
break
if not bool(self.grammar[non_terminal]):
first_sets[non_terminal].add('epsilon')
has_changed = True
if not has_changed:
break
return first_sets
# Calculate the FOLLOW sets for each non-terminal
def calculate_follow_sets(self):
follow_sets = {}
# Initialize the FOLLOW sets with empty sets for each non-terminal
for non_terminal in self.non_terminals:
follow_sets[non_terminal] = set()
# Add the end-of-input symbol to the start symbol's FOLLOW set
follow_sets[list(self.non_terminals)[0]].add('$')
# Calculate the FOLLOW sets iteratively
while True:
has_changed = False
for non_terminal in self.non_terminals:
for production in self.grammar[non_terminal]:
for i in range(len(production)):
symbol = production[i]
# If the symbol is a non-terminal, add the FOLLOW set of the current non-terminal to its FIRST set
if symbol in self.non_terminals:
if i == len(production) - 1:
for s in follow_sets[non_terminal]:
if s not in follow_sets[symbol]:
follow_sets[symbol].add(s)
has_changed = True
else:
for j in range(i+1, len(production)):
if production[j] in self.terminals:
if production[j] not in follow_sets[symbol]:
follow_sets[symbol].add(production[j])
has_changed = True
break
else:
for s in self.first_sets[production[j]]:
if s != 'epsilon' and s not in follow_sets[symbol]:
follow_sets[symbol].add(s)
has_changed = True
if 'epsilon' not in self.first_sets[production[j]]:
break
elif j == len(production) - 1:
for s in follow_sets[non_terminal]:
if s not in follow_sets[symbol]:
follow_sets[symbol].add(s)
has_changed = True
else:
continue
if not has_changed:
break
return follow_sets
# Check if a symbol is nullable
def is_nullable(self, symbol):
if symbol in self.terminals:
return False
elif not bool(self.grammar[symbol]):
return True
else:
for production in self.grammar[symbol]:
for s in production:
if not self.is_nullable(s):
break
else:
return True
return False
# Parse a given input string
def parse(self, input_str):
stack = ['$']
input_str += '$'
index = 0
while stack:
top = stack.pop()
curr = input_str[index]
if top in self.terminals:
if top == curr:
index += 1
else:
return False
elif (top, curr) in self.ll1_table:
if self.ll1_table[(top, curr)] == 'epsilon':
continue
elif self.ll1_table[(top, curr)] == 'error':
return False
else:
for symbol in reversed(self.ll1_table[(top, curr)]):
stack.append(symbol)
else:
return False
return index == len(input_str) - 1