-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (37 loc) · 1.11 KB
/
main.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
43
44
45
46
47
48
49
50
51
from grammar import Grammar, GrammarRule
from parser import Parser
def main():
alphabet = {}
print("Enter non-terminals count:")
non_terminal_count = int(input())
print("Enter non-terminals:")
for i in range(non_terminal_count):
alphabet[input()] = False
print("Enter terminals count:")
terminal_count = int(input())
print("Enter terminals:")
for i in range(terminal_count):
alphabet[input()] = True
grammar = Grammar()
grammar.set_alphabet(alphabet)
print("Enter rules count:")
rule_count = int(input())
print("A->aB enter like A a B\n"
"Enter rules:")
for i in range(rule_count):
rule = input().split()
right_part = []
if len(rule) != 1:
right_part = rule[1:]
grammar.add_rules({GrammarRule(rule[0], right_part)})
print("Enter start non-terminal:")
start = input()
grammar.set_start(start)
print("Enter word:")
word = input()
lr = Parser(grammar)
lr.build_states(start, start)
lr.build_table()
print(lr.parse(word))
if __name__ == "__main__":
main()