Your first PEG parser
Building a tokenizer and parser from scratch is a very tedious task. Luckily, many libraries exist that you can use to generate a parser automatically from some kind of formal grammar definition.
In PHP, you can use the hafriedlander/php-peg
library to generate the PHP code for a parser for any kind of formal language that can be described by a parsing expression grammar. For this, create a new project directory and create a new composer.json
file with the following contents:
{ "name": "packt-php7/chp8-calculator", "authors": [{ "name": "Martin Helmich", "email": "php7-book@martin-helmich.de" }], "require": { "hafriedlander/php-peg": "dev-master" }, "autoload": { "psr-4": { "Packt\\Chp8\\DSL": "src/" }, "files": [ "vendor/hafriedlander/php-peg/autoloader.php" ...
Get PHP 7 Programming Blueprints now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.