简体   繁体   English

Python解析器模块教程

[英]Python parser Module tutorial

I am writing an application which reads an input file that currently has its own grammar, which is processed by lex/yacc.我正在编写一个应用程序,它读取当前具有自己的语法的输入文件,该文件由 lex/yacc 处理。

I'm looking to modify this so as to make this input file a Python script instead, and was wondering if someone can point me to a beginner's guide to using the parser module in Python.我正在寻找修改它以便将此输入文件改为 Python 脚本,并且想知道是否有人可以向我指出在 Python 中使用解析器模块的初学者指南。 I'm fairly new to Python itself, but have worked through a fair chunk of the online tutorial.我对 Python 本身还很陌生,但已经完成了相当多的在线教程。

From what I have researched, I know there are options (such as pyparsing) which can allow me to keep the existing grammar and use Pyparsing as a replacement for lex/yacc.根据我的研究,我知道有一些选项(例如 pyparsing)可以让我保留现有的语法并使用 Pyparsing 作为 lex/yacc 的替代品。 However, I am curious to learn the Python parser module in more detail and explore its feasibility.但是,我很想更详细地学习 Python 解析器模块并探索其可行性。

Thanks.谢谢。

You mean the parser module ?你的意思是解析器模块 It's a parser for Python source code only, not a general purpose parser.它只是一个 Python 源代码的解析器,而不是一个通用的解析器。 You can't use it to parse anything else.你不能用它来解析其他任何东西。

As Jochen said, the parser module is for parsing Python code.正如 Jochen 所说,解析器模块用于解析 Python 代码。 I think you're best off checking out Ned Batchelder's list of parsers .我认为您最好查看Ned Batchelder 的解析器列表 PyParsing does things pretty differently from Lex and Yacc, so I'm not sure why you think you could keep your existing grammar and lexer. PyParsing 的处理方式与 Lex 和 Yacc 完全不同,所以我不确定您为什么认为可以保留现有的语法和词法分析器。 A better bet might be David Beazley's PLY toolkit.更好的选择可能是 David Beazley 的PLY工具包。 It's solid and has excellent documentation.它很可靠,并且有很好的文档。

I recommend that you check out https://github.com/erezsh/lark我建议您查看https://github.com/erezsh/lark

It's great for newcomers to parsing: It can parse ALL context-free grammars, it automatically builds an AST (with line & column numbers), and it accepts the grammar in EBNF format, which is considered the standard and is very easy to write.非常适合新手解析:可以解析所有上下文无关的语法,自动构建AST(带行号和列号),接受EBNF格式的语法,这被认为是标准的,非常容易编写。

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

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