How to use PHP to write a simple interpreter

Source: Internet
Author: User
This article mainly introduces about how to use PHP to write a simple interpreter, has a certain reference value, now share to everyone, the need for friends can refer to

Occasionally in the circle of friends to find someone reading a "two-week scripting language", think that writing a script language is very good, convenient for themselves to understand the language itself. So, bought down to see, write very easy to understand, but the inconvenience is, the language is java,php is the best language! Why Java should be used.

These days, I also search some information on the Internet, found this good. Https://github.com/rspivak/ls ..., but again, this tutorial is not PHP. As the author says, the language you choose, the interpreter does not depend on language features.

So, I rewrote the part1 section with PHP, and in the next few days, I'll rewrite all the parts in PHP.

Write the code here to make it easy for you to find, but also hope that some of the interpreter interested in learning together with friends.

<?phpclass token{Private $type;    Private $value;        Public function __construct ($type, $value) {$this->type= $type;    $this->value= $value;    The Public Function __get ($name) {return $this->{$name};    The Public Function __tostring () {return ' type: '. $this->type. ' Value: '. $this->value;    }}class interpreter{private $current _char;    Private $current _token;    Private $text;    Private $pos = 0;    Public function __construct ($text) {$this->text=trim ($text);    Public Function error () {throw (' Error parsing input ');        Public Function Get_next_token () {$text = $this->text;        if ($this->pos > strlen ($text)-1) {return new Token (' EOF ', null);        } $this->current_char = $text [$this->pos];            if (Is_numeric ($this->current_char)) {$token =new token (' INTEGER ', intval ($this->current_char)); $this->pos++;        return $token;            } if ($this->current_char== "+") {$token = new token (' PLUS ', $this->current_char);            $this->pos + +;        return $token;    } $this->error (); Public function Eat ($token _type) {if ($this->current_token->type== $token _type) {$this        ->current_token= $this->get_next_token ();        }else{$this->error ();        }} public Function expr () {$this->current_token= $this->get_next_token ();        $left = $this->current_token;        $this->eat (' INTEGER ');        $op = $this->current_token;        $this->eat (' PLUS ');        $right = $this->current_token;        $this->eat (' INTEGER ');        $result = $left->value+ $right->value;    return $result;    }}do{fwrite (STDOUT, ' xav> ');    $input =fgets (STDIN);    $Interpreter =new Interpreter ($input);    echo $Interpreter->expr (); UnSet ($Interpreter); }while (TRUE);

Only single-digit integer additions are currently supported

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.