jagomart
digital resources
picture1_Production Pdf 192007 | Mca Ii Cd Bottom Up Parser Continued


 122x       Filetype PDF       File size 2.33 MB       Source: ccsuniversity.ac.in


File: Production Pdf 192007 | Mca Ii Cd Bottom Up Parser Continued
bottom up parsing lr parsers lr 0 slr clr and lalr parsers lr parsers are non recursive shift reduce bottom up parser it uses wide class of context free grammar ...

icon picture PDF Filetype PDF | Posted on 05 Feb 2023 | 2 years ago
Partial capture of text on file.
                      Bottom Up Parsing - LR parsers 
                       ( LR(0), SLR, CLR and LALR Parsers)
                        
                      LR parsers are non-recursive, shift reduce bottom up parser. It uses wide class of context free grammar.
                       
                      Steps:
                      1. Augment the given grammar.
                      2. Draw the conical collection of LR(o) items.
                      3. Number the production.
                      4. Create the parsing table
                      5. Stack implementation
                      6. Draw parse tree.
                       
                       
                        SLR parser, CLR parser and LALR parser which are the parts of Bottom Up parser.
                      SLR Parser
                      The SLR parser is similar to LR(0) parser except that the reduced entry. The reduced productions are written 
                      only in the FOLLOW of the variable whose production is reduced.
                      Construction of SLR parsing table –
                        1.    Construct C = { I , I , ……. I }, the collection of sets of LR(0) items for G’.
                                                  0  1          n
                        2.    State i is constructed from Ii. The parsing actions for state i are determined as follow :
                                      If [ A -> ?.a? ] is in I and GOTO(I , a) = I , then set ACTION[i, a] to “shift j”. Here a 
                                
                                                                  i                  i         j
                                      must be terminal.
                                      If  [A  ->  ?.]  is  in  I,  then  set  ACTION[i,  a]  to  “reduce  A  ->  ?”  for  all  a  in 
                                
                                                                   i
                                      FOLLOW(A); here A may not be S’.
                                      Is [S -> S.] is in I, then set action[i, $] to “accept”. If any conflicting actions are 
                                
                                                                i
                                      generated by the above rules we say that the grammar is not SLR.
                        3.    The goto transitions for state i are constructed for all nonterminals A using the rule:
                              if GOTO( I , A ) = I then GOTO [i, A] = j.
                                           i          j
                        4.    All entries not defined by rules 2 and 3 are made error.
                      Eg:
                      If in the parsing table we have multiple entries then it is said to be a conflict.
                      Consider the grammar 
                      E -> T+E | T
                      T ->id
                      Augmented grammar - E’ -> E
                      E -> T+E | T
                      T -> id 
                      Compiler Design- MCA II                                                    Dr Vandana
                                                                       
           
          Note 1 
               –   in the GOTO graph just look for the reduce and shifts occurring together in one state.. In case of two 
          reductions,if the follow of bo=th the reduced productions have something common then it will result in multiple 
          entries in table hence not SLR. In case of one shift and one reduction,if their is a GOTO operation from that 
          state on a terminal which is the follow of the reduced production than it will result in multiple entries hence not 
          SLR.
          Note 2
                – Every SLR grammar is unambiguous but their are many unambiguous grammars that are not SLR.
          CLR PARSER
          In the SLR method we were working with LR(0)) items. In CLR parsing we will be using LR(1) items. LR(k) 
          item is defined to be an item using lookaheads of length k. So , the LR(1) item is comprised of two parts : the 
          LR(0) item and the lookahead associated with the item.
          LR(1) parsers are more powerful parser.
          For LR(1) items we modify the Closure and GOTO function.
          Closure Operation
          Closure(I)
          repeat 
              for (each item [ A -> ?.B?, a ] in I )
                  for (each production B -> ? in G’)
                    for (each terminal b in FIRST(?a))
                      add [ B -> .? , b ] to set I;
          until no more items are added to I;
          return I;
           
          Compiler Design- MCA II                                                    Dr Vandana
         Lets understand it with an example –
         Goto Operation
         Goto(I, X)
         Initialise J to be the empty set;
         for ( each item A -> ?.X?, a ] in I )
             Add item A -> ?X.?, a ] to se J;   /* move the dot one step */
         return Closure(J);    /* apply closure to the set */
         Eg-
         Compiler Design- MCA II                                                    Dr Vandana
                      LR(1) items
                      Void items(G’)
                      Initialise C to { closure ({[S’ -> .S, $]})};
                      Repeat
                          For (each set of items I in C)
                              For (each grammar symbol X)
                                  if( GOTO(I, X) is not empty and not in C)
                                      Add GOTO(I, X) to C;
                      Until no new set of items are added to C;
                       
                      Construction of GOTO graph
                               State I  – closure of augmented LR(1) item.
                         
                                      0
                               Using I  find all collection of sets of LR(1) items with the help of DFA
                         
                                        0
                               Convert DFA to LR(1) parsing table
                         
                      Construction of CLR parsing table-
                      Input – augmented grammar G’
                         1.    Construct C = { I , I , ……. I } , the collection of sets of LR(0) items for G’.
                                                    0  1          n
                         2.    State i is constructed from Ii. The parsing actions for state i are determined as follow :
                               i) If [ A -> ?.a?, b ] is in I and GOTO(I , a) = I, then set ACTION[i, a] to “shift j”. Here a must be 
                                                              i               i         j
                               terminal.
                               ii) If [A -> ?. , a] is in I , A ≠ S, then set ACTION[i, a] to “reduce A -> ?”.
                                                           i
                               iii) Is [S -> S. , $ ] is in I, then set action[i, $] to “accept”.
                                                             i
                               If any conflicting actions are generated by the above rules we say that the grammar is
                               not CLR.
                      Compiler Design- MCA II                                                    Dr Vandana
The words contained in this file might help you see if this file matches what you are looking for:

...Bottom up parsing lr parsers slr clr and lalr are non recursive shift reduce parser it uses wide class of context free grammar steps augment the given draw conical collection o items number production create table stack implementation parse tree which parts is similar to except that reduced entry productions written only in follow variable whose construction construct c i sets for g n state constructed from ii actions determined as if goto a then set action j here must be terminal all may not s accept any conflicting generated by above rules we say transitions nonterminals using rule entries defined made error eg have multiple said conflict consider e t id augmented compiler design mca dr vandana note graph just look shifts occurring together one case two reductions bo th something common will result hence reduction their operation on than every unambiguous but many grammars method were working with k item an lookaheads length so comprised lookahead associated more powerful modify clos...

no reviews yet
Please Login to review.