PHPackages                             mrsuh/php-bison-skeleton - 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. mrsuh/php-bison-skeleton

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

mrsuh/php-bison-skeleton
========================

PHP skeleton for Bison

1.2.0(3y ago)3911.0k↑750%21GPL-3.0-onlyM4PHP &gt;=7.4

Since Mar 8Pushed 1y ago3 watchersCompare

[ Source](https://github.com/mrsuh/php-bison-skeleton)[ Packagist](https://packagist.org/packages/mrsuh/php-bison-skeleton)[ RSS](/packages/mrsuh-php-bison-skeleton/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)Dependencies (2)Versions (6)Used By (1)

PHP skeleton for Bison
======================

[](#php-skeleton-for-bison)

[![](https://github.com/mrsuh/php-bison-skeleton/actions/workflows/tests.yml/badge.svg)](https://github.com/mrsuh/php-bison-skeleton/actions/workflows/tests.yml/badge.svg)[![](https://camo.githubusercontent.com/5b3a3be9029d8ce542fe087b63221517cc4c3e945bb8e02afeb2b362ddbfec19/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d727375682f7068702d6269736f6e2d736b656c65746f6e2e737667)](https://camo.githubusercontent.com/5b3a3be9029d8ce542fe087b63221517cc4c3e945bb8e02afeb2b362ddbfec19/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d727375682f7068702d6269736f6e2d736b656c65746f6e2e737667)[![](https://camo.githubusercontent.com/f8a8419f775e32cbd518ee23ab19cc0d1a7c1d9677d3605f950f7cefa5b4a431/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6d727375682f7068702d6269736f6e2d736b656c65746f6e)](https://camo.githubusercontent.com/f8a8419f775e32cbd518ee23ab19cc0d1a7c1d9677d3605f950f7cefa5b4a431/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6d727375682f7068702d6269736f6e2d736b656c65746f6e)

A set of Bison skeleton files that can be used to generate a Bison parser written in PHP.

Requirements:
-------------

[](#requirements)

- PHP &gt;= 7.4
- Bison &gt;= 3.8

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

[](#installation)

```
composer require --dev mrsuh/php-bison-skeleton
```

Usage
-----

[](#usage)

```
bison -S vendor/mrsuh/php-bison-skeleton/src/php-skel.m4 -o parser.php grammar.y
```

Posts
-----

[](#posts)

- [PHP Skeleton for Bison](https://mrsuh.com/articles/2023/php-skeleton-for-bison/)
- [AST parser with PHP and Bison](https://mrsuh.com/articles/2023/ast-parser-with-php-and-bison/)
- [Nginx parser with PHP and Bison](https://mrsuh.com/articles/2023/nginx-parser-with-php-and-bison/)
- [JSON parser with PHP and Bison](https://mrsuh.com/articles/2023/json-parser-with-php-and-bison/)

Docker
------

[](#docker)

- [Bison docker image](https://github.com/mrsuh/docker-bison)

Example
-------

[](#example)

`grammar.y`

```
%define api.parser.class {Parser}
%token T_NUMBER
%left '-' '+'

%%
start:
  expression                       { printf("%d\n", $1); }
;

expression:
  T_NUMBER                         { $$ = $1; }
| expression '+' expression        { $$ = $1 + $3;  }
| expression '-' expression        { $$ = $1 - $3;  }
;

%%
class Lexer implements LexerInterface {
    private array $words;
    private int   $index = 0;
    private int   $value = 0;

    public function __construct($resource)
    {
        $this->words = explode(' ', trim(fgets($resource)));
    }

    public function yyerror(string $message): void
    {
        printf("%s\n", $message);
    }

    public function getLVal()
    {
        return $this->value;
    }

    public function yylex(): int
    {
        if ($this->index >= count($this->words)) {
            return LexerInterface::YYEOF;
        }

        $word = $this->words[$this->index++];
        if (is_numeric($word)) {
            $this->value = (int)$word;

            return LexerInterface::T_NUMBER;
        }

        return ord($word);
    }
}

$lexer  = new Lexer(STDIN);
$parser = new Parser($lexer);
if (!$parser->parse()) {
    exit(1);
}
```

```
bison -S vendor/mrsuh/php-bison-skeleton/src/php-skel.m4 -o parser.php grammar.y
```

```
php parser.php
