Я уже некоторое время пытаюсь вычислить следующий набор грамматик и столкнулся с еще одной проблемой. Вот мой следующий калькулятор:
def gen_follow_set(grammar, start_sym, first_sets):
follow_sets = {nterm: set() for nterm in grammar}
follow_sets[start_sym].add("$")
for _, prods in grammar.items():
for alt in prods:
for item in alt:
if item.isupper():
follow_sets[item] = set()
while True:
changes = copy.deepcopy(follow_sets)
for nterm, prods in grammar.items():
for alt in prods:
for i, item in enumerate(alt):
la = alt[i + 1] if i + 1 != len(alt) else nterm
if i == len(alt) - 1 and item != "":
follow_sets[item] |= follow_sets[nterm]
elif item != "":
if "" in first_sets[la]:
follow_sets[item] |= first_sets[la].union(
first_sets[alt[i + 2] if i + 2 <= len(alt) -
1 else nterm]) - {""}
else:
follow_sets[item] |= first_sets[la]
if changes == follow_sets:
return follow_sets
Это называется так:
grammar = {
"expr": [["term", "etail"]],
"term": [["LPAREN", "expr", "RPAREN"], ["INT", "ttail"]],
"etail": [["PLUS", "expr"], [""]],
"ttail": [["TIMES", "term"], [""]]
}
first = calc_first_set(...)
pprint.pprint(gen_follow_set(grammar, "expr", first))
Это выводит:
Working on: term ; la has epsilon; la: etail
Working on: etail ; it is at the end of the production: expr
Working on: LPAREN ; la doesn't have epsilon; la: expr
Working on: expr ; la doesn't have epsilon; la: RPAREN
Working on: RPAREN ; it is at the end of the production: term
Working on: INT ; la has epsilon; la: ttail
Working on: ttail ; it is at the end of the production: term
Working on: PLUS ; la doesn't have epsilon; la: expr
Working on: expr ; it is at the end of the production: etail
Working on: TIMES ; la doesn't have epsilon; la: term
Working on: term ; it is at the end of the production: ttail
Working on: term ; la has epsilon; la: etail
Working on: etail ; it is at the end of the production: expr
Working on: LPAREN ; la doesn't have epsilon; la: expr
Working on: expr ; la doesn't have epsilon; la: RPAREN
Working on: RPAREN ; it is at the end of the production: term
Working on: INT ; la has epsilon; la: ttail
Working on: ttail ; it is at the end of the production: term
Working on: PLUS ; la doesn't have epsilon; la: expr
Working on: expr ; it is at the end of the production: etail
Working on: TIMES ; la doesn't have epsilon; la: term
Working on: term ; it is at the end of the production: ttail
Working on: term ; la has epsilon; la: etail
Working on: etail ; it is at the end of the production: expr
Working on: LPAREN ; la doesn't have epsilon; la: expr
Working on: expr ; la doesn't have epsilon; la: RPAREN
Working on: RPAREN ; it is at the end of the production: term
Working on: INT ; la has epsilon; la: ttail
Working on: ttail ; it is at the end of the production: term
Working on: PLUS ; la doesn't have epsilon; la: expr
Working on: expr ; it is at the end of the production: etail
Working on: TIMES ; la doesn't have epsilon; la: term
Working on: term ; it is at the end of the production: ttail
{'INT': {'INT', 'TIMES', 'LPAREN'},
'LPAREN': {'INT', 'LPAREN'},
'PLUS': {'INT', 'LPAREN'},
'RPAREN': {'INT', 'LPAREN', 'PLUS'},
'TIMES': {'INT', 'LPAREN'},
'etail': {'$', 'RPAREN'},
'expr': {'$', 'RPAREN'},
'term': {'INT', 'LPAREN', 'PLUS'},
'ttail': {'INT', 'LPAREN', 'PLUS'}}
etail
и expr
верны, но term
и ttail
не верны. Как я могу получить правильный ответ?