-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_parser.py
More file actions
32 lines (26 loc) · 1.15 KB
/
lambda_parser.py
File metadata and controls
32 lines (26 loc) · 1.15 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
#pylint: disable = consider-using-enumerate, invalid-name, missing-class-docstring, missing-function-docstring
from grammar import Application, Expression, Function, Name
import lambda_helper as helper
class Lambda_Parser:
def __init__(self):
self.lambda_expression = ""
def parse_input(self,s):
# Grammar rules:
# <expression> = <name> | <function> | <application>
# <function> = ($<name>.<expression>)
# <application> = (<function><expression>)
# <name> = [a-z] +
# Check if value is actually a string
if not isinstance(s, str):
raise TypeError("Input must be a string.")
# Check if amount of opening and closing parentheses match
opening_parens_count = s.count("(")
closing_parens_count = s.count(")")
if opening_parens_count != closing_parens_count:
raise ValueError("Mismatched number of parentheses.")
s = helper.syntax_sugaring(s)
expression = helper.get_grammar_type(s)
print (expression.evaluate().replace("$", "λ"))
return s
def get_parse_input(self):
return self.lambda_expression