PEGTL

解析表达式语法模板库。(Parsing Expression Grammar Template Library)

Github stars Tracking Chart

欢迎来到 PEGTL

解析表达式语法模板库(PEGTL)是一个零依赖的 C++ 纯标题(header-only)解析器组合子库,用于根据解析表达式语法(PEG)创建解析器。

文档

介绍

语法是用模板编程(不是模板元编程)创建的常规 C++ 代码编写的,也就是说,嵌套的模板实例化自然对应于 PEG 的归纳定义(和其他解析器组合器方法)。

包括一整套可以由用户组合和扩展的解析器规则,以及用于调试语法以及将用户定义的动作附加到语法规则的机制。 这是一个如何使用 PEGTL 将 PEG 语法规则实现为 C++ 类的示例。

// PEG rule for integers consisting of a non-empty
// sequence of digits with an optional sign:
// sign ::= '+' / '-'
// integer ::= sign? digit+
// The same parsing rule implemented with the PEGTL:
using namespace tao::pegtl;
struct sign : one< '+', '-' > {};
struct integer : seq< opt< sign >, plus< digit > > {};

PEG 表面上与上下文无关文法(CFG)相似,但是 PEG 更具确定性,因此产生了一些非常重要的区别。包括的语法分析发现了 PEG 中的几种典型错误,包括左递归。

设计

PEGTL 被设计为“精益求精”,核心库由大约6000行代码组成。重点是简单性和效率,相对于复杂的优化,我们希望使用一种经过良好调整的简单方法。

PEGTL 主要与解析组合器和语法规则有关,并与给库的用户(可能)完全控制解析运行的所有其他方面有关。在分析运行期间是否/将采取什么动作以及是否/将创建数据结构完全取决于用户。

其中包括一些典型情况的示例,例如在字符串中转义转义序列,构建通用 JSON 数据结构以及对算术表达式进行即时评估。

通过使用模板编程和模板专业化,可以编写一次语法,然后在不同(或相同)解析运行中以不同(语义)动作以多种方式使用该语法。

有了 PEG 形式主义,通常就不需将其分为词法分析器和解析器两个阶段了 -- 一切都在一个语法中完成。规则以 C++ 形式表示为模板实例,优化 PEGTL 语法是编译器的任务。

状态

每个提交都将使用多种体系结构,操作系统,编译器及其版本进行自动测试。

  • Windows
    • Visual Studio 2017 (x86, x64)
    • Visual Studio 2019 (x86, x64)
  • macOS (using libc++)
    • macOS 10.15, Xcode 11.4
  • Ubuntu 16.04 LTS (using libstdc++)
    • GCC 8.x, 9.x
    • Clang 5.x, 6.x, 7.x, 8.x, 9.x
  • Ubuntu 18.04 LTS (using libstdc++)
    • GCC 10.x
    • Clang 10.x

代码质量

Language grade: C/C++

每个提交都使用Clang的静态分析器,GCC和Clang的消毒剂,clang-tidy和valgrind进行检查。 此外,我们使用LGTM扫描(安全)问题。 请注意,LGTM有时会产生误报,因此上述标志可能无法显示准确的等级。

代码覆盖率

Coverage

代码覆盖率是自动测量的,单元测试覆盖了100%的核心库代码(对于发行版)。 请注意,基础架构已部分损坏,因此上述标记可能不会显示100%。

版本控制

Release

根据语义版本进行发布。 不兼容的API更改仅允许在主要版本之间进行。 有关详细信息,请参阅更改日志。

谢谢

感谢所有为 PEGTL 和/或其开发做出直接贡献的人们。

C++ 的艺术(The Art of C++)

PEGTL是 The Art of C++(C++ 艺术) 的一部分。

联系

对于有关 PEGTL,成功或失败的故事以及任何其他类型的反馈的问题和建议,请随时在 GitHub 上发布问题或 PR,或通过 taocpp(at)icemx.net 与作者联系。

许可

PEGTL 是经过认证的开源软件。 它可以完全免费用于任何目的,包括商业目的。 它是根据此处转载的 MIT 许可的条款分发的。

(The first version translated by vz on 2020.07.22)

Overview

Name With Ownertaocpp/PEGTL
Primary LanguageC++
Program languageMakefile (Language Count: 3)
PlatformLinux, Mac, Windows
License:Boost Software License 1.0
Release Count43
Last Release Name3.2.7 (Posted on )
First Release Namepegtl-1.0.0 (Posted on )
Created At2014-12-23 15:29:50
Pushed At2024-05-07 18:58:49
Last Commit At
Stargazers Count1.9k
Watchers Count64
Fork Count228
Commits Count3.1k
Has Issues Enabled
Issues Count163
Issue Open Count1
Pull Requests Count86
Pull Requests Open Count1
Pull Requests Close Count29
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Welcome to the PEGTL

Release
Download
TravisCI
AppVeyor
Coverage
Language grade: C/C++

The Parsing Expression Grammar Template Library (PEGTL) is a zero-dependency C++ header-only parser combinator library for creating parsers according to a Parsing Expression Grammar (PEG).

Documentation

Introduction

Grammars are written as regular C++ code, created with template programming (not template meta programming), i.e. nested template instantiations that naturally correspond to the inductive definition of PEGs (and other parser-combinator approaches).

A comprehensive set of parser rules that can be combined and extended by the user is included, as are mechanisms for debugging grammars, and for attaching user-defined actions to grammar rules.
Here is an example of how a PEG grammar rule is implemented as C++ class with the PEGTL.

// PEG rule for integers consisting of a non-empty
// sequence of digits with an optional sign:

// sign ::= '+' / '-'
// integer ::= sign? digit+

// The same parsing rule implemented with the PEGTL:

using namespace tao::pegtl;

struct sign : one< '+', '-' > {};
struct integer : seq< opt< sign >, plus< digit > > {};

PEGs are superficially similar to Context-Free Grammars (CFGs), however the more deterministic nature of PEGs gives rise to some very important differences.
The included grammar analysis finds several typical errors in PEGs, including left recursion.

Design

The PEGTL is designed to be "lean and mean", the core library consists of approximately 6000 lines of code.
Emphasis is on simplicity and efficiency, preferring a well-tuned simple approach over complicated optimisations.

The PEGTL is mostly concerned with parsing combinators and grammar rules, and with giving the user of the library (the possibility of) full control over all other aspects of a parsing run. Whether/which actions are taken, and whether/which data structures are created during a parsing run, is entirely up to the user.

Included are some examples for typical situation like unescaping escape sequences in strings, building a generic JSON data structure, and on-the-fly evaluation of arithmetic expressions.

Through the use of template programming and template specialisations it is possible to write a grammar once, and use it in multiple ways with different (semantic) actions in different (or the same) parsing runs.

With the PEG formalism, the separation into lexer and parser stages is usually dropped -- everything is done in a single grammar.
The rules are expressed in C++ as template instantiations, and it is the compiler's task to optimise PEGTL grammars.

Status

Each commit is automatically tested with multiple architectures, operating systems, compilers, and versions thereof.

  • Windows

    • Visual Studio 2017 (x86, x64)
    • Visual Studio 2019 (x86, x64)
  • macOS (using libc++)

    • macOS 10.13, Xcode 9.4
    • macOS 10.14, Xcode 10.3
    • macOS 10.14, Xcode 11.2
  • Ubuntu 16.04 LTS (using libstdc++)

    • GCC 7.x, 8.x, 9.x
    • Clang 5.x, 6.x, 7.x, 8.x, 9.x

Additionally, each commit is checked with Clang's Static Analyzer, GCC's and Clang's sanitizers, clang-tidy, and valgrind.
Code coverage is automatically measured and the unit tests cover 100% of the core library code (for releases).

Releases are done in accordance with Semantic Versioning.
Incompatible API changes are only allowed to occur between major versions.
For details see the changelog.

Thank You

In appreciation of all contributions here are the people that have directly contributed to the PEGTL and/or its development.
































The Art of C++

The PEGTL is part of The Art of C++.



Contact

For questions and suggestions regarding the PEGTL, success or failure stories, and any other kind of feedback, please feel free to open an issue or a PR on GitHub or contact the authors at taocpp(at)icemx.net.

License

The PEGTL is certified Open Source software. It may be used for any purpose, including commercial purposes, at absolutely no cost. It is distributed under the terms of the MIT license reproduced here.

Copyright (c) 2007-2020 Dr. Colin Hirsch and Daniel Frey

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

To the top