提案してください。コードが入りきらなかったので2ページに分けました。ダウンロード アドレス http://download.csdn.net/detail/xuzuning/4529066
include 'ttrie.php';<br><br>class Rule extends TTrie {<br> public $rule = array( );<br> public $savematch = 0;<br><br> function __construct($s='') {<br> $this->set( array(<br> ' ' => '分離' 、<br> "rn" => 'set_rule'、<br> "n" => '分離'、<br> '-> ; '分離',<br> '→' => '分離',<br> '|' 'set_Parallel_rule',<br> );<br> $this->match($s) ;<br> if($this->rule[0][0] == $this->rule[0][1]) {<br> if(count($this->rule[0] ) == 2) $this->rule[0][0] .= "'";<br> else array_unshift($this->rule, array($this->rule[0][0] ."'", $this->rule[0][0]));<br> }else {<br> $c = $this->rule[0][0];<br> $n = 0;<br> foreach($this->rule as $r) if($r[0] == $c) $n ;<br> if($n > 1) array_unshift($this-> ;rule, array($this->rule[0][0]."'", $this->rule[0][0]));<br> }<br> }<br> 関数 分離() {<br> }<br> function set_rule() {<br> $this->rule[] = $this->buffer;<br> $this->buffer = array();<br> }<br> 関数 set_Parallel_rule() {<br> $t = $this->buffer[0];<br> $this->set_rule();<br> $this->buffer[] = $t;<br> }<br>}<br><br><br>class 文法 {<br> var $closure = array();<br> var $first = array();<br> var $ follow = array();<br> var $rule = array();<br> var $identifier = array();<br> var $leay = array();<br> var $forecast = array(); <br> var $stack = array();<br> var $ll = 'LL(0)';<br> var $lr = 'LR(0)';<br><br> function __construct($s ='') {<br> $p = new Rule($s);<br> $this->rule = $p->rule;<br> $this->set_grammar();<br> }<br><br> function set_grammar() {<br> foreach($this->rule as $rule) {<br> if(! in_array($rule[0], $this->identifier)) $this->identifier[] = $rule[0];<br> }<br> foreach($this->rule as $rule) {<br> foreach($rule as $v)<br> if (! in_array($v, $this->identifier) && ! in_array($v, $this->leay))<br> $this->leay[] = $v;<br> } <br> $this->set_first();<br> $this->set_follow();<br> $this->set_closure();<br> $this->set_select();<br> $ this->set_forecast();<br> }<br> function set_first() {<br> foreach($this->rule as $rule) $this->first[$rule[0]] = array ();<br> //形状 U->a... (a は終端記号) の生成を直接収集し、a を First(U)<br> foreach($this- >rule as $v) {<br> if(in_array($v[1], $this->leay)) $this->first[$v[0]][] = $v[1] ;<br> }<br> // U->P1P2P3...Pn (P は非終端記号) の形式の生成を繰り返し転送します。 First(P1) の内容はすべて First( に転送されます。 U) まず、P1 に ε がある場合、First(P2) の内容を First(U) に転送し、Pi<br> に ε がなくなるまで続きます。 do {<br> $t = Serialize( $this->first);<br> foreach ($this->rule as $rule) {<br> for($i=1; $i<count></count> $v = $rule[$i];<br> if(in_array($v, $this->identifier)) {<br> $this->first[$rule[0]] = array_unique(array_merge( $this->first[$rule[0]] ], $this->first[$v]));<br> if(! in_array('#', $this->first[$v] )) ブレーク;<br> }else ブレーク;<br> }<br> }<br> }while($t != Serialize($this->first));<br> }<br> function set_follow( ) {<br>foreach($this->rule as $rule) $this->follow[$rule[0]] = array();<br> // 直接收取 形如 …Ua… 的、握a直接收入到フォロー(U)中<br> foreach($this->rule as $rule) {<br> for($i=1; $i<count></count> if( in_array($rule[$i], $this->identifier) && in_array($rule[$i 1], $this->leay))<br> $this->follow[$rule[$i] ]][] = $rule[$i 1];<br> }<br> if(in_array($rule[$i], $this->identifier)) $this->follow[$rule[$ i]][] = '#';<br> }<br> foreach($this->follow as &$v) if(! $v) $v[] = '#';<br> //直接收取形如…UP…(P非终结符)的,ハンドルFirst(P)中非ε收入toFollow(U)中<br> foreach($this->rule as $rule) {<br> for ($i=1; $i<count($rule)-1; $i ) {<br /> if(in_array($rule[$i], $this->identifier) && in_array($rule[$i] 1], $this->identifier)) {<br> $this->follow[$rule[$i]] = array_unique(array_merge($this->gt;follow[$rule[$i]], array_diff ($this->first[$rule[$i 1]], array('#'))));<br> }<br> }<br> }<br> //反复传递 形如U- >aP の (P は终结符) または U->aPQ(P,Q は非终结符であり、Q に含まれます)、应ハンドルFollow(U) 内のすべてのコンテンツが Follow(P) 内に送信されます<br> do {<br> $t = serialize($this->follow);<br> foreach($this->rule as $rule) {<br> $s = $rule[0];<br> $d = end($rule);<br> if(in_array($d, $this->leay)) Continue;<div class="clear"></div>
ログイン後にコピー