-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
32 lines (29 loc) · 981 Bytes
/
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
from FiniteAutomata import *
from LR import *
def syntaxAnalysis(lr_struct):
while True:
try:
s = input('Please input a sentence to analysis: ')
if lr_struct.Analysis(s):
print('Accepted')
else:
print('Unaccepted')
except EOFError:
break
if __name__ == '__main__':
# using example
# syntax-demo.txt no new line at the end of file
f = open('syntax-demo.txt', 'r', encoding = 'UTF-8')
a = LR()
a.scan(f) # read the file
a.BuildSimpleDFA()
if a.BuildLR0AnalyseTable():
a.dfa.displaySimpleSquare('simpledfa.gv', 'simple_dfa', a.projectSet)
syntaxAnalysis(a)
elif a.BuildSLR1AnalyseTable():
syntaxAnalysis(a)
else:
a.BuildDFA()
a.dfa.displaySquare('dfa.gv', 'dfa_with_lookahead', a.projectSet, a.LATerminal)
if a.BuildLR1AnalyseTable():
syntaxAnalysis(a)