macfja/lexer-expression 问题修复 & 功能扩展

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

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

macfja/lexer-expression

Composer 安装命令:

composer require macfja/lexer-expression

包简介

Library to transform a Doctrine lexer into a RPN expression and solve it

README 文档

README

  1. Description
  2. What is Shunting Yard algorithm
  3. The Shunting Yard implementation
  4. The RPN expression Solver
  5. The AST Tree builder
  6. Install
  7. Usage
  8. Similar projects
  9. Documentations

Description

The main goal of this library is to transform an expression into a computer understandable expression, by using

  1. Doctrine Lexer
  2. The Shunting Yard algorithm

As the input of the Shunting Yard is a Doctrine Lexer, the library is not limited to mathematics expression.

The library provide an "evaluator" of the Shunting Yard algorithm output, or transform it into a AST Tree

What is Shunting Yard algorithm

The best explanation can by found on Wikipedia, or on this article of @igorw.

But to make simple, the main idea is to transform a list of chars into multiple of small group.

Simple example: (1 + 2) * 3 is really simple for us (human) to process, but a computer can do it, it need to split it into small chunk. And evaluate it piece by piece. For a computer, the expression need to be evaluate like this:

        *
       / \
      +   3
     / \
    1   2

This a binary tree, and yes it's also a kind of AST. So to solve the expression a computer first evaluate the group (we will name the result as (a)):

      +
     / \
    1   2

And then it evaluate the group:

   *
  / \
(a)  3

The Shunting Yard algorithm transform your expression into an intermediate expression: a RPN expression. The RPN expression of (1 + 2) * 3 is 1 2 + 3 *.

This expression can be read as:

  • Parameter 1
  • Parameter 2
  • Operator + (that take 2 parameters: the two previous parameters)
  • Parameter 3
  • Operator * (that take 2 parameters: the result of the operation and the previous parameter)

The Shunting Yard implementation

Like most of Shunting Yard implementation, it can parse "simple" expression (with simple operator) and make parenthesis reduction. It can parse unary, and binary functions (like sin(x), max(x,y)) but also any functions (ex: fct(a,b,c,x,y,z)).

But the main "feature" it's the use of Doctrine Lexer.

The RPN expression Solver

The RPN solver allow you to solve a RPN expression (a list of Doctrine Lexer token). It support Operator/Function with any arity.

The AST Tree builder

The AST Tree builder can transform the RPN expression (a list of Doctrine Lexer token) into an AST Tree.
The tree root is a node, the last(final) node (in the What is Shunting Yard algorithm, it's the * operator). This root node is composed of values (leafs of a tree) and nodes (branches).

Limitation

The Solver can not solver function with variable arity (or optional parameters)

Install

To install with composer:

composer require macfja/lexer-expression

Usage

Very simple Doctrine DQL

use \Doctrine\ORM\Query\Lexer;

$dql = 'SELECT u FROM User u WHERE u.id = MAX(5, 7)';
$lexer = new Lexer($dql);
$sy = new ShuntingYard();
$sy->setLexer($lexer);
$sy->setOpenParenthesis(Lexer::T_OPEN_PARENTHESIS);
$sy->setCloseParenthesis(Lexer::T_CLOSE_PARENTHESIS);
$sy->setOperators(array(
    new ShuntingYardOperator(Lexer::T_MAX,    10, ShuntingYardOperator::ASSOCIATIVITY_LEFT),
    new ShuntingYardOperator(Lexer::T_SELECT, 10, ShuntingYardOperator::ASSOCIATIVITY_LEFT),
    new ShuntingYardOperator(Lexer::T_FROM,   10, ShuntingYardOperator::ASSOCIATIVITY_LEFT),
    new ShuntingYardOperator(Lexer::T_WHERE,  2,  ShuntingYardOperator::ASSOCIATIVITY_LEFT),
    new ShuntingYardOperator(Lexer::T_DOT,    10, ShuntingYardOperator::ASSOCIATIVITY_LEFT),
    new ShuntingYardOperator(Lexer::T_EQUALS, 10, ShuntingYardOperator::ASSOCIATIVITY_LEFT)
));
$sy->setArgumentSeparator(Lexer::T_COMMA);
var_dump($sy->parse());

Very simple Mathematics expression

3 examples are available in the test directory. The expression is (1 + 2) / ((3 + 4 * 5) - 6).

  • test/MathRPN.php: Just an expression to RPN example
  • test/MathSolve.php: An example with the solver
  • test/MathAST.php: An example with the tree builder

The tree of the expression is:

          "/"
         /   \
        /     \
       /      "-"
      /      /   \
     /     "+"    6
    /     /   \
  "+"    3    "*"
 /   \       /   \
1     2     4     5

Similar projects

Documentations

macfja/lexer-expression 适用场景与选型建议

macfja/lexer-expression 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.27k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「doctrine」 「lexer」 「shunting yard」 「RPN」 「Reverse Polish Notation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 macfja/lexer-expression 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 macfja/lexer-expression 我们能提供哪些服务?
定制开发 / 二次开发

基于 macfja/lexer-expression 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.27k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-28