python抽象语法树_Python随笔(四)抽象语法树AST
什么是抽象语法树嘞?
在计算机科学中,抽象语法和抽象语法树其实是源代码的抽象语法结构的树状表现形式
我们可以⽤⼀个在线的AST编辑器来观察AST的构建
Python语⾔的执⾏过程就是通过将Python字节码转化为抽象语法树来进⾏下⼀步的分析等其他操作,所以将Python转化为抽象语法树更利于程序的分析
⼀般来说,我们早期的学习当中固然会⽤到⼀种叫做表达式树的东西,我们⽤Python来实现⼀下表达式树
class StackEmptyException(Exception): pass
class StackFullException(Exception): pass
class Node:
def __init__(self, val=None, nxt=None):
self.value = val
< = nxt
def __str__(self):
return str(self.value)
class Stack:
def __init__(self, max=0):
self._top = None
self._max = 0
self.max = max
@property
def max(self):
return self._max
@max.setter
def max(self, m):
m = int(m)
if m < self.length:
raise Exception('Resize stack failed, please pop some elements first.')
self._max = m
if self._max < 0:
self._max = 0
def init(self, iterable=()):
if not iterable:
return
self._top = Node(iterable[0])
for i in iterable[1:]:
node = self._top
self._top = Node(i)
self._ = node
def show(self):
def _traversal(self):
node = self._top
while node :
yield node
node =
yield node
print('\n'.join(map(lambda x: '|{:^7}|'.format(str(x)), _traversal(self))) + '\n ' + 7 * '-') @property
def length(self):
if self._top is None:
return 0
node = self._top
i = 1
:
node =
i += 1
return i
@property
def is_empty(self):
return self._top is None
@property
def is_full(self):
return bool(self._max and self.length == self._max)
def push(self, item):
if self.is_full:
raise StackFullException('Error: trying to push element into a full stack!')
if not self._top:
self._top = Node(item)
return测试工程师需要考什么证书
node = self._top
self._top = Node(item)
self._ = node
def pop(self):
if self.is_empty:
raise StackEmptyException('Error: trying to pop element from an empty stack!') node = self._top
self._top = self._
return node.value
def top(self):
return self._top.value if self._top else self._top
def clear(self):
while self._top:
self.pop()
def test(stack):
print('\nShow stack:')
stack.show()
print('\nInit linked list:')
elasticsearch中文分词stack.init([1, 2, 3, 4, 5])
stack.show()
print('\nPush element to stack:')
stack.push(6)
stack.push(7)
stack.push('like')
stack.show()
print('\nCheck top element:')
p())
print('\nPop element from stack:')
e = stack.pop()
print('Element %s popped,' % e)
stack.show()
print('\nSet stack max size:')
try:
stack.max = 1
except Exception as e:
print(e)
print('\nSet stack max size:')
stack.max = 7
程序调试print(stack.max)
print('\nPush full stack:')
try:
stack.push(7)
except StackFullException as e:
python在线编辑器python3
print(e)
print('\nClear stack:')
stack.clear()
stack.show()
print('\nStack is empty:')
print(stack.is_empty)
print('\nPop empty stack:')
try:
stack.pop()
except StackEmptyException as e:
print(e)
class TreeNode:
def __init__(self, val=None, lef=None, rgt=None):
self.value = val
self.left = lef
self.right = rgt
def __str__(self):
return str(self.value)
class BinaryTree:
def __init__(self, root=None):
self._root = root
def __str__(self):
return '\n'.join(map(lambda x: x[1]*4*' '+str(x[0]), self.pre_traversal())) def pre_traversal(self, root=None):
if not root:
powerpoint使用母版的目的是root = self._root
x = []
depth = -1
def _traversal(node):
nonlocal depth
depth += 1
vim界面x.append((node, depth))
if node and node.left is not None:
_traversal(node.left)
if node and node.right is not None: _traversal(node.right)
depth -= 1
return x
return _traversal(root)
def in_traversal(self, root=None):
if not root:
root = self._root
x = []
depth = -1
def _traversal(node):
nonlocal depth
depth += 1
if node and node.left is not None:
_traversal(node.left)
x.append((node, depth))
if node and node.right is not None: _traversal(node.right)
depth -= 1
return x
return _traversal(root)
def post_traversal(self, root=None): if not root:
root = self._root
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论