对编译原理有兴趣的入

php中文网
发布: 2016-06-13 10:48:46
原创
838人浏览过

对编译原理有兴趣的进
最近尝试做了文法分析的东东,问题较多。
请提建议。代码放不下,分两页。下载地址 http://download.csdn.net/detail/xuzuning/4529066

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->include 'ttrie.php';class Rule extends TTrie {  public $rule = array();  public $savematch = 0;  function __construct($s='') {    $this-&gt;set( array(        ' ' =&gt; 'Separated',        "\r\n" =&gt; 'set_rule',        "\n" =&gt; 'set_rule',        "\t" =&gt; 'Separated',        '-&gt;' =&gt; 'Separated',        '→' =&gt; 'Separated',        '|' =&gt; 'set_parallel_rule',        ));    $this-&gt;match($s);    if($this-&gt;rule[0][0] == $this-&gt;rule[0][1]) {        if(count($this-&gt;rule[0]) == 2) $this-&gt;rule[0][0] .= "'";        else array_unshift($this-&gt;rule, array($this-&gt;rule[0][0]."'", $this-&gt;rule[0][0]));    }else {        $c = $this-&gt;rule[0][0];        $n = 0;        foreach($this-&gt;rule as $r) if($r[0] == $c) $n++;        if($n &gt; 1) array_unshift($this-&gt;rule, array($this-&gt;rule[0][0]."'", $this-&gt;rule[0][0]));    }  }  function Separated() {  }  function set_rule() {    $this-&gt;rule[] = $this-&gt;buffer;    $this-&gt;buffer = array();  }  function set_parallel_rule() {    $t = $this-&gt;buffer[0];    $this-&gt;set_rule();    $this-&gt;buffer[] = $t;  }}class Grammar {  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 = new Rule($s);    $this-&gt;rule = $p-&gt;rule;    $this-&gt;set_grammar();  }  function set_grammar() {    foreach($this-&gt;rule as $rule) {        if(! in_array($rule[0], $this-&gt;identifier)) $this-&gt;identifier[] = $rule[0];    }    foreach($this-&gt;rule as $rule) {        foreach($rule as $v)            if(! in_array($v, $this-&gt;identifier) &amp;&amp; ! in_array($v, $this-&gt;leay))                $this-&gt;leay[] = $v;    }    $this-&gt;set_first();    $this-&gt;set_follow();    $this-&gt;set_closure();    $this-&gt;set_select();    $this-&gt;set_forecast();  }  function set_first() {    foreach($this-&gt;rule as $rule) $this-&gt;first[$rule[0]] = array();    //直接收取 形如U-&gt;a…的产生式(其中a是终结符),把a收入到First(U)中    foreach($this-&gt;rule as $v) {        if(in_array($v[1], $this-&gt;leay)) $this-&gt;first[$v[0]][] = $v[1];    }    //反复传递 形入U-&gt;P1P2P3…Pn的产生式(其中P是非终结符),应先把First(P1)中的全部内容传送到First(U)中,如果P1中有ε,把First(P2)中的内容传送到First(U)中,类推直到Pi中无ε    do {        $t = serialize($this-&gt;first);        foreach($this-&gt;rule as $rule) {            for($i=1; $i<count if>identifier)) {                    $this-&gt;first[$rule[0]] = array_unique(array_merge($this-&gt;first[$rule[0]], $this-&gt;first[$v]));                    if(! in_array('#', $this-&gt;first[$v])) break;                }else break;            }        }    }while($t != serialize($this-&gt;first));  }  function set_follow() {    foreach($this-&gt;rule as $rule) $this-&gt;follow[$rule[0]] = array();    //直接收取 形如 …Ua… 的,把a直接收入到Follow(U)中    foreach($this-&gt;rule as $rule) {        for($i=1; $i<count if>identifier) &amp;&amp; in_array($rule[$i+1], $this-&gt;leay))                $this-&gt;follow[$rule[$i]][] = $rule[$i+1];        }        if(in_array($rule[$i], $this-&gt;identifier)) $this-&gt;follow[$rule[$i]][] = '#';    }    foreach($this-&gt;follow as &amp;$v) if(! $v) $v[] = '#';    //直接收取 形如 …UP…(P是非终结符)的,把First(P)中非ε收入到Follow(U)中    foreach($this-&gt;rule as $rule) {        for($i=1; $i<count if>identifier) &amp;&amp; in_array($rule[$i+1], $this-&gt;identifier)) {                $this-&gt;follow[$rule[$i]] = array_unique(array_merge($this-&gt;follow[$rule[$i]], array_diff($this-&gt;first[$rule[$i+1]], array('#'))));            }        }    }    //反复传递 形如U-&gt;aP的(P是非终结符)或U-&gt;aPQ(P,Q为非终结符且Q中含ε),应把Follow(U)中的全部内容传送到Follow(P)中    do {        $t = serialize($this-&gt;follow);        foreach($this-&gt;rule as $rule) {            $s = $rule[0];            $d = end($rule);            if(in_array($d, $this-&gt;leay)) continue;            $p = prev($rule);            if(in_array($p, $this-&gt;leay)) $this-&gt;follow[$d] = array_unique(array_merge($this-&gt;follow[$d], $this-&gt;follow[$s]));            elseif(in_array('#', $this-&gt;follow[$d])) $this-&gt;follow[$p] = array_unique(array_merge($this-&gt;follow[$p], $this-&gt;follow[$s]));        }    }while($t != serialize($this-&gt;follow));  }  function set_closure() {    $shift = array();    $this-&gt;closure[0][] = array('offs' =&gt; 1, 'rule' =&gt; 0);    for($i=0 ; $i closure); $i++) {        $cnt = count($this-&gt;closure);        //构造闭包 closure        $ex = array();        $j = 0;        $tmp = array();        do {            $size = count($this-&gt;closure[$i]);            for($j=0; $j<count>closure[$i]); $j++) {                $dfa = $this-&gt;closure[$i][$j];                $rule = $this-&gt;rule[$dfa['rule']];                if(isset($rule[$dfa['offs']])) {                    $ch = $ex[] = $rule[$dfa['offs']];                }                foreach($this-&gt;rule as $r=&gt;$rule) {                    if(in_array($rule[0], $ex)) {                        $t = array('offs' =&gt; 1, 'rule' =&gt; $r);                        if(!isset($tmp[$r][1])) $this-&gt;closure[$i][] = $t;                        $tmp[$r][1] = 1;                    }                }            }        }while(count($this-&gt;closure[$i]) != $size); //直到不再增大        //判断状态转向 go        $out = array();        foreach($this-&gt;closure[$i] as $k=&gt;$dfa) {            $rule = $this-&gt;rule[$dfa['rule']];            if(isset($rule[$dfa['offs']])) {                $t = "$dfa[rule],$dfa[offs]";                $ch = $rule[$dfa['offs']];                $this-&gt;closure[$i][$k]['char'] = $ch;                if(isset($out[$ch])) $shift[$t] = $out[$ch];                if(isset($shift[$t])) {                    $this-&gt;closure[$i][$k]['target'] = $shift[$t];                    $dfa['offs']++;                    if(!$this-&gt;in_closure($dfa, $this-&gt;closure[$shift[$t]])) $this-&gt;closure[$shift[$t]][] = $dfa;                } else {                    $cnt = count($this-&gt;closure);                    $this-&gt;closure[$i][$k]['target'] = $cnt;                    $shift[$t] = $cnt;                    $dfa['offs']++;                    $this-&gt;closure[count($this-&gt;closure)][] = $dfa;                    $out[$ch] = $cnt;                }            }        }        //构造状态转换表        foreach($this-&gt;closure[$i] as $k=&gt;$dfa) {            if(isset($dfa['target'])) {                $v = $dfa['char'];                if(in_array($v, $this-&gt;identifier)) $this-&gt;goto[$i][$v] = $dfa['target'];                else {                    $this-&gt;action[$i][$v][] = "S$dfa[target]";                    $this-&gt;request[$i][$v] = $dfa['rule'];                }            } else {                $ch = $this-&gt;rule[$dfa['rule']][0];                foreach($this-&gt;follow[$ch] as $v) {                    $this-&gt;action[$i][$v][] = "r$dfa[rule]";                    $this-&gt;request[$i][$v] = $dfa['rule'];                }            }        }        foreach($this-&gt;action[$i] as $c=&gt;$v) {            $v = array_unique($v);            if(count($v) &gt; 1) $this-&gt;lr = 'SLR(1)';            $this-&gt;action[$i][$c] = $v;        }    }  }  function in_closure($t, $s) {    foreach($s as $r) if($t['offs'] == $r['offs'] &amp;&amp; $t['rule'] == $r['rule']) return true;    return false;    return in_array(serialize($t), array_map('serialize', $s));  }  function set_select() {    foreach($this-&gt;rule as $i=&gt;$rule) {        $y = array($rule[1]);        if(in_array($y[0], $this-&gt;leay)) {            if($y[0] != '#') {                $this-&gt;select[$i] = $y;                continue;            }        }else $y = $this-&gt;first[$rule[1]];        $x = $this-&gt;follow[$rule[0]];        //SELECT(X-&gt;Y)=(FIRST(Y)-{ε})并FOLLOW(X)        $this-&gt;select[$i] = array_unique( array_merge(array_diff($y, array('#')), $x) );    }  }  /**   * 构造预测分析表   **/  function set_forecast() {    foreach($this-&gt;select as $i=&gt;$r) {        $c = $this-&gt;rule[$i][0];        $v = array_reverse(array_slice($this-&gt;rule[$i], 1));        foreach($r as $k) {            $this-&gt;forecast[$c][$k][] = $v;        }    }    //检查冲突    foreach($this-&gt;forecast as $c=&gt;$r) {        foreach($r as $k) {            if(count($k) &gt; 1) {                $this-&gt;ll = 'LL(1)';            }        }    }  }<div class="clear"></div></count></count></count></count>
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号