2

Ok I have a PHP RDR for the language:

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

That should be able to accept the input 1+2*2$. However, when the input is entered and the form is accessed there is no output. Same with (1+2)-2$. I have an echo at the beginning of the PHP form making sure that it is accessed. Also, I can get grammar's like 1$ to work, just not when there is an multiple operators such as more than one of +,-,*,/, also doesn't work with (, or ). I believe that the problem occurs in the exper, term or factor function. I just can't get a grasp on it. The HTML page accepts the user input, and here is the HTML code:

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

Here is the PHP code:

<?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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.