旗下导航:搜·么
当前位置:网站首页 > PHP问题 > 正文

php 完成类似于pyhon中的Construct库的功用(四)完成do-while功用【php题目】

作者:搜搜PHP网发布时间:2019-11-26分类:PHP问题浏览:99


导读:弁言在文章《php完成类似于pyhon中的Contruct库的功用(一)基础设想思绪》引见了用php剖析二进制数据的基础思绪在文章《php完成类似于pyhon...
弁言

在文章《php 完成类似于pyhon中的Contruct库的功用(一) 基础设想思绪》引见了用php剖析二进制数据的基础思绪

在文章《php 完成类似于pyhon中的Contruct库的功用(二)完成适配器功用》申明了怎样完成适配器功用。

在文章《php 完成类似于pyhon中的Contruct库的功用(三)完成if-else功用》申明了怎样if-else功用。

这一次要完成的是do-while功用。

引荐PHP视频教程:https://www.ki4.cn/course/list/29/type/2.html

基础思绪

1,修正词法剖析划定规矩,使其能够接收do,while关键字,为了援用内部变量,还需要辨认 $ 标记

2,修正语法剖析划定规矩,使其能够接收do,while语句

3,修正编码器,生成可运转的php目的代码

4,目的代码的模板文件也要做一些小的修正

个中重要的事情内容照样修正语法剖析划定规矩文件。

完成内容

预备剖析的构造体定义文件

struct student
{
  char name[2];
  do{
  	int;
  }while($lastError == 0);
};
为了聚焦在do-while功用的完成上,此次只定义了一个构造体 student。轮回语句定义以下:
  do{
  	int;
  }while($lastError == 0);

剖析历程中有一个内部变量 $lastError,保留每次剖析的毛病代号,假如为0则示意没有毛病。

下面是完全的语法划定规矩处置惩罚文件的内容

<?php
/*!
 * structwkr的语法划定规矩处置惩罚器
 * 45022300@qq.com
 * Version 0.9.0
 *
 * Copyright 2019, Zhu Hui
 * Released under the MIT license
 */

namespace Ados;

require_once 'const.php';
require_once __SCRIPTCORE__.'syntax_rule/base_rules_handler.php';

class StructwkrRulesHandler extends BaseRulesHandler{

//语法剖析的肇端暗号,归约到最后获得就是若干个构造体定义的列表
function startToken(){

	return '_structList';
}


//求出放在附加信息中的数组长度
function elementSize($extraArray){
	if(count($extraArray)>0){
		return intval($extraArray[0]);
	}else{
		return 0;
	}
}

//二元操作符的通用处置惩罚函数
function biOpertors($stack,$op1Index,$op,$op2Index,$coder){

	$t1= $this->topItem($stack,$op1Index);
	$exp1=$t1[TokenValueIndex];

	$t2= $this->topItem($stack,$op2Index);
	$exp2=$t2[TokenValueIndex];

	$s=$exp1.$op.$exp2;
	return [$s,[]];
}

//处置惩罚移进,返回附加信息数组
function handleShift($tokenName,$stack,$coder){

	if($tokenName=='_if'){
		//插进去一个空行,空行地点的序号存入附加信息数组,今后能够替换为准确的内容
		return [$coder->pushLine('')];
	}

	if($tokenName=='_else'){
		//插进去一个空行,空行地点的序号存入附加信息数组,今后能够替换为准确的内容
		return [$coder->pushLine('')];
	}

	if($tokenName=='_do'){
		//插进去一个空行,空行地点的序号存入附加信息数组,今后能够替换为准确的内容
		$coder->isInLoop = True;
		return [$coder->pushLine('')];
	}

	if($tokenName=='_while'){
		//插进去一个空行,空行地点的序号存入附加信息数组,今后能够替换为准确的内容
		return [$coder->pushLine('')];
	}	

	return [];
}


//语法划定规矩处置惩罚函数名由划定规矩右侧部份与划定规矩左侧部份拼接而成
//语法划定规矩定义的前后决议了归约时婚配的递次,要根据现实的语法部署
//假如不熟悉语法,随便调解语法划定规矩的前后序次将有能够致使语法毛病

// struct list  {{{

function _structList_0_structList_struct($stack,$coder){

	$coder->pushBlockTail();
	return ['#',[]];
	
	
}

function _structList_0_struct($stack,$coder){	
	$coder->pushBlockTail();
	return ['#',[]];
}

// struct list  }}}


// struct   {{{

function _struct_0_structName_blockStatement_semi($stack,$coder){

	$t1= $this->topItem($stack,3);
	$structName = 	$t1[TokenValueIndex];

	$t2= $this->topItem($stack,2);
	$extraArray=$t2[TokenExtraIndex];
	
	return [$structName,$extraArray];
}

// struct   }}}

// struct name     {{{

function _structName_0_strukey_iden($stack,$coder){  

	$t1= $this->topItem($stack,1);
	$structName = 	$t1[TokenValueIndex];

	$coder->pushBlockHeader($structName);	
	return $this->pass($stack,1);
}

// struct name     }}}

// blockStatement    {{{

function _blockStatement_0_lcb_statementList_rcb($stack,$coder){

	return $this->pass($stack,2);

}

// blockStatement    }}}

// statement list  {{{

function _statementList_0_statementList_statement($stack,$coder){
	
	return $this->pass($stack,1);
}

function _statementList_0_statement($stack,$coder){
	//此处0示意statementList是上一级节点,要做特别处置惩罚
	return $this->pass($stack,1);
}

// statement list  }}}


// statement       {{{

function _statement_0_wholeExpression_semi($stack,$coder){

	$t1= $this->topItem($stack,2);
	$elementName = 	$t1[TokenValueIndex];	
	$coder->pushCheckBody($elementName);	
	return $this->pass($stack,2);
}



// statement        }}}


// if          {{{

function _statement_0_ifStatement($stack,$coder){
	
	return $this->pass($stack,1);
}

function _ifStatement_0_ifStatement_else_blockStatement($stack,$coder){	

	//掏出_else 暗号中保留的空白行地点的地点,替换为准确的内容
	$t1= $this->topItem($stack,2);
	$lineIndex=$t1[TokenExtraIndex][0];

	$content='else{';

	$coder->resetLine($lineIndex,$content);	

	$coder->pushLine('}');	

	return $this->pass($stack,3);
}

function _ifStatement_0_if_wholeExpression_blockStatement($stack,$coder){
	
	//掏出_if 暗号中保留的空白行地点的地点,替换为准确的内容
	$t1= $this->topItem($stack,3);
	$lineIndex=$t1[TokenExtraIndex][0];

	$t2= $this->topItem($stack,2);
	$condtionExp=$t2[TokenValueIndex];

	$content='if'.$condtionExp.'{';

	$coder->resetLine($lineIndex,$content);	

	$coder->pushLine('}');

	return $this->pass($stack,3);
}

//  if        }}}

// do while   {{{

function _statement_0_whileStatement($stack,$coder){
	
	return $this->pass($stack,1);
}

function _whileStatement_0_doBlock_whileExpression_semi($stack,$coder){
	
	$t1= $this->topItem($stack,2);
	
	$condtionExp=$t1[TokenValueIndex];

	$content='while('.$condtionExp.');';

	$coder->pushLine($content);		

	$coder->isInLoop = False;

	return $this->pass($stack,3);
}

function _whileExpression_0_whileLp_wholeExpression_rp($stack,$coder){
	
	return $this->pass($stack,2);
}

function _whileLp_0_while_lp($stack,$coder){
	
	return $this->pass($stack,2);
}

function _doBlock_0_do_blockStatement($stack,$coder){

	//掏出_do 暗号中保留的空白行地点的地点,替换为准确的内容
	$t1= $this->topItem($stack,2);
	$lineIndex=$t1[TokenExtraIndex][0];

	$content='do {';

	$coder->resetLine($lineIndex,$content);	

	$coder->pushLine('}');
	
	return $this->pass($stack,1);
}

//  do while    }}}


// function expression {{{


//函数表达式
function _term_0_funcTerm($stack,$coder){  

	$t1= $this->topItem($stack,1);
	$funcName=$t1[TokenValueIndex];
	$paraArray=$t1[TokenExtraIndex]; 
	$paras = implode(",", $paraArray);
	$exp = $funcName.'('.$paras.')';	
	return [$exp,[]];

}

function _funcTerm_0_funcExpLp_rp($stack,$coder){  

	return $this->pass($stack,2);
}

function _funcTerm_0_funcExpLeft_rp($stack,$coder){  

	return $this->pass($stack,2);
}

function _funcExpLeft_0_funcExpLeft_comma_expression($stack,$coder){  
			
	$t1= $this->topItem($stack,3);
	$t2= $this->topItem($stack,1);

	//函数的参数列表存放在附加信息中
	$paraArray=$t1[TokenExtraIndex]; 
	array_push($paraArray, $t2[TokenValueIndex]);
	
	return [$t1[TokenValueIndex],$paraArray];
}

function _funcExpLeft_0_funcExpLp_expression($stack,$coder){  
	
	$t1= $this->topItem($stack,2);
	$t2= $this->topItem($stack,1);

	//函数的参数列表存放在附加信息中
	$paraArray=$t1[TokenExtraIndex]; 
	array_push($paraArray, $t2[TokenValueIndex]);
	
	return [$t1[TokenValueIndex],$paraArray];
}

function _funcExpLp_0_iden_lp($stack,$coder){  
	
	return $this->pass($stack,2);

}

// function expression }}}


// whole Expression  {{{

function _wholeExpression_0_wholeExpression_bieq_expression($stack,$coder){
	
	return $this->biOpertors($stack,3,'==',1,$coder);
}

function _wholeExpression_0_expression($stack,$coder){
	
	return $this->pass($stack,1);
}

// whole Expression      }}}


//    Expression         {{{

//表达式能够举行管道运算
function _expression_0_expression_pipe_factor($stack,$coder){

	$t1= $this->topItem($stack,1);
	$handlerName = 	$t1[TokenValueIndex];

	$t2= $this->topItem($stack,3);
	$elementName = 	$t2[TokenValueIndex];

	$coder->pushPipeBody($handlerName,$elementName);	
	
	return $this->pass($stack,3);
}


function _expression_0_double_factor($stack,$coder){	

	$t1= $this->topItem($stack,1);
	$elementName = 	$t1[TokenValueIndex];
	$parseFuncName = 'parseDouble';	
	$coder->pushParseBody($parseFuncName,$elementName);	
	
	return $this->pass($stack,1);
}

function _expression_0_float_factor($stack,$coder){	

	$t1= $this->topItem($stack,1);
	$elementName = 	$t1[TokenValueIndex];
	$parseFuncName = 'parseFloat';	
	$coder->pushParseBody($parseFuncName,$elementName);	
	
	return $this->pass($stack,1);
}


function _expression_0_char_factor($stack,$coder){

	$t1= $this->topItem($stack,1);
	$elementName = 	$t1[TokenValueIndex];
	$size = $this->elementSize($t1[TokenExtraIndex]);
	
	$parseFuncName = 'parseFixStr';	
	$coder->pushParseBody($parseFuncName,$elementName,$size);		
	
	return $this->pass($stack,1);
}

function _expression_0_int_factor($stack,$coder){	

	$t1= $this->topItem($stack,1);
	$elementName = 	$t1[TokenValueIndex];
	$parseFuncName = 'parseInt';	
	$coder->pushParseBody($parseFuncName,$elementName,4);	
	
	return $this->pass($stack,1);
}

function _expression_0_int($stack,$coder){	

	$elementName = 	'';
	$parseFuncName = 'parseInt';	
	$coder->pushParseBody($parseFuncName,$elementName,4);	
	
	return ['',[]];
}

function _expression_0_factor($stack,$coder){
	
	return $this->pass($stack,1);
}

//   Expression         }}}

// factor       {{{

function _factor_0_term($stack,$coder){
	
	return $this->pass($stack,1);
}

//  factor        }}}

//   term    {{{

function _term_0_lp_wholeExpression_rp($stack,$coder){
	
	$t1= $this->topItem($stack,2);
	$s='('.$t1[TokenValueIndex].')';
	return [$s,[]];
}

function _term_0_term_dot_iden($stack,$coder){  
		
	$t1= $this->topItem($stack,3);
	$obj = $t1[TokenValueIndex];
	$t2= $this->topItem($stack,1);
	$var = $t2[TokenValueIndex];
	$exp = '$'.$obj.'[\''.$var.'\']';

	return [$exp,[]];
}

function _term_0_dollar_iden($stack,$coder){
	$t1= $this->topItem($stack,1);	
	$exp = '$'.$t1[TokenValueIndex];
	return [$exp,[]];	
}

function _term_0_iden($stack,$coder){
	$t1= $this->topItem($stack,1);
	//未指定数据长度时将长度值设为0 
	$valLen = 	'0';	
	$t2= $this->topItem($stack,2);
	return [$t1[TokenValueIndex],[$valLen]];	
}



function _term_0_num($stack,$coder){
	return $this->pass($stack,1);
}

function _term_0_array($stack,$coder){
	return $this->pass($stack,1);
}

//   term     }}}


// array 	  {{{

function _array_0_arrayLb_num_rb($stack,$coder){

	$t1= $this->topItem($stack,2);
	$valLen = 	$t1[TokenValueIndex];	
	
	$t2= $this->topItem($stack,3);
	//将数据长度放入附加信息 
	return [$t2[TokenValueIndex],[$valLen]];		
}

function _arrayLb_0_iden_lb($stack,$coder){  
	return $this->pass($stack,2);
}

// array 	   }}}

}// end of class

下面是革新事后的编码器的内容

<?php
/*!
 * structwkr编码器,
 *
 * 45022300@qq.com
 * Version 0.9.0
 *
 * Copyright 2019, Zhu Hui
 * Released under the MIT license
 */

namespace Ados;

require_once __SCRIPTCORE__.'coder/base_coder.php';
require_once __STRUCT_PARSE_TEMP__.'templateReplaceFuncs.php';


class StructwkrCoder extends BaseCoder{

	public function __construct($engine)
	{
		if($engine){ 
			$this->engine = $engine;
		}else{ 
			exit('the engine is not valid in StructwkrCoder construct.');
		}
		$this->isInLoop = False;
	}

	//编译获得的终究效果
	public function codeLines(){
		if(count($this->codeLines)<1){
			return '';
		}
		$script='';		
		for ($i=0;$i< count($this->codeLines);$i+=1) {
			$script.=$this->codeLines[$i];
		}	
		return $script;
	}	

	//输出编译后的效果
	public function printCodeLines(){
		echo $this->codeLines();	
	}

	//增加一个块剖析函数头
	public function pushLine($content){		
		array_push($this->codeLines, $content);
		$lineIndex=$this->lineIndex;
		$this->lineIndex+=1;
		return $lineIndex;		
	}

	//重置一行的内容
	public function resetLine($lineIndex,$line){

		$this->codeLines[$lineIndex]=$line;
	}

	//增加一个块剖析函数头
	public function pushBlockHeader($structName){
		$structName=ucfirst($structName);
		$content = makeBlockHeader($structName);		
		return $this->pushLine($content);		
	}

	//增加一个块剖析函数体
	public function pushParseBody($parseFuncName,$filedName='',$filedSize=0){
		$content = makeParseBody($parseFuncName,$filedName,$filedSize);
		return $this->pushLine($content);
	}

	//增加一个管道处置惩罚
	public function pushPipeBody($handler,$filedName=''){
		$content = makePipeBody($handler,$filedName);		
		return $this->pushLine($content);
	}

	//增加一个搜检效果值的body
	public function pushCheckBody($filedName=''){
		if($this->isInLoop){
			$content = makeCheckBodyInLoop($filedName);
		}else{
			$content = makeCheckBody($filedName);
		}		
		return $this->pushLine($content);
	}	

	//增加一个块剖析类的tail
	public function pushBlockTail(){
		$content = makeblockTail();
		return $this->pushLine($content);
	}	

}

完成效果

自动生成的测试文件以下

<?php

namespace Ados;

//加载常量定义文件
require_once 'const.php';
require_once __STRUCT_PARSE_TEMP__.'templateBuidinFuncs.php';
require_once __STRUCT_PARSE_ADAPTER__.'int2str.adapter.php';
require_once __STRUCT_PARSE_ADAPTER__.'intoffset.adapter.php';

$context['pos']=0;
$context['data']="\x41\x42\x01\x00\x00\x00\x02\x00\x00\x00\x41\x42\x43\x41\x42\x01\x00\x00\x00\x41\x42\x43";

$expRes = Student::parse($context);
$context['pos']+=$expRes['size'];
print_r($expRes);

/*
$expRes = Teacher::parse($context);
$context['pos']+=$expRes['size'];
print_r($expRes);
*/

class Student{

	static function parse($context,$size=0){
		$valueArray=[];
		$totalSize = 0;
	
		$name = parseFixStr($context,2);
		$lastError = $name['error'];
	
		/*checkBodyInLoop{{*/
		if($name['error']==0){
			$filed = 'name';
			if($filed){
				$valueArray[$filed]=$name['value'];
			}else{
				$valueArray[]=$name['value'];
			}		
			$context['pos']+=$name['size'];
			$totalSize+= $name['size'];
		}
		/*checkBodyInLoop}}*/
		else{
			return ['value'=>False,'size'=>0,'error'=>$name['error'],'msg'=>$name['msg']];
		}
	do {
		$expRes = parseInt($context,4);
		$lastError = $expRes['error'];
	
		if($expRes['error']==0){
			$filed = '';
			if($filed){
				$valueArray[$filed]=$expRes['value'];
			}else{
				$valueArray[]=$expRes['value'];
			}		
			$context['pos']+=$expRes['size'];
			$totalSize+= $expRes['size'];
		}
		}while($lastError==0);
		return ['value'=>$valueArray,'size'=>$totalSize,'error'=>0,'msg'=>'ok'];
	}
}	

能够看出在测试文件中自动生成了 do-while轮回构造

运转测试文件的效果

Array
(
    [value] => Array
        (
            [name] => AB
            [0] => 1
            [1] => 2
            [2] => 1094926913
            [3] => 322
            [4] => 1128415488
        )

    [size] => 22
    [error] => 0
    [msg] => ok
)

对照测试数据

$context['data']="\x41\x42\x01\x00\x00\x00\x02\x00\x00\x00\x41\x42\x43\x41\x42\x01\x00\x00\x00\x41\x42\x43";

在剖析完两个字节的定长字符串 ‘AB’以后,最先按4个节一段 轮回剖析 整数值,直到剩下的内容没法剖析为一个整数为止。

结论:do-while功用已完成并通过了考证。

更多相干题目请接见ki4网:https://www.ki4.cn/

以上就是php 完成类似于pyhon中的Construct库的功用(四)完成do-while功用的细致内容,更多请关注ki4网别的相干文章!

标签:construct