木蘭編譯器技術驗證——交互控制檯集成 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 語法樹後再執行的關鍵技術。


分享到:


相關文章: