This article mainly introduces how to use PHP to write a simple interpreter. It has certain reference value. Now I share it with everyone. Friends in need can refer to it.
By chance in the circle of friends I found someone reading the book "Two Weeks of Self-Made Scripting Language" and thought it would be good to write a scripting language so that I can better understand the language itself. So, I bought it and took a look at it. The writing was quite easy to understand, but the inconvenience was that the language used was Java. Is PHP the best language? Why use Java.
In the past few days, I have also searched for some information on the Internet and found this to be good. https://github.com/rspivak/ls..., but again, this tutorial does not use PHP. As the author said, it's up to you which language you choose, and the interpreter does not depend on language features.
So, I rewrote part 1 in PHP, and in the next few days, I will rewrite all parts in PHP.
Write the code here to facilitate your own search, and I also hope that some friends who are interested in the interpreter can learn together.
<?php class Token{ private $type; private $value; public function __construct($type,$value) { $this->type=$type; $this->value=$value; } public function __get($name) { return $this->{$name}; } 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);
Currently only supports the addition of single-digit integers
The above is the entire content of this article, I hope it will be helpful to everyone's learning, more related content Please pay attention to PHP Chinese website!
Related recommendations:
thinkphp3.2.3 Introduction to how to use think-phpunit for unit testing
The above is the detailed content of How to write a simple interpreter using PHP. For more information, please follow other related articles on the PHP Chinese website!