PHPackages                             tacoberu/bnf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. tacoberu/bnf

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

tacoberu/bnf
============

Framework for building BNF like parsers.

v1.2.1(1y ago)53.0k↑133.3%11MITPHPPHP &gt;=5.3.3

Since Jun 22Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tacoberu/bnf-php)[ Packagist](https://packagist.org/packages/tacoberu/bnf)[ RSS](/packages/tacoberu-bnf/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (12)Used By (1)

A PHP BNF like parser
=====================

[](#a-php-bnf-like-parser)

[![Latest Stable Version](https://camo.githubusercontent.com/34b998b654f206bafecb75d3852deea341adca9b1480e6381ba087d2a7c4a2cd/687474703a2f2f706f7365722e707567782e6f72672f7461636f626572752f626e662f76)](https://packagist.org/packages/tacoberu/bnf)[![Latest Unstable Version](https://camo.githubusercontent.com/990b8fc94e972bf83325bcefcc521154478e26b2c92df8e43998f8ad701393e6/687474703a2f2f706f7365722e707567782e6f72672f7461636f626572752f626e662f762f756e737461626c65)](https://packagist.org/packages/tacoberu/bnf)[![PHP Version Require](https://camo.githubusercontent.com/278a927347851641cdb19799ac058e43605bdff8cc9ed3fe29cb892a76f49958/687474703a2f2f706f7365722e707567782e6f72672f7461636f626572752f626e662f726571756972652f706870)](https://packagist.org/packages/tacoberu/bnf)[![License](https://camo.githubusercontent.com/3544f83758c27508b4b3c61a0022b39f76a4733cd90fa3eb64354818a904e356/687474703a2f2f706f7365722e707567782e6f72672f7461636f626572752f626e662f6c6963656e7365)](https://packagist.org/packages/tacoberu/bnf)

This is a PHP implementation of BNF like parser.

Installation
------------

[](#installation)

The recommended way to install is via Composer:

```
    composer require tacoberu/bnf

```

Usage
-----

[](#usage)

```
require __dir__ . '/vendor/autoload.php';

use Taco\BNF\Parser;
use Taco\BNF\Combinators\Pattern;
use Taco\BNF\Combinators\Whitechars;

$parser = new Parser([
	new Whitechars(Null, False),
	new Pattern('element', ['~[^\n]+~']),
]);
$tree = $parser->parse('
-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
');

print_r($tree); /*

array (
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Pattern (...)
		[content] => "-brand-name = Foo 3000"
		[start] => 1
		[end] => 23
	)
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Pattern (...)
		[content] => "welcome = Welcome, {$name}, to {-brand-name}!"
		[start] => 24
		[end] => 69
	)
)

*/
```

or more complex:

```
require __dir__ . '/vendor/autoload.php';

use Taco\BNF\Parser;
use Taco\BNF\Combinators\Pattern;
use Taco\BNF\Combinators\Whitechars;

$parser = new Parser([
	new Whitechars(Null, False),
	new Sequence('element', [
		new Pattern('id', ['~[a-z\-]+~']),
		new Whitechars(Null, False),
		new Match(Null, ['='], False),
		new Whitechars(Null, False),
		new Pattern('element', ['~[^\n]+~']),
	]),
]);
$tree = $parser->parse('
-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
');

print_r($tree); /*

array (
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Sequence (...)
		[content] => array(
			[0] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "-brand-name"
				[start] => 1
				[end] => 12
			)
			[1] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "Foo 3000"
				[start] => 15
				[end] => 23
			)
		)
		[start] => 1
		[end] => 23
	)
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Pattern (...)
		[content] => array(
			[0] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "welcome"
				[start] => 24
				[end] => 31
			)
			[1] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "Welcome, {$name}, to {-brand-name}!"
				[start] => 34
				[end] => 69
			)
		)
		[start] => 24
		[end] => 69
	)
)

*/
```

See more examples in 'tests/ExhibitionTest.php'.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance45

Moderate activity, may be stable

Popularity26

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~172 days

Total

11

Last Release

432d ago

PHP version history (2 changes)v1.0.1PHP &gt;=5.3.3

v1.1.2PHP &gt;=5.3.3 &lt;8.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d9535bc57ab9e7c844b76be57d024b97eb84e9f8a9d308537afffe9cf81c409?d=identicon)[tacoberu](/maintainers/tacoberu)

---

Top Contributors

[![tacoberu](https://avatars.githubusercontent.com/u/1828339?v=4)](https://github.com/tacoberu "tacoberu (29 commits)")

---

Tags

bnfbnf-parserparserparser-combinatorsparserlexertokenizerastbnfabnfebnf

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tacoberu-bnf/health.svg)

```
[![Health](https://phpackages.com/badges/tacoberu-bnf/health.svg)](https://phpackages.com/packages/tacoberu-bnf)
```

###  Alternatives

[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[cerbero/json-parser

Zero-dependencies pull parser to read large JSON from any source in a memory-efficient way.

803474.6k5](/packages/cerbero-json-parser)[jakubledl/dissect

Lexing and parsing in pure PHP

2244.6M11](/packages/jakubledl-dissect)[creof/geo-parser

Parser for geography coordinate strings

624.4M15](/packages/creof-geo-parser)[creof/wkt-parser

Parser for well-known text (WKT) object strings

554.8M16](/packages/creof-wkt-parser)[nicoswd/php-rule-parser

Rule Engine - Rule Parser &amp; Evaluator

13078.6k7](/packages/nicoswd-php-rule-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
