3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Trie { public $data = []; public $value = false; } $tokens = [ "apple" => "T_APPLE", "ape" => "T_APE", "cape" => "T_CAPE", "(" => "T_L_PARENTH", ")" => "T_R_PARENTH", "is_string" => "T_IS_STRING", "is_bool" => "T_IS_BOOL", "!is_bool" => "T_NOT_IS_BOOL", "and" => "T_AND", "or" => "T_OR", ]; $root = new Trie; foreach ($tokens as $name => $value) { $node = $root; // For each character in the string for ($i = 0; $i < strlen($name); $i++) { $char = $name[$i]; if (!isset($node->data[$char])) { // If we don't have a child with that char // Create it $node->data[$char] = new Trie; } // Reset the node for the next character $node = $node->data[$char]; } // Finally, set the value on the final node $node->value = $value; } // "ape" is a valid token, so we expect T_APE // var_dump($root->data['a']->data['p']->data['n']->value); // T_APE var_dump( $root ->data['a'] ->data['p'] ->data['e'] ->value ); // T_APE var_dump( $root ->data['('] ->value ); // T_L_PARENTH var_dump( $root ->data['i'] ->data['s'] ->data['_'] ->data['s'] ->data['t'] ->data['r'] ->data['i'] ->data['n'] ->data['g'] ->value ); // T_APE function lex($string, Trie $root) { $length = strlen($string); $i = 0; $tokens = []; $node = $root; $buffer = ''; // We want to iterate over the entire string. while ($i < $length) { // Get the current character $char = $string[$i]; if (isset($node->data[$char])) { // We have a valid next character $i++; // Save the character in the buffer $buffer .= $char; // Move to the next state $node = $node->data[$char]; } elseif ($node->value) { // We have a value and no valid next character // Emit the token $tokens[] = [$node->value, $buffer]; // Clear the buffer $buffer = ''; // Reset back to the root for the next token $node = $root; } else { // We can't continue parsing this node throw new Exception("Syntax error at offset $i"); } } if ($buffer !== '') { // We finished without flushing one token if ($node->value) { $tokens[] = [$node->value, $buffer]; } else { // Not a valid complete token throw new Exception("Syntax error at offset $i"); } } return $tokens; } var_dump(lex('(is_string)', $root)); var_dump(lex('(is_string)&(is_string|!is_bool)', $root));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 31
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 31
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 10
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 10
Branch analysis from position: 28
Branch analysis from position: 10
Branch analysis from position: 21
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/OJAXH
function name:  (null)
number of ops:  85
compiled vars:  !0 = $tokens, !1 = $root, !2 = $value, !3 = $name, !4 = $node, !5 = $i, !6 = $char
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   ASSIGN                                                   !0, <array>
   23     1        NEW                                              $8      'Trie'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $8
   24     4      > FE_RESET_R                                       $11     !0, ->31
          5    > > FE_FETCH_R                                       ~12     $11, !2, ->31
          6    >   ASSIGN                                                   !3, ~12
   25     7        ASSIGN                                                   !4, !1
   27     8        ASSIGN                                                   !5, 0
          9      > JMP                                                      ->25
   28    10    >   FETCH_DIM_R                                      ~16     !3, !5
         11        ASSIGN                                                   !6, ~16
   29    12        FETCH_OBJ_IS                                     ~18     !4, 'data'
         13        ISSET_ISEMPTY_DIM_OBJ                         0  ~19     ~18, !6
         14        BOOL_NOT                                         ~20     ~19
         15      > JMPZ                                                     ~20, ->21
   32    16    >   NEW                                              $23     'Trie'
         17        DO_FCALL                                      0          
         18        FETCH_OBJ_W                                      $21     !4, 'data'
         19        ASSIGN_DIM                                               $21, !6
         20        OP_DATA                                                  $23
   35    21    >   FETCH_OBJ_R                                      ~25     !4, 'data'
         22        FETCH_DIM_R                                      ~26     ~25, !6
         23        ASSIGN                                                   !4, ~26
   27    24        PRE_INC                                                  !5
         25    >   STRLEN                                           ~29     !3
         26        IS_SMALLER                                               !5, ~29
         27      > JMPNZ                                                    ~30, ->10
   38    28    >   ASSIGN_OBJ                                               !4, 'value'
         29        OP_DATA                                                  !2
   24    30      > JMP                                                      ->5
         31    >   FE_FREE                                                  $11
   43    32        INIT_FCALL                                               'var_dump'
   45    33        FETCH_OBJ_R                                      ~32     !1, 'data'
         34        FETCH_DIM_R                                      ~33     ~32, 'a'
   46    35        FETCH_OBJ_R                                      ~34     ~33, 'data'
         36        FETCH_DIM_R                                      ~35     ~34, 'p'
   47    37        FETCH_OBJ_R                                      ~36     ~35, 'data'
         38        FETCH_DIM_R                                      ~37     ~36, 'e'
   48    39        FETCH_OBJ_R                                      ~38     ~37, 'value'
         40        SEND_VAL                                                 ~38
         41        DO_ICALL                                                 
   52    42        INIT_FCALL                                               'var_dump'
   54    43        FETCH_OBJ_R                                      ~40     !1, 'data'
         44        FETCH_DIM_R                                      ~41     ~40, '%28'
   55    45        FETCH_OBJ_R                                      ~42     ~41, 'value'
         46        SEND_VAL                                                 ~42
         47        DO_ICALL                                                 
   58    48        INIT_FCALL                                               'var_dump'
   60    49        FETCH_OBJ_R                                      ~44     !1, 'data'
         50        FETCH_DIM_R                                      ~45     ~44, 'i'
   61    51        FETCH_OBJ_R                                      ~46     ~45, 'data'
         52        FETCH_DIM_R                                      ~47     ~46, 's'
   62    53        FETCH_OBJ_R                                      ~48     ~47, 'data'
         54        FETCH_DIM_R                                      ~49     ~48, '_'
   63    55        FETCH_OBJ_R                                      ~50     ~49, 'data'
         56        FETCH_DIM_R                                      ~51     ~50, 's'
   64    57        FETCH_OBJ_R                                      ~52     ~51, 'data'
         58        FETCH_DIM_R                                      ~53     ~52, 't'
   65    59        FETCH_OBJ_R                                      ~54     ~53, 'data'
         60        FETCH_DIM_R                                      ~55     ~54, 'r'
   66    61        FETCH_OBJ_R                                      ~56     ~55, 'data'
         62        FETCH_DIM_R                                      ~57     ~56, 'i'
   67    63        FETCH_OBJ_R                                      ~58     ~57, 'data'
         64        FETCH_DIM_R                                      ~59     ~58, 'n'
   68    65        FETCH_OBJ_R                                      ~60     ~59, 'data'
         66        FETCH_DIM_R                                      ~61     ~60, 'g'
   69    67        FETCH_OBJ_R                                      ~62     ~61, 'value'
         68        SEND_VAL                                                 ~62
         69        DO_ICALL                                                 
  113    70        INIT_FCALL                                               'var_dump'
         71        INIT_FCALL                                               'lex'
         72        SEND_VAL                                                 '%28is_string%29'
         73        SEND_VAR                                                 !1
         74        DO_FCALL                                      0  $64     
         75        SEND_VAR                                                 $64
         76        DO_ICALL                                                 
  114    77        INIT_FCALL                                               'var_dump'
         78        INIT_FCALL                                               'lex'
         79        SEND_VAL                                                 '%28is_string%29%26%28is_string%7C%21is_bool%29'
         80        SEND_VAR                                                 !1
         81        DO_FCALL                                      0  $66     
         82        SEND_VAR                                                 $66
         83        DO_ICALL                                                 
         84      > RETURN                                                   1

Function lex:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 9
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 54
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 48
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 54
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 30
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 30
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/OJAXH
function name:  lex
number of ops:  56
compiled vars:  !0 = $string, !1 = $root, !2 = $length, !3 = $i, !4 = $tokens, !5 = $node, !6 = $buffer, !7 = $char
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   73     2        STRLEN                                           ~8      !0
          3        ASSIGN                                                   !2, ~8
   74     4        ASSIGN                                                   !3, 0
   75     5        ASSIGN                                                   !4, <array>
   76     6        ASSIGN                                                   !5, !1
   77     7        ASSIGN                                                   !6, ''
   79     8      > JMP                                                      ->36
   81     9    >   FETCH_DIM_R                                      ~14     !0, !3
         10        ASSIGN                                                   !7, ~14
   82    11        FETCH_OBJ_IS                                     ~16     !5, 'data'
         12        ISSET_ISEMPTY_DIM_OBJ                         0          ~16, !7
         13      > JMPZ                                                     ~17, ->20
   84    14    >   PRE_INC                                                  !3
   86    15        ASSIGN_OP                                     8          !6, !7
   88    16        FETCH_OBJ_R                                      ~20     !5, 'data'
         17        FETCH_DIM_R                                      ~21     ~20, !7
         18        ASSIGN                                                   !5, ~21
         19      > JMP                                                      ->36
   89    20    >   FETCH_OBJ_R                                      ~23     !5, 'value'
         21      > JMPZ                                                     ~23, ->30
   92    22    >   FETCH_OBJ_R                                      ~25     !5, 'value'
         23        INIT_ARRAY                                       ~26     ~25
         24        ADD_ARRAY_ELEMENT                                ~26     !6
         25        ASSIGN_DIM                                               !4
         26        OP_DATA                                                  ~26
   94    27        ASSIGN                                                   !6, ''
   96    28        ASSIGN                                                   !5, !1
         29      > JMP                                                      ->36
   99    30    >   NEW                                              $29     'Exception'
         31        NOP                                                      
         32        FAST_CONCAT                                      ~30     'Syntax+error+at+offset+', !3
         33        SEND_VAL_EX                                              ~30
         34        DO_FCALL                                      0          
         35      > THROW                                         0          $29
   79    36    >   IS_SMALLER                                               !3, !2
         37      > JMPNZ                                                    ~32, ->9
  102    38    >   IS_NOT_IDENTICAL                                         !6, ''
         39      > JMPZ                                                     ~33, ->54
  104    40    >   FETCH_OBJ_R                                      ~34     !5, 'value'
         41      > JMPZ                                                     ~34, ->48
  105    42    >   FETCH_OBJ_R                                      ~36     !5, 'value'
         43        INIT_ARRAY                                       ~37     ~36
         44        ADD_ARRAY_ELEMENT                                ~37     !6
         45        ASSIGN_DIM                                               !4
         46        OP_DATA                                                  ~37
         47      > JMP                                                      ->54
  108    48    >   NEW                                              $38     'Exception'
         49        NOP                                                      
         50        FAST_CONCAT                                      ~39     'Syntax+error+at+offset+', !3
         51        SEND_VAL_EX                                              ~39
         52        DO_FCALL                                      0          
         53      > THROW                                         0          $38
  111    54    > > RETURN                                                   !4
  112    55*     > RETURN                                                   null

End of function lex

Class Trie: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.17 ms | 1411 KiB | 17 Q