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 6b79f15 + 71844b0 commit d87a0ea
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 87 deletions.
8 changes: 4 additions & 4 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
General
Read from file!
> Read from file!
> Rho for vars & funs
several statements
> args really just in dictio in function body?
parser doing proper separation? -> nope
change imports in main
> parser doing proper separation? -> nope
> change imports in main

b
> basic (BasicValue)
Expand All @@ -30,7 +30,7 @@ x
( e' e0 ... ek−1 )
> app
> fun has to be known
> split e0 to ek-1 correctly?
split e0 to ek-1 correctly?
convert "39 + 3" to "39+3"

( fun x0 ... xk−1 -> e )
Expand Down
5 changes: 5 additions & 0 deletions input/slide_102.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let rec fac = fun x ->
if x <= 1
then 1
else x * fac (x-1) in
fac 7
5 changes: 5 additions & 0 deletions input/slide_114.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let c = 5 in
let f = fun a ->
let b = a * a in
b + c in
f c
3 changes: 3 additions & 0 deletions input/slide_131.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let a = 19 in
let b = a * a in
a + b
4 changes: 4 additions & 0 deletions input/slide_140.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let a = 17 in
let f = fun b ->
a + b in
f 42
5 changes: 5 additions & 0 deletions input/slide_159.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let rec f = fun x y ->
if y <= 1
then x
else f ( x * y ) ( y - 1 ) in
f 1
7 changes: 7 additions & 0 deletions input/test_1.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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))
95 changes: 23 additions & 72 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,31 @@
from parser import *
from code_generator import *
from parser import parse_tree
from code_generator import * # TODO just import necessary method

if __name__ == '__main__':
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

# default values
input_file = "input.txt"
output_file = "output.txt"
# default values
input_file = "input/test1.ml"
output_file = "output/test1.txt"

# parse command line arguments
parser = ArgumentParser(
description="translate functional code to MaMa language",
formatter_class=ArgumentDefaultsHelpFormatter
)
parser.add_argument("-i", "--input", type=str, help="path to a file containing functional language", default=input_file)
parser.add_argument("-o", "--output", type=str, help="path to output file", default=output_file)
args = parser.parse_args()
# parse command line arguments
parser = ArgumentParser(
description="translate functional code to MaMa language",
formatter_class=ArgumentDefaultsHelpFormatter
)
parser.add_argument("-i", "--input", type=str, help="path to a file containing functional language", default=input_file)
parser.add_argument("-o", "--output", type=str, help="path to output file", default=output_file)
args = parser.parse_args()

# usage: main.py [-h] [-i INPUT] [-o OUTPUT]
#print(args.input)
#print(args.output)
# usage: main.py [-h] [-i INPUT] [-o OUTPUT]

# own examples
'''
print(parse_tree("let a = 17 in let f = fun b -> a + b in f (39 + 2)"))
print()
print(parse_tree("if 1 then 3 else 4"))
print()
print(parse_tree("let x3 = 4 in + x3 "))
print()
print(parse_tree("((34))"))
print()
print(parse_tree("3 / 4"))
print()
print(parse_tree("let rec f = fun x y -> if y <= 1 then x else f ( x * y ) ( y - 1 ) in f 1"))
print()
print(parse_tree("let y_x = 4 in y_x"))
print()
with open(args.input) as i_f:

# from lecture slides
print(parse_tree("let rec fac = fun x -> if x <= 1 then 1 else x * fac (x-1) in fac 7")) # 102
print()
# read input and remove whitespace
expr = i_f.read()
expr = ' '.join(expr.split())

# TODO: for let f, first "in" is chosen (a in b)
# print(parse_tree("let c = 5 in let f = fun a -> let b = a * a in b + c in f c")) # 114
# print()
print(parse_tree("let a = 19 in let b = a * a in a + b")) # 131
print()
# parse syntax tree
tree = parse_tree(expr)

print(parse_tree("let a = 17 in let f = fun b -> a + b in f 42")) # 140
print()
print(parse_tree("let rec f = fun x y -> if y <= 1 then x else f ( x * y ) ( y - 1 ) in f 1")) # 159
print()
'''

##examples handled by code generation
#result = parse_tree("let a = 19 in let b = a * a in a + b") # 131
#parse_syntaxTree(result)
#result = parse_tree("if 1 then 3 else 4")
#parse_syntaxTree(result)
#result = parse_tree("let a = 17 in let f = fun b -> a + b in f (39 + 2)")
#parse_syntaxTree(result)
#result = parse_tree("let a = 17 in let f = fun b -> a + b in f 42") # 140
#parse_syntaxTree(result)
#result = parse_tree("let rec f = fun x y -> if y <= 1 then 5 else f ( x * y ) ( y - 1 ) in f 1")
#parse_syntaxTree(result)
#result = parse_tree("let x3 = 4 in + x3 ")
#parse_syntaxTree(result)
'''Needs Work:'''
#result = parse_tree("let f = fun x y -> let a = 5 in let b = x + 2*y in b + a*x in f 0 1")
#parse_syntaxTree(result)
#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))"))
# TODO generate code and print to file
print(tree)
40 changes: 29 additions & 11 deletions parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from tree_nodes import *
from parse import *
import numpy as np
import re

def parse_brack(expr):
if(parse("({e})", expr) is None):
Expand Down Expand Up @@ -214,6 +213,20 @@ def mk_if(x, depth, dictio):

return Other("if", [e0, e1, e2], depth)

def check_brack(expr):
count = 1

for char in expr:
if(char == '('):
count += 1
if(char == ')'):
count -= 1

if not count:
return False

return True

def mk_op2(x, depth, dictio, tag):
e1 = parse_tree(x['e1'], depth+1, dictio)
e2 = parse_tree(x['e2'], depth+1, dictio)
Expand Down Expand Up @@ -242,7 +255,7 @@ def is_int(expr):
def parse_tree(expr, depth=0, dictio={}):

# remove unnecessary whitespace
expr = re.sub(' +', ' ',expr).strip()
expr = ' '.join(expr.split())

# (e)
x = parse_brack(expr)
Expand Down Expand Up @@ -276,47 +289,52 @@ def parse_tree(expr, depth=0, dictio={}):

# e1 >= e2
x = parse("{e1}>={e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "ge")

# e1 > e2
x = parse("{e1}>{e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "gt")

# e1 <= e2
x = parse("{e1}<={e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "le")

# e1 < e2
x = parse("{e1}<{e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "lt")

# e1 == e2
x = parse("{e1}=={e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "eq")

# e1 != e2
x = parse("{e1}!={e2}", expr)
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "neq")

# e1 + e2
x = parse("{e1}+{e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "add")

# e1 - e2
x = parse("{e1}-{e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "sub")

# e1 * e2
x = parse("{e1}*{e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "mul")

# e1 / e2
x = parse("{e1}/{e2}", expr)
if(x != None):
if(x != None and check_brack(x['e2'])):
return mk_op2(x, depth, dictio, "div")

# +e
Expand Down
45 changes: 45 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from parser import parse_tree
from code_generator import parse_syntaxTree

# own examples
print(parse_tree("let a = 17 in let f = fun b -> a + b in f (39 + 2)"))
print()
print(parse_tree("if 1 then 3 else 4"))
print()
print(parse_tree("let x3 = 4 in + x3 "))
print()
print(parse_tree("((34))"))
print()
print(parse_tree("3 / 4"))
print()
print(parse_tree("let rec f = fun x y -> if y <= 1 then x else f ( x * y ) ( y - 1 ) in f 1"))
print()
print(parse_tree("let y_x = 4 in y_x"))
print()

# examples handled by code generation
result = parse_tree("let a = 19 in let b = a * a in a + b") # 131
parse_syntaxTree(result)
print()
result = parse_tree("if 1 then 3 else 4")
parse_syntaxTree(result)
print()
result = parse_tree("let a = 17 in let f = fun b -> a + b in f (39 + 2)")
parse_syntaxTree(result)
print()
result = parse_tree("let a = 17 in let f = fun b -> a + b in f 42") # 140
parse_syntaxTree(result)
print()
result = parse_tree("let rec f = fun x y -> if y <= 1 then 5 else f ( x * y ) ( y - 1 ) in f 1")
parse_syntaxTree(result)
print()
result = parse_tree("let x3 = 4 in + x3 ")
parse_syntaxTree(result)

# needs work
'''
result = parse_tree("let f = fun x y -> let a = 5 in let b = x + 2*y in b + a*x in f 0 1")
parse_syntaxTree(result)
result = parse_tree("if y > x then x else 7 + y * x")
parse_syntaxTree(result)
'''

0 comments on commit d87a0ea

Please sign in to comment.