LEPL 介绍
LEPL是一个用 Python 开发的向下递归解析器。It is based on parser combinator libraries popular
in functional programming, but also exploits Python language features.
Operators provide a friendly syntax, and the consistent use of generators
supports full backtracking and resource management. Backtracking implies that
a wide variety of grammars are supported; appropriate memoisation ensures that
even left-recursive grammars terminate.
>>> from lepl import * >>> class Term(Node): pass >>> class Factor(Node): pass >>> class Expression(Node): pass >>> expr = Delayed() >>> number = Digit()[1:,...] > 'number' >>> spaces = Drop(Regexp(r'\s*')) >>> with Separator(spaces): >>> term = number | '(' & expr & ')' > Term >>> muldiv = Any('*/') > 'operator' >>> factor = term & (muldiv & term)[:] > Factor >>> addsub = Any('+-') > 'operator' >>> expr += factor & (addsub & factor)[:] > Expression >>> line = expr & Eos() >>> parser = line.parse_string >>> parser('1 + 2 * (3 + 4 - 5)')[0] Expression +- Factor | +- Term | | `- number '1' | `- ' ' +- operator '+' +- ' ' `- Factor +- Term | `- number '2' +- ' ' +- operator '*' +- ' ' `- Term +- '(' +- Expression | +- Factor | | +- Term | | | `- number '3' | | `- ' ' | +- operator '+' | +- ' ' | +- Factor | | +- Term | | | `- number '4' | | `- ' ' | +- operator '-' | +- ' ' | `- Factor | `- Term | `- number '5' `- ')
LEPL 官网
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。