-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleftRecursion.py
27 lines (27 loc) · 1006 Bytes
/
leftRecursion.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
from input import rules, diction, firsts, follows, nonterm_userdef, term_userdef, sample_input_string
def removeLeftRecursion(rulesDiction):
store = {}
for lhs in rulesDiction:
alphaRules = []
betaRules = []
allrhs = rulesDiction[lhs]
for subrhs in allrhs:
if subrhs[0] == lhs:
alphaRules.append(subrhs[1:])
else:
betaRules.append(subrhs)
if len(alphaRules) != 0:
lhs_ = lhs + "'"
while (lhs_ in rulesDiction.keys()) \
or (lhs_ in store.keys()):
lhs_ += "'"
for b in range(0, len(betaRules)):
betaRules[b].append(lhs_)
rulesDiction[lhs] = betaRules
for a in range(0, len(alphaRules)):
alphaRules[a].append(lhs_)
alphaRules.append(['#'])
store[lhs_] = alphaRules
for left in store:
rulesDiction[left] = store[left]
return rulesDiction