Generating LR Collection

The first step is to create an augmented grammar. The augmentation process starts by bringing the start symbol on the right-hand side of a production rule. We cannot alter the existing rule so we add a new rule in the productions. Bringing the start symbol on RHS ensures that the parsing reaches the acceptance state. The REDUCE operation on this newly added rule determines the string acceptance.  

For example, 

IF we have ‘K’ as a start symbol

THEN L → K is added in productions

(where ‘L’ represents any non-preexisting symbol in the collection)

CLOSURE and GOTO Operations

In the process of construction of deterministic finite automaton, there are two requirements. The first is creating “States” and the second is developing “Transitions” in the automaton.

1) CLOSURE

Closure operation helps us to form the “States”. Before taking the closure operation all the rules must be separated. Then number all the rules. This will be helpful later for making the Shift and Reduce entries in the parsing table. Let I0 be the collection of rules obtained after grammar augmentation. This means we also have a newly added rule in collection I0.  

Assumption – (consider [L, K] are non-terminals and [m, t, p] are set of zero or more terminals or non-terminals)

DO REPEAT (Till-No-New-Rule-Gets-Added) {

IF (any production of the form “ L → m · K t ” exists) and (we have production K → · p)

THEN {add production K → · p to the Closure set if not preexisting}

}

2) GOTO

GOTO operation helps us to form the “Transitions”. In operation GOTO (I, X), ‘I’ can be elaborated as the state we are looking at and ‘X’ is the symbol pointed by Dot (·). So, GOTO takes in a state with items and a grammar symbol and produces the new or existing state as output.

The GOTO (I, X) represents the state transition from “I” on the input symbol “X”.

For any production rule “ L → m · K t ” in “I”

GOTO (I, X) outputs the closure of a set of all the productions “ L → m  K · t ”

Compiler Design SLR(1) Parser Using Python

Prerequisite: LR Parser, SLR Parser

Similar Reads

SLR (1) grammar

SLR stands for Simple LR grammar. It is an example of a bottom-up parser. The “L” in SLR represents the scanning that advances from left to right and the “R” stands for constructions of derivation in reverse order, and the “(1)” represents the number of input symbols of lookahead....

Generating LR Collection

The first step is to create an augmented grammar. The augmentation process starts by bringing the start symbol on the right-hand side of a production rule. We cannot alter the existing rule so we add a new rule in the productions. Bringing the start symbol on RHS ensures that the parsing reaches the acceptance state. The REDUCE operation on this newly added rule determines the string acceptance....

Program approach

The input for the program is the list of rules having a set of items and lists determining terminal and non-terminal symbols. The control starts form grammarAugmentation() function, then we have to calculate I0 state, that is calculated by calling findClosure(), now for new states generation generateStates() function is called and finally createParseTable() function is called....