What are LR Parsers?

Compiler DesignProgramming LanguagesComputer Programming

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 where

  • L represents Left to Right Scanning of Input
  • R represents Rightmost Derivation
  • K is the number of input symbols of Look ahead that are used in developing parsing decisions.

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 feasible. An achievable prefix of a right sentential form is that prefix that includes a handle, however no symbol to the right of the handle.

Thus, if a finite-state machine that recognizes viable prefixes of the right sentential forms is assembled, it can be used to influence the handle selection in the shiftreduce Parser.

Because the LR parser creates the use of a DFA that identifies viable prefixes to manage the selection of handles, it should maintain track of the states of the DFA. Therefore, the 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.

If there is a transition from the state Ii on 'a' in the DFA going to state Ij therefore the symbol a is followed by state symbol Ij onto the stack.

If there is no transition from Ii on a in the DFA, and if the state Ii on the top of the stack identifies, when listed a viable prefix that includes the handle A → a, thus the parser gives out the reduction by popping α and pushing A onto the stack.

This is similar to developing a backward transition from Ii on α in the DFA and then developing a forward transition on A. Each shift action of the parser correlates to a transition on a terminal symbol in the DFA.

Thus, the current state of the DFA and the next input symbol determines whether the parser shifts the next input symbol or goes for reduction.

If it can produce a table mapping each state and input symbol pair as either "shift", "reduce", "accept", or "error", we obtain a table that can be used to manage the parsing phase. Such a table is known as the parsing "action" table.

raja
Updated on 01-Nov-2021 12:39:44

Advertisements