-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontextFree.py
More file actions
131 lines (116 loc) · 4.09 KB
/
contextFree.py
File metadata and controls
131 lines (116 loc) · 4.09 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
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
from unrestricted import UnrestrictedGrammar
from contextSensitive import ContextSensitiveGrammar
from grammar import isVariable
class ContextFreeGrammar(ContextSensitiveGrammar):
def validRule(self, r ):
if not super(ContextFreeGrammar, self).validRule(r):
if r.rhs: return False
elif len(r.lhs) != 1 or not isVariable(r.lhs[0]):
return False
return True
"""
Algorithm 4.2.1
Construction of the Set of Nullable Variables
input: context-free grammar G = (V, Σ, P, S)
1. NULL := {A | A -> null ∈ P}
2. repeat
2.1 PREV := NULL
2.2 for each variable A ∈ V do
if there is an A rule A -> w and w ∈ PREV*, then
NULL := NULL ∪ {A}
until NULL = PREV
"""
def constructSetOfNullableVars( self ):
NULL = {r.lhs[0] for r.lhs[0] in self.rules if len(r.rhs) == 0}
while True:
PREV = NULL.copy()
for var in self.vars:
for rule in self.rules:
if rule.lhs[0] == var and all(v in PREV for v in rule.rhs):
A = rule.lhs[0]
NULL = NULL.union({A})
if NULL == PREV:
break
return NULL
"""
Algorithm 4.3.1
Contruction of the Set CHAIN(A)
input: essentially noncontracting context-free grammar G = (V, Σ, P, S)
1. CHAIN(A) := {A}
2. PREV := ∅
3. repeat
3.1 NEW := CHAIN(A) - PREV
3.2 PREV := CHAIN(A)
3.3 for each variable B ∈ NEW do
for each rule B -> C do
CHAIN(A) := CHAIN(A) ∪ {C}
until CHAIN(A) = PREV
"""
def chain( self, A ):
if A not in self.vars:
print(A, " not in grammar's variables.")
return False
chain_A = set(A)
prev = set()
while True:
new = set()
prev = chain_A.copy()
for var in new:
for rule in self.rules:
if len(rule.lhs) == 1 and isVariable(rule.lhs[0]):
if len(rule.rhs) == 1 and isVariable(rule.rhs[0]):
# rule is of form B -> C
C = rule.rhs[0]
chain_A = chain_A.union(set(C))
if chain_A == prev:
break
return chain_A
"""
Algorithm 4.4.2
Construction of the Set of Variables That Derive Terminal Strings
input: context-free grammar G = (V, Σ, P, S)
1. TERM := {A | there is a rule A -> w ∈ P with w ∈ Σ*}
2. repeat
2.1 PREV := TERM
2.2 for each variable A ∈ V do
if there is an A rule A -> w and w ∈ (PREV ∪ Σ*) then
TERM := TERM ∪ {A}
until PREV = TERM
"""
def getVariablesThatDeriveTerminalStrings( self ):
TERM = {r.lhs[0] for r.lhs[0] in self.rules if all(t in self.terminals for t in r.rhs)}
while True:
PREV = TERM.copy()
for A in self.vars:
for r in self.rules:
if r.lhs[0] == A and all(t in self.terminals.union(PREV) for t in r.rhs):
TERM = TERM.union({A})
if PREV == TERM:
break
return TERM
"""
Algorithm 4.4.4
Construction of the Set of Reachable Variables
input: context-free grammar G = (V, Σ, P, S)
1. REACH := {S}
2. PREV := ∅
3. repeat
3.1 NEW := REACH - PREV
3.2 PREV := REACH
3.3 for each variable A ∈ NEW do
for each rule A -> w do add all variable in w to REACH
until REACH = PREV
"""
def constructSetOfReachableVars( self ):
REACH = {self.getStartSymbol()}
PREV = {}
while True:
NEW = REACH - PREV
PREV = REACH.copy()
for A in NEW:
for r in self.rules:
if r.lhs[0] == A:
REACH = REACH.union({x for x in r.rhs if isVariable(x)})
if REACH == PREV:
break
return REACH