An open API service indexing awesome lists of open source software.

https://github.com/antlr/antlr-php-runtime

PHP Runtime for ANTLR4
https://github.com/antlr/antlr-php-runtime

antlr-runtime antlr4 parser-generator php8

Last synced: 24 days ago
JSON representation

PHP Runtime for ANTLR4

Awesome Lists containing this project

README

        

# ANTLR4 Runtime for PHP
[![Build](https://github.com/antlr/antlr-php-runtime/actions/workflows/branch-validations.yaml/badge.svg)](https://github.com/antlr/antlr-php-runtime/actions/workflows/branch-validations.yaml)
[![Latest Stable Version](https://poser.pugx.org/antlr/antlr4-php-runtime/v/stable)](https://packagist.org/packages/antlr/antlr4-php-runtime)
[![License](https://poser.pugx.org/antlr/antlr4-php-runtime/license)](https://packagist.org/packages/antlr/antlr4-php-runtime)

### First steps

#### 1. Install ANTLR4

[The getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md)
should get you started.

#### 2. Install the PHP ANTLR runtime

Each target language for ANTLR has a runtime package for running parser
generated by ANTLR4. The runtime provides a common set of tools for using your parser.

Install the runtime with Composer:

```bash
composer require antlr/antlr4-php-runtime
```

#### 3. Generate your parser

You use the ANTLR4 "tool" to generate a parser. These will reference the ANTLR
runtime, installed above.

Suppose you're using a UNIX system and have set up an alias for the ANTLR4 tool
as described in [the getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md).
To generate your PHP parser, run the following command:

```bash
antlr4 -Dlanguage=PHP MyGrammar.g4
```

For a full list of antlr4 tool options, please visit the
[tool documentation page](https://github.com/antlr/antlr4/blob/master/doc/tool-options.md).

### Complete example

Suppose you're using the JSON grammar from https://github.com/antlr/grammars-v4/tree/master/json.

Then, invoke `antlr4 -Dlanguage=PHP JSON.g4`. The result of this is a
collection of `.php` files in the `parser` directory including:
```
JsonParser.php
JsonBaseListener.php
JsonLexer.php
JsonListener.php
```

Another common option to the ANTLR tool is `-visitor`, which generates a parse
tree visitor, but we won't be doing that here. For a full list of antlr4 tool
options, please visit the [tool documentation page](tool-options.md).

We'll write a small main func to call the generated parser/lexer
(assuming they are separate). This one writes out the encountered
`ParseTreeContext`'s:

```php
getText();
}
}

$input = InputStream::fromPath($argv[1]);
$lexer = new JSONLexer($input);
$tokens = new CommonTokenStream($lexer);
$parser = new JSONParser($tokens);
$parser->addErrorListener(new DiagnosticErrorListener());
$parser->setBuildParseTree(true);
$tree = $parser->json();

ParseTreeWalker::default()->walk(new TreeShapeListener(), $tree);
```

Create a `example.json` file:
```json
{"a":1}
```

Parse the input file:

```
php json.php example.json
```

The expected output is:

```
{"a":1}
{"a":1}
"a":1
1
```

### Useful information

* [Official site](http://www.antlr.org/)
* [Documentation](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/index.md)
* [Getting started with v4](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/getting-started.md)
* [PHPStan Exension](https://github.com/antlr/antlr-php-runtime-phpstan)
* [FAQ](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/faq/index.md)

### The Definitive ANTLR 4 Reference

Programmers run into parsing problems all the time. Whether it’s a data format like JSON, a network protocol like SMTP, a server configuration file for Apache, a PostScript/PDF file, or a simple spreadsheet macro language—ANTLR v4 and this book will demystify the process. ANTLR v4 has been rewritten from scratch to make it easier than ever to build parsers and the language applications built on top. This completely rewritten new edition of the bestselling Definitive ANTLR Reference shows you how to take advantage of these new features.

You can buy the book [The Definitive ANTLR 4 Reference](http://amzn.com/1934356999) at amazon or an [electronic version at the publisher's site](https://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference).

You will find the [Book source code](http://pragprog.com/titles/tpantlr2/source_code) useful.

### Additional grammars

[This repository](https://github.com/antlr/grammars-v4) is a collection of grammars without actions where the
root directory name is the all-lowercase name of the language parsed by the grammar.