PHPackages                             aerialship/lex - 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. aerialship/lex

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

aerialship/lex
==============

1.0.1(9y ago)4242MITPHP

Since Jan 19Pushed 9y ago1 watchersCompare

[ Source](https://github.com/aerialship/Lex)[ Packagist](https://packagist.org/packages/aerialship/lex)[ RSS](/packages/aerialship-lex/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

[![Build Status](https://camo.githubusercontent.com/c354e080a7462142c77c6058b39b8f9beee3d1554e44b919944758a3753388c3/68747470733a2f2f7472617669732d63692e6f72672f61657269616c736869702f4c65782e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/aerialship/Lex)

Lex
===

[](#lex)

PHP implementation of Lexical Analyzer.

> **Warning**This is not a GENERATOR like classical lex is. It does not produce any php code. It's a simple plain scanner of the given input string and tokenizer into given set of tokens by matching regular expressions. Thus, at runtime you can change the token definition and use one same code for any token set.

Usage
=====

[](#usage)

Token definition
----------------

[](#token-definition)

Define the tokens in the yaml file. Key is regex pattern for the token, and value is token name. All patterns with empty name will be ignored and will not result in a token, but will keep the scan running and not brake it. On the other hand, if scanner encounters a non-recognizable chunk it will raise an exception.

```
# math.yml
\s:
\d+: number
\+: plus
-: minus
\*: mul
/: div

```

> **Note:**Regex pattern MUST NOT include delimiters, nor start and end of string meta-chars (^ and $). They are prepeded with those during the scan. If invalid pattern supplied it will trigger InvalidArgumentException during the configuration load

Scanning with a callback
------------------------

[](#scanning-with-a-callback)

Construct the lexer with such tokens config

```
$lexer = new Lexer(new LexConfig(new YamlFileConfig('math.yml')));
```

And now you are ready to tokenize the input string

```
$lexer->tokenizeAsync(' 2131 + 33   / 567', function(Token $token) {
    print "{$token->getToken()}({$token->getValue()})\n";
});
```

Which will print the result on output

```
number(2131)
plus(+)
number(33)
div(/)
number(567)

```

Scan and return array of tokens
-------------------------------

[](#scan-and-return-array-of-tokens)

Optionally you could get all tokens collected into an array and returned together, without supplying a callback for individual tokens

```
$result = $lexer->tokenize(' 2131 + 33   / 567');
print_r($result);
```

Unknown token exception
-----------------------

[](#unknown-token-exception)

If at any position content of input string does not match with any regex pattern from the config the UnknownTokenException will be thrown containing the offset of its position

```
try {
    $lexer->tokenize(' 2131 + blabla');
} catch (UnknownTokenException $ex) {
    print $ex->getOffset();  // 8
}
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1036 days

Total

2

Last Release

3458d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/28047195d6048ce03dcb0d7eb3c4a2b25ab811d468cd60cb1b3d825e29b866f2?d=identicon)[aerialship](/maintainers/aerialship)

---

Top Contributors

[![tmilos](https://avatars.githubusercontent.com/u/1818373?v=4)](https://github.com/tmilos "tmilos (8 commits)")

### Embed Badge

![Health badge](/badges/aerialship-lex/health.svg)

```
[![Health](https://phpackages.com/badges/aerialship-lex/health.svg)](https://phpackages.com/packages/aerialship-lex)
```

###  Alternatives

[prohalexey/the-choice

253.3k](/packages/prohalexey-the-choice)[duckfusion/waddle

PHP Library for parsing running GPS activities and calculating metrics

364.9k](/packages/duckfusion-waddle)[eprofos/user-agent-analyzer

A powerful Symfony bundle for user-agent analysis. It provides accurate detection of operating systems (Windows, MacOS, Linux, iOS, Android...), browsers (Chrome, Firefox, Safari...), and device types (Desktop, Mobile, Tablet, TV...). Supports specific version detection and includes advanced handling of special cases like WebViews and compatibility modes. Features comprehensive logging and detailed analysis results.

182.3k](/packages/eprofos-user-agent-analyzer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
