PHPackages                             alamirault/grammar - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. alamirault/grammar

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alamirault/grammar
==================

00PHP

Since Apr 24Pushed 7y ago1 watchersCompare

[ Source](https://github.com/alamirault/grammar)[ Packagist](https://packagist.org/packages/alamirault/grammar)[ RSS](/packages/alamirault-grammar/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Overview
========

[](#overview)

This package allows you to write Grammar in PHP OOP.

Installation
============

[](#installation)

Install PHP grammar parser using [Composer](https://getcomposer.org/download/):

```
composer require alamirault/grammar
```

Or add the package to your `composer.json`

```
require: {
    // ...
    "alamirault/grammar": "~1.0" // check packagist.org for more tags
    // ...
}
```

Usage
=====

[](#usage)

Simple example
--------------

[](#simple-example)

```
$floatMarker = new OrOperator(
    new Constant(","),
    new Constant(".")
);

$digit = new Pattern("[0-9]");
$digits = new Repeat($digit);

$float = new AndOperator($digits, $floatMarker, $digits);

$grammar = new Grammar('Float', [
    new Definition("Float", $float),
]);

dump($grammar->parse("12.34"));

Alamirault\Grammar\Result {#11
  -value: "12.34"
  -length: 5
  -offset: 0
  -match: true
}
```

More complex example
--------------------

[](#more-complex-example)

```
$oneSpace = new Constant(' ');
$spaces = new Repeat($oneSpace);

$action = new OrOperator(
    new Constant("ALLOW"),
    new Constant("REFUSE")
);

$if = new Constant("if");

$userType = new Constant("userType");

$userTypeValues = new OrOperator(
    new Constant("WRITER"),
    new Constant("ACTOR")
);

$leftParenthesis = new Constant('(');
$rightParenthesis = new Constant(')');

$comma = new Constant(',');
$arrayElements = new AndOperator(
    new Repeat(
        new OrOperator(
            $spaces,
            new AndOperator($userTypeValues, $comma)
        )
    ),
    $userTypeValues
);

$in = new OrOperator(
    new Constant('in'),
    new Constant('IN')
);

$rule = new AndOperator($action, $spaces, $if, $spaces, $userType, $spaces, $in, $spaces, $leftParenthesis, $arrayElements, $rightParenthesis);

$grammar = new Grammar('Rule1', [
    new Definition("Rule1", $rule),
]);

dump($grammar->parse("ALLOW if userType in (WRITER, ACTOR)")); will return

Alamirault\Grammar\Result {#26
  -value: "ALLOW if userType in (WRITER, ACTOR)"
  -length: 36
  -offset: 0
  -match: true
}
```

Extending package with your own types: example of list
------------------------------------------------------

[](#extending-package-with-your-own-types-example-of-list)

Running the test suits
======================

[](#running-the-test-suits)

```
./vendor/bin/phpunit
```

License
=======

[](#license)

The project is released under the BSD-3 Clause license. For the full copyright and license information, please view the LICENSE file that was distributed with this source code.

Contributing
============

[](#contributing)

If you want to contribute to a project and make it better, your help is very welcome.

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8048aedac5cda1f5f612b044fff91e8b360539466379d12ab867c87d6e1e9584?d=identicon)[al37350](/maintainers/al37350)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/alamirault-grammar/health.svg)

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

PHPackages © 2026

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