Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)

Similar presentations


Presentation on theme: "Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)"— Presentation transcript:

1 Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)

2 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 1. State 2. Transition 3. A small augmentation in grammar

3 There are various types of LR parsers, namely 1. SLR (Simple LR) Parser 2. CLR (Canonical LR) Parser 3. LALR (look-Ahead LR) Parser For all these parsers we need State transition machine The construction of STM will be further discussed

4 Augment the grammar G to G’ 1. Start symbol S 2. Introduce new non-terminal S’  S Find CLOSURE() Find GOTO()

5 Let there be a production A  XYZ We can construct 4 items from this production, as listed below 1. A .XYZ 2. A  X.YZ 3. A  XY.Z 4. A  XYZ.

6 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.

7 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: 1. CLOSURE() 2. GOTO()

8 Helps us find state of the automaton. Algorithm to find CLOSURE 1. Put all items of I 0 in CLOSURE I 0 2. If there is a production of the form A  B.CD in I 0 and there is a production of the form C  E then add the production C .E to the CLOSURE I 0 Let we have an example grammar as follows  E  E + T | T  T  T * F | F  F  id

9 First augment the grammar by introducing a new non terminal as E’  E To find the state S 0 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 S 0 Again we get an item with T appearing to the right of the dot, add the corresponding items. Proceed similarly…

10 GOTO(I i, X)  I i, where I i is the current state(set of items), X is the terminal/non-terminal symbol and I j is the next state. If there is a production of the form A  A.XB in I i and a production of the form A  AX.B in I j then if the next input matches X in the state I i then the GOTO() function returns I j Let the input string is of the form x 1 x 2 …x m x m=1 …x n-1 x n then x 1 x 2 …x m has already been matched to B and the remaining x m=1 …x n-1 x n can be matched to X.

11 Let I 0 be a start state for the above mentioned example grammar, then the GOTO() functions will be as follows: GOTO(I 0, E)  I 1 Item E’  E is available so we add the item E’  E. in I 1 and take the CLOSURE of the items. So we get state I 1 as E’  E. and E’  E. + T GOTO(I 0, T)  I2 For the item E .T and T .T* F we add T  T. * F to I 2 and find its CLOSURE. So, we get I 2 as E  T. and T  T. * F

12 Similarly we get  GOTO(I 0, F)  I 3 : T  F.  GOTO(I 0, id)  I 4 : F  id. Each of the above set of items I 1, I 2, I 3, I 4 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.


Download ppt "Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)"

Similar presentations


Ads by Google