Two issue is multiple option. In 2010 issue we have: 1) we possess a SLR (1) Grammar H as pursuing. We use SLR (1) parser creator and produce a parse table S for Gary the gadget guy. We use LALR (1) parser creator and generate a parse table M for H. And the query designer choose the answer as. – Bottom -up parsing is also known as shift reduce parsing. – Operator-Precedence Parsing –simple, restrictive, easy to implement – LR Parsing –much general form of shift-reduce parsing, LR, SLR, LALR Semantic Analyzer A semantic analyzer checks the source program for. Several implementations in Python language of some of the parsing techniques such as LL(1) parsing, SLR(1) parsing,LR(0) parsing and LR(1) parsing. Programming-language parser parsing compiler lr-parser grammar python3 ll-parser context-free-grammar compiler-design compiler-construction ll-grammar slr-parser first-follow-compiler first-follow.
SLR(1) Parsing
It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1) parsing use canonical LR(0) item. The reduce move is placed only in the FOLLOW of those variables whose production is reduced.
The step involves in SLR(1) parsing is given below:
Example
E => BB
B => cB / d
Add augment production and insert ‘.’ symbol in the first position for every production in G.
E’ => .E
E => .BB
B => .cB
B => .d
I0 state
Add starting production to the I0 State and Compute the Closure.
I0 = closure (E’ => .E)
Since “.” is on the left side of the symbol. It means we have not seen anything on the right-hand side. Therefore, all productions beginning with E will be added in the I0 State because “.” is followed by the non-terminal. So, the modified I0 State will be:
I0 = E’ => .E
E => .BB
Here we can see that “.” is on the left of the variable B. Therefore, all productions beginning with B will be added in the I0 State because “•” is followed by the non-terminal. So, the modified I0 State becomes:
E’ => .E
E => .BB
B => .cB
B => .d
I1 = Here we have applied Goto on (I0 E) = closure (E’ => E.)
Here, the “.” is on the right side of the symbol, so production is reduced so close the state.
I1 becomes E’ => E.
I2 = we have applied Goto on (I0 B) = Closure (E => B.B)
We apply closure and will get all the A production with “.” in the beginning. So I2 becomes:
E => B.B
B => .cB
B => .d
Goto (I2 c) = Closure (B => c.B) (Same as I3)
Goto on (I2 d) = Closure (B => .d) (Same as I4)
I3 = Goto on (I0 c) = Closure (B => c.B)
Add productions starting with B in I3.
B => c.B
B => .cB
B => .d
Go to on (I3 c) = Closure (B => c. B) (Same as I3)
Go to on (I3 d) = Closure (B => d.) (Same as I4)
I4 = Go to on (I0 d) = Closure (B => d.) = B => d.
I5 = Go to on (I2 B) = Closure (E => BB.)
I6 = Go to on (I3 B) = Closure (B =>cB.)
Productions are numbered as follows:
E =>BB (1)
B => cB (2)
B => d (3)
Drawing DFA
SLR(1) Table
Explanations:
Follow of B = First of B = c and d and Follow of E = $
NOTE:
When a state moves to some other state on terminals, it will correspond to a shift move in the action part.
When a state moves to another state on a variable, it will correspond to the goto move in the go-to part.
When a state has a final item with no transition to the next state, the production is known as reduce production.
The user may build a SLR(1) parse table for a grammar with this operator. As with LL(1) table building, a full discussion of the method of building an SLR(1) parse table is beyond the scope of this discussion. The procedure followed is more or less the same as the SLR parse table building in the Dragon compiler book.
This shares some similarities with the LL(1) parse table builder view. The similarities include the building of the first and follow sets, the operations of controls at the top of the help window and so forth. That may be helpful as a reference.
NB: In this view, you may resize the views to see things better. For example, dragging the bar between the sets of items diagram and the parse table will adjust the sizes of both.
The following steps are completed in order to build an SLR(1) parse table:
The next step is the construction of the sets of items in a transitional diagram. What a transitional diagram is and how it is put together is not covered here, as a full description would be prohibitively long. JFLAP represents a transitional diagram by a FSA as shown in the figure at the top of the page, with each state corresponding to a set of items. The initial set of items corresponds to the initial state, and final states represent terminating sets of items. The items in a set are shown in the label below a state.
An item is essentially a production with some placeholder character; JFLAP uses an underscore, _, as this special character. A production A BCD can produce the four items A _BCD, A B_CD, A BC_D, and A BCD_. The marker _ in an item represents which symbols have been processed (to the left of the marker) and which have not been processed (to the right of the marker).
During the first part of the sets of items construction, the initial set of items is put together (i.e., S' S and its closure). To expand that set, use the transition tool (), and drag from a set to an empty space in the diagram. The user is queried as to the symbol to expand the set of items on. Once the user enters a grammar symbol and JFLAP confirms that the set of items does goto on that set, a dialog then pops up allowing the user to define the set of items.
The view used for defining a set of items is shown above. The left hand column lists those productions in the grammar. The right hand column lists items already part of the set. To add an item to the set, click a production in the left hand list. A pop-up menu will appear with all possible placements of the sets of items. To save time, the 'Closure' item will take whatever items are selected in the right column, and add all items which comprise the closure of the selected item. The 'Finish' button will add all items that should be in the set of items. When 'OK' is pressed, the answer will be checked for mistakes. If everything is fine, a new set of items will appear at the point in the empty space that the user initially dragged to.
If the user wishes to define a transition from an existing set of items to another existing set of items, in creating the transition she may drag to the second set rather than to empty space. This will avoid the bother of defining the second set of items, as JFLAP will assume the set dragged to is that second set.
One uses the attribute tool () to drag states around, and set final and initial states as per the usual practice when editing. The attribute tool here does not have the ability to change transitions or set labels.
The style of help that is available for SLR(1) table construction is nearly identical to that available for LL(1) table construction: 'Do Selected,' 'Do Step,' and 'Do All' are all there and work in the same fashion.
In the case of the sets of items construction, the help options available are 'Do Step' and 'Do All,' which will randomly place any set of items. After the items are placed, one may rearrange them to make the view more aesthetically pleasing, but it will otherwise be immutable.
The top figure shows a completed SLR(1) parse table. Once the table is completed, the user has the option to proceed to parsing strings using the table by pressing the 'Parse' button. The interface for that parsing is explained here.
There may be at least one cell in the table with more than one rule, and the user may choose which of the rules to use before parsing. Cells with more than one expansion are highlighted in a sort of sherbet orange color. One may click on these cells, and a pop-up menu will appear allowing the user to select which expansion to use for this cell. Note that if clarifying ambiguity becomes necessary, the parse table may not accept the same language as the original grammar since, obviously, the parser will not be aware that alternate shifts/reductions/whatever exist.