I have two files file.index.php and lexical.php I use namespace in lexical.php and in this file I use from EXCEPTION class. When I run the code I receive error and I don't know why!!!
( ! ) Fatal error: Uncaught exception 'Exception' with message 'invalid character: ' in C:\wamp\www\oop\lexical\lexical.php on line 50
( ! ) Exception: invalid character: in C:\wamp\www\oop\lexical\lexical.php on line 50
this is index.php ::
<?php
require_once 'lexical/lexical.php';
require_once('Token.php');
use lexical;
$input = '[ a, b, c, d]';
$lexer = new lexical\ListLexer($input);
$token = $lexer->nextToken();
...
and this is lexical.php code ::
<?php
namespace lexical;
use \Exception;
require_once('lexical/lexer.php');
class ListLexer extends \lexical\Lexer {
const NAME = 2;
const COMMA = 3;
const LBRACK = 4;
const RBRACK = 5;
static $tokenNames = array("n/a", "<EOF>",
"NAME", "COMMA",
"LBRACK", "RBRACK" );
public function getTokenName($x) {
return ListLexer::$tokenNames[$x];
}
public function ListLexer($input) {
parent::__construct($input);
}
public function isLETTER() {
return $this->c >= 'a' &&
$this->c <= 'z' ||
$this->c >= 'A' &&
$this->c <= 'Z';
}
public function nextToken() {
while ( $this->c != self::EOF ) {
switch ( $this->c ) {
case ' ' : case '\t': case '\n': case '\r': $this->WS();
continue;
case ',' : $this->consume();
return new Token(self::COMMA, ",");
case '[' : $this->consume();
return new Token(self::LBRACK, "[");
case ']' : $this->consume();
return new Token(self::RBRACK, "]");
default:
if ($this->isLETTER() ) return $this->NAME();
throw new Exception("invalid character: " . $this->c);
}
}
return new Token(self::EOF_TYPE,"<EOF>");
}
.
.
.
this is lexer.php ::
abstract class Lexer {
const EOF = -1; // represent end of file char
const EOF_TYPE = 1; // represent EOF token type
protected $input; // input string
protected $p = 0; // index into input of current character
protected $c; // current character
public function Lexer($input) {
$this->input = $input;
// prime lookahead
$this->c = substr($input, $this->p, 1);
}
/** Move one character; detect "end of file" */
public function consume() {
$this->p++;
if ($this->p >= strlen($this->input)) {
$this->c = Lexer::EOF;
}
else {
$this->c = substr($this->input, $this->p, 1);
}
}
public abstract function nextToken();
public abstract function getTokenName($tokenType);
}
What i do ?
Read your exception, $this->c is empty.
default:
if ($this->isLETTER() ) return $this->NAME();
+ var_dump($this->c);
throw new Exception("invalid character: " . $this->c);
What does the self::EOF equal? I think the issue is that input string you provided '[ a, b, c, d]' does not contain self::EOF character and hence you get an Exception when $this->c equal empty string at end of the input.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.