在php

时间:2015-11-21 19:37:21

标签: php exception-handling namespaces

我有两个文件file.index.php和lexical.php 我在lexical.php中使用了命名空间,在这个文件中我使用了EXCEPTION类。 当我运行代码时,我收到错误,我不知道为什么!!!

( ! ) 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

这是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();
...

这是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>");
    }
.
.
.

这是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);
}

我做什么?

2 个答案:

答案 0 :(得分:0)

阅读您的例外情况,$ this-&gt; c为空。

            default:
                if ($this->isLETTER() ) return $this->NAME();
+               var_dump($this->c);
                throw new Exception("invalid character: " . $this->c);

答案 1 :(得分:0)

自我:: EOF是什么?我认为问题在于您提供的输入字符串&#39; [a,b,c,d]&#39;不包含self :: EOF字符,因此当$ this-&gt; c在输入结束时等于空字符串时会出现异常。