ftdebugger/jungle

此软件包最新版本(dev-master)没有可用的许可信息。

PHP SRL 解析器生成器

dev-master 2013-07-08 16:42 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:02:49 UTC


README

Jungle - 是一个SLR解析器生成器,用PHP编写,为PHP使用

用法

首先,准备一个包含你语法的.jungle文件。它与yaml文件类似,但更简单

// jungle format not supported comments now,
// so this is fake comment

rules:
  global: // this is root rule, it specify what will be searching first of all
    - expression

  expression: // new line and char '-' tell about productions
    - component
    - expression + component {$$ = $1 + $3;} // { between braces php code }
    - expression - component {$$ = $1 - $3;} // '+' and '-' automaticly will be added to terminals as literal

  factor:
    - number { $$ = (float)$1; } // $1 means first token
    - ( expression ) { $$ = $2; } // $2 means the second, and $$ means return value

  component:
    - power
    - component * power {$$ = $1 * $3;}
    - component / power {$$ = $1 / $3;}

  power:
    - factor
    - power ^ factor {$$ = pow($1, $3);}

terminals:
  number: /^[0-9]+/ // /^I AM REGEXP/, if no /^/ specify - then it is string

当语法文件准备就绪后,你可以调用

bin/jungle.php parse -o parser.php example/json/schema.jungle