木兰编译器技术验证——交互控制台集成 rply 语法分析器

续前文,集成了四则运算语法分析器。运行效果如下:

<code>$ python3 交互环境.py 
Welcome to ulang's REPL..
Type 'help' for more informations.
> 2*5/3
3.3333333333333335
> 1-4
-3
> (1+2)*3-4
5
> quit
/<code>

除了之前的语法分析器,只加了此方法:

<code>class 木兰(cmd.Cmd):
...
def default(self, line):
print(分析器.parse(分词器.lex(line)).求值())
/<code>

接下去还需验证将代码转换为 Python 语法树后再执行的关键技术。


分享到:


相關文章: