-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.py
42 lines (35 loc) · 1.29 KB
/
checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from __future__ import annotations
from typing import Union
from utils import Rule, Grammar
def check(algorithm: Union[Earley, LR]) -> None:
try:
nonterm_count, term_count, rules_count = [int(x) for x in input().split()]
nonterms = {x for x in input()}
terms = {x for x in input()}
grammar = Grammar(nonterms, terms)
except:
raise Exception('Wrong input format')
for _ in range(rules_count):
row = input()
for letter in row:
if ((letter not in set(['-', '>'])) and
(letter not in nonterms) and
(letter not in terms)):
raise Exception('Wrong input format')
grammar.add_rule(Rule(*row.split('->')))
try:
grammar.start = input()
except:
raise Exception('Wrong input format')
if grammar.start not in nonterms:
raise Exception('Start symbol is not a nonterminal')
if not grammar.is_context_free():
raise Exception('Wrong grammar')
algorithm.fit(grammar)
words_count = int(input())
for _ in range(words_count):
word = input()
for letter in word:
if not grammar.is_terminal(letter):
raise Exception('Wrong word')
print('Yes' if algorithm.predict(word) else 'No')