Skip to content

Instantly share code, notes, and snippets.

@rlmacsween
Created June 4, 2019 19:33
Show Gist options
  • Save rlmacsween/2fbc924afefd73175389788ff5254ef3 to your computer and use it in GitHub Desktop.
Save rlmacsween/2fbc924afefd73175389788ff5254ef3 to your computer and use it in GitHub Desktop.
Andersen's analysis
def parse_statement(s):
a, b = s.split(' = ')
A, x = a[:-1], a[-1]
B, y = b[:-1], b[-1]
return (A, x, B, y)
def do_andersen(statements, iterations):
statements = [parse_statement(s) for s in statements]
pt = {}
for _ in range(iterations):
for s in statements:
A, x, B, y = s
if A == '':
xs = {x}
elif A == '*':
xs = {z for z in pt[x]}
if B == '&':
ys = {y}
elif B == '':
ys = {z for z in pt[y]}
elif B == '*':
ys = pt[y]
ys = set.union(*[pt[z] for z in ys])
elif B == '**':
ys = {z for z in pt[y]}
ys = set.union(*[pt[z] for z in ys])
ys = set.union(*[pt[z] for z in ys])
for x in xs:
if x not in pt:
pt[x] = set()
pt[x] |= ys
return pt
if __name__ == '__main__':
statements = ['c = &b',
'*c = &c',
'a = c',
'c = &d',
'*c = **a',
'*c = *b',
'*a = &a',
]
print(do_andersen(statements, 1))
print(do_andersen(statements, 2))
print(do_andersen(statements, 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment