PHPackages                             christophercastro/slr-phparser - 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. christophercastro/slr-phparser

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

christophercastro/slr-phparser
==============================

A very simple LR(1) parser generator written in PHP.

29PHP

Since May 11Pushed 11y agoCompare

[ Source](https://github.com/ChristopherCastro/slr-phparser)[ Packagist](https://packagist.org/packages/christophercastro/slr-phparser)[ RSS](/packages/christophercastro-slr-phparser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

SLR PHParser
============

[](#slr-phparser)

A very simple LR(0) and LR(1) parser generator written in PHP.

Example
-------

[](#example)

Simple calculator with operator precedence:

```
use Phparser\Grammar\Grammar;
use Phparser\Lexer\Lexer;
use Phparser\Parser\Parser;
use Phparser\Rule\LR1\RulesCollection;

/**
 * This will be used to hold the result of the calculator.
 */
$RESULT = 0;

/**
 * Calculator's language definition.
 */
$productions = new RulesCollection([
    'S -> exp_a' => function ($info) {
        global $RESULT;
        $info = $info[0];
        $RESULT = $info;
    },
    'exp_a -> exp_a T_PLS exp_a ' => function (&$info) {
       $info = $info[0] + $info[2];
    },
    'exp_a -> exp_a T_SUB exp_a' => function (&$info) {
       $info = $info[0] - $info[2];
    },
    'exp_a -> exp_a T_MUL exp_a' => function (&$info) {
        $info = $info[0] * $info[2];
    },
    'exp_a -> exp_a T_DIV exp_a' => function (&$info) {
        $info = $info[0] / $info[2];
    },
    'exp_a -> T_LP exp_a T_RP' => function (&$info) {
        $info = $info[1];
    },
    'exp_a -> T_SQRT T_LP exp_a T_RP' => function (&$info) {
        $info = sqrt($info[2]);
    },
    'exp_a -> T_NUM' => function (&$info) {
        $info = intval($info[0]->value());
    },
]);

/**
 * Tokens definition.
 */
$tokens = [
    '\*' => 'T_MUL',
    '\/' => 'T_DIV',
    '\+' => 'T_PLS',
    '\-' => 'T_SUB',
    'sqrt' => 'T_SQRT',
    '[0-9]+' => 'T_NUM',
    '\(' => 'T_LP',
    '\)' => 'T_RP',
    '\s+|\n+|\t+' => '',
];

/**
 * Startup the parser.
 */
$lexer = new Lexer($tokens);
$grammar = new Grammar($productions);
$parser = new Parser($lexer, $grammar);

/**
 * Configure tokens precedence.
 */
$grammar->prec('T_MUL', 4);
$grammar->prec('T_DIV', 3);
$grammar->prec('T_SUB', 2);
$grammar->prec('T_PLS', 1);

/**
 * The expression to parse.
 */
$exp = '50 + 20 * 3 / 2 - 1';

/**
 * Run the parser.
 */
$parser->run($exp);

/**
 * Draw parsing table.
 */
require 'tests/config/drawer.php';
echo "Parsing table:";
echo draw_table($grammar->transitionTable()->toArray());
```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.9% 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/31416f909ed957d5ec1df38578ee5d8e8fc6b6dca85889c2321999f39fa5d7bf?d=identicon)[ChristopherCastro](/maintainers/ChristopherCastro)

---

Top Contributors

[![quickapps](https://avatars.githubusercontent.com/u/1129842?v=4)](https://github.com/quickapps "quickapps (13 commits)")[![botchris](https://avatars.githubusercontent.com/u/749463?v=4)](https://github.com/botchris "botchris (1 commits)")

### Embed Badge

![Health badge](/badges/christophercastro-slr-phparser/health.svg)

```
[![Health](https://phpackages.com/badges/christophercastro-slr-phparser/health.svg)](https://phpackages.com/packages/christophercastro-slr-phparser)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

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