All Projects → TheLartians → Pegparser

TheLartians / Pegparser

Licence: bsd-3-clause
💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.

Programming Languages

cpp
1120 projects
dsl
153 projects
grammar
57 projects

Projects that are alternatives of or similar to Pegparser

Pigeon
Command pigeon generates parsers in Go from a PEG grammar.
Stars: ✭ 603 (+267.68%)
Mutual labels:  parser-generator, peg, parser
Npeg
PEGs for Nim, another take
Stars: ✭ 163 (-0.61%)
Mutual labels:  parser-generator, peg, parser
String Calc
PHP calculator library for mathematical terms (expressions) passed as strings
Stars: ✭ 60 (-63.41%)
Mutual labels:  parser, calculator
Parser
Generate a JSON documentation for a SFC Vue component. Contribute: https://gitlab.com/vuedoc/parser#contribute
Stars: ✭ 74 (-54.88%)
Mutual labels:  parser, parse
Antlr4
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
Stars: ✭ 11,227 (+6745.73%)
Mutual labels:  parser-generator, parse
Foundatio.parsers
A lucene style query parser that is extensible and allows modifying the query.
Stars: ✭ 39 (-76.22%)
Mutual labels:  peg, parse
Lug
Parsing expression grammar (PEG) embedded domain specific language and parsing machine for C++17
Stars: ✭ 44 (-73.17%)
Mutual labels:  parser-generator, peg
Postcss Less
PostCSS Syntax for parsing LESS
Stars: ✭ 93 (-43.29%)
Mutual labels:  parser, parse
Parse Code Context
Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype properties, prototype methods etc.
Stars: ✭ 7 (-95.73%)
Mutual labels:  parser, parse
Csly
a C# embeddable lexer and parser generator (.Net core)
Stars: ✭ 129 (-21.34%)
Mutual labels:  parser-generator, parser
Php Zephir Parser
The Zephir Parser delivered as a C extension for the PHP language.
Stars: ✭ 129 (-21.34%)
Mutual labels:  parser-generator, parser
Json Autotype
Automatic Haskell type inference from JSON input
Stars: ✭ 139 (-15.24%)
Mutual labels:  parser, parse
Sharpmath
A small .NET math library.
Stars: ✭ 36 (-78.05%)
Mutual labels:  parser, calculator
Algebra Latex
Parse and calculate latex formatted math
Stars: ✭ 20 (-87.8%)
Mutual labels:  parser, parse
Html React Parser
📝 HTML to React parser.
Stars: ✭ 846 (+415.85%)
Mutual labels:  parser, parse
Ts Pegjs
Plugin for pegjs to generate TypeScript parsers.
Stars: ✭ 76 (-53.66%)
Mutual labels:  parser-generator, peg
Genieparser
sub-component of Genie that parse the device output into structured datastructure
Stars: ✭ 146 (-10.98%)
Mutual labels:  parser, parse
Ccalc
Scientific calculator in which you can define new constants and functions
Stars: ✭ 19 (-88.41%)
Mutual labels:  parser, calculator
Librini
Rini is a tiny, non-libc dependant, .ini file parser programmed from scratch in C99.
Stars: ✭ 25 (-84.76%)
Mutual labels:  parser, parse
Netcopa
Network Configuration Parser
Stars: ✭ 112 (-31.71%)
Mutual labels:  parser, parse

Actions Status Actions Status Actions Status Actions Status codecov

PEGParser

A linear-time C++17 PEG parser generator supporting memoization, left-recursion and context-dependent grammars.

Example

The following defines a simple calculator program. It is able to parse and evaluate the basic operations +, -, *, / while obeying operator and bracket precedence and ignoring whitespace characters between tokens.

#include <peg_parser/generator.h>
#include <iostream>

void example() {
  peg_parser::ParserGenerator<float> g;

  // Define grammar and evaluation rules
  g.setSeparator(g["Whitespace"] << "[\t ]");
  g["Sum"     ] << "Add | Subtract | Product";
  g["Product" ] << "Multiply | Divide | Atomic";
  g["Atomic"  ] << "Number | '(' Sum ')'";
  g["Add"     ] << "Sum '+' Product"    >> [](auto e){ return e[0].evaluate() + e[1].evaluate(); };
  g["Subtract"] << "Sum '-' Product"    >> [](auto e){ return e[0].evaluate() - e[1].evaluate(); };
  g["Multiply"] << "Product '*' Atomic" >> [](auto e){ return e[0].evaluate() * e[1].evaluate(); };
  g["Divide"  ] << "Product '/' Atomic" >> [](auto e){ return e[0].evaluate() / e[1].evaluate(); };
  g["Number"  ] << "'-'? [0-9]+ ('.' [0-9]+)?" >> [](auto e){ return stof(e.string()); };
  g.setStart(g["Sum"]);

  // Execute a string
  auto input = "1 + 2 * (3+4)/2 - 3";
  float result = g.run(input); // -> 5
  std::cout << input << " = " << result << std::endl;
}

Quickstart

PEGParser requires at least cmake 3.14 and the ability to compile C++17 code. The following shows how to compile and run the calculator example.

git clone https://github.com/TheLartians/PegParser
cd PegParser
cmake -Sexample -Bbuild/example
cmake --build build/example -j8
./build/example/calculator

You should familiarize yourself with the syntax of parsing expression grammars. The included examples should help you to get started.

Installation and usage

PEGParser can be easily added to your project through CPM.cmake.

CPMAddPackage(
  NAME PEGParser
  VERSION 2.1.1
  GITHUB_REPOSITORY TheLartians/PEGParser
)

target_link_libraries(myProject PEGParser::PEGParser)

Project goals

PEGParser is designed for ease-of-use and rapid prototyping of grammars with arbitrary complexity, and builds its parsers at run time. So far no work has been invested on optimizing the library, however it runs fast enough to be used in several production projects.

Time complexity

PEGParser uses memoization, resulting in linear time complexity (as a function of input string length) for grammars without left-recursion. Left-recursive grammars have squared time complexity in worst case. Memoization can also be disabled on a per-rule basis, reducing the memory footprint and allowing context-dependent rules.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].