-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathll1.py
130 lines (113 loc) · 3.62 KB
/
ll1.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#Epsilon represented by @
#Starting Variable must be in the left hand side of first production
#Production Rules should be in the following format only
# A -> R/G/...
import numpy as np
from prettytable import PrettyTable
def first(seq,gra,nonterm):
first_=set()
for i in seq:
flag=0
if i in nonterm:
for j in gra[i]:
first_= first_ | first(j,gra,nonterm)
if '@' in first(j,gra,nonterm):
flag=1
if flag==0:
break
else:
first_=first_|{i}
break
return first_
def follow(var_nt,gra,nonterm):
follow_=set()
start_var=(list(gra.keys())[0])
if var_nt==start_var:
follow_=follow_|{'$'}
for left_v in gra:
for prod in gra[left_v]:
if prod!='@':
for i in range(len(prod)):
if prod[i]==var_nt:
if i!=(len(prod)-1):
next_str=prod[(i+1):]
first_str=first(next_str,gra,nonterm)
if '@' in first_str:
follow_= follow_ | (first_str-{'@'})
if(left_v!=var_nt):
follow_=follow_ | follow(left_v,gra,nonterm)
else:
follow_= follow_ | first_str
else:
if(left_v!=var_nt):
follow_=follow_ | follow(left_v,gra,nonterm)
return follow_
def ll1_parser(gra,nonterm,term):
mat=np.array([["0"]*(len(term)+2)]*(len(nonterm)+1))
mat=mat.astype('<U100')
term.append('$')
for i in range((len(nonterm)+1)):
for j in range((len(term)+1)):
if i==0 and j>=1:
mat[i,j]=term[j-1]
elif i>=1 and j==0:
mat[i,j]=nonterm[i-1]
isll1=True
for left_v in gra:
row_n=nonterm.index(left_v)+1
for prod in gra[left_v]:
first_rule=first(prod,gra,nonterm)
if '@' in first_rule:
first_rule=first_rule-{'@'}
first_rule=first_rule | follow_set[left_v]
for i in first_rule:
col_n=term.index(i)+1
if mat[row_n,col_n]=='0':
mat[row_n,col_n]=left_v+'->'+prod
else:
mat[row_n,col_n]=mat[row_n,col_n]+'\n'+left_v+'->'+prod
isll1=False
x=PrettyTable()
x.field_names=mat[0]
for i in range(len(mat)):
if i>0:
x.add_row(mat[i])
print(x)
if isll1:
print("This grammar can be used for LL1 Parser")
else:
print("This grammar cannot be used for LL1 Parser")
def read_gra(fname):
f=open(fname,"r")
raw_gra=f.read()
lines=raw_gra.split('\n')
gra=dict({})
for i in lines:
words=i.split()
prod=words[2].split('/')
gra[words[0]]=prod
return gra
follow_set=dict({})
first_set=dict({})
nonterm=[]
term=[]
n_nonterm=int(input("Enter number of non terminals: "))
print("Enter the non terminals:")
for i in range(n_nonterm):
c=input()
nonterm.append(c)
n_term=int(input("Enter number of terminals: "))
print("Enter the terminals:")
for i in range(n_term):
c=input()
term.append(c)
gra=read_gra("grammar.txt")
for i in gra:
first_set[i]=first(i,gra,nonterm)
for i in gra:
follow_set[i]=follow(i,gra,nonterm)
print("First of Variables of Grammar: ",end='')
print(first_set)
print("Follow of Variables of Grammar: ",end='')
print(follow_set)
ll1_parser(gra,nonterm,term)