-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
69 lines (62 loc) · 1.36 KB
/
Code.py
File metadata and controls
69 lines (62 loc) · 1.36 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Grammar = {}
start = ""
states = []
Non_Terminals = []
Terminals = []
table = {}
def Read(filename):
global Grammar,start,Non_Terminals,Terminals
data = open(filename,"r").readlines()
productions = data[:-3]
start = data[-3].strip()
Non_Terminals = data[-2].strip().split(",")
Terminals = data[-1].strip().split(",")
Grammar = {}
for prod in productions:
print(prod.strip())
RHS,LHS = prod.split("->")
if RHS is Grammar:
Grammar[RHS].append(*LHS.strip().split("|"))
Grammar[RHS] = LHS.strip().split("|")
return Grammar
def First(NT,output):
if NT == "":
output.add("_")
return NT
if NT[0] in Terminals:
if NT[0] == '_':
First(NT.replace(NT[0],""),output)
output.add(NT[0])
return NT
if NT[0] in Non_Terminals:
for RHS in Grammar[NT[0]]:
First(NT.replace(NT[0],RHS),output)
return NT
if NT[0] == "_":
First(NT[1:],output)
def Follow(NT,output):
if NT == start:
output.add("$")
for prod in Grammar:
for RHS in Grammar[prod]:
if NT in RHS:
if RHS[-1] == NT:
Follow(prod)
First(RHS[RHS.index(NT)+1:],output)
if '_' in output:
output.remove("_")
Follow(prod,output)
def create_table():
global table
for nt in Non_Terminals:
table[nt] = {}
for t in Terminals:
table[nt][t] = ""
print(table[nt])
Read("read.txt")
create_table()
out = set()
# First("S",out)
# print(out)
Follow("A",out)
print(out)