Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phplrt/phplrt
PHP Language Recognition Tool
https://github.com/phplrt/phplrt
ast combinator compiler grammar lexer library ll1 llk lookahead parser php phplrt recursive-descent-parser tool trace
Last synced: 3 months ago
JSON representation
PHP Language Recognition Tool
- Host: GitHub
- URL: https://github.com/phplrt/phplrt
- Owner: phplrt
- License: mit
- Created: 2019-04-20T15:42:57.000Z (almost 6 years ago)
- Default Branch: 3.x
- Last Pushed: 2024-02-21T00:53:48.000Z (11 months ago)
- Last Synced: 2024-04-01T09:34:13.426Z (10 months ago)
- Topics: ast, combinator, compiler, grammar, lexer, library, ll1, llk, lookahead, parser, php, phplrt, recursive-descent-parser, tool, trace
- Language: PHP
- Homepage: https://phplrt.org
- Size: 6.63 MB
- Stars: 204
- Watchers: 9
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## Introduction
The phplrt is a set of tools for programming languages recognition. The library
provides lexer, parser, grammar compiler, library for working with errors,
text analysis and so on.## Installation
Phplrt is available as composer repository and can be
installed using the following command in a root of your project:```bash
$ composer require phplrt/phplrt
```More detailed installation instructions [are here](https://phplrt.org/docs/installation).
## Documentation
- https://phplrt.org/
## Quick Start
First, we will create the grammar for our parser.
> You can read more about the grammar syntax [here](https://phplrt.org/docs/compiler/grammar).
```php
load(<< (Operator() )*
;#Operator
:
|
|
|
;EBNF);
```### Execution
In order to quickly check the performance of what has been written, you can use
the simple `parse()` method. As a result, it will output the recognized abstract
syntax tree along with the predefined AST classes which can be converted to their
string representation.```php
parse('2 + 2');//
// Output:
//
//
// 2
//
// +
//
// 2
//
//
```### Compilation
After your grammar is ready and tested, it should be compiled. After that,
you no longer need the `phplrt/compiler` dependency (see https://phplrt.org/docs/installation#runtime-only).```php
file_put_contents(__DIR__ . '/grammar.php', (string)$compiler->build());
```This file will contain your compiled data that can be used in your custom parser.
```php
use Phplrt\Lexer\Lexer;
use Phplrt\Parser\Parser;
use Phplrt\Parser\BuilderInterface;
use Phplrt\Parser\Context;$data = require __DIR__ . '/grammar.php';
// Create Lexer from compiled data
$lexer = new Lexer($data['tokens']['default'], $data['skip']);// Create Parser from compiled data
$parser = new Parser($lexer, $data['grammar'], [// Recognition will start from the specified rule
Parser::CONFIG_INITIAL_RULE => $data['initial'],// Rules for the abstract syntax tree builder.
// In this case, we use the data found in the compiled grammar.
Parser::CONFIG_AST_BUILDER => new class($data['reducers']) implements BuilderInterface {
public function __construct(private array $reducers) {}public function build(Context $context, $result)
{
$state = $context->getState();return isset($this->reducers[$state]))
? $this->reducers[$state]($context, $result)
: $result
;
}
}
]);// Now we are ready to parse any code using the compiled grammar
$parser->parse(' ..... ');
```