{"id":15739734,"url":"https://github.com/antlr/antlr-php-runtime","last_synced_at":"2025-05-16T04:07:01.670Z","repository":{"id":35111949,"uuid":"190903566","full_name":"antlr/antlr-php-runtime","owner":"antlr","description":"PHP Runtime for ANTLR4","archived":false,"fork":false,"pushed_at":"2025-03-17T12:21:10.000Z","size":192,"stargazers_count":87,"open_issues_count":10,"forks_count":21,"subscribers_count":8,"default_branch":"dev","last_synced_at":"2025-05-14T07:52:24.506Z","etag":null,"topics":["antlr-runtime","antlr4","parser-generator","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antlr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-08T15:24:50.000Z","updated_at":"2025-04-29T20:15:09.000Z","dependencies_parsed_at":"2024-06-18T13:50:11.265Z","dependency_job_id":"e1e757d7-a013-4350-ba20-f22692a734cd","html_url":"https://github.com/antlr/antlr-php-runtime","commit_stats":{"total_commits":38,"total_committers":8,"mean_commits":4.75,"dds":0.5789473684210527,"last_synced_commit":"f487f766053e61313dfea2570c9b3fa737a968b4"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlr%2Fantlr-php-runtime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlr%2Fantlr-php-runtime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlr%2Fantlr-php-runtime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antlr%2Fantlr-php-runtime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antlr","download_url":"https://codeload.github.com/antlr/antlr-php-runtime/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254464897,"owners_count":22075571,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["antlr-runtime","antlr4","parser-generator","php8"],"created_at":"2024-10-04T02:06:36.888Z","updated_at":"2025-05-16T04:06:56.648Z","avatar_url":"https://github.com/antlr.png","language":"PHP","readme":"# ANTLR4 Runtime for PHP\n[![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)\n[![Latest Stable Version](https://poser.pugx.org/antlr/antlr4-php-runtime/v/stable)](https://packagist.org/packages/antlr/antlr4-php-runtime)\n[![License](https://poser.pugx.org/antlr/antlr4-php-runtime/license)](https://packagist.org/packages/antlr/antlr4-php-runtime)\n\n### First steps\n\n#### 1. Install ANTLR4\n\n[The getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md) \nshould get you started.\n\n#### 2. Install the PHP ANTLR runtime\n\nEach target language for ANTLR has a runtime package for running parser \ngenerated by ANTLR4. The runtime provides a common set of tools for using your parser.\n\nInstall the runtime with Composer:\n\n```bash\ncomposer require antlr/antlr4-php-runtime\n```\n\n#### 3. Generate your parser\n\nYou use the ANTLR4 \"tool\" to generate a parser. These will reference the ANTLR \nruntime, installed above.\n\nSuppose you're using a UNIX system and have set up an alias for the ANTLR4 tool \nas described in [the getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md). \nTo generate your PHP parser, run the following command:\n\n```bash\nantlr4 -Dlanguage=PHP MyGrammar.g4\n```\n\nFor a full list of antlr4 tool options, please visit the \n[tool documentation page](https://github.com/antlr/antlr4/blob/master/doc/tool-options.md).\n\n### Complete example\n\nSuppose you're using the JSON grammar from https://github.com/antlr/grammars-v4/tree/master/json.\n\nThen, invoke `antlr4 -Dlanguage=PHP JSON.g4`. The result of this is a \ncollection of `.php` files in the `parser` directory including:\n```\nJsonParser.php\nJsonBaseListener.php\nJsonLexer.php\nJsonListener.php\n```\n\nAnother common option to the ANTLR tool is `-visitor`, which generates a parse \ntree visitor, but we won't be doing that here. For a full list of antlr4 tool \noptions, please visit the [tool documentation page](tool-options.md).\n\nWe'll write a small main func to call the generated parser/lexer \n(assuming they are separate). This one writes out the encountered \n`ParseTreeContext`'s:\n\n```php\n\u003c?php\n\nnamespace JsonParser;\n\nuse Antlr\\Antlr4\\Runtime\\CommonTokenStream;\nuse Antlr\\Antlr4\\Runtime\\Error\\Listeners\\DiagnosticErrorListener;\nuse Antlr\\Antlr4\\Runtime\\InputStream;\nuse Antlr\\Antlr4\\Runtime\\ParserRuleContext;\nuse Antlr\\Antlr4\\Runtime\\Tree\\ErrorNode;\nuse Antlr\\Antlr4\\Runtime\\Tree\\ParseTreeListener;\nuse Antlr\\Antlr4\\Runtime\\Tree\\ParseTreeWalker;\nuse Antlr\\Antlr4\\Runtime\\Tree\\TerminalNode;\n\nfinal class TreeShapeListener implements ParseTreeListener {\n    public function visitTerminal(TerminalNode $node) : void {}\n    public function visitErrorNode(ErrorNode $node) : void {}\n    public function exitEveryRule(ParserRuleContext $ctx) : void {}\n\n    public function enterEveryRule(ParserRuleContext $ctx) : void {\n        echo $ctx-\u003egetText();\n    }\n}\n\n$input = InputStream::fromPath($argv[1]);\n$lexer = new JSONLexer($input);\n$tokens = new CommonTokenStream($lexer);\n$parser = new JSONParser($tokens);\n$parser-\u003eaddErrorListener(new DiagnosticErrorListener());\n$parser-\u003esetBuildParseTree(true);\n$tree = $parser-\u003ejson();\n\nParseTreeWalker::default()-\u003ewalk(new TreeShapeListener(), $tree);\n```\n\nCreate a `example.json` file:\n```json\n{\"a\":1}\n```\n\nParse the input file:\n\n```\nphp json.php example.json\n```\n\nThe expected output is:\n\n```\n{\"a\":1}\n{\"a\":1}\n\"a\":1\n1\n```\n\n### Useful information\n\n* [Official site](http://www.antlr.org/)\n* [Documentation](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/index.md)\n* [Getting started with v4](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/getting-started.md)\n* [PHPStan Exension](https://github.com/antlr/antlr-php-runtime-phpstan)\n* [FAQ](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/faq/index.md)\n\n### The Definitive ANTLR 4 Reference\n\nProgrammers 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.\n\nYou 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).\n\nYou will find the [Book source code](http://pragprog.com/titles/tpantlr2/source_code) useful.\n\n### Additional grammars\n\n[This repository](https://github.com/antlr/grammars-v4) is a collection of grammars without actions where the\nroot directory name is the all-lowercase name of the language parsed by the grammar.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantlr%2Fantlr-php-runtime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantlr%2Fantlr-php-runtime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantlr%2Fantlr-php-runtime/lists"}