Download the PHP package didix16/php-grammar without Composer
On this page you can find all versions of the php package didix16/php-grammar. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-grammar
PHP Grammar
A simple library to make Lexer and Parsers to build a language.
Content
- What is a Token
- What is a Lexer
- What is a Parser
- Installation
- Usage
- Check also
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
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:
Lets say our language has variables, functions and some else.
A result of applying a parser will generate the following tokens:
- T_VAR < library > A variable identifier called 'library'
- T_ASIGN < = > A single character which represents an asignation from right to left
- T_FUNC < require > A function name. the language should recongize the function and execute it.
- T_FUNC_ARG < 'path/to/lib' > A function argument passed to the previous function.
- T_ENDLINE <;> 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
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
To program a grammar, we are gona use the Chomsky formal form since we must have well defined rules for our grammar and also most important, we are gonna use the BNF - Backus-Naur Form to define each rules.
Installation
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:
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
We are gonna define our Grammar now. Take a look on how to apply the BNF into a programmary form.
I assume that APILexer and APIParser are on the same directory.
Finally lets do a simple code example
Now, with the parsed tokens you can do anything that evaluates the actions with its arguments.
For example, you may have an Interpreter that evaluates the tokens and do the corresponding actions.
I.e, GET may use some library to make HTTP GET requests; PIPETO may pass the gathered data to some of your system service and SAVEDB may save the result from PIPETO $mySuperService to the specified DDBB host.
Check also
- php-interpreter - An interpreter base made in PHP to parse your own Tokens.