vanderlee/comprehend

Framework for building BNF LR(1) parsers

1.0.2 2018-10-02 22:17 UTC

This package is auto-updated.

Last update: 2024-10-11 11:36:41 UTC


README

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

  • Closely follows BNF syntax using objects as operands.
  • Includes various pre-defined RFC syntax rules.
  • Whitespace skipping.
  • Support for tokenizing.
  • Add your own custom parsers.
  • Create full sets of rules.
  • Optional case (in)sensitivity.

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])),
]);