ozhantr/ebnf
Composer 安装命令:
composer require ozhantr/ebnf
包简介
Framework-agnostic PHP library for parsing, analyzing, and validating EBNF grammars.
README 文档
README
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?
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
This library helps you work with grammars defined in EBNF.
You can use it to:
- read
.ebnfgrammar 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
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
This library currently supports a practical subset of EBNF syntax:
rule = expression ;defines a grammar rulea , bmeansafollowed byba | bmeansaorb[ a ]makesaoptional{ a }meansacan repeat zero or more times( a )groups expressions"text"matches a literal string/.../matches a regex terminalidentifierreferences another rule
See docs/grammar-syntax.md for a fuller syntax guide.
Features
- Strictly typed token system
- Line/column-aware lexer
- Recursive descent parser
- Immutable AST nodes
- Rich syntax exceptions
- Support for inline grammar strings and
.ebnfgrammar 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
Lexer: breaks grammar text into small meaningful pieces calledtokens, 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
composer require ozhantr/ebnf
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
Parse a grammar definition into an AST:
<?php declare(strict_types=1); use Ozhantr\Ebnf\Lexer\Lexer; use Ozhantr\Ebnf\Parser\Parser; $grammar = 'digit = /^[0-9]/ ; number = digit , { digit } ;'; $tokens = (new Lexer($grammar))->tokenize(); $ast = (new Parser($tokens))->parse();
Regex terminals are also supported:
$grammar = 'identifier = /^[a-z_][a-z0-9_]*/ ;';
You can also load a .ebnf file from disk and parse it:
<?php declare(strict_types=1); use Ozhantr\Ebnf\Lexer\Lexer; use Ozhantr\Ebnf\Parser\Parser; $grammar = file_get_contents('path/to/grammar.ebnf'); if ($grammar === false) { throw new RuntimeException('Could not read grammar file.'); } $ast = (new Parser((new Lexer($grammar))->tokenize()))->parse();
Example grammar files in this repository:
Validate real input against a start rule:
<?php declare(strict_types=1); use Ozhantr\Ebnf\Lexer\Lexer; use Ozhantr\Ebnf\Parser\Parser; use Ozhantr\Ebnf\Runtime\GrammarRuntime; $grammar = 'number = /^[0-9]+/ ;'; $ast = (new Parser((new Lexer($grammar))->tokenize()))->parse(); $runtime = new GrammarRuntime($ast); $result = $runtime->match('number', '12345');
Analyze a grammar for semantic issues:
<?php declare(strict_types=1); use Ozhantr\Ebnf\Analyzer\GrammarAnalyzer; use Ozhantr\Ebnf\Lexer\Lexer; use Ozhantr\Ebnf\Parser\Parser; $grammar = 'start = missing ;'; $ast = (new Parser((new Lexer($grammar))->tokenize()))->parse(); $analysis = (new GrammarAnalyzer())->analyze($ast);
End-to-End Example
Imagine that you want to validate a custom search box in your application.
- Define the search syntax in an EBNF grammar file such as examples/grammars/task-search.ebnf.
- Parse that grammar into an AST.
- Use the runtime layer to validate real user input against a start rule.
For example, this input is valid for the search_request rule:
status:open priority:high "payment bug"
And this input is invalid:
status:open
priority:
The runtime validator can report that failure with line and column information, which makes it suitable for textarea validation, editor tooling, or custom query inputs.
Architecture
The library is organized into separate grammar parsing, runtime validation, analyzer, and export layers. See docs/architecture.md for the current design.
Examples Guide
For a short guide to each script in examples/, see docs/examples.md.
Running Examples
Install dependencies first if you are working from the repository source:
composer install php examples/basic.php
See examples/basic.php for the runnable example.
Additional examples:
php examples/parse-file.php php examples/parse-file.php examples/grammars/calculator.ebnf php examples/pretty-print.php php examples/runtime-validate.php php examples/runtime-validate.php 'status:open priority:high "payment bug"' php examples/runtime-validate.php $'status:open\npriority:' php examples/analyze-grammar.php php examples/showcase.php
Current Scope
Supported today:
- Rule definitions, choices, sequences, optionals, repetitions, and grouping
- String and regex terminals
- Grammar AST export and pretty-printing
- Runtime validation against a chosen start rule
- Semantic grammar analysis
Not yet supported:
- Native EBNF comments such as
(* ... *) - Match tree generation
- Advanced ambiguity or recursion analysis
ozhantr/ebnf 适用场景与选型建议
ozhantr/ebnf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「DSL」 「ast」 「lexer」 「grammar」 「ebnf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ozhantr/ebnf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ozhantr/ebnf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ozhantr/ebnf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Editable JSON AST with visitor traversal and formatting-preserving printing.
Feature complete, object oriented, composable, extendable Elasticsearch query DSL builder for PHP.
Parse Handlebars templates to a spec-compliant AST with PHP.
Elasticsearch DSL library
An MT940 bank statement parser for PHP
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 43
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-06