Skip to main content

metasequoia-parser 水杉语法解析器

水杉语法解析器提供了 LR(0)、SLR、LR(1)、LALR(1) 四种解析器,以及根据语法自动编译 LALR(1) 解析器的 Python 代码的功能。

安装方法

pip install metasequoia-parser

使用方法

定义语法规则

使用 create_grammar 函数创建语法类的构造器,使用构造器的 group_append 方法添加语义组,在语法定义完成后调用构造器的 build 方法构造语法类(Grammar)。

使用 create_group 函数创建语义组,其参数为语义组名称(name)和语义组备选规则的列表(rules)。

使用 create_rule 函数创建备选规则,其参数为符号列表(symbols)和规约函数(action)。

import metasequoia_parser as ms_parser

grammar = ms_parser.create_grammar(
    start="S",
    terminal_type_enum=ms_parser.TerminalType.create_by_terminal_name_list(["a", "b", "c"])
).group_append(ms_parser.create_group(name="S", rules=[
    ms_parser.create_rule(symbols=["a", "B"], action=lambda x: f"{x[0]}{x[1]}"),
    ms_parser.create_rule(symbols=["a", "B", "c"], action=lambda x: f"{x[0]}{x[1]}{x[2]}"),
])).group_append(ms_parser.create_group(name="B", rules=[
    ms_parser.create_rule(symbols=["b"], action=lambda x: f"{x[0]}"),
])).build()

构造解析器

将语法类作为参数构造解析器:

parser = ms_parser.parser.ParserLR0(grammar)  # 构造 LR(0) 解析器
parser = ms_parser.parser.ParserSLR(grammar)  # 构造 SLR 解析器
parser = ms_parser.parser.ParserLR1(grammar)  # 构造 LR(1) 解析器
parser = ms_parser.parser.ParserLALR1(grammar)  # 构造 LALR(1) 解析器

使用解析器对象

直接使用解析器解析词法解析的结果,适用于测试场景:

from metasequoia_parser.lexical import create_lexical_iterator_by_name_list

parser.parse(create_lexical_iterator_by_name_list(grammar, ["a", "b"]))

在正式场景下,需实现词法解析器(ms_parser.lexical.LexicalBase)抽象基类,作为 parse 方法的参数。

编译解析器

使用 ms_parser.compiler.compile_lalr1 函数,可以将 LALR(1) 解析器对象编译为 Python 代码,适用于生产环境:

source_code = ms_parser.compiler.compile_lalr1(parser, import_list=[
    "from my_package import my_tree_node"
])
with open("my_parser.py", "w+", encoding="UTF-8") as file:
    for row in source_code:
        file.write(f"{row}\n")

使用编译的解析器

引用编译的 Python 代码所在文件中的 parse 函数,其参数及返回值等价于解析器对象的 parse 方法。

from metasequoia_parser.lexical import create_lexical_iterator_by_name_list
from my_parser import parse  # 引用编译的 Python 代码所在文件中的 parse 函数

parse(create_lexical_iterator_by_name_list(grammar, ["a", "b"]))  # 测试场景

在正式场景下,需实现词法解析器(ms_parser.lexical.LexicalBase)抽象基类,作为 parser.parse 函数的参数。

Metadata

Release files for metasequoia-parser 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metasequoia-parser 0.2.0
File Size Uploaded Provenance Actions
metasequoia-parser-0.2.0.tar.gz 48.9 kB Without provenance Details for metasequoia-parser-0.2.0.tar.gz

Release files / metasequoia-parser-0.2.0.tar.gz

Download URL metasequoia-parser-0.2.0.tar.gz
Size 48.9 kB
Tags Source
SHA-256 checksum
How to use checksums
76c94cfbf49da09291eb5bfa641f6769ce2aeb330dbf48182e45e5be809b3b7d
BLAKE2b-256 checksum
How to use checksums
9f1c9f7a20498c6492c34739967ee5a76eeca269a1892b4bcc385725ab8af368
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.5

Security for metasequoia-parser 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page