vanderlee / Comprehend

PHP 5/7 framework for creating complex scanners, lexer, tokenizers and parsers based on BNF (and variant) formal syntax definitions.
MIT License
6 stars 2 forks source link

Comprehend - a PHP *BNF parser framework

Build object oriented LR(1) lexer, tokenizers and parsers in PHP using BNF-based syntax.

Packagist PHP from Packagist Packagist

Scrutinizer Code Quality Build Status Code Coverage Codacy Badge Travis (.org) Maintainability

Copyright © 2011-2024 Martijn W. van der Lee Toyls.com, MIT license applies.

Features

Example

ABNF

word    = [A-Za-z]+
list    = word *[ ',' word ]    

Comprehend, using objects:

$word   = new Repeat(new Regex('/[a-z][A-Z]/'), 1);
$list   = new Sequence($word, new Repeat(new Sequence(',', $word)));

Comprehend, using objects and array notation:

$word   = new Repeat(new Regex('/[a-z][A-Z]/'), 1);
$list   = new Sequence($word, new Repeat([',', $word]));

Comprehend, using library functions:

$word   = plus(regex('/[a-z][A-Z]/'));
$list   = s($word, star([',', $word]));

Comprehend, using Ruleset constructor

$list   = new Ruleset([
    'word'           => plus(regex('/[a-z][A-Z]/')), 
    Ruleset::ROOT => s($word, star([',', $word])),
]);