使用标记化程序

时间:2017-01-07 13:02:11

标签: php parsing tokenize php-parser

我正在尝试解析通过PHP中的API提供的“规则字符串”。

鉴于我有一系列项目,例如

$items = [41, 42, 51, 54, 65, 12];

我希望评估此表达式

({41} AND {51}) OR ({31} AND {42})

在这种情况下,它应该返回true,因为$ items数组同时包含41和51项。

我开始使用https://github.com/nette/tokenizer库创建一个tokenizer,并且现在将它解析为一个标记数组。

array:25 [
  0 => array:3 [
    0 => "("
    1 => 0
    2 => 1
  ]
  1 => array:3 [
    0 => "{"
    1 => 1
    2 => 2
  ]
  2 => array:3 [
    0 => "41"
    1 => 2
    2 => 3
  ]
  3 => array:3 [
    0 => "}"
    1 => 4
    2 => 4
  ]
  4 => array:3 [
    0 => " "
    1 => 5
    2 => 8
  ]
  5 => array:3 [
    0 => "AND"
    1 => 6
    2 => 5
  ]
  6 => array:3 [
    0 => " "
    1 => 9
    2 => 8
  ]
  7 => array:3 [
    0 => "{"
    1 => 10
    2 => 2
  ]
  8 => array:3 [
    0 => "51"
    1 => 11
    2 => 3
  ]
  9 => array:3 [
    0 => "}"
    1 => 13
    2 => 4
  ]
  10 => array:3 [
    0 => ")"
    1 => 14
    2 => 7
  ]
  11 => array:3 [
    0 => " "
    1 => 15
    2 => 8
  ]
  12 => array:3 [
    0 => "OR"
    1 => 16
    2 => 6
  ]
  13 => array:3 [
    0 => " "
    1 => 18
    2 => 8
  ]
  14 => array:3 [
    0 => "("
    1 => 19
    2 => 1
  ]
  15 => array:3 [
    0 => "{"
    1 => 20
    2 => 2
  ]
  16 => array:3 [
    0 => "31"
    1 => 21
    2 => 3
  ]
  17 => array:3 [
    0 => "}"
    1 => 23
    2 => 4
  ]
  18 => array:3 [
    0 => " "
    1 => 24
    2 => 8
  ]
  19 => array:3 [
    0 => "AND"
    1 => 25
    2 => 5
  ]
  20 => array:3 [
    0 => " "
    1 => 28
    2 => 8
  ]
  21 => array:3 [
    0 => "{"
    1 => 29
    2 => 2
  ]
  22 => array:3 [
    0 => "42"
    1 => 30
    2 => 3
  ]
  23 => array:3 [
    0 => "}"
    1 => 32
    2 => 4
  ]
  24 => array:3 [
    0 => ")"
    1 => 33
    2 => 7
  ]
]

然而,这是我的天赋耗尽的地方,我不知道如何处理令牌,以便我可以针对我的$ items数组运行条件。

可能是我接近这个完全错误。

0 个答案:

没有答案