In the previous article The LR (0) analysis table is an important component of the LR (0) analyzer.ProgramThe basis for analyzing the action. It is constructed by the LR (0) project set standard family. Its structure mainly includes two parts: Action and Goto.
First, let's take a look at the Guiding Principles. You can skip them directly. When you look at the examples, you can return them for reference.
Assume that the LR (0) project set standard family has been constructed: c = {I0, i1 ,... , In}, where IK is the name of the project set, k is the state name, and K is the subscript K of the Set ik that contains the s' → · s project is the initial state of the analyzer. The steps for constructing the action table and goto table of the analysis table are as follows:
① If Project A → α · A β belongs to ik and the conversion function go (Ik, A) = IJ, when a is the Terminator, set action [K, a] to SJ.
② If Project A → α · belongs to Ik, set action [K, A] and action [K, #] To "RJ" for any Terminator A and ", J is the sequence number of the generative expression A → α in grammar G.
③ If go (Ik, A) = IJ, set goto [K, a] To "J", where A is not the Terminator.
④ If project s' → S · belongs to Ik, set action [K, #] To "ACC" to accept.
⑤ All elements of the analysis table that cannot be filled in using the above method should be filled with the "error mark ". For Table clarity, we only use blank spaces to indicate error marks.
Part 1ArticleThe example is as follows: LR (0) project set specification cluster has also been calculated, a total of 6 I, from the I0-I5, the final structure of LR (0) analysis table a total of 7 rows, this includes the header line, that is, action and Goto, and then the status line. The status line and action are divided into three columns, which are the Terminator and # Terminator. That is, the number of columns to be separated depends on the number of terminologies. The Goto column is not a Terminator, and the number of columns to be separated depends on the number of non-terminologies ., Then there are six specific States. After the table structure is drawn, you do not have to worry about how to write the table content.
Then fill in the table against the constructor principles. Then you will find it too difficult to find the corresponding formulas from so many go functions and the I project team one by one, we use the go function to connect the LR (0) project set canonicalized family into a DFA that identifies the active prefix produced by the syntax. It is a bit like a flowchart. First, we will draw out each I project, then, we need to express their relationships, which are determined by the go function. For example, if I5 = go (I2, S), we will draw an arrow between I2 and I5, which points to I5 from I2, write "S" online, which is determined by the second value in the brackets. The DFA constructed by this question is as simple as that.
Then let's start. The first guiding rule says that if Project A → α · A β belongs to ik and the conversion function go (Ik, A) = IJ, when a is the Terminator, set action [K, a] is SJ. for I0, we first find that s-> · As belongs to I0 and go (I0, A) = I1. All our actions [0, a] is set to S1. Similarly, S-> · BS belongs to I0, go (I0, B) = I2, so action [0, B] is set to S2.
Next, let's look at the second rule. If Project A → α · belongs to Ik, set action [K, A] and action [K, for any Terminator A and, #] is "RJ", and J is the sequence number of the generative expression A → α in grammar G'. That is to say, here J is not the label of the I project, but the augmented grammar.
(0) s '→ S
(1) S →
(2) S → BS
(3) S →
From 0 to 3. We examined I1 and found that S → · As belongs to I1, And go (I1, A) = I1, so we should set the grid between 1 and A to S1, however, when using the second rule, we will find that s-> A · also belongs to I1, then we should set action [1, A] To = R3, action [1, #] to R3, in this case, a conflict occurs. This is because most grammar cannot meet the LR (0) grammar conditions. For this conflict, we cannot determine whether the Protocol or the transfer is performed when S->, some syntaxes can be directly constructed. Therefore, we cannot have an early LR (0) analysis table here. After the structure is improved, a new SLR (1) syntax is obtained, there is no big difference, mainly in conflict resolution.
The guiding principles for conflict resolution are as follows:
* Assume that a LR (0) project set standard family contains the following project sets:
{X → α. B β, A → Gamma., B → Delta .}
There are migration-reduction conflicts and reduction-reduction conflicts.
* If follow (a) contains follow (B) Conflict {B} = conflict, you can resolve the conflict as follows (assuming the current symbol is ):
1. If a = B, move
2. If a ε follow (A), generate a → gamma reduction.
3. If a ε follow (B), generate B → Delta reduction
4. Otherwise, an error is reported.
When a conflict occurs here, the current symbol is a, and there is no B derivation in the project set at this time, and B in the guiding rule is actually S-> here. as, so the calculation of follow (s) operator {A} is null, so the conflict can be resolved, because the current symbol is a, and B in the rule is, therefore, the first step of constructing a step using the action table and the Goto table of the analysis table, instead of R3, is to set the action [1, A] To = S1, so the conflict is resolved.
Then let's look at the third step in the construction step. If go (Ik, A) = IJ, set goto [K, a] To "J", where A is not the Terminator. In this question, only S is not the Terminator. Check the I0 in DFA and find that go (I0, S) = I3, so set goto [0, s] to 3, OK
Step 4: If project s' → S · belongs to Ik, set action [K, #] To "ACC", which is very simple. In DFA, I3 meets, therefore, set action [3, #] To "ACC ". The explanation is complete.
Use it repeatedly until the table is filled out.
Table 1 is shown in. It's too complicated. I have burned my mind. I will introduce how to use it for analysis in the next article. In fact, the rest is not very difficult. You can understand it.
Link to the original article on my website:Http://leaver.me/archives/574.html
Refer:
Http://metc.gdut.edu.cn/compile/nandian/n-7.htm
Http://jpkc.hdu.edu.cn/computer/byyl/online/4-3.htm