Interested in compiling the principle of

Source: Internet
Author: User
Tags getting started with php
This post was last edited by xuzuning on 2012-08-27 16:40:33

Recently tried to do a grammar analysis of the east, more problems.
Please make suggestions. The code does not fit, two pages. Download Address 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 (' = ' = ' separated ', ' \ r \ n ' = ' set_rule ', ' \ n ' = ' set_rule ', "\ T "= = ' separated ', '--" and ' = ' 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 ($thi  S->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 ();//Direct charge form as u->a ... (where A is terminator) and the A proceeds to the first (U) foreach ($this->rule as $v) {if (In_array ($v [1], $this->leay)) $this->first[$v [0 ]][] = $v [1];} Repeated transfer form into u->p1p2p3 ... The production of PN (itsP is non-terminator), first (P1) should be sent to the entire content in the start (U), if there is ε in P1, the contents of first (P2) is transferred to First (U), and so on until the pi is not εdo {$t = serialize ($this- First), 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])); else break;}}}  while ($t! = Serialize ($this->first)); } function Set_follow () {foreach ($this->rule as $rule) $this->follow[$rule [0]] = array ();//Direct Charge form ... Ua... , a direct income to 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 [] = ' # ';//Direct Charge form ... Up ... (P is non-terminator), the first (p) non-ε income to follow (U) in 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 repetition of a u->ap (P is non-terminator) or U->APQ (P,q is non-terminator and Q contains ε), the entire contents of the follow (U) should be transmitted to the 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 = PR EV ($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)); The function set_closure () {$shift = array (); $this->closure[0][] = array (' offs ' = 1, ' rule ' = 0); for ($i =0; $i & Lt Count ($this->closure); $i + +) {$cnt = count ($this->closure);//Construction Closure Closure$ex = Array (); $j = 0; $tmp = array();d o {$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 (!isse T ($tmp [$r][1]) $this->closure[$i] = $t; $tmp [$r][1] = 1;}}} while (count ($this->closure[$i])! = $size); Until the status is no longer increased//judged 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;}}} The Construct State conversion table 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)-{ε}) and follow (x) $  this->select[$i] = Array_unique (Array_merge (Array_diff ($y, Array (' # ')), $x));}  }/** * Constructs the 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) {foreach ($r as $k) {if (count ($k) > 1) {$this->ll = ' ll (1) ';}}} }
    
   
  
 


Reply to discussion (solution)

Continued

function Ll_start ($s) {$t = array (); foreach ($this->rule as $rule) if ($rule [0] = = $rule [1]) $t [] = $rule; if ($t) {foreach ($t as $rule) printf ('Left recursion exists for%s', preg_replace ('//', ' → ', join (', $rule), 1); return;} $stack = Array (' # ', Key ($this->forecast)), $i = 0; $step = 1; $timeout = ten * strlen ($s); while ($stack && $i < s Trlen ($s) && $timeout-) {$r = end ($stack), if ($r = = $s {$i}) {$msg = $r = = ' # '? ' Success ': ' $r match ';} ElseIf (Isset ($this->forecast[$r [$s {$i}])) $msg = $r. ' → '. Join ("', Array_reverse ($this->forecast[$r [$s {$i}][0])), Else $msg = ' ERROR ';p rintf ("%d%s%s%s", $step + +, substr (Join (", $stack), -50), substr ($s, $i), $msg); if ($r = = = $s {$i}) {Array_pop ($stack); $i + +;} ElseIf (Isset ($this->forecast[$r [$s {$i}])) {Array_pop ($stack), if (current ($this->forecast[$r] [$s {$i}][0])! = ' # ') $stack = Array_merge ($stack, $this->forecast[$r] [$s {$i}][0]);}  else break;} } function Lr_start ($s) {$State = array (0);//Status Stack $symbol = Array (' # ');//symbol stack $i = 0; $step = 1; $timeout = ten * strlen ($s); w Hile ($i < strlen ($s) && $timeout-) {$ch = $s {$i}; $sp = end ($State); $msg = substr ($s, $i); if (Isset ($this->ac tion[$SP] [$ch]) && $this->action[$sp] [$ch][0] = = ' R0 ') $msg = ' acc '; if (isset ($this->request[$sp] [$ch])) $request = Preg_replace ('//', ' → ', join (', $this->rule[$this->request[$sp] [$ch]]), 1); Else $request = ' ERROR ';p Rintf ("%d%s%s%s%s", $step + +, substr (Join (', $State), -50), join (', $Symbol), $msg, $request); if (Isset ($this->action[$sp] [$ch]) | | Isset ($this->goto[$sp [$ch])) {$t = Isset ($this->action[$sp] [$ch])? $this->action[$sp] [$ch][0]: $this goto[$SP] [$ch]; $n = substr ($t, 1) + 0;if ($t {0} = = ' R ') {for ($j =0; $j
 
  rule[$n])-1; $j + +) {Array_pop ($State); Array_pop ($Symbol);} if ($n = = 0) break; $c = $Symbol [] = $this->rule[$n][0]; $State [] = $this->goto[end ($State) [$c];} ElseIf ($t {0} = = ' S ') {$State [] = $n; $Symbol [] = $ch; $i + +;} else;}  else break;} The function report ($in = ") {if ($in) $in = Trim ($in, ' # '). ' # ', Echo '
  '; Echo '
  
 '; foreach ($this->rule as $rule) {echo ' 
   ';} Echo ' 
  
Grammar
echo preg_replace ('//', ' → ', join (', $rule), 1), Echo '
'; $identifier = $this->identifier;echo ' '; Echo ' '; Echo '
Identifier' . Join (', $identifier). '
'; $leay = Array_diff ($this->leay, Array (' # ')); $leay [] = ' # '; Echo ' '; Echo ' '; Echo '
Terminator' . Join (', $leay). '
'; Echo ' First '; foreach ($identifier as $ch) {echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' ';} Echo '
identifiers Launch Emptyset Follow set
' . $ch. '' . (In_array (' # ', $this->first[$ch])? ' True ': ' false '). '' . Join (", $this->first[$ch]). '' . Join (", $this->follow[$ch]). '
'; Echo '
' . $this->ll. ' Grammatical analysis
'; Echo ' '; foreach ($this->rule as $i = + $rule) {echo ' '; Echo ' '; Echo ' '; Echo ' ';} Echo '
Production Type Select Set
' . Preg_replace ('///', ' → ', join (', $rule), 1). '' . Join (", $this->select[$i]). '
'; $forecast = $this->forecast;echo ' '; Echo ' '; Echo '
Forecast Analysis Table

'; Echo ' '; foreach ($identifier as $ch) {echo ' '; foreach ($leay as $v) {$s = ', if (Isset ($forecast [$ch] [$v]) {foreach ($forecast [$ch] [$v] as $t) {if ($s) $s. = '; $s. = $c H. ' → '. Join (', Array_reverse ($t));} if (count ($forecast [$ch] [$v]) > 1) $s = "$s";} else $s. = '; Echo ' ';} Echo ' ';} Echo '
'. Join (' , $leay). '
' . $ch. '' . $s. '
' If ($in) {echo ' '; Echo '
test String' . Trim ($in, ' # '). '
'; Echo ' '; Echo ' '; $this->ll_start ($in); Echo '
Steps Analysis Stack remaining characters generated or matched
';} Echo ' '; Echo '
' . $this->LR. ' Grammatical analysis
'; Echo '
State transfer Table
'; Echo ' '; Echo ' '; Echo ' '; foreach ($this->action as $i = + $item) {echo " "; foreach ($leay as $v) {$s = Isset ($item [$v])? Join (', ', $item [$v]): '; if (Strpos ($s, ', ')) $s = "$s"; echo " ";} foreach ($identifier as $v) {$s = Isset ($this->goto[$i] [$v])? $this->goto[$i] [$v]: "; echo" ";} Echo ' '; Echo ' ';} Echo '
Status Action Goto DFA
'. Join (' , $leay). ' . Join (' , $identifier). '
$i$s$s' . $this->showdfa ($i). '
' If ($in) {echo ' '; Echo '
test String' . Trim ($in, ' # '). '
'; Echo ' '; Echo ' in '; $this->lr_start ($in); Echo '
Steps Status Stack symbol Stacks remaining characters Built-
';} } function Showdfa ($i) {$res = Array (), foreach ($this->closure[$i] as $DFA) {$rule = $this->rule[$dfa [' rule ']];arra Y_splice ($rule, $dfa [' offs '], 0, ' · '); Array_splice ($rule, 1, 0, ' → '), if (Isset ($dfa [' target ')) $rule [] = "[$DFA [target]]"; $res [] = Join (', $rule);} return join ('; ', $res); }}for ($i =1; $i <=count (Glob (' *.txt ')); $i + +) echo "$i"; $n = current ($_get) or $n = 1; $S = "; include" $n. txt "; $p = new Grammar ($G); $p->report ($S);

ttrie.php
 $v) $this->set ($k, $v); return;} $p = count ($this->dict); $cur = 0; Current node number foreach (Str_split ($word) as $c) {if (Isset ($this->dict[$cur [$c])) {//already exists, move down $cur = $this->dict[$cur] [$c] ; continue;} $this->dict[$p]= Array (); Create a new node $this->dict[$cur] [$c] = $p; The parent node records the child node number $cur = $p; Sets the current node as the newly inserted $p++;} $this->dict[$cur [' acc '] = $action;  A word ends, tag leaf node} function match ($s) {$ret = array (); $cur = 0;//current node, initial root node $i =& $this->input;//String current offset $p =& $this->backtracking; string backtracking position $s. = "n"; Additional Terminator $len = strlen ($s), $buf = ", while ($i < $len) {$c = $s {$i};if (isset ($this->dict[$cur] [$c])) {//if there is $cur = $th is->dict[$cur] [$c]; Go to the corresponding position if (Isset ($this->dict[$cur [$s [$i +1]])) {//check if the next character can match, length first $i++;continue;} if (Isset ($this->dict[$cur [' ACC '])) {//Is leaf node, Word match! if ($buf) {$this->buffer[] = $buf; $buf = ";} if ($this->savematch) $this->buffer[] = substr ($s, $p, $i-$p + 1); Remove the matching position and match the word $ar = explode (', ', $this->dict[$cur] [' ACC ']); call_user_func_aRray (Array ($this, Array_shift ($ar)), $ar); $p = $i + 1; Set the next backtracking position $cur = 0; Resets the current node to the root node}} else {//mismatch $buf. = $s {$p};//substr ($s, $p, $i-$p + 1);//Save unmatched location and unmatched content $cur = 0;//reset current node to root node $i = $p; /set current offset to backtracking position $p = $i + 1; Set next backtracking position} $i + +;  The next character}if (Trim ($buf, "s")) $this->buffer[] = Trim ($buf, "n"); } function __call ($method, $param) {if ($this->debug) printf ("Offset:%d backtracking:%d\n", $this->input, $this-  backtracking); }}

Related Data files
1.txt

 
  A MA->etxt; $S = ' aaaebbd# ', #A, A, BM, a, HH, a M DH
2.txt
 
    $S = ' i* (i+i) # iTXt, E + TE, TT-F, T * FT--, EE
3.txt
 4.txt
  5.txt
   6.txt
    7.txt
     8.txt
       

Interested, unfortunately the compilation principle completely forgot.
Weak and weak to ask: high-level language compiler, the efficiency will not be too low.

It's better to write some text and draw a little picture. Look at the code, it's too hard

It's better to write some text and draw a little picture. Look at the code, it's too hard
You copy the code and run it.

I packed it, but I couldn't see it.

I don't even know what the compiling principle is.
Have time to study slowly

Nagging is a scientist.
We're a frontline production worker.
Come in queued the scientists study

I don't even know what the compiling principle is.
Have time to study slowly

Good thing, after the afternoon, take a good look

The principle of compiling is simply not learned!

Go back to the compiling principle, published by Tsinghua University Press, see.
Lexical analysis, grammatical analysis, semantic analysis .... Just graduated with a little impression of drifting over

Yacc.lex tools are powerful.

It's better to have comments

It's better to have comments

You're a college-school pie.

Looks like you're making a complicated mess.

You can draw a scoop from this LUA code. His piece of code is very concise. It's just a bit more layered.

The language is too advanced, but there is no need to worry about performance.

Uh, asking for comments.

Ox, I can't read it.

Describe your grammar first, so you don't have to read it.

Pay attention!

Thanks for providing
Study the

Oh, no!

Soon to learn, will be more concerned about

The station big God is indeed many, the younger brother first to your treasure land, hope everybody cares more

Don't understand compile, so focus on

Understand the principles of compiling are very tough!!

Sorry, replies earn points.

The course design that was compiled at that time is this, if you put it up two years earlier, I don't have to do it myself.
20 points directly into the total score yes there are wood and wood there ~

Ox, I can't read it.

Enemies.

Describe your grammar first, so you don't have to read it.

The support is this sentence. On the first floor, the quote was wrong.

If you want to describe the grammar first, the layman says that it is great to guess the pear directly from the code.

Friends aren't Chinese, are they?

Formal language is learning, just contact grammar, compiling the principle of the semester to learn it

Half a dozen? To understand, to do?????? Note....

I don't understand it at all.

Download number of times: 33
Number of replies: 45

No one challenged the outcome of the output.
No one suggests any improvement to the program.
No one questioned the problem with the algorithm.

The data used are all online (and of course copied from textbooks).
There is no processing of any conflicts in the program (just list)

It can be seen that these grammatical examples have been carefully designed to make no mistakes.

Only looked ll (1)
1 Conflict selection not in #
2,3,8 why not eliminate left recursion?
5,7 to process the extraction factor.

LR look again at night.

Compiling principle really don't understand

Only looked ll (1)
1 Conflict selection not in #
2,3,8 why not eliminate left recursion?
5,7 to process the extraction factor.

LR look again at night. Thank you
In fact, I am also a beginner, I have seen before, it is not clear

This is a clear point, the elimination of left recursion, extraction factor of the algorithm implementation of this in the study. So the program you didn't

PHP Oh,,,,,

Think of the university that will take this course to test the full score. I forgot all about it now.
Go over the review.

Look, with the 1+1=2 that I wrote at that time is completely not so!

Quite complex algorithm, did not expect to do 3 years of business system still have a part to see not understand

We have to study the algorithm.

I don't understand it at all. In the study:

I also recently made a very simple two-step compiler, landlord more communication and more communication AH


The program is to run the grammar.php file directly, run out is. I can not understand, the pressure is very big.

I just saw the $

Super-powerful lexical analysis tool: Flex
Super-powerful grammar analysis tool: BISON,BYACC.
PHP is interpreted as the programming language, the original syntax is to emulate the C language, do grammar analysis is too slow.
Go straight to the C language +flex+bison.

With the use of LR analysis to write a grammar analyzer passing, landlord your program to the larger grammar support how, how fast?
I have a part of my own SQL language grammar, do you want to give it a try?

Algorithm is very important in learning!

Good thing, after the afternoon, take a good look

I'm just getting started with PHP and trying to climb up

Something familiar, but it seems to have returned to the teacher already

I can't read it.

Write it in PHP, take it.

Looks like a very advanced look, PHP small white, will do something simple, mark A, learn under

Hey, this thing was returned to the teacher after a semester in college.

Completely dazzled @@

Completely dazzled @@

Too complicated to pull, can't read

hahaha, the landlord of the whole good to a connection Bai, online compilation.

I haven't learned it, and I can't use it for the time being. Let's talk about it later, there is limited energy.

Running results look very familiar, but can not read ...

Look up in ...

Write more notes, it looks very tired.

I didn't see it clearly.
What algorithm do you implement based on?

Hello, Senior nag.

  • 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.