PHP code example of tacoberu / bnf

1. Go to this page and download the library: Download tacoberu/bnf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tacoberu / bnf example snippets




use Taco\BNF\Parser;
use Taco\BNF\Combinators\Pattern;
use Taco\BNF\Combinators\Whitechars;

$parser = new Parser([
	new Whitechars(Null, False),
	new Pattern('element', ['~[^\n]+~']),
]);
$tree = $parser->parse('
-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
');

print_r($tree); /*

array (
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Pattern (...)
		[content] => "-brand-name = Foo 3000"
		[start] => 1
		[end] => 23
	)
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Pattern (...)
		[content] => "welcome = Welcome, {$name}, to {-brand-name}!"
		[start] => 24
		[end] => 69
	)
)

*/



use Taco\BNF\Parser;
use Taco\BNF\Combinators\Pattern;
use Taco\BNF\Combinators\Whitechars;

$parser = new Parser([
	new Whitechars(Null, False),
	new Sequence('element', [
		new Pattern('id', ['~[a-z\-]+~']),
		new Whitechars(Null, False),
		new Match(Null, ['='], False),
		new Whitechars(Null, False),
		new Pattern('element', ['~[^\n]+~']),
	]),
]);
$tree = $parser->parse('
-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
');

print_r($tree); /*

array (
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Sequence (...)
		[content] => array(
			[0] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "-brand-name"
				[start] => 1
				[end] => 12
			)
			[1] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "Foo 3000"
				[start] => 15
				[end] => 23
			)
		)
		[start] => 1
		[end] => 23
	)
    [0] => Taco\BNF\Token (
		[type] => Taco\BNF\Combinators\Pattern (...)
		[content] => array(
			[0] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "welcome"
				[start] => 24
				[end] => 31
			)
			[1] => Taco\BNF\Token (
				[type] => Taco\BNF\Combinators\Pattern (...)
				[content] => "Welcome, {$name}, to {-brand-name}!"
				[start] => 34
				[end] => 69
			)
		)
		[start] => 24
		[end] => 69
	)
)

*/