Free to do nothing, put the Dragon book out have looked at, the recent learning summary.
First (X) Collection Definition: A collection of the first symbols of the string that can be deduced from X, where x is any grammatical symbol. If x=> =>ε, then ε is also in first (X). (Definition from Dragon book)
Algorithm pseudo-code (non-accurate version):
<span style= "FONT-SIZE:14PX;" >foreach (nonterminal N) First (n) = {}while (some set is changing) foreach (Production p:n->β1 ... βn) if (β1== a ...) First (N) ∪= {a}if (β1== M ...) First (N) ∪= First (M) </span>
The algorithm is still well understood, for every production a->b1 B2 BN, if B1 is Terminator, first (a) u= {B1}, if B1 is non-Terminator then first (a) u= first (B1), here is the recursive solution. As long as the current set changes, the second layer loops again, until no collection has changed. This is a fixed point algorithm.
The inductive definition of the Nullable collection:
1 Basic conditions:
ε , X
2 Induction conditions:
X-Y1 ... Yn
Y1, ..., Yn is n non-terminator, and all belong to nullable set
Algorithm pseudo-code:
<span style= "FONT-SIZE:14PX;" ></span><pre name= "code" class= "cpp" >nullable = {};while (NULLABLE is still changing) foreach ( Production p:x->β) if (β==ε) nullable∪= {X} if (β== Y1 ... Yn) if (y1∈nullable && ... && yn∈nullable) nullable∪= {X}
Well, that 's a good idea.
We do not hurry to see Follow collection, and then look at the pseudo code of the previous first, this brother pseudo-code a cursory look, the feeling is right, but a fine thought, if said B1 is non-terminator, but if b1->ε set up, then whether to think about it? The answer is correct, then first (x) u= First (B2), if b2-> ε isstill established, continue first (x) u= First (B3),and so on.
New seek first set pseudo-code:
foreach (nonterminal N) First (n) = {}while (some set is changing) foreach (Production p:n->β1 ... βn) foreach (βi fromβ1 uptoβn) if (βi== a ...) First (N) ∪= {a}breakif (βi== M ...) First (N) ∪= first (m) if (M is not in NULLABLE) break
The next is the follow collection:
For non-terminating symbols X,follow (A) is defined as a collection of summary symbols that may be immediately to the right of x in some sentence patterns. (Definition from Dragon book)
For example, if there is a S=>ABCD (A,b,d is a string of grammatical symbols), then C is in follow (b).
Fixed point algorithm for follow set:
<span style= "FONT-SIZE:14PX;" >foreach (nonterminal N) follow (n) = {}while (some set is changing) foreach (production p:n->β1 ...) βn) temp = follow (N) foreach (βi fromβn downtoβ1)//reverse order! if (βi== a ...) temp = {a}if (βi== M ...) Follow (m) ∪= Tempif (M is not NULLABLE) temp = First (m) Else temp∪= first (m) </span>
Well, by completing this step, we can get the first set of any string,
The following pseudo-code:
foreach (production p) first_s (P) = {}calculte_first_s (production p:n->β1 ... βn) foreach (βi fromβ1 toβn) if (βi== a ...) first_s (P) ∪= {a}return;if (βi== M ...) first_s (P) ∪= first (m) if (M is not NULLABLE) return; first_s (P) ∪= Follow (N)
Okay, here's the summary.
If there is a wrong place, the level is limited, also hope to point out.
Thank you
With June
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Compiler practice four first set, Nullable collection, follow collection