Skip to content

Commit

Permalink
Merge branch 'parser'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraGrass committed Jul 28, 2019
2 parents 7ccb6e1 + 6fc7f1f commit 6b79f15
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@
#result = parse_tree("if y > x then x else 7 + y * x")
#parse_syntaxTree(result)

# Testing let rec & and parsing
# print(parse_tree("let rec x = fun a -> 1 and y = fun b -> let rec z0 = fun c -> c and z1 = fun d -> d in z1 1 and z = fun a -> a in ((x 2) + (y 2))"))
89 changes: 86 additions & 3 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,91 @@
import numpy as np
import re

def parse_brack(expr):
if(parse("({e})", expr) is None):
return None

expr = expr[1:-1]
expr = expr.split()

counter = 1

for word in expr:
if word[0] == "(":
counter += 1
if word[-1] == ")":
counter -= 1

if not counter:
return None

x = dict()
x['e'] = " ".join(expr)

return x

def parse_let_rec(expr):

expr = expr.split()
if (len(expr) < 2 or expr[0] != "let" or expr[1] != "rec"):
return None

expr = expr[2:]
counter = 1
ind = -1

for i in range(len(expr)):
if (expr[i] == "let"):
counter += 1
elif (expr[i] == "in"):
counter -= 1

if(counter == 0):
ind = i
break

if (ind == -1):
return None

x = dict()
x['defs'] = " ".join(expr[:ind])
x['e0'] = " ".join(expr[ind+1:])

return x

def parse_and(defs):
defs = defs.split()

collect = []
last = 0

counter = 0
for i in range(len(defs)):

if(not counter and defs[i] == "and"):
collect.append(defs[last:i])
last = i+1
continue

if(defs[i] == "let"):
counter += 1
continue

if(defs[i] == "in"):
counter -= 1
continue

collect.append(defs[last:])

defs = []

for part in collect:
defs.append(" ".join(part))

return defs

def mk_rec(x, depth, dictio):
defs = x['defs'].split('and')
defs = parse_and(x['defs'])

new_dict = dictio.copy()

Expand Down Expand Up @@ -162,12 +245,12 @@ def parse_tree(expr, depth=0, dictio={}):
expr = re.sub(' +', ' ',expr).strip()

# (e)
x = parse("({e})", expr)
x = parse_brack(expr)
if(x != None):
return parse_tree(x['e'], depth, dictio)

# let rec x1 = e1 and ... and xn = en in e0
x = parse("let rec {defs} in {e0}", expr)
x = parse_let_rec(expr)
if(x != None):
return mk_rec(x, depth, dictio)

Expand Down

0 comments on commit 6b79f15

Please sign in to comment.