PHPackages                             didix16/php-grammar - 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. didix16/php-grammar

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

didix16/php-grammar
===================

A simple library for make Lexer and Parsers to build a language

1.0.1(4y ago)19.0k2MITPHPPHP &gt;= 7.2

Since Aug 5Pushed 4y ago1 watchersCompare

[ Source](https://github.com/didix16/php-grammar)[ Packagist](https://packagist.org/packages/didix16/php-grammar)[ Docs](https://github.com/didix16/php-grammar)[ RSS](/packages/didix16-php-grammar/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (1)DependenciesVersions (3)Used By (2)

PHP Grammar
===========

[](#php-grammar)

A simple library to make Lexer and Parsers to build a language.

Content
-------

[](#content)

- [What is a Token](#what-is-a-token)
- [What is a Lexer](#what-is-a-lexer)
- [What is a Parser](#what-is-a-parser)
- [Installation](#installation)
- [Usage](#usage)
- [Check also](#check-also)

### What is a Token

[](#what-is-a-token)

A token is the most atomic piece of your language. It could be a single character in an specific alphabet A or a composition of some of them.

### What is a Lexer

[](#what-is-a-lexer)

Lexer is the responsible of parsing the raw text and tokenize it for a Parser, which will interpret them using a grammar.

Example: Supose we have the following text:

```
$library = require('path/to/lib');

```

Lets say our language has variables, functions and some else.

A result of applying a parser will generate the following tokens:

- **T\_VAR &lt; library &gt;** A variable identifier called 'library'
- **T\_ASIGN &lt; = &gt;** A single character which represents an asignation from right to left
- **T\_FUNC &lt; require &gt;** A function name. the language should recongize the function and execute it.
- **T\_FUNC\_ARG &lt; 'path/to/lib' &gt;** A function argument passed to the previous function.
- **T\_ENDLINE &lt;;&gt;** A single character which represents and end of line

With that tokens then the Parser should identify in its grammar a properly syntax for the language you are building.

### What is a Parser

[](#what-is-a-parser)

A parser or grammar is the responsible of interpret the tokens generated by the Lexer and evaluate them identifying the correct syntax of your language you are builing.

Also we call the grammar, Context Free Grammar. More info here [Context Free Grammar - Wikipedia](https://en.wikipedia.org/wiki/Context-free_grammar)

To program a grammar, we are gona use the [Chomsky formal form](https://en.wikipedia.org/wiki/Chomsky_normal_form) since we must have well defined rules for our grammar and also most important, **we are gonna use the [BNF - Backus-Naur Form](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form)** to define each rules.

### Installation

[](#installation)

```
composer require didix16/php-grammar
```

### Usage

[](#usage)

I'm gonna show a simple usage for build a language which recognise a supe simple script to instruct some API do something like GET url and process it.

Let's gonna call it APIScript and should use APILexer and APIParser.

A simple script would have a structure like this:

```
GET https://some-awesome-api.com/endpoint?token=TOKEN
PIPETO $mySuperService
SAVEDB localhost:10000

```

We can identify lines, which each line is an action. Each action has a keyname, a space and a param ( it could be a list of params)

So lets say we have to identify tokens like:

T\_ACTION, T\_WHITESPACE, T\_ARG and T\_NEWLINE

```
