当前位置:Gxlcms > PHP教程 > 对编译原理有兴趣的进解决方法

对编译原理有兴趣的进解决方法

时间:2021-07-01 10:21:17 帮助过:24人阅读

对编译原理有兴趣的进
本帖最后由 xuzuning 于 2012-08-27 16:40:33 编辑

最近尝试做了文法分析的东东,问题较多。
请提建议。代码放不下,分两页。下载地址 http://download.csdn.net/detail/xuzuning/4529066
  1. include 'ttrie.php';<br><br>class Rule extends TTrie {<br> public $rule = array();<br> public $savematch = 0;<br><br> function __construct($s='') {<br>
  2. $this->set( array(<br>
  3. ' ' => 'Separated',<br>
  4. "\r\n" => 'set_rule',<br>
  5. "\n" => 'set_rule',<br>
  6. "\t" => 'Separated',<br>
  7. '->' => 'Separated',<br>
  8. '→' => 'Separated',<br>
  9. '|' => 'set_parallel_rule',<br>
  10. ));<br>
  11. $this->match($s);<br>
  12. if($this->rule[0][0] == $this->rule[0][1]) {<br>
  13. if(count($this->rule[0]) == 2) $this->rule[0][0] .= "'";<br>
  14. else array_unshift($this->rule, array($this->rule[0][0]."'", $this->rule[0][0]));<br>
  15. }else {<br>
  16. $c = $this->rule[0][0];<br>
  17. $n = 0;<br>
  18. foreach($this->rule as $r) if($r[0] == $c) $n++;<br>
  19. if($n > 1) array_unshift($this->rule, array($this->rule[0][0]."'", $this->rule[0][0]));<br>
  20. }<br> }<br> function Separated() {<br> }<br> function set_rule() {<br>
  21. $this->rule[] = $this->buffer;<br>
  22. $this->buffer = array();<br> }<br> function set_parallel_rule() {<br>
  23. $t = $this->buffer[0];<br>
  24. $this->set_rule();<br>
  25. $this->buffer[] = $t;<br> }<br>}<br><br><br>class Grammar {<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>
  26. $p = new Rule($s);<br>
  27. $this->rule = $p->rule;<br>
  28. $this->set_grammar();<br> }<br><br> function set_grammar() {<br>
  29. foreach($this->rule as $rule) {<br>
  30. if(! in_array($rule[0], $this->identifier)) $this->identifier[] = $rule[0];<br>
  31. }<br>
  32. foreach($this->rule as $rule) {<br>
  33. foreach($rule as $v)<br>
  34. if(! in_array($v, $this->identifier) && ! in_array($v, $this->leay))<br>
  35. $this->leay[] = $v;<br>
  36. }<br>
  37. $this->set_first();<br>
  38. $this->set_follow();<br>
  39. $this->set_closure();<br>
  40. $this->set_select();<br>
  41. $this->set_forecast();<br> }<br> function set_first() {<br>
  42. foreach($this->rule as $rule) $this->first[$rule[0]] = array();<br>
  43. //直接收取 形如U->a…的产生式(其中a是终结符),把a收入到First(U)中<br>
  44. foreach($this->rule as $v) {<br>
  45. if(in_array($v[1], $this->leay)) $this->first[$v[0]][] = $v[1];<br>
  46. }<br>
  47. //反复传递 形入U->P1P2P3…Pn的产生式(其中P是非终结符),应先把First(P1)中的全部内容传送到First(U)中,如果P1中有ε,把First(P2)中的内容传送到First(U)中,类推直到Pi中无ε<br>
  48. do {<br>
  49. $t = serialize($this->first);<br>
  50. foreach($this->rule as $rule) {<br>
  51. for($i=1; $i<count($rule); $i++)="" {<br="">
  52. $v = $rule[$i];<br>
  53. if(in_array($v, $this->identifier)) {<br>
  54. $this->first[$rule[0]] = array_unique(array_merge($this->first[$rule[0]], $this->first[$v]));<br>
  55. if(! in_array('#', $this->first[$v])) break;<br>
  56. }else break;<br>
  57. }<br>
  58. }<br>
  59. }while($t != serialize($this->first));<br> }<br> function set_follow() {<br>
  60. foreach($this->rule as $rule) $this->follow[$rule[0]] = array();<br>
  61. //直接收取 形如 …Ua… 的,把a直接收入到Follow(U)中<br>
  62. foreach($this->rule as $rule) {<br>
  63. for($i=1; $i<count($rule)-1; $i++)="" {<br="">
  64. if(in_array($rule[$i], $this->identifier) && in_array($rule[$i+1], $this->leay))<br>
  65. $this->follow[$rule[$i]][] = $rule[$i+1];<br>
  66. }<br>
  67. if(in_array($rule[$i], $this->identifier)) $this->follow[$rule[$i]][] = '#';<br>
  68. }<br>
  69. foreach($this->follow as &$v) if(! $v) $v[] = '#';<br>
  70. //直接收取 形如 …UP…(P是非终结符)的,把First(P)中非ε收入到Follow(U)中<br>
  71. foreach($this->rule as $rule) {<br>
  72. for($i=1; $i<count($rule)-1; $i++)="" {<br="">
  73. if(in_array($rule[$i], $this->identifier) && in_array($rule[$i+1], $this->identifier)) {<br>
  74. $this->follow[$rule[$i]] = array_unique(array_merge($this->follow[$rule[$i]], array_diff($this->first[$rule[$i+1]], array('#'))));<br>
  75. }<br>
  76. }<br>
  77. }<br>
  78. //反复传递 形如U->aP的(P是非终结符)或U->aPQ(P,Q为非终结符且Q中含ε),应把Follow(U)中的全部内容传送到Follow(P)中<br>
  79. do {<br>
  80. $t = serialize($this->follow);<br>
  81. foreach($this->rule as $rule) {<br>
  82. $s = $rule[0];<br>
  83. $d = end($rule);<br>
  84. if(in_array($d, $this->leay)) continue; </count($rule)-1;></count($rule)-1;></count($rule);>

人气教程排行