Source: ryangjchandler.co.uk

Write a Lexer in PHP with Lexical
I recently released a new PHP package called "Lexical". It provides a set of attributes and objects to build regular expression based lexers in PHP.

As somebody who enjoys experimenting with parsers and text processing, writing lexers has become a somewhat regular task for me.

Lexical provides a set of attributes that can be added to each case in the enumeration: Regex - accepts a single regular expression.

enum TokenType { #[Regex("[0-9]+")] case Number; #[Literal("+")] case Add; #[Literal("-")] case Subtract; #[Literal("*")] case Multiply; #[Literal("/")] case Divide; }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners