What is Components of LR Parsers in compiler design?


LR Parser is a class of Bottom-Up Parser that is used to parse Context-Free Grammars. LR Parsing is known as LR (K) parsing. LR parser is a shift-reduce parser that creates the use of deterministic finite automata, identifying the collection of all applicable prefixes by reading the stack from bottom to top.

It decides what handle, if any, is available. A viable prefix of a right sequential form is that prefix that includes a handle, but no symbol to the right of the handle. Thus, if a finite state machine that identifies viable prefixes of the right sentential form is constructed, it is used to guide the handle selection in the shift-reduce parser.

LR parser stack includes two kinds of symbols− state symbols can identify the states of the DFA and grammar symbols. The parser begins with the initial state of a DFA, I0 on the stack. The parser operates by considering the next input symbol 'a' and the state symbol Ii on the top of the stack.

There are various components of LR Parser which are as follows.

  • Input Buffer − It includes the string to be parsed, followed by $, a symbol used as the right end marker to denote the end of the string.
  • Stack − It is used to store grammar symbols and states.

         s0X1s1X2……………………Xmsm$

       Where X1, X2……………….Xm are grammar symbols

       and s0, s1, s2………………..sm are states

  • Parsing Table: Parsing Table is divided into two sections −
    • Actions: Actions can be a shift, reduce, accept and error.
    • goto: It takes state and Grammar symbols as arguments and produces a state example, goto (s, X).
  • Parsing Program: It is a driving program that performs the following functions:
    • It maintains Initial configuration (s0,w$) where s0 is starting state, w is a string.
    • It supports the following configuration
    • s0X1s1X2 … … XmSm, ai, ai+1 … … an$
           Stack           Input String (w)

    • It determines Sm the state currently on top of the stack and ai the current input symbol.
    • It takes Actions depending on the entry action [Sm,ai] in the Parsing Table.
  • Actions− Actions taken by Parsers are of the following types−
  • Shift − If Action [Sm, ai] = shift s then shift ai with state s onto the stack, i.e., the configuration becomes.

  • Reduce − If Action [Sm, ai] = reduce A → β, then Parser perform reduction, i.e., the configuration becomes.

              Here

                 s = goto[sm−r, A]

                 r = length of β

Number of symbols popped = 2r (r state symbols + r grammar symbols)

  • Accept − If Action [sm, ai] =accept, Parsing is completed.
  • Error − If Action [sm, ai] = error, Parser calls an error-correcting function.

Updated on: 03-Nov-2021

861 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements