SLR
SLR
SLR
The easiest technique for generating LR-based parse table is known as SLR (Simple LR).
Understanding this technique should provide you with what you need to know to
understand how LR parsers work in general; it is also the foundation for the more
complex techniques (LR and LALR).
Remember that the idea behind LR parsing is to produce a DFA that defines the handles
(string of terminals and non-terminals that indicate a reduction) of the input language.
The SLR technique is based on generating sets of LR(0) items that describe the states of
the DFA, as well as a transition function that maps between these states.
A production that directly derives ε, only generates a single LR(0) item. Hence, E Æ ε
has only one associate item : E Æ .
In order to generate these LR(0) item sets, we need to be able to compute closures across
them.
The key to producing the required sets is the Goto function that maps an item set and a
grammar symbol (terminal or non-terminal) to an item set. Remember that items sets
represent states in the handle DFA; that means the Goto function gives us the transitions.
Here are some examples of computing the Goto function for the expression grammar.
Goto({E’ÆE ., E Æ E . + T},+) = closure({E Æ E + . T}) =
{E Æ E + . T, T Æ . T * F, T Æ . F, F Æ . id, F Æ . ( E )}
Goto({T Æ T * . F, T Æ . F},F) = closure({T Æ T * F ., T Æ F .}) =
{T Æ T * F ., T Æ F .}
Goto({E’ÆE ., E Æ E + . T},+) = closure({ }) = { }
We are now ready to look at the actual algorithm for generating the DFA.
Algorithm:
• C ={closure({S’Æ. S})}, where S’ Æ S is the production added for augmentation
• Repeat
– For each item I in C and grammar symbol X such that Goto(I,X) is not
empty and not in already an element of C
• Add Goto(I,X) to C
This is easiest to see by working through the augmented expression grammar example.
We start with the computing
C = {closure({E’ Æ . E}) } = {E’ Æ . E, E Æ . E + T, E Æ . T, T Æ . T * F, T Æ
. F, F Æ . ( E ), F Æ . id}.
This gives us the items for the first state (state 0) of our DFA. Now we need to compute
Goto functions for all of the relevant symbols in the set. In this case, we care about the
symbols E, T, F, (, and id, since those are the symbols that have a . symbol in front of
them in some item of the set C.
1
Augmentation is only required if the grammar does not have a single production that signals the end of the
parsing. However, augmentation never changes the language, so it never hurts.
I typically write the above information in a table format where each item of each state is
annotated with the appropriate state 1-5. The kernel of each state (the items we started
with before computing the closure) is in boldface.
We continue by computing Goto in each of the newly created states. When the . occurs
at the end of a production this is going to correspond to a reduction in the parsing
algorithm so we won’t have a goto value there. We will deal with this situation once we
have completed the DFA.
Notice in the table that we don’t have to create new states when the goto function can use
a state that already exists. In state 4, we have transitions to states 2, 3 and 4 because any
newly created state (using our rules) would look like these states. However, we can only
re-use a state if it is exactly the same. For example, we must create a state 8 even though
its kernel is similar to that of state 2.
The table on the next page has the complete table for the grammar. Before using this
table to find the SLR action/goto tables, we need to compute the follow sets for all of the
non-terminals in the grammar and we need to number the productions.
0: E’ Æ E
1: E Æ E + T Follow(E’) = {$}
2: E Æ T Follow(E) = {$,),+}
3: T ÆT * F Follow(T) = {$,),+,*}
4: T Æ F Follow(F) = {$,),+,*}
5: F Æ ( E )
6: FÆ id
The action/goto tables are extracted directly from the above information in the following
manner. First, each state in the above table will be a row in the action/goto tables.
Filling in the entries for row I is done as follows:
• Action[I,c] = shift to state goto(items in state I, c) for terminal c
• Goto[I,c] = goto(items in state I, c) for non-terminal c
• For production number n: A Æ α in state I where . occurs at the end, Action[I,a] =
reduce production n, for all elements a in Follow(A)
• For the added production(augmented production) with the . at the end in state I,
Action[I,$] = accept.