I am interested in the compilation principles. at the end of this post, xuzuning was edited at 2012-08-2716: 40: 33. Recently, I tried to perform grammar analysis, which has many problems. Please provide suggestions. The code cannot be stored. There are two pages. & Nbsp; download. csdn. netdetailxuzuning4529066include & nbsp; ttrie. php;
This post was last edited by xuzuning at 16:40:33
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
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" => 'paramted ',
'->' => 'Paramted ',
'→' => 'Paramd ',
'|' => '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 ();
// Directly collect data, such as U->... Generate (where a is the Terminator), and send a's revenue to 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 ε in Pi.
Do {
$ T = serialize ($ this-> first );
Foreach ($ this-> rule as $ rule ){
For ($ I = 1; $ I