ayushnoori commited on
Commit
c9abdca
·
1 Parent(s): a417ea3

Working AST representation

Browse files
abstract_syntax_tree.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ ABSTRACT SYNTAX TREE
3
+ This file contains the Python class that defines the abstract syntax tree (AST) representation.
4
+ '''
5
+
6
+ class OperatorNode:
7
+ '''
8
+ Class to represent operator nodes (i.e., an operator and its operands) as an AST.
9
+
10
+ Args:
11
+ operator (object): operator object (e.g., Add, Subtract, etc.)
12
+ children (list): list of children nodes (operands)
13
+
14
+ Example of usage:
15
+
16
+ add_node = OperatorNode(Add(), [IntegerConstant(7), IntegerConstant(5)])
17
+ subtract_node = OperatorNode(Subtract(), [IntegerConstant(3), IntegerConstant(1)])
18
+ multiply_node = OperatorNode(Multiply(), [add_node, subtract_node])
19
+ '''
20
+ def __init__(self, operator, children):
21
+ self.operator = operator # Operator object (e.g., Add, Subtract, etc.)
22
+ self.children = children # list of children nodes (operands)
23
+
24
+ def evaluate(self):
25
+ # check arity
26
+ if len(self.children) != self.operator.arity:
27
+ raise ValueError("Invalid number of operands for operator")
28
+ # recursively evaluate the operator and its operands
29
+ operands = [child.evaluate() for child in self.children]
30
+ return self.operator.evaluate(*operands)
31
+
32
+ def str(self):
33
+ # check arity
34
+ if len(self.children) != self.operator.arity:
35
+ raise ValueError("Invalid number of operands for operator")
36
+ # recursively generate a string representation of the AST
37
+ operand_strings = [child.str() for child in self.children]
38
+ return self.operator.str(*operand_strings)
abstract_syntax_trees.py DELETED
@@ -1,5 +0,0 @@
1
- '''
2
- ABSTRACT SYNTAX TREES
3
- This file contains Python classes that define the abstract syntax tree (AST) for program synthesis.
4
- '''
5
-
 
 
 
 
 
 
arithmetic.py CHANGED
@@ -20,6 +20,12 @@ class IntegerVariable:
20
  def assign(self, value):
21
  self.value = value
22
 
 
 
 
 
 
 
23
  class IntegerConstant:
24
  '''
25
  Class to represent an integer constant.
@@ -28,6 +34,12 @@ class IntegerConstant:
28
  self.value = value # value of the constant
29
  self.type = int # type of the constant
30
 
 
 
 
 
 
 
31
  class Add:
32
  '''
33
  Operator to add two numerical values.
@@ -38,11 +50,11 @@ class Add:
38
  self.return_type = int # return type
39
  self.weight = 1 # weight
40
 
41
- def __call__(self, x, y):
42
  return x + y
43
 
44
- def str(x, y):
45
- return f"{x} + {y}"
46
 
47
  class Subtract:
48
  '''
@@ -54,11 +66,11 @@ class Subtract:
54
  self.return_type = int # return type
55
  self.weight = 1 # weight
56
 
57
- def __call__(self, x, y):
58
  return x - y
59
 
60
- def str(x, y):
61
- return f"{x} - {y}"
62
 
63
  class Multiply:
64
  '''
@@ -70,11 +82,11 @@ class Multiply:
70
  self.return_type = int # return type
71
  self.weight = 1 # weight
72
 
73
- def __call__(self, x, y):
74
  return x * y
75
 
76
- def str(x, y):
77
- return f"{x} * {y}"
78
 
79
  class Divide:
80
  '''
@@ -86,14 +98,14 @@ class Divide:
86
  self.return_type = int # return type
87
  self.weight = 1 # weight
88
 
89
- def __call__(self, x, y):
90
  try: # check for division by zero error
91
  return x / y
92
  except ZeroDivisionError:
93
  return None
94
 
95
- def str(x, y):
96
- return f"{x} / {y}"
97
 
98
 
99
  '''
 
20
  def assign(self, value):
21
  self.value = value
22
 
23
+ def evaluate(self):
24
+ return self.value
25
+
26
+ def str(self):
27
+ return f"x{self.position}"
28
+
29
  class IntegerConstant:
30
  '''
31
  Class to represent an integer constant.
 
34
  self.value = value # value of the constant
35
  self.type = int # type of the constant
36
 
37
+ def evaluate(self):
38
+ return self.value
39
+
40
+ def str(self):
41
+ return str(self.value)
42
+
43
  class Add:
44
  '''
45
  Operator to add two numerical values.
 
50
  self.return_type = int # return type
51
  self.weight = 1 # weight
52
 
53
+ def evaluate(self, x, y):
54
  return x + y
55
 
56
+ def str(self, x, y):
57
+ return f"({x} + {y})"
58
 
59
  class Subtract:
60
  '''
 
66
  self.return_type = int # return type
67
  self.weight = 1 # weight
68
 
69
+ def evaluate(self, x, y):
70
  return x - y
71
 
72
+ def str(self, x, y):
73
+ return f"({x} - {y})"
74
 
75
  class Multiply:
76
  '''
 
82
  self.return_type = int # return type
83
  self.weight = 1 # weight
84
 
85
+ def evaluate(self, x, y):
86
  return x * y
87
 
88
+ def str(self, x, y):
89
+ return f"({x} * {y})"
90
 
91
  class Divide:
92
  '''
 
98
  self.return_type = int # return type
99
  self.weight = 1 # weight
100
 
101
+ def evaluate(self, x, y):
102
  try: # check for division by zero error
103
  return x / y
104
  except ZeroDivisionError:
105
  return None
106
 
107
+ def str(self, x, y):
108
+ return f"({x} / {y})"
109
 
110
 
111
  '''
demonstration.ipynb CHANGED
@@ -27,10 +27,34 @@
27
  "\n",
28
  "# import arithmetic module\n",
29
  "# from arithmetic import *\n",
 
30
  "from examples import example_set, check_examples\n",
31
  "import config"
32
  ]
33
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  {
35
  "cell_type": "markdown",
36
  "metadata": {},
@@ -40,7 +64,7 @@
40
  },
41
  {
42
  "cell_type": "code",
43
- "execution_count": 2,
44
  "metadata": {},
45
  "outputs": [],
46
  "source": [
@@ -66,7 +90,7 @@
66
  },
67
  {
68
  "cell_type": "code",
69
- "execution_count": 8,
70
  "metadata": {},
71
  "outputs": [],
72
  "source": [
@@ -176,7 +200,7 @@
176
  },
177
  {
178
  "cell_type": "code",
179
- "execution_count": 9,
180
  "metadata": {},
181
  "outputs": [],
182
  "source": [
@@ -229,7 +253,7 @@
229
  },
230
  {
231
  "cell_type": "code",
232
- "execution_count": 16,
233
  "metadata": {},
234
  "outputs": [],
235
  "source": [
@@ -267,7 +291,7 @@
267
  },
268
  {
269
  "cell_type": "code",
270
- "execution_count": 2,
271
  "metadata": {},
272
  "outputs": [],
273
  "source": [
 
27
  "\n",
28
  "# import arithmetic module\n",
29
  "# from arithmetic import *\n",
30
+ "# from abstract_syntax_tree import OperatorNode\n",
31
  "from examples import example_set, check_examples\n",
32
  "import config"
33
  ]
34
  },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 14,
38
+ "metadata": {},
39
+ "outputs": [
40
+ {
41
+ "data": {
42
+ "text/plain": [
43
+ "24"
44
+ ]
45
+ },
46
+ "execution_count": 14,
47
+ "metadata": {},
48
+ "output_type": "execute_result"
49
+ }
50
+ ],
51
+ "source": [
52
+ "add_node = OperatorNode(Add(), [IntegerConstant(7), IntegerConstant(5)])\n",
53
+ "subtract_node = OperatorNode(Subtract(), [IntegerConstant(3), IntegerConstant(1)])\n",
54
+ "multiply_node = OperatorNode(Multiply(), [add_node, subtract_node])\n",
55
+ "multiply_node.evaluate()"
56
+ ]
57
+ },
58
  {
59
  "cell_type": "markdown",
60
  "metadata": {},
 
64
  },
65
  {
66
  "cell_type": "code",
67
+ "execution_count": null,
68
  "metadata": {},
69
  "outputs": [],
70
  "source": [
 
90
  },
91
  {
92
  "cell_type": "code",
93
+ "execution_count": null,
94
  "metadata": {},
95
  "outputs": [],
96
  "source": [
 
200
  },
201
  {
202
  "cell_type": "code",
203
+ "execution_count": null,
204
  "metadata": {},
205
  "outputs": [],
206
  "source": [
 
253
  },
254
  {
255
  "cell_type": "code",
256
+ "execution_count": null,
257
  "metadata": {},
258
  "outputs": [],
259
  "source": [
 
291
  },
292
  {
293
  "cell_type": "code",
294
+ "execution_count": null,
295
  "metadata": {},
296
  "outputs": [],
297
  "source": [