Home

Awesome

Welcome to the PEGTL

Windows macOS Linux Android <br> clang-analyze clang-tidy Sanitizer CodeQL Codecov

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).

During development of a new major version the main branch can go through incompatible changes. For a stable experience please download the latest release rather than using the main branch.

Documentation

Contact

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

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 parsing expression 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.

Each commit is checked with the GCC and Clang sanitizers, Clang's Static Analyzer, and clang-tidy. Additionally, we use CodeQL to scan for (security) issues.

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.

Thank You

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

<img alt="amphaal" src="https://avatars.githubusercontent.com/u/13903151" width="120"> <img alt="anand-bala" src="https://avatars.githubusercontent.com/u/7420072" width="120"> <img alt="andoma" src="https://avatars.githubusercontent.com/u/216384" width="120"> <img alt="barbieri" src="https://avatars.githubusercontent.com/u/1838" width="120"> <img alt="bjoe" src="https://avatars.githubusercontent.com/u/727911" width="120"> <img alt="bwagner" src="https://avatars.githubusercontent.com/u/447049" width="120"> <img alt="cdiggins" src="https://avatars.githubusercontent.com/u/1759994" width="120"> <img alt="clausklein" src="https://avatars.githubusercontent.com/u/1833050" width="120"> <img alt="delpinux" src="https://avatars.githubusercontent.com/u/35096584" width="120"> <img alt="dkopecek" src="https://avatars.githubusercontent.com/u/1353140" width="120"> <img alt="gene-hightower" src="https://avatars.githubusercontent.com/u/3957811" width="120"> <img alt="irrequietus" src="https://avatars.githubusercontent.com/u/231192" width="120"> <img alt="jedelbo" src="https://avatars.githubusercontent.com/u/572755" width="120"> <img alt="joelfrederico" src="https://avatars.githubusercontent.com/u/458871" width="120"> <img alt="johelegp" src="https://avatars.githubusercontent.com/u/21071787" width="120"> <img alt="jovermann" src="https://avatars.githubusercontent.com/u/6087443" width="120"> <img alt="jubnzv" src="https://avatars.githubusercontent.com/u/12023585" width="120"> <img alt="kelvinhammond" src="https://avatars.githubusercontent.com/u/1824682" width="120"> <img alt="kneth" src="https://avatars.githubusercontent.com/u/1225363" width="120"> <img alt="kuzmas" src="https://avatars.githubusercontent.com/u/1858553" width="120"> <img alt="lambdafu" src="https://avatars.githubusercontent.com/u/1138455" width="120"> <img alt="lichray" src="https://avatars.githubusercontent.com/u/433009" width="120"> <img alt="michael-brade" src="https://avatars.githubusercontent.com/u/8768950" width="120"> <img alt="mkrupcale" src="https://avatars.githubusercontent.com/u/13936020" width="120"> <img alt="newproggie" src="https://avatars.githubusercontent.com/u/162319" width="120"> <img alt="obiwahn" src="https://avatars.githubusercontent.com/u/741109" width="120"> <img alt="ohanar" src="https://avatars.githubusercontent.com/u/1442822" width="120"> <img alt="pauloscustodio" src="https://avatars.githubusercontent.com/u/70773" width="120"> <img alt="pleroux0" src="https://avatars.githubusercontent.com/u/39619854" width="120"> <img alt="quadfault" src="https://avatars.githubusercontent.com/u/30195320" width="120"> <img alt="quarticcat" src="https://avatars.githubusercontent.com/u/70888415" width="120"> <img alt="ras0219" src="https://avatars.githubusercontent.com/u/533828" width="120"> <img alt="redmercury" src="https://avatars.githubusercontent.com/u/4424222" width="120"> <img alt="robertcampion" src="https://avatars.githubusercontent.com/u/4220569" width="120"> <img alt="samhocevar" src="https://avatars.githubusercontent.com/u/245089" width="120"> <img alt="sanssecours" src="https://avatars.githubusercontent.com/u/691989" width="120"> <img alt="sgbeal" src="https://avatars.githubusercontent.com/u/235303" width="120"> <img alt="skyrich62" src="https://avatars.githubusercontent.com/u/23705081" width="120"> <img alt="studoot" src="https://avatars.githubusercontent.com/u/799344" width="120"> <img alt="svenjo" src="https://avatars.githubusercontent.com/u/1538181" width="120"> <img alt="wickedmic" src="https://avatars.githubusercontent.com/u/12001183" width="120"> <img alt="wravery" src="https://avatars.githubusercontent.com/u/6502881" width="120"> <img alt="zhihaoy" src="https://avatars.githubusercontent.com/u/43971430" width="120">

The Art of C++

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

<img alt="colinh" src="https://avatars.githubusercontent.com/u/113184" width="120"> <img alt="d-frey" src="https://avatars.githubusercontent.com/u/3956325" width="120"> <img alt="uilianries" src="https://avatars.githubusercontent.com/u/4870173" width="120">

License

<a href="https://opensource.org/licenses/BSL-1.0"><img align="right" src="https://opensource.org/wp-content/uploads/2009/06/OSIApproved.svg" width="150" hspace="20" alt="Open Source Initiative"></a>

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

The PEGTL is certified Open Source software. It is licensed under the terms of the Boost Software License, Version 1.0 reproduced here.

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following:

The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor.

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, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.