If you are interested in the compilation principle

Source: Internet
Author: User
I recently tried grammar analysis for those who are interested in compilation principles. There are many problems. Please provide suggestions. The code cannot be stored. There are two pages. Download. csdn. netdetailxuzuning4529066PHPcodeincludettrie. php; classRuleextendsTTrie {public $ rulearray (for instructions on compilation principles)
I have tried grammar analysis recently, and there are many problems.
Please provide suggestions. The code cannot be stored. There are two pages. Http://download.csdn.net/detail/xuzuning/4529066
PHP code
  Include 'ttrie. php '; class Rule extends TTrie {public $ rule = array (); public $ savematch = 0; function _ construct ($ s = '') {$ this-> set (array (''=> 'paramted'," \ r \ n "=> 'set _ rule ', "\ n" => 'set _ rule', "\ t" => 'secparated ','-> '=> 'secparated ', '→' => 'paramted', '|' => 'set _ parallel_rule',); $ this-> match ($ s ); if ($ this-> rule [0] [0] = $ this-> rule [0] [1]) {if (count ($ this-> rule [0]) = 2) $ this-> r Ule [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_p Arallel_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 (); // directly charged for a shape such as U->... Generate the formula (where a is the Terminator) to generate a into the First (U) foreach ($ this-> rule as $ v) {if (in_array ($ v [1], $ this-> leay) $ this-> first [$ v [0] [] = $ v [1];} // repeatedly transmitted to the U-> P1P2P3... In the production formula of Pn (where P is not a Terminator), the full content in First (P1) should be First transmitted to First (U). If P1 contains ε, the First (P2) the content in is transmitted to First (U), and so on until there is no ε do {$ t = serialize ($ this-> first) in Pi ); foreach ($ this-> rule as $ rule) {for ($ I = 1; $ I
  
   
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 (); // directly collects data such... Ua... To directly generate a into the Follow (U) foreach ($ this-> rule as $ rule) {for ($ I = 1; $ I
   
    
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 [] = '#'; // directly collects data such... UP... (P is not a Terminator). It transfers non-ε income of First (P) to foreach ($ this-> rule as $ rule) {for ($ I = 1; $ I
    
     
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 ('#'))));}}} // The Follow (U) should be set) to 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 <count ($ this-> closure); $ I ++) {$ cnt = count ($ this-> closure ); // Construct the closure $ ex = array (); $ j = 0; $ tmp = array (); do {$ size = count ($ this-> closure [$ I]); for ($ j = 0; $ j
     
      
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); // wait until it no longer increases. // The judgment status changes to 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 ;}}// structure the State conversion table foreach ($ this-> closure [$ I] as $ k =>$ dfa) {if (isset ($ dfa ['target']) {$ v = $ dfa ['char ']; if (in_array ($ v, $ this-> id Entifier) $ 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)-{ε }) and FOLLOW (X) $ this-> select [$ I] = array_unique (array_merge (array_diff ($ y, array ('#'), $ x ));}} /*** construct a prediction analysis table **/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 ;}} // Check for conflicting foreach ($ this-> forecast as $ c =>r r) {foreach ($ r as $ k) {if (count ($ k)> 1) {$ this-> ll ='ll (1 )';}}}}
      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.