PHPackages                             hoa/compiler - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. hoa/compiler

AbandonedArchivedLibrary[Testing &amp; Quality](/categories/testing)

hoa/compiler
============

The Hoa\\Compiler library.

3.17.08.08(8y ago)45116.0M—1%51[24 issues](https://github.com/hoaproject/Compiler/issues)[7 PRs](https://github.com/hoaproject/Compiler/pulls)18BSD-3-ClausePHP

Since Sep 16Pushed 5y ago8 watchersCompare

[ Source](https://github.com/hoaproject/Compiler)[ Packagist](https://packagist.org/packages/hoa/compiler)[ Docs](https://hoa-project.net/)[ RSS](/packages/hoa-compiler/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (20)Used By (18)

 [![Hoa](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)

---

 [![Build status](https://camo.githubusercontent.com/41888ee3739d0adce45351ec9e47cd3b9006bd4780bfffd89d53edc9db4f1b83/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f686f6170726f6a6563742f436f6d70696c65722f6d61737465722e737667)](https://travis-ci.org/hoaproject/Compiler) [![Code coverage](https://camo.githubusercontent.com/6838c369f8f5ff75cee2221e91aa356375811cfc0cebbbbc545ff9b9d058d623/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f686f6170726f6a6563742f436f6d70696c65722f6d61737465722e737667)](https://coveralls.io/github/hoaproject/Compiler?branch=master) [![Packagist](https://camo.githubusercontent.com/fcf87e1acabe61d182617fa89a7d29c318ef2d3b38a87954a4671470bcafe747/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f612f636f6d70696c65722e737667)](https://packagist.org/packages/hoa/compiler) [![License](https://camo.githubusercontent.com/d8d82fce29f83dd464f80f6de314aabdd711cda2f6ebd188dc46f2ca2ad5814b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f686f612f636f6d70696c65722e737667)](https://hoa-project.net/LICENSE)

 Hoa is a **modular**, **extensible** and **structured** set of PHP libraries.
 Moreover, Hoa aims at being a bridge between industrial and research worlds.

Hoa\\Compiler
=============

[](#hoacompiler)

[![Help on IRC](https://camo.githubusercontent.com/4dbc9c9d28c30cf1ab591f4bb8212fe4dbddc734145df532a9bb86b09878d4c6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d253233686f6170726f6a6563742d6666303036362e737667)](https://webchat.freenode.net/?channels=#hoaproject)[![Help on Gitter](https://camo.githubusercontent.com/8c4c85951788ff606b1268cb3dd946be05e3054795455d0a7b9250711bc2ac05/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d6769747465722d6666303036362e737667)](https://gitter.im/hoaproject/central)[![Documentation](https://camo.githubusercontent.com/7059ad5f1a363f9098686c59d432f01d7330aed9d4b6c8111d985fd64cfc6c60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63756d656e746174696f6e2d6861636b5f626f6f6b2d6666303036362e737667)](https://central.hoa-project.net/Documentation/Library/Compiler)[![Board](https://camo.githubusercontent.com/fd81654ba14b3aca3a713e1b471bc3fc3ba7b5bb3761ccffd6eea2e2ed1fa5ca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6f7267616e69736174696f6e2d626f6172642d6666303036362e737667)](https://waffle.io/hoaproject/compiler)

This library allows to manipulate LL(1) and LL(k) compiler compilers. A dedicated grammar description language is provided for the last one: the PP language.

[Learn more](https://central.hoa-project.net/Documentation/Library/Compiler).

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

[](#installation)

With [Composer](https://getcomposer.org/), to include this library into your dependencies, you need to require [`hoa/compiler`](https://packagist.org/packages/hoa/compiler):

```
$ composer require hoa/compiler '~3.0'
```

For more installation procedures, please read [the Source page](https://hoa-project.net/Source.html).

Testing
-------

[](#testing)

Before running the test suites, the development dependencies must be installed:

```
$ composer install
```

Then, to run all the test suites:

```
$ vendor/bin/hoa test:run
```

For more information, please read the [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html).

Quick usage
-----------

[](#quick-usage)

As a quick overview, we will look at the PP language and the LL(k) compiler compiler.

### The PP language

[](#the-pp-language)

A grammar is constituted by tokens (the units of a word) and rules (please, see the documentation for an introduction to the language theory). The PP language declares tokens with the following construction:

```
%token [source_namespace:]name value [-> destination_namespace]

```

The default namespace is `default`. The value of a token is represented by a [PCRE](http://pcre.org/). We can skip tokens with the `%skip` construction.

As an example, we will take the *simplified* grammar of the [JSON language](http://json.org/). The complete grammar is in the `hoa://Library/Json/Grammar.pp` file. Thus:

```
%skip   space          \s
// Scalars.
%token  true           true
%token  false          false
%token  null           null
// Strings.
%token  quote_         "        -> string
%token  string:string  [^"]+
%token  string:_quote  "        -> default
// Objects.
%token  brace_         {
%token _brace          }
// Arrays.
%token  bracket_       \[
%token _bracket        \]
// Rest.
%token  colon          :
%token  comma          ,
%token  number         \d+

value:
     |  |  | string() | object() | array() | number()

string:
    ::quote_::  ::_quote::

number:

#object:
    ::brace_:: pair() ( ::comma:: pair() )* ::_brace::

#pair:
    string() ::colon:: value()

#array:
    ::bracket_:: value() ( ::comma:: value() )* ::_bracket::

```

We can see the PP constructions:

- `rule()` to call a rule;
- `` and `::token::` to declare a token;
- `|` for a disjunction;
- `(…)` to group multiple declarations;
- `e?` to say that `e` is optional;
- `e+` to say that `e` can appear at least 1 time;
- `e*` to say that `e` can appear 0 or many times;
- `e{x,y}` to say that `e` can appear between `x` and `y` times;
- `#node` to create a node the AST (resulting tree);
- `token[i]` to unify tokens value between them.

Unification is very useful. For example, if we have a token that expresses a quote (simple or double), we could have:

```
%token  quote   "|'
%token  handle  \w+

string:
    ::quote::  ::quote::

```

So, the data `"foo"` and `'foo'` will be valid, but also `"foo'` and `'foo"`! To avoid this, we can add a new constraint on token value by unifying them, thus:

```
string:
    ::quote[0]::  ::quote[0]::

```

All `quote[0]` for the rule instance must have the same value. Another example is the unification of XML tags name.

### LL(k) compiler compiler

[](#llk-compiler-compiler)

The `Hoa\Compiler\Llk\Llk` class provide helpers to manipulate (load or save) a compiler. The following code will use the previous grammar to create a compiler, and we will parse a JSON string. If the parsing succeed, it will produce an AST (stands for Abstract Syntax Tree) we can visit, for example to dump the AST:

```
// 1. Load grammar.
$compiler = Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp'));

// 2. Parse a data.
$ast = $compiler->parse('{"foo": true, "bar": [null, 42]}');

// 3. Dump the AST.
$dump = new Hoa\Compiler\Visitor\Dump();
echo $dump->visit($ast);

/**
 * Will output:
 *     >  #object
 *     >  >  #pair
 *     >  >  >  token(string, foo)
 *     >  >  >  token(true, true)
 *     >  >  #pair
 *     >  >  >  token(string, bar)
 *     >  >  >  #array
 *     >  >  >  >  token(null, null)
 *     >  >  >  >  token(number, 42)
 */
```

Pretty simple.

### Compiler in CLI

[](#compiler-in-cli)

This library proposes a script to parse and apply a visitor on a data with a specific grammar. Very useful. Moreover, we can use pipe (because `Hoa\File\Read` —please, see the [`Hoa\File`library](http://central.hoa-project.net/Resource/Library/File/)— supports `0` as `stdin`), thus:

```
$ echo '[1, [1, [2, 3], 5], 8]' | hoa compiler:pp Json.pp 0 --visitor dump
>  #array
>  >  token(number, 1)
>  >  #array
>  >  >  token(number, 1)
>  >  >  #array
>  >  >  >  token(number, 2)
>  >  >  >  token(number, 3)
>  >  >  token(number, 5)
>  >  token(number, 8)
```

You can apply any visitor classes.

### Errors

[](#errors)

Errors are well-presented:

```
$ echo '{"foo" true}' | hoa compiler:pp Json.pp 0 --visitor dump
Uncaught exception (Hoa\Compiler\Exception\UnexpectedToken):
Hoa\Compiler\Llk\Parser::parse(): (0) Unexpected token "true" (true) at line 1
and column 8:
{"foo" true}
       ↑
in hoa://Library/Compiler/Llk/Parser.php at line 1
```

### Samplers

[](#samplers)

Some algorithms are available to generate data based on a grammar. We will give only one example with the coverage-based generation algorithm that will activate all branches and tokens in the grammar:

```
$sampler = new Hoa\Compiler\Llk\Sampler\Coverage(
    // Grammar.
    Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp')),
    // Token sampler.
    new Hoa\Regex\Visitor\Isotropic(new Hoa\Math\Sampler\Random())
);

foreach ($sampler as $i => $data) {
    echo $i, ' => ', $data, "\n";
}

/**
 * Will output:
 *     0 => true
 *     1 => {" )o?bz " : null , " %3W) " : [false, 130    , " 6"   ]  }
 *     2 => [{" ny  " : true } ]
 *     3 => {" Ne;[3 " :[ true , true ] , " th: " : true," C[8} " :   true }
 */
```

Research papers
---------------

[](#research-papers)

- *Grammar-Based Testing using Realistic Domains in PHP*, presented at [A-MOST 2012](https://sites.google.com/site/amost2012/) (Montréal, Canada) ([article](https://hoa-project.net/En/Literature/Research/Amost12.pdf), [presentation](http://keynote.hoa-project.net/Amost12/EDGB12.pdf), [details](https://hoa-project.net/En/Event/Amost12.html)).

Documentation
-------------

[](#documentation)

The [hack book of `Hoa\Compiler`](https://central.hoa-project.net/Documentation/Library/Compiler) contains detailed information about how to use this library and how it works.

To generate the documentation locally, execute the following commands:

```
$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open
```

More documentation can be found on the project's website: [hoa-project.net](https://hoa-project.net/).

Getting help
------------

[](#getting-help)

There are mainly two ways to get help:

- On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)IRC channel,
- On the forum at [users.hoa-project.net](https://users.hoa-project.net).

Contribution
------------

[](#contribution)

Do you want to contribute? Thanks! A detailed [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains everything you need to know.

License
-------

[](#license)

Hoa is under the New BSD License (BSD-3-Clause). Please, see [`LICENSE`](https://hoa-project.net/LICENSE) for details.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity68

Solid adoption and visibility

Community35

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 95.1% 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 ~66 days

Recently: every ~142 days

Total

17

Last Release

3206d ago

Major Versions

1.14.09.16 → 2.14.09.172014-09-17

2.15.10.29 → 3.16.01.112016-01-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/a410060743cec0e0d8654661b726a7a25f5888f18059d4c942a8fd9a8128f0da?d=identicon)[Hoa](/maintainers/Hoa)

---

Top Contributors

[![Hywan](https://avatars.githubusercontent.com/u/946104?v=4)](https://github.com/Hywan "Hywan (366 commits)")[![SerafimArts](https://avatars.githubusercontent.com/u/2461257?v=4)](https://github.com/SerafimArts "SerafimArts (7 commits)")[![vonglasow](https://avatars.githubusercontent.com/u/1275202?v=4)](https://github.com/vonglasow "vonglasow (3 commits)")[![Metalaka](https://avatars.githubusercontent.com/u/5406767?v=4)](https://github.com/Metalaka "Metalaka (2 commits)")[![jubianchi](https://avatars.githubusercontent.com/u/327237?v=4)](https://github.com/jubianchi "jubianchi (2 commits)")[![lovenunu](https://avatars.githubusercontent.com/u/6106094?v=4)](https://github.com/lovenunu "lovenunu (1 commits)")[![shulard](https://avatars.githubusercontent.com/u/482993?v=4)](https://github.com/shulard "shulard (1 commits)")[![stephpy](https://avatars.githubusercontent.com/u/232744?v=4)](https://github.com/stephpy "stephpy (1 commits)")[![jwage](https://avatars.githubusercontent.com/u/97422?v=4)](https://github.com/jwage "jwage (1 commits)")[![lyrixx](https://avatars.githubusercontent.com/u/408368?v=4)](https://github.com/lyrixx "lyrixx (1 commits)")

---

Tags

compilergrammargrammar-based-testinghoalibraryparserphprandomlanguagecoverageparserlibrarylexertokenregularsyntaxcompilersamplertraceastgrammaralgebraiccontext-freeruleppisotropicuniformexhaustivell1llk

### Embed Badge

![Health badge](/badges/hoa-compiler/health.svg)

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

###  Alternatives

[unionofrad/li3_quality

This li₃ plugin adds code quality assurance to your toolbelt.

1288.2k](/packages/unionofrad-li3-quality)[yoeunes/regex-parser

A powerful PCRE regex parser with lexer, AST builder, validation, ReDoS analysis, and syntax highlighting. Zero dependencies, blazing fast, and production-ready.

2946.4k5](/packages/yoeunes-regex-parser)

PHPackages © 2026

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