PHPackages                             ozhantr/ebnf - 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. ozhantr/ebnf

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

ozhantr/ebnf
============

Framework-agnostic PHP library for parsing, analyzing, and validating EBNF grammars.

v0.1.0(3mo ago)00MITPHPPHP ^8.2CI passing

Since Apr 6Pushed 3mo agoCompare

[ Source](https://github.com/ozhantr/ebnf)[ Packagist](https://packagist.org/packages/ozhantr/ebnf)[ Docs](https://github.com/ozhantr/ebnf)[ RSS](/packages/ozhantr-ebnf/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

EBNF
====

[](#ebnf)

Framework-agnostic PHP 8.2+ library for reading `.ebnf` grammar files, parsing EBNF syntax, analyzing grammar definitions, and validating input against those grammars.

What Is EBNF?
-------------

[](#what-is-ebnf)

`EBNF` stands for `Extended Backus-Naur Form`.

It is a formal notation used to describe the syntax of a language or mini-language. Developers often use EBNF to define things like:

- search query languages
- filter expressions
- calculator-like expressions
- config formats
- small domain-specific languages

What This Library Does
----------------------

[](#what-this-library-does)

This library helps you work with grammars defined in EBNF.

You can use it to:

- read `.ebnf` grammar files from disk
- parse those grammar definitions into a structured AST
- analyze grammars for semantic issues
- validate user input against a chosen grammar rule
- report syntax errors with line and column information

In practice, this is useful when you want to build features such as custom query inputs, textarea validation, editor tooling, or mini-DSL parsers in PHP.

Typical Use Cases
-----------------

[](#typical-use-cases)

You might use this library when you want to:

- validate a custom search or filter input
- parse a small expression language
- define and enforce a config file format
- build grammar-driven textarea or editor validation
- experiment with or debug EBNF grammars in PHP

EBNF Syntax Basics
------------------

[](#ebnf-syntax-basics)

This library currently supports a practical subset of EBNF syntax:

- `rule = expression ;`defines a grammar rule
- `a , b`means `a` followed by `b`
- `a | b`means `a` or `b`
- `[ a ]`makes `a` optional
- `{ a }`means `a` can repeat zero or more times
- `( a )`groups expressions
- `"text"`matches a literal string
- `/.../`matches a regex terminal
- `identifier`references another rule

See [docs/grammar-syntax.md](docs/grammar-syntax.md) for a fuller syntax guide.

Features
--------

[](#features)

- Strictly typed token system
- Line/column-aware lexer
- Recursive descent parser
- Immutable AST nodes
- Rich syntax exceptions
- Support for inline grammar strings and `.ebnf` grammar files
- Regex terminals such as `/^[a-z]+/`
- Grammar analyzer for duplicate, undefined, and unreachable rules
- Runtime validation with line/column-aware syntax errors
- JSON export and pretty-print helpers

Key Terms
---------

[](#key-terms)

- `Lexer`: breaks grammar text into small meaningful pieces called `tokens`, such as names, strings, and symbols.
- `Parser`: turns those tokens into a structured grammar model.
- `AST`: an Abstract Syntax Tree, the structured representation of a parsed grammar.

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

[](#installation)

```
composer require ozhantr/ebnf
```

Development Setup
-----------------

[](#development-setup)

If you want to work on the library itself:

```
git clone https://github.com/ozhantr/ebnf.git
cd ebnf
composer install
```

Quick Start
-----------

[](#quick-start)

Parse a grammar definition into an AST:

```
