这篇文章主要介绍了关于如何使用php来写一个简单的解释器,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
偶然间在朋友圈发现有人在看一本《两周自制脚本语言》,觉得写个脚本语言挺不错的,方便自己对语言本身进一步了解。于是乎,买了下来看了看,写的挺通俗易懂,但是不便的是,采用的语言是Java,PHP才是最好的语言么!为什么要采用Java。
这几日,我也在网上搜索了一些资料,发现这个不错。https://github.com/rspivak/ls...,不过同样,该教程采用的也不是PHP。正如作者所言,选什么语言由你,解释器并不依赖语言特性。
于是乎,我用PHP重写了part1的部分,并在以后几日,将会采用PHP重写所有部分。
在这里写出代码方便自己查找,同时也希望一些对解释器感兴趣的朋友一同学习。
立即学习“PHP免费学习笔记(深入)”;
<?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);![1531289790332330.png 1340919138-5b455fcc88e15_articlex[1].png](https://img.php.cn//upload/image/397/322/136/1531289790332330.png)
目前仅支持个位整数相加
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
thinkphp3.2.3怎样使用think-phpunit来进行单元测试的介绍
以上就是如何使用PHP来写一个简单的解释器的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号