我可以用具体语法树做什么?

发布于 2024-08-20 01:32:42 字数 417 浏览 4 评论 0原文

我正在使用 pyPEG 为简单语法创建解析树。该树使用列表和元组来表示。这是一个例子:

[('command',
  [('directives',
    [('directive',
      [('name', 'retrieve')]),
     ('directive',
      [('name', 'commit')])]),
   ('filename',
    [('name', 'f30502')])])]

我的问题是此时我该如何处理它?我知道很大程度上取决于我想要做什么,但我无法找到太多有关使用/使用解析树的信息,只能创建它们。有人有我可能使用的参考资料吗?

感谢您的帮助。

I'm using pyPEG to create a parse tree for a simple grammar. The tree is represented using lists and tuples. Here's an example:

[('command',
  [('directives',
    [('directive',
      [('name', 'retrieve')]),
     ('directive',
      [('name', 'commit')])]),
   ('filename',
    [('name', 'f30502')])])]

My question is what do I do with it at this point? I know a lot depends on what I am trying to do, but I haven't been able to find much about consuming/using parse trees, only creating them. Does anyone have any pointers to references I might use?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2024-08-27 01:32:42

CST(具体语法树)很难用于 一些原因。因此,它们通常会转换为 AST(抽象语法树)以进行进一步处理(同一篇文章中有详细信息)。例如,Python 编译器(将 Python 源代码转换为 Python 的组件) VM 字节码)将 CST 转换为 AST,作为其工作的一部分。

现在,它确实很大程度上取决于您的最终目标。你在解析什么?你想用它做什么?如果您要重新创建经典编译流程,转换为 AST 可能是一个好方法。否则,您可能会发现 CST 就足够了 - 这完全取决于您的需要。

CSTs (concrete syntax trees) are quite hard to work with for some reasons. Therefore they're commonly converted to ASTs (abstract syntax tree) for further processing (details in the same article). For instance, the Python compiler (the component that turns Python source code into Python VM bytecode) translates CSTs to ASTs as part of its work.

Now, it really does strongly depend on your final goal. What are you parsing? What do you want to do with it? If you're re-creating a classical compilation flow, converting to an AST is probably a good way to proceed. Otherwise, you may find the CST enough - it all depends on what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文