phplrt 4.0

Examples

A few complete grammars, picked one per idea. Each one is short enough to read in a sitting and close enough to a real language to be worth stealing from, and each page carries the grammar in full, the PHP that runs it, and a note about the one thing it is there to show.

This is a selection and not the whole set: many more grammars for real languages live in phplrt/grammars.

Example What it shows
JSON A rule with no reducer, and the shape of a comma-separated list that may be empty
Cron Expression Asking "is this valid?" with Mode::SyntaxCheck, without building a value
Composer Constraints Where a grammar stops and PHP begins
URL Naming every part of a format parse_url() decides quietly
Graphviz DOT Throwing three kinds of comment away with %skip
EBNF Reading a notation that spells half of itself two ways
Rule Engine Property and method chains an evaluator can walk against anything
PhpDoc Types Generics, array shapes and conditionals - a type language in earnest
Regular Expressions Reading PCRE itself, groups and all

Start with JSON: it is the smallest of them, and the two habits it shows turn up in every grammar after it. PhpDoc Types and Regular Expressions are the two largest, and the ones to read once a grammar of your own has started to get long.

Every example is a .pp3 file plus a few lines of PHP, so the fastest way to try one is to copy the grammar into a file and hand it to the compiler:

1use Phplrt\Compiler\Compiler;
2use Phplrt\Source\FileSource;
3
4$parser = new Compiler()
5    ->load(FileSource::createFromPathname(__DIR__ . '/example.pp3'))
6    ->getParser();

Quick Start walks through that from the beginning.

More grammars. phplrt/grammars collects ready to read grammars for real languages - JSON5, TSV, semantic versions, DQL, PHQL, JMS types, PSR-5 and Doctrine annotations, Symfony expressions, Go! AOP pointcuts, Praspel contracts and more - each with sample inputs and a test that keeps it honest.