vovan-ve/lr0-parser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vovan-ve/lr0-parser

Composer 安装命令:

composer require vovan-ve/lr0-parser

包简介

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

README 文档

README

Latest Stable Version Latest Dev Version Build Status License

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

Synopsis

See also following example in examples/.

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:

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:

  • 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.

Installation

Install through composer:

composer require vovan-ve/lr0-parser

or add to require section in your composer.json:

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

Theory

LR parser.

License

This package is under MIT License

统计信息

  • 总下载量: 103
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固