简体   繁体   English

我可以在 Python 中查看词法分析器和解析器的中间结果吗?

[英]Can I view intermediate results of lexer and parser in Python?

With the release of Python 3.9, which changed the parser from an LL(1) parser to a PEG-based parser, it got me thinking that I've never seen the lexer or parser for Python (CPython).随着 Python 3.9 的发布,将解析器从 LL(1) 解析器更改为基于 PEG 的解析器,这让我想到我从未见过 Python (CPython) 的词法分析器或解析器。 As I understand it the picture looks like据我了解,图片看起来像

source -> lexer -> TOKENS
TOKENS -> parser -> AST
AST -> ??? -> pyc file

Is it possible to view these intermediate results (TOKENS, AST, any others I missed)?是否可以查看这些中间结果(令牌、AST、我错过的任何其他结果)?

ast.parse(some_string) will give you the ast tree. ast.parse(some_string)会给你 ast 树。 You can then use various methods in the module to print it out or examine it programmatically.然后,您可以使用模块中的各种方法将其打印出来或以编程方式进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM