LR Method
LR parsing is a more general approach than LL, and LR parser is an efficient, bottom-up syntax parsing technique for context-independent grammars.
The L, R, and K in the LR (k) method represent: L:left-to-right scan left-to-right scanning r:construct a rightmost derivation in reverse right derivation k:the number of input sy Mbols of Look ahead characters per scan
The LR method is divided into three types: slr:simple LR simple LR method, this article introduces simple LR (1) method LR (1): Canonical LR canonical LR method Lalr:look ahead LR
More about LL syntax analysis methods, such as predictive analytics, can be viewed here.
In this case:
E = e + T
E = T
T + = T * F
T = F
F = (E)
F = i construction automata
The first is to construct its own state machine for each production type.
First, we need to understand the meaning of these state machines. Each state machine describes the state transitions that are required for each production derivation process. Such conversions are layered . For example t=>t+f This state transition, when the final F that step, can be directly through I to convert, also can be converted by (E), that is the last two expressions, each non-terminator for the emitting edge of the state transition, such as E, F, etc., all means that such a conversion needs to be decomposed into a more refined transformation, These "finer transformations" are defined later, as for F with f=> (E) and f=>i.
Therefore, we are connected to the specific decomposition state.
(Note: blank edges refer to Ε\epsilon edges)
We do not need to look at each line in the diagram (in fact, we can not draw this picture), but we need to understand the state of the hierarchical relationship, that is, each non-terminator composition of the emitted edge must be decomposed, the way of decomposition is to this non-terminating character is the derivation of the left. Because each derivation is converted to a state machine, this means that it can be decomposed into the corresponding finer state.
Q: Why has S ' =>e this state exist.
A:
In order to have a common starting point. Since the beginning E + + e + t,e = T is a side-by-side layer, you need to define a common starting point. Generate DFA step1:state naming naming State
Before we directly named the status of 1, 2, 3, 4, and so on, here such a simple name can not reflect the characteristics of stratification , how can not only reflect the stratification, but also to specify the above diagram of each specific state. Our solution is to use a production-named layer to represent the location with dots .
Two named examples are shown in the figure above. step2:determination the NFA into a LRDFA
In the previous article, we mentioned how to convert the NFA to DFA using the table-driven approach, when two concepts were mentioned: Ε\epsilon Closure and subset construction. Here we list the featured parts of this approach.
Ε\epsilon closures- in-state expansion
How to find the starting state S ' =>. E's "Ε\epsilon closure". The method is that if there is a point in front of the non-terminator, then the non-terminator to the left of the corresponding generation of the first state to join the new state. Speaking is, for S ' => E, dot in front of E, then e=> E+t and E=>. T joins S ' =>. E of the Ε\epsilon closure, which is a recursive process, e=> T dot in front of T, then t=> T*f and T=>. F also joins S ' =>. E Ε\epsilon closure, and then find F ... We find a new name for this method called in-state extension, which is, of course, a simpler approach, and the same results can be found entirely using the previous Ε\epsilon closure method.
Subset constructor- state extension
Unlike before, it is also considered that the issuing edge of the non-Terminator composition. The way to find a subset is simpler, as long as you move the corresponding point to the right. such as T=>. The state of t*f from T is t=>t.*f, again, this is the same as the previous subset construction method can be solved, but this naming method for the conversion of DFA provides a great convenience.
The following figure is the complete LRDFA diagram:
Constructing LR Analysis Tables
The number of S and Goto in the action is well filled, and can be filled directly according to the generated state of the above, such as I0 by issuing the edge T to I2, then in state0 row t column write 2,i0 by issuing the edge I to I5, then in STATE0 row I column write S5 (SHIFT5). The explanation here is the completion of the R (Reduce).
REDUCE
First for all production labels:
(0) S ' =>e
(1) E = + E + T
(2) E = T
(3) T + = T * F
(4) T = F
(5) F = (E)
(6) F = i
Then, observe each specific state. If there is a state that contains the terminating state (that is, the point at the far right), it means that the sub-state analysis is complete and requires a protocol. We need to follow this derivation (follow is what I see in the previous article).
For example, for I2, there is e=>t. This state, which points at the back of T, that is, at the end, we need to ask for e follow, we can know the follow of E is + and (and $, so we fill state2 in R2, The 2 here means that the second generating e=>t is the one that produces the statute. Analysis Process
The LR parsing model is a little different from the previous one, with the biggest difference being that the left stack has two columns, the leftmost column is for the current state, and the right column holds the temporary character and state. If the current state and character pointer refers to a character in the LR table corresponding to an action shift (SI, state transition), then the character is stacked, because each time the stack is pressed into the character and state at the same time, the corresponding state is pressed into the action is indicated in the state, and the character pointer shifted to the right. Key : If the current state and character pointer refers to a character in the LR table corresponding to the action of reduce (that is, RI), first look at the RI corresponding to the production, such as f=>e+f, then you need to pop up the stack of e+f (if the steps are smooth, Then there is already e,+ and F in the stack, put the F stack, and f at the same time the state of the stack is the current state (all pop up after the state of the following line) and F in the LR table corresponding Goto, if the current state is 0, then the state of the stack with F is (0,f) the corresponding goto state. The character will pop up with the state when the stack is in play. R0 means accept, which is the end of the analysis success.
PS: You can draw a stack diagram of each step when you are free
PS: The time to write this flower is too long, it is really from Monday to Friday, there are not all the lessons of each class is even more.