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", ]; /* $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 */ 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; } foreach ($tokens as $token) { lex($token); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/fKHcS
function name:  (null)
number of ops:  9
compiled vars:  !0 = $tokens, !1 = $token
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   ASSIGN                                                   !0, <array>
   76     1      > FE_RESET_R                                       $3      !0, ->7
          2    > > FE_FETCH_R                                               $3, !1, ->7
   77     3    >   INIT_FCALL                                               'lex'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0          
   76     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $3
   78     8      > 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/fKHcS
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
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   36     2        STRLEN                                           ~8      !0
          3        ASSIGN                                                   !2, ~8
   37     4        ASSIGN                                                   !3, 0
   38     5        ASSIGN                                                   !4, <array>
   39     6        ASSIGN                                                   !5, !1
   40     7        ASSIGN                                                   !6, ''
   42     8      > JMP                                                      ->36
   44     9    >   FETCH_DIM_R                                      ~14     !0, !3
         10        ASSIGN                                                   !7, ~14
   45    11        FETCH_OBJ_IS                                     ~16     !5, 'data'
         12        ISSET_ISEMPTY_DIM_OBJ                         0          ~16, !7
         13      > JMPZ                                                     ~17, ->20
   47    14    >   PRE_INC                                                  !3
   49    15        ASSIGN_OP                                     8          !6, !7
   51    16        FETCH_OBJ_R                                      ~20     !5, 'data'
         17        FETCH_DIM_R                                      ~21     ~20, !7
         18        ASSIGN                                                   !5, ~21
         19      > JMP                                                      ->36
   52    20    >   FETCH_OBJ_R                                      ~23     !5, 'value'
         21      > JMPZ                                                     ~23, ->30
   55    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
   57    27        ASSIGN                                                   !6, ''
   59    28        ASSIGN                                                   !5, !1
         29      > JMP                                                      ->36
   62    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
   42    36    >   IS_SMALLER                                               !3, !2
         37      > JMPNZ                                                    ~32, ->9
   65    38    >   IS_NOT_IDENTICAL                                         !6, ''
         39      > JMPZ                                                     ~33, ->54
   67    40    >   FETCH_OBJ_R                                      ~34     !5, 'value'
         41      > JMPZ                                                     ~34, ->48
   68    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
   71    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
   74    54    > > RETURN                                                   !4
   75    55*     > RETURN                                                   null

End of function lex

Class Trie: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.35 ms | 1403 KiB | 14 Q