3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Task 2: Parse the string * * Parse the $input string and build object tree that represents this structure: * https://drive.google.com/file/d/0BwhWcFzxN6_mZ2hkQlhZTnRrMDA/view?usp=sharing * Use Node class to build the tree and dump it to output * * Realize Node::dump() function and use it to output the tree in format: * A * B * C * C * C * B * C * C * A * B * B * C */ $input = 'A(B(CCC)B(CC))A(BB(C))'; class Node { public $letter; private $items = []; public function __construct($letter) { $this->letter = $letter; } public function addItem($item) { $this->items[] = $item; } public function dump($level = 0) { // write your code here... } } function createTree(string $input) { $jsonInput = $input; $jsonInput = str_replace(['(', ')'], ['[', ']'], $jsonInput); $jsonInput = preg_replace('~(\w)\[~', '"${1}":\[', $jsonInput); echo $jsonInput; } createTree($input);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8mgef
function name:  (null)
number of ops:  5
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   ASSIGN                                                   !0, 'A%28B%28CCC%29B%28CC%29%29A%28BB%28C%29%29'
   50     1        INIT_FCALL                                               'createtree'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   1

Function createtree:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8mgef
function name:  createTree
number of ops:  16
compiled vars:  !0 = $input, !1 = $jsonInput
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   43     1        ASSIGN                                                   !1, !0
   44     2        INIT_FCALL                                               'str_replace'
          3        SEND_VAL                                                 <array>
          4        SEND_VAL                                                 <array>
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !1, $3
   45     8        INIT_FCALL                                               'preg_replace'
          9        SEND_VAL                                                 '%7E%28%5Cw%29%5C%5B%7E'
         10        SEND_VAL                                                 '%22%24%7B1%7D%22%3A%5C%5B'
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                         $5      
         13        ASSIGN                                                   !1, $5
   46    14        ECHO                                                     !1
   47    15      > RETURN                                                   null

End of function createtree

Class Node:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8mgef
function name:  __construct
number of ops:  4
compiled vars:  !0 = $letter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        ASSIGN_OBJ                                               'letter'
          2        OP_DATA                                                  !0
   32     3      > RETURN                                                   null

End of function __construct

Function additem:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8mgef
function name:  addItem
number of ops:  5
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   34     1        FETCH_OBJ_W                                      $1      'items'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   35     4      > RETURN                                                   null

End of function additem

Function dump:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8mgef
function name:  dump
number of ops:  2
compiled vars:  !0 = $level
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV_INIT                                        !0      0
   38     1      > RETURN                                                   null

End of function dump

End of class Node.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.38 ms | 1403 KiB | 18 Q