糖果派对官方网站_可以赌钱的糖果游戏_手机版
【bb电子糖果派对】对编写翻译原理风野趣的进

【bb电子糖果派对】对编写翻译原理风野趣的进

作者:操作系统    来源:未知    发布时间:2020-02-12 22:02    浏览量:

bb电子糖果派对,明天尝试做了文法分析的东东,难题很多。请提提出。代码放不下,分两页。下载地址

include'ttrie.php';classRuleextendsTTrie{public$rule=array();public$savematch=0;function__construct($s=''){$this-set(array(''='Separated',"rn"='set_rule',"n"='set_rule',"t"='Separated','-'='Separated','→'='Separated','|'='set_parallel_rule',));$this-match($s);if($this-rule[0][0]==$this-rule[0][1]){if(count($this-rule[0])==2)$this-rule[0][0].="'";elsearray_unshift($this-rule,array($this-rule[0][0]."'",$this-rule[0][0]));}else{$c=$this-rule[0][0];$n=0;foreach($this-ruleas$r)if($r[0]==$c)$n++;if($n1)array_unshift($this-rule,array($this-rule[0][0]."'",$this-rule[0][0]));}}functionSeparated(){}functionset_rule(){$this-rule[]=$this-buffer;$this-buffer=array();}functionset_parallel_rule(){$t=$this-buffer[0];$this-set_rule();$this-buffer[]=$t;}}classGrammar{var$closure=array();var$first=array();var$follow=array();var$rule=array();var$identifier=array();var$leay=array();var$forecast=array();var$stack=array();var$ll='LL(0)';var$lr='LR(0)';function__construct($s=''){$p=newRule($s);$this-rule=$p-rule;$this-set_grammar();}functionset_grammar(){foreach($this-ruleas$rule){if(!in_array($rule[0],$this-identifier))$this-identifier[]=$rule[0];}foreach($this-ruleas$rule){foreach($ruleas$v)if(!in_array($v,$this-identifier)!in_array($v,$this-leay))$this-leay[]=$v;}$this-set_first();$this-set_follow();$this-set_closure();$this-set_select();$this-set_forecast();}functionset_first(){foreach($this-ruleas$rule)$this-first[$rule[0]]=array();//直接收取形如U-a…的产生式,把a收入到First(U)中foreach($this-ruleas$v){if(in_array($v[1],$this-leay))$this-first[$v[0]][]=$v[1];}//反复传递形入U-P1P2P3…Pn的产生式,应先把First(P1)中的全部内容传送到First(U)中,如果P1中有ε,把First(P2)中的内容传送到First(U)中,类推直到Pi中无εdo{$t=serialize($this-first);foreach($this-ruleas$rule){for($i=1;$icount($rule);$i++){$v=$rule[$i];if(in_array($v,$this-identifier)){$this-first[$rule[0]]=array_unique(array_merge($this-first[$rule[0]],$this-first[$v]));if(!in_array('#',$this-first[$v]))break;}elsebreak;}}}while($t!=serialize($this-first));}functionset_follow(){foreach($this-ruleas$rule)$this-follow[$rule[0]]=array();//直接收取形如…Ua…的,把a直接收入到Follow(U)中foreach($this-ruleas$rule){for($i=1;$icount($rule)-1;$i++){if(in_array($rule[$i],$this-identifier)in_array($rule[$i+1],$this-leay))$this-follow[$rule[$i]][]=$rule[$i+1];}if(in_array($rule[$i],$this-identifier))$this-follow[$rule[$i]][]='#';}foreach($this-followas$v)if(!$v)$v[]='#';//直接收取形如…UP…(P是非终结符)的,把First(P)中非ε收入到Follow(U)中foreach($this-ruleas$rule){for($i=1;$icount($rule)-1;$i++){if(in_array($rule[$i],$this-identifier)in_array($rule[$i+1],$this-identifier)){$this-follow[$rule[$i]]=array_unique(array_merge($this-follow[$rule[$i]],array_diff($this-first[$rule[$i+1]],array('#'))));}}}//反复传递形如U-aP的(P是非终结符)或U-aPQ(P,Q为非终结符且Q中含ε),应把Follow(U)中的全部内容传送到Follow(P)中do{$t=serialize($this-follow);foreach($this-ruleas$rule){$s=$rule[0];$d=end($rule);if(in_array($d,$this-leay))continue;$p=prev($rule);if(in_array($p,$this-leay))$this-follow[$d]=array_unique(array_merge($this-follow[$d],$this-follow[$s]));elseif(in_array('#',$this-follow[$d]))$this-follow[$p]=array_unique(array_merge($this-follow[$p],$this-follow[$s]));}}while($t!=serialize($this-follow));}functionset_closure(){$shift=array();$this-closure[0][]=array('offs'=1,'rule'=0);for($i=0;$icount($this-closure);$i++){$cnt=count($this-closure);//构造闭包closure$ex=array();$j=0;$tmp=array();do{$size=count($this-closure[$i]);for($j=0;$jcount($this-closure[$i]);$j++){$dfa=$this-closure[$i][$j];$rule=$this-rule[$dfa['rule']];if(isset($rule[$dfa['offs']])){$ch=$ex[]=$rule[$dfa['offs']];}foreach($this-ruleas$r=$rule){if(in_array($rule[0],$ex)){$t=array('offs'=1,'rule'=$r);if(!isset($tmp[$r][1]))$this-closure[$i][]=$t;$tmp[$r][1]=1;}}}}while(count($this-closure[$i])!=$size);//直到不再增大//判断状态转向go$out=array();foreach($this-closure[$i]as$k=$dfa){$rule=$this-rule[$dfa['rule']];if(isset($rule[$dfa['offs']])){$t="$dfa[rule],$dfa[offs]";$ch=$rule[$dfa['offs']];$this-closure[$i][$k]['char']=$ch;if(isset($out[$ch]))$shift[$t]=$out[$ch];if(isset($shift[$t])){$this-closure[$i][$k]['target']=$shift[$t];$dfa['offs']++;if(!$this-in_closure($dfa,$this-closure[$shift[$t]]))$this-closure[$shift[$t]][]=$dfa;}else{$cnt=count($this-closure);$this-closure[$i][$k]['target']=$cnt;$shift[$t]=$cnt;$dfa['offs']++;$this-closure[count($this-closure)][]=$dfa;$out[$ch]=$cnt;}}}//构造状态转换表foreach($this-closure[$i]as$k=$dfa){if(isset($dfa['target'])){$v=$dfa['char'];if(in_array($v,$this-identifier))$this-goto[$i][$v]=$dfa['target'];else{$this-action[$i][$v][]="S$dfa[target]";$this-request[$i][$v]=$dfa['rule'];}}else{$ch=$this-rule[$dfa['rule']][0];foreach($this-follow[$ch]as$v){$this-action[$i][$v][]="r$dfa[rule]";$this-request[$i][$v]=$dfa['rule'];}}}foreach($this-action[$i]as$c=$v){$v=array_unique($v);if(count($v)1)$this-lr='SLR(1)';$this-action[$i][$c]=$v;}}}functionin_closure($t,$s){foreach($sas$r)if($t['offs']==$r['offs']$t['rule']==$r['rule'])returntrue;returnfalse;returnin_array(serialize($t),array_map('serialize',$s));}functionset_select(){foreach($this-ruleas$i=$rule){$y=array($rule[1]);if(in_array($y[0],$this-leay)){if($y[0]!='#'){$this-select[$i]=$y;continue;}}else$y=$this-first[$rule[1]];$x=$this-follow[$rule[0]];//SELECT(X-Y)=(FIRST(Y)-{ε})并FOLLOW(X)$this-select[$i]=array_unique(array_merge(array_diff($y,array('#')),$x));}}/***构造预测分析表**/functionset_forecast(){foreach($this-selectas$i=$r){$c=$this-rule[$i][0];$v=array_reverse(array_slice($this-rule[$i],1));foreach($ras$k){$this-forecast[$c][$k][]=$v;}}//检查冲突foreach($this-forecastas$c=$r){foreach($ras$k){if(count($k)1){$this-ll='LL(1)';}}}}

友情链接: 网站地图
Copyright © 2015-2019 http://www.tk-web.com. bb电子糖果派对有限公司 版权所有