3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php namespace Fu { final class Exception extends \Exception { } final class Token { public $tokenType; public $content; public $contentLength; final public function __construct($tokenType, $content, $contentLength) { $this->tokenType = $tokenType; $this->content = $content; $this->contentLength = $contentLength; } } final class Parser { const FU_TOKENTYPE_WHITESPACE = 0x00000001; const FU_TOKENTYPE_DATATYPE_CHAR = 0x00000002; const FU_TOKENTYPE_VARIABLE = 0x00000003; const FU_TOKENTYPE_SEMICOLON = 0x00000004; const FU_TOKENTYPE_OPERATOR_ASSIGN = 0x00000005; const FU_TOKENTYPE_LITERAL_INTEGER = 0x00000006; const FU_TOKENTYPE_OPERATOR_ADD = 0x00000007; //const FU_SCOPE_ROOT = 0x00000001; //const FU_SCOPE_DECLARATION_VARIABLE = 0x00000002; //const FU_SCOPE_ final public static function parse($source) { $tokens = []; while ($source !== '') { $token = NULL; switch (substr($source, 0, 1)) { case ' ': case "\t": case "\n": $i = 1; while ($source{$i} === ' ' || $source{$i} === "\t" || $source{$i} === "\n") { ++$i; } $tokens[] = new Token(self::FU_TOKENTYPE_WHITESPACE, substr($source, 0, $i), $i); $source = substr($source, $i + 1); break; case 'c': if (substr($source, 0, 4) === 'char') { $tokens[] = new Token(self::FU_TOKENTYPE_DATATYPE_CHAR, 'char', 4); $source = substr($source, 4); } else { throw new Exception('Parse error at line x, position x; unidentified symbol'); } break; case '$': $i = 1; while (in_array($source{$i}, ['_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])) { ++$i; } $tokens[] = new Token(self::FU_TOKENTYPE_VARIABLE, substr($source, 0, $i), $i); $source = substr($source, $i + 1); break; case ';': $tokens[] = new Token(self::FU_TOKENTYPE_SEMICOLON, ';', 1); $source = substr($source, 1); break; case '=': $tokens[] = new Token(self::FU_TOKENTYPE_OPERATOR_ASSIGN, '=', 1); $source = substr($source, 1); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': $i = 1; while (in_array($source{$i}, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])) { ++$i; } // TODO: float parsing $tokens[] = new Token(self::FU_TOKENTYPE_LITERAL_INTEGER, substr($source, 0, $i), $i); $source = substr($source, $i + 1); break; case '+': $tokens[] = new Token(self::FU_TOKENTYPE_OPERATOR_ADD, '+', 1); $source = substr($source, 1); break; } } return $tokens; } } } namespace { ini_set('memory_limit', '16M'); var_dump(Fu\Parser::parse('char $x;$x=1+2;')); }
Output for 5.4.0 - 7.1.0

Process exited with code 137.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/taqnh on line 28
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/taqnh on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/taqnh on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/taqnh on line 3
Process exited with code 255.