PHPackages                             antlr/antlr4-php-runtime - 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. antlr/antlr4-php-runtime

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

antlr/antlr4-php-runtime
========================

PHP 8.0+ runtime for ANTLR 4

0.9.1(2y ago)96636.7k—4.6%20[3 PRs](https://github.com/antlr/antlr-php-runtime/pulls)13BSD-3-ClausePHPPHP ^8.0

Since Sep 12Pushed 1y ago5 watchersCompare

[ Source](https://github.com/antlr/antlr-php-runtime)[ Packagist](https://packagist.org/packages/antlr/antlr4-php-runtime)[ RSS](/packages/antlr-antlr4-php-runtime/feed)WikiDiscussions dev Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (13)Used By (13)

ANTLR4 Runtime for PHP
======================

[](#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://camo.githubusercontent.com/5189101273a45d87a6dbd0c7716bae1b396870e3e31d31bb6794ef0821f9eb1f/68747470733a2f2f706f7365722e707567782e6f72672f616e746c722f616e746c72342d7068702d72756e74696d652f762f737461626c65)](https://packagist.org/packages/antlr/antlr4-php-runtime)[![License](https://camo.githubusercontent.com/dfb2a7cda81965768bfed9d5059f7f7d46983584c1a7899edddc060daac3e235/68747470733a2f2f706f7365722e707567782e6f72672f616e746c722f616e746c72342d7068702d72756e74696d652f6c6963656e7365)](https://packagist.org/packages/antlr/antlr4-php-runtime)

### First steps

[](#first-steps)

#### 1. Install ANTLR4

[](#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

[](#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:

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

#### 3. Generate your parser

[](#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:

```
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

[](#complete-example)

Suppose you're using the JSON grammar from .

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:

```
