2017 © Pedro Peláez
 

library lr0-parser

LR(0) parser with state table generator for any LR(0) grammar

image

vovan-ve/lr0-parser

LR(0) parser with state table generator for any LR(0) grammar

  • Sunday, November 19, 2017
  • by Vovan-VE
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 20 Versions
  • 0 % Grown

The README.md

LR(0) parser

Latest Stable Version Latest Dev Version Build Status License, (*1)

This package contains LR(0) parser to parse texts according to custom LR(0) grammar., (*2)

Synopsis

See also following example in examples/., (*3)

use VovanVE\parser\actions\ActionsMadeMap;
use VovanVE\parser\Parser;

$grammar = <<<'_END'
    Goal        : Sum $
    Sum(add)    : Sum "+" Product
    Sum(sub)    : Sum "-" Product
    Sum(P)      : Product
    Product(mul): Product "*" Value
    Product(div): Product "/" Value
    Product(V)  : Value
    Value(neg)  : "-" Value
    Value       : "+" Value
    Value       : "(" Sum ")"
    Value       : int
    int         : /\d+/

    -ws         : /\s+/
    -mod        : 'u'
_END;

$parser = new Parser($grammar);

$actions = new ActionsMadeMap([
    'int' => function ($content) { return (int)$content; },

    'Value' => Parser::ACTION_BUBBLE_THE_ONLY,
    'Value(neg)' => function ($v) { return -$v; },

    'Product(V)' => Parser::ACTION_BUBBLE_THE_ONLY,
    'Product(mul)' => function ($a, $b) { return $a * $b; },
    'Product(div)' => function ($a, $b) { return $a / $b; },

    'Sum(P)' => Parser::ACTION_BUBBLE_THE_ONLY,
    'Sum(add)' => function ($a, $b) { return $a + $b; },
    'Sum(sub)' => function ($a, $b) { return $a - $b; },
]);

$tree = $parser->parse('2 * (-10 + 33) - 4', $actions);

echo 'Result is ', $tree->made(), PHP_EOL;
echo 'Tree:', PHP_EOL;
echo $tree->dumpAsString();

Output:, (*4)

Result is 42
Tree:
 `- Sum(sub)
     `- Sum(P)
     |   `- Product(mul)
     |       `- Product(V)
     |       |   `- Value
     |       |       `- int <2>
     |       `- Value
     |           `- Sum(add)
     |               `- Sum(P)
     |               |   `- Product(V)
     |               |       `- Value(neg)
     |               |           `- Value
     |               |               `- int <10>
     |               `- Product(V)
     |                   `- Value
     |                       `- int <33>
     `- Product(V)
         `- Value
             `- int <4>

Description

This package contains:, (*5)

  • Lexer to parse input string into tokens. There is separate Lexer configurable by regexps.
  • Grammar object to describe a grammar of a language.
  • Parsing table to let parser to switch states with respect to grammar.
  • LR(0) parser itself. It parse input string for AST using the table.

First this package was made just to apply the theory in practice. It may easily be used for small grammars to parse small source codes. But later I did apply it in my another package., (*6)

Installation

Install through composer:, (*7)

composer require vovan-ve/lr0-parser

or add to require section in your composer.json:, (*8)

"vovan-ve/lr0-parser": "~2.0.0"

Theory

LR parser., (*9)

License

This package is under MIT License, (*10)

The Versions

19/11 2017

dev-grammar-config

dev-grammar-config

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

16/11 2017

v1.5.x-dev

1.5.9999999.9999999-dev

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

01/11 2017

dev-master

9999999-dev

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

01/11 2017

v1.4.2

1.4.2.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

29/10 2017

v1.4.1

1.4.1.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

29/10 2017

v1.4.x-dev

1.4.9999999.9999999-dev

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

29/10 2017

v1.4.0

1.4.0.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

22/10 2017

v1.3.x-dev

1.3.9999999.9999999-dev

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

22/10 2017

v1.3.1

1.3.1.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

22/10 2017

v1.3.0

1.3.0.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

19/08 2017

v1.2.0

1.2.0.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

19/08 2017

v1.2.x-dev

1.2.9999999.9999999-dev

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

12/07 2017

v1.1.x-dev

1.1.9999999.9999999-dev

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

12/07 2017

v1.1.0

1.1.0.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

08/07 2017

v1.0.3

1.0.3.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

08/07 2017

dev-devel

dev-devel

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

08/07 2017

v1.0.x-dev

1.0.9999999.9999999-dev

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

04/07 2016

v1.0.2

1.0.2.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

04/07 2016

v1.0.1

1.0.1.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE

04/07 2016

v1.0.0

1.0.0.0

LR(0) parser with state table generator for any LR(0) grammar

  Sources   Download

MIT

The Requires

  • php >=5.5
  • lib-pcre *

 

The Development Requires

by Avatar Vovan-VE