PHP递归下降识别器 - 不工作

时间:2015-05-13 01:30:49

标签: php parsing recursion

好的我正在尝试让我的PHP RDR正常工作,当我从HTML文件访问PHP表单时,我没有得到任何输出。例如,我没有收到回音“Grammer有效/无效”。一开始的回声让我知道我的html正确访问了表单,但之后它就是空白。输入在开头时正确输入,但我相信调用我的函数时出错。这大部分已经完成,所以不应该花太多精力去工作。我正在使用的输入的有效语法测试是225 $。任何建议表示赞赏。 这是HTML:

<html>
<body>
<form action="rdrTest2.php" method="post">
Enter string: <input type="text" name="userInput"><br><br>
<input type ="submit" value ="Submit">
</form>
</body>
</html>

这是PHP:

<?php
/**
* Grammar class
* The Grammar class is a main/top level class for parsing a string
* and matching it to a grammar.
* grammar rules will be implemented in to another class which will extend this basic class
*/

echo "PHP Form Accessed.<br>";
$input = $_POST["userInput"];

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 exp();

    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->exp(); // 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:
* EXP ::= + NUM | -NUM | NUM
* NUM ::= NUM DIGIT | DIGIT
* DIGIT ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
* Assume the input ends with '$'.
*/

class RDR4 extends Grammar {
    function exp() {
        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]);
                }
                $this->num();
            }
        }
    }
    /*
    * handle processing for the term rule in the grammar
    * handle processing for the factor
    */
    function num() {
        $this->digit();
        while($this->resultString && !$this->endOfInput())
        $this->digit();
    }
    /*
    * If the character at the current position is in [0..3]
    * advance the position pointer else change resultString to false.
    */
    function digit() {
        $digitArray = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        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.";
        }
    }
}

1 个答案:

答案 0 :(得分:1)

感谢Ryan的评论,我能够发现我的问题。 RDR4类是Grammar类的扩展,需要创建。我使用行$ parser = new RDR4($ input)创建了对象;在$ input = $ _POST [“input”]之后;在PHP表单的开头。这解决了我的问题,现在可以调用函数。再次感谢Ryan!