/
First_Follow.py
183 lines (149 loc) · 4.53 KB
/
First_Follow.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
February 4, 2016
Generates First and Follow of a grammar.
"""
epsila = "E"
marker = "$"
class First_Follow(object):
"""Class to find first and follow of a grammar"""
def __init__(self, grammar):
super(First_Follow, self).__init__()
self.grammar = grammar
self.first = {}
self.follow = {}
for symbol in self.grammar.non_terminals:
self.calculate_first(symbol)
self.calculate_follow(symbol)
def get_first(self, symbol):
return self.first[symbol]
def get_follow(self, symbol):
return self.follow[symbol]
def has_epsila_derivation(self, symbol):
# Returns True if any production of the symbol has epsila.
for production in self.grammar.get(symbol):
if not len(production.production):
return True
def calculate_first(self, g_symbol):
# Calculates First of the symbol. Returns a list of symbols and also
# places it in first dict.
if g_symbol in self.grammar.terminals or g_symbol == epsila:
return [g_symbol]
first_set = set()
# Get productions of given symbol only.
productions = self.grammar.get(g_symbol)
for production in productions:
if len(production.production) == 0:
first_set.add(epsila)
for symbol in production.production:
first_value = symbol.symbol
if first_value in self.grammar.terminals:
first_set.add(first_value)
break
elif first_value == epsila:
first_set.add(epsila)
break
elif first_value in self.grammar.non_terminals:
first_set.update(self.calculate_first(first_value))
# Check whether it gives epsila derivation. In that case,
# see the first of next literal.
if not self.has_epsila_derivation(first_value):
break
else:
first_set.remove(epsila)
else:
raise ValueError('Invalid literal: no terminal or non-terminal or epsila')
r = list(first_set)
self.first[g_symbol] = r
return r
def calculate_follow(self, symbol, called_by = []):
# Calculates Follow of the symbol. Returns a list of symbols and also
# places it in follow dict. List called_by is used to ensure that if
# follow has already been calculated previously, it won't be calculated
# again -- to avoid infinite recursion.
follow_set = set()
if symbol == self.grammar.grammar[0].non_terminal:
follow_set.add(marker)
for production in self.grammar.grammar:
if symbol in production.production:
alpha_index = production.production.index(symbol)
next_index = alpha_index + 1
while(True):
if next_index >= len(production.production):
if not production.non_terminal in called_by and production.non_terminal != symbol:
follow_set.update(self.calculate_follow(production.non_terminal, called_by + [symbol]))
break
elif production.production[next_index].symbol in self.grammar.terminals:
follow_set.add(production.production[next_index].symbol)
# check if the symbol exists in remaining production symbols.
# if yes, continue to generate follow from that index.
if symbol not in production.production[next_index:]:
break
else:
next_index += production.production[next_index:].index(symbol)
elif production.production[next_index].symbol in \
self.grammar.non_terminals:
follow_set.update(self.calculate_first(\
production.production[next_index].symbol))
if not self.has_epsila_derivation(\
production.production[next_index].symbol):
if symbol not in production.production[next_index:]:
break
else:
next_index += production.production[next_index:]\
.index(symbol)
next_index += 1
try:
follow_set.remove(epsila)
except KeyError:
pass
r = list(follow_set)
self.follow[symbol] = r
return r
def display(self):
print "\n## First and Follow Calculation\n"
for key, value in self.first.iteritems():
print "First[%s] = %s" %(key, value)
# print self.first
print ""
for key, value in self.follow.iteritems():
print "Follow[%s] = %s" %(key, value)
print "\n"
def main():
from Grammar import Grammar
grammar = """
S' := S
S := A c A
S := B c c B
A := c A
A := a
B := c c B
B := b
"""
grammar = """
S' := S
S := q A B C
A := a | b b D
B := a | E
C := b | E
D := C | E
"""
# grammar = """
# S := a S a | b S b | D
# D := b D'
# D' := a a D' | E
# # """
# grammar = """
# S := i X t S S' | a
# S' := e S | E
# X := b
# """
g = Grammar(grammar)
g.parse()
print g.first_follow.first
print g.first_follow.follow
# f = First_Follow(g)
# # print f.calculate_first("B")
# print f.first
# print f.follow
if __name__ == '__main__':
main()