PHP递归下降识别器没有输出有效输入

时间:2015-05-13 07:09:09

标签: php html parsing recursion recursive-descent

好的我有这种语言的PHP RDR:

EXP    ::= EXP  + TERM | EXP - TERM | TERM
TERM   ::= TERM * FACTOR | TERM / FACTOR | FACTOR
FACTOR ::= ( EXP ) | DIGIT
DIGIT  ::= 0 | 1 | 2 | 3

那应该能够接受输入1 + 2 * 2 $。但是,当输入输入并访问表单时,没有输出。与(1 + 2)-2 $相同。我在PHP表单的开头有一个回显,确保它被访问。此外,我可以让语法像1 $一样工作,只是当有多个运算符时,例如+, - ,*,/中的多个运算符,也不能使用(,或) 。我相信问题出现在专家,术语或因子函数中。我无法掌握它。 HTML页面接受用户输入,这是HTML代码:

<!DOCTYPE html>
<html>
<head>
    <title>PHP Recursive Descent Parser</title>
</head>
<body>  
    <center><form action="rdrTest2.php" method="post">
    <input type="text" name="userInput" />
    <input type="submit" name="sub" value="submit" />
    </form></center>
</body>
</html>

这是PHP代码:

<?php

echo "PHP Form Accessed.<br>";
$input = $_POST["userInput"];
$parser = new RDR4($input);

abstract class Grammar{
    // User input - string
    protected $inputString;
    //Pointer pointing to current position in input string
    protected $pointerInString;
    // boolean variable which will return true or false based on parsing result
    protected $resultString;
    //end of string variable '$ - in this case'.
    protected $endOfString;

    /**
    * Recursive Descent Parser
    * This function will get overridden by child classes
    */
    abstract protected function exper();

    function __construct($input, $delimiter = '$') {
        $this->inputString = $input; // user input string taken from input page
        $this->pointerInString = 0; // initial pointer value will be 0 - pointer pointing to first character in input string
        $this->resultString = true; // it will be set to false if program can not match string to the expected at any point in time while execution
        $this->endOfString = $delimiter;
        $this->exper(); // starting point for each parsing
        if(!$this->endOfInput())
        $this->resultString = false; // this means the string contains some unparsable character
    }
    /*
    * True if expression is resultString else False
    */
    function isresultString() {
        return $this->resultString;
    }

    /*
    */
    protected function endOfInput() {
        // check for end of the string
        $isDone = ($this->pointerInString >= strlen($this->inputString)) || (strlen($this->inputString) == 0);

        if($this->pointerInString == (strlen($this->inputString) - 1)) {

            if($this->inputString[$this->pointerInString] == $this->endOfString) {
                $isDone = true;
            }
            echo "Grammar is valid.";
            return $isDone;
        }
    }
    /*
    * match function basically matches character with current pointer character
    * if matches, it will advance pointer to next character and return true.
    */
    protected function match($myToken) {
        if(($this->pointerInString < strlen($this->inputString)) &&
        ($this->inputString[$this->pointerInString] == $myToken))
        {
            $this->pointerInString += 1;
            return true;
        }
        else
        return false;
    }
}

/**
* Grammar for RDR4 is:
* EXPER ::= EXPER + TERM | EXPER - TERM | TERM
* TERM ::= TERM * FACTOR | EXPER / FACTOR | FACTOR
* DIGIT ::= 0 | 1 | 2 | 3
* Assume the input ends with '$'.
*/

class RDR4 extends Grammar {
    function exper() {
$this->term();
        if($this->endOfInput())
        {
            $this->resultString = false;
            echo "Grammar is invalid.";
        }
        else
        {
            if($this->resultString)
            {
                if(($this->inputString[$this->pointerInString] == '+') || ($this->inputString[$this->pointerInString] == '-'))
                {
                    $this->match($this->inputString[$this->pointerInString]);
                }
                //echo "About to call term in exper.";
                $this->term();
            }
        }
    }

    function term() {
        if($this->endOfInput())
        {
            $this->resultString = false;
            echo "Grammar is invalid.";
        }
        else
        {
            if($this->resultString)
            {
                if(($this->inputString[$this->pointerInString] == '*') || ($this->inputString[$this->pointerInString] == '/'))
                {
                    $this->match($this->inputString[$this->pointerInString]);
                }
                //echo "About to call factor in term.";
                $this->factor();
            }
        }
    }

    function factor() {
        if($this->endOfInput()) {
            $this->resultString = false;
            echo "Grammar is invalid.";
        }
        if($this->resultString)
            {
            if($this->inputString[$this->pointerInString] == '0' || '1' || '2' || '3')
            {
                $this->digit(); //$this->match($this->inputString[$this->pointerInString]);
            }
            elseif($this->inputString[$this->pointerInString] == '(')
            {
                $this->match($this->inputString[$this->pointerInString]);
                $this->exper();
                if($this->endOfInput()) {
                    $this->resultString = false;
                    echo "Grammar is invalid.";
                }
                if($this->resultString)
                {
                    $this->match(')');
                }
            }
            else {
                $this->resultString = false;
                echo "Grammar is invalid.";
            }
        }
    }

    function digit() {
        $digitArray = array('0', '1', '2', '3');
        if($this->endOfInput()) {
            $this->resultString = false;
            echo "Grammar is invalid.";
        }
        elseif(array_search($this->inputString[$this->pointerInString], $digitArray) !== False)
        {
            $this->resultString = $this->resultString && $this->match($this->inputString[$this->pointerInString]);
        }
        else{
            $this->resultString = false;
            echo "Grammar is invalid.";
        }
    }
}

0 个答案:

没有答案
相关问题