120 likes | 277 Views
COMPILERS. Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013). LR Parsers. In LR Parser L stands for Left to right and R stands for Rightmost derivation LR Parsers are also known as Shift-Reduce Parser
E N D
COMPILERS Bhaskar Bagchi (11CS10058) Lecture Slides( 9th Sept. 2013)
LR Parsers • In LR Parser L stands for Left to right and R stands for Rightmost derivation • LR Parsers are also known as Shift-Reduce Parser • Whenever we have a transition in the input symbol we either perform a SHIFT or perform a REDUCE. • For the shift and reduce operations we need an FSM, to construct which we need • State • Transition • A small augmentation in grammar
Types of LR parser There are various types of LR parsers, namely • SLR (Simple LR) Parser • CLR (Canonical LR) Parser • LALR (look-Ahead LR) Parser • For all these parsers we need State transition machine • The construction of STM will be further discussed
Construction of State Transition Machine • Augment the grammar G to G’ • Start symbol S • Introduce new non-terminal S’ S • Find CLOSURE() • Find GOTO()
Construction of ITEMS form a production • Let there be a production A XYZ • We can construct 4 items from this production, as listed below • A .XYZ • A X.YZ • A XY.Z • A XYZ.
… ITEMS from production • Each of the item tell us to how much the input string has been recognized. • A X.YZ implies that the prefix of the input parsed has been reduced to non-terminal X and the remaining string may be expected to match with YZ. • Similarly A XY.Z says that XY has been recognized and Z is expected. • Also, A .XYZ means that no prefix has been matched yet but it may match to XYZ.
Construction of LR(0) automaton • In LR parser to know whether to shift or to reduce we need a parsing table, and in order to construct this parsing table we first need a State Transition Machine, the LR(0) automaton. • Each state of this automaton is a set of items • As mentioned earlier, to construct this automaton we need two functions: • CLOSURE() • GOTO()
The CLOSURE() function • Helps us find state of the automaton. • Algorithm to find CLOSURE • Put all items of I0 in CLOSURE I0 • If there is a production of the form A B.CD in I0 and there is a production of the form C E then add the production C .E to the CLOSURE I0 • Let we have an example grammar as follows • E E + T | T • T T * F | F • F id
The CLOSURE() function • First augment the grammar by introducing a new non terminal as E’ E • To find the state S0 add the item E’ .E to the state. Now for all productions in the augmented grammar G’ with E as its head, say, E E + T | T, add the corresponding items E .E + T and E .T to the state S0 • Again we get an item with T appearing to the right of the dot, add the corresponding items. • Proceed similarly…
The GOTO() function • GOTO(Ii , X) Ii , where Ii is the current state(set of items), X is the terminal/non-terminal symbol and Ij is the next state. • If there is a production of the form A A.XB in Ii and a production of the form A AX.B in Ij then if the next input matches X in the state Ii then the GOTO() function returns Ij • Let the input string is of the form x1 x2 …xm xm=1 …xn-1 xn then x1 x2 …xm has already been matched to B and the remaining xm=1 …xn-1 xn can be matched to X.
The GOTO() function • Let I0 be a start state for the above mentioned example grammar, then the GOTO() functions will be as follows: • GOTO(I0 , E) I1 Item E’ E is available so we add the item E’ E. in I1 and take the CLOSURE of the items. So we get state I1 as E’E. and E’E. + T • GOTO(I0 , T) I2 For the item E .T and T .T* F we add T T. * F to I2 and find its CLOSURE. So, we get I2 as E T. and TT. * F
The GOTO() function • Similarly we get • GOTO(I0 , F) I3 : T F. • GOTO(I0 , id) I4 : F id. • Each of the above set of items I1 , I2 , I3 , I4 corresponding to new states of the LR(0) automaton. For each of these states we use GOTO() function for the terminals and non-terminals appearing in the items in the states and right shift the dot, i.e. match one symbol in at least one of the items present in the state.