对编译原理有兴趣的进
最近尝试做了文法分析的东东,问题较多。
请提建议。代码放不下,分两页。下载地址 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->set( array( ' ' => 'Separated', "\r\n" => '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] .= "'"; else array_unshift($this->rule, array($this->rule[0][0]."'", $this->rule[0][0])); }else { $c = $this->rule[0][0]; $n = 0; foreach($this->rule as $r) if($r[0] == $c) $n++; if($n > 1) array_unshift($this->rule, array($this->rule[0][0]."'", $this->rule[0][0])); } } function Separated() { } function set_rule() { $this->rule[] = $this->buffer; $this->buffer = array(); } function set_parallel_rule() { $t = $this->buffer[0]; $this->set_rule(); $this->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->rule = $p->rule; $this->set_grammar(); } function set_grammar() { foreach($this->rule as $rule) { if(! in_array($rule[0], $this->identifier)) $this->identifier[] = $rule[0]; } foreach($this->rule as $rule) { foreach($rule as $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(); } function set_first() { foreach($this->rule as $rule) $this->first[$rule[0]] = array(); //直接收取 形如U->a…的产生式(其中a是终结符),把a收入到First(U)中 foreach($this->rule as $v) { if(in_array($v[1], $this->leay)) $this->first[$v[0]][] = $v[1]; } //反复传递 形入U->P1P2P3…Pn的产生式(其中P是非终结符),应先把First(P1)中的全部内容传送到First(U)中,如果P1中有ε,把First(P2)中的内容传送到First(U)中,类推直到Pi中无ε do { $t = serialize($this->first); foreach($this->rule as $rule) { for($i=1; $i<count if>identifier)) { $this->first[$rule[0]] = array_unique(array_merge($this->first[$rule[0]], $this->first[$v])); if(! in_array('#', $this->first[$v])) break; }else break; } } }while($t != serialize($this->first)); } function set_follow() { foreach($this->rule as $rule) $this->follow[$rule[0]] = array(); //直接收取 形如 …Ua… 的,把a直接收入到Follow(U)中 foreach($this->rule as $rule) { for($i=1; $i<count if>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->follow as &$v) if(! $v) $v[] = '#'; //直接收取 形如 …UP…(P是非终结符)的,把First(P)中非ε收入到Follow(U)中 foreach($this->rule as $rule) { for($i=1; $i<count if>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->rule as $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)); } function set_closure() { $shift = array(); $this->closure[0][] = array('offs' => 1, 'rule' => 0); for($i=0 ; $i closure); $i++) { $cnt = count($this->closure); //构造闭包 closure $ex = array(); $j = 0; $tmp = array(); do { $size = count($this->closure[$i]); for($j=0; $j<count>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->rule as $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; } } } function in_closure($t, $s) { foreach($s as $r) if($t['offs'] == $r['offs'] && $t['rule'] == $r['rule']) return true; return false; return in_array(serialize($t), array_map('serialize', $s)); } function set_select() { foreach($this->rule as $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) ); } } /** * 构造预测分析表 **/ function set_forecast() { foreach($this->select as $i=>$r) { $c = $this->rule[$i][0]; $v = array_reverse(array_slice($this->rule[$i], 1)); foreach($r as $k) { $this->forecast[$c][$k][] = $v; } } //检查冲突 foreach($this->forecast as $c=>$r) { foreach($r as $k) { if(count($k) > 1) { $this->ll = 'LL(1)'; } } } } <div class="clear"> </div></count></count></count></count>

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Dreamweaver CS6
视觉化网页开发工具